Skip to content

Commit

Permalink
color_map_legend: Add option to prune trailing zero fractions from ti…
Browse files Browse the repository at this point in the history
…ck labels
  • Loading branch information
brussig-tud committed Jan 15, 2024
1 parent a725692 commit 4450534
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 24 additions & 2 deletions libs/cgv_app/color_map_legend.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
namespace cgv {
namespace app {

// local helpers
namespace {
void prune_trailing_0 (std::string &decimal_str) {
if (decimal_str.front() != '0') {
decimal_str.erase(decimal_str.find_last_not_of('0')+1, std::string::npos);
decimal_str.erase(decimal_str.find_last_not_of('.')+1, std::string::npos);
}
}
};

color_map_legend::color_map_legend() {

set_name("Color Map Legend");
Expand All @@ -27,6 +37,7 @@ color_map_legend::color_map_legend() {
num_ticks = 3;
label_precision = 0;
label_auto_precision = true;
label_prune_trailing_zeros = false;
label_integer_mode = false;
title_align = AO_START;
show_opacity = true;
Expand Down Expand Up @@ -73,6 +84,7 @@ void color_map_legend::handle_member_change(const cgv::utils::pointer_test & m)
num_ticks,
label_precision,
label_auto_precision,
label_prune_trailing_zeros,
label_integer_mode)) {
post_recreate_layout();
}
Expand Down Expand Up @@ -279,6 +291,11 @@ void color_map_legend::set_label_auto_precision(bool f) {
on_set(&label_auto_precision);
}

void color_map_legend::set_label_prune_trailing_zeros(bool f) {
label_prune_trailing_zeros = f;
on_set(&label_prune_trailing_zeros);
}

void color_map_legend::set_label_integer_mode(bool enabled) {
label_integer_mode = enabled;
on_set(&label_integer_mode);
Expand Down Expand Up @@ -371,8 +388,13 @@ void color_map_legend::create_labels() {

if(label_integer_mode)
str = std::to_string(static_cast<int>(round(val)));
else
str = cgv::utils::to_string(val, -1, precision, /*fixed*/true);
else {
str = cgv::utils::to_string(
val, -1, precision, /*fixed*/std::abs(val) >= 10 // prevent scientfic notation for two-digit and up numbers
);
if (label_prune_trailing_zeros)
prune_trailing_0(str);
}

labels.add_text(str, ivec2(0), cgv::render::TextAlignment::TA_NONE);
max_length = std::max(max_length, labels.ref_texts().back().size.x());
Expand Down
2 changes: 2 additions & 0 deletions libs/cgv_app/color_map_legend.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class CGV_API color_map_legend : public canvas_overlay {
unsigned num_ticks;
unsigned label_precision;
bool label_auto_precision;
bool label_prune_trailing_zeros;
bool label_integer_mode;
AlignmentOption title_align;
bool show_opacity;
Expand Down Expand Up @@ -130,6 +131,7 @@ class CGV_API color_map_legend : public canvas_overlay {

void set_label_precision(unsigned p);
void set_label_auto_precision(bool enabled);
void set_label_prune_trailing_zeros(bool enabled);
void set_label_integer_mode(bool enabled);
void set_show_opacity(bool enabled);
};
Expand Down

0 comments on commit 4450534

Please sign in to comment.