From 8a85c754ef6a8f270dee694d88d120d4ea96a6f3 Mon Sep 17 00:00:00 2001 From: Vaclav Petras Date: Tue, 30 Jan 2024 16:53:54 -0500 Subject: [PATCH] d.vect.thematic: Do not show decimal places for ints (#3096) Number of minimal decimal places is fixed to 2 in the legend generated by d.vect.thematic. This adds logic which changes the number of decimal places to 0 when the column type is integer (DB_C_TYPE_INT). This does not let user decide the number, but it covers more cases than the current code and it prepares a way for making it parametrized in the future. --- display/d.vect.thematic/legend.c | 23 +++++++----- display/d.vect.thematic/local_proto.h | 2 +- display/d.vect.thematic/main.c | 54 +++++++++++++-------------- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/display/d.vect.thematic/legend.c b/display/d.vect.thematic/legend.c index 85b7f57940d..291f9799804 100644 --- a/display/d.vect.thematic/legend.c +++ b/display/d.vect.thematic/legend.c @@ -20,7 +20,7 @@ void write_into_legend_file(const char *legfile, const char *icon, double stats_max, double *breakpoints, int nbreaks, int size, struct color_rgb bcolor, struct color_rgb *colors, int default_width, - int *frequencies, const char *topo) + int *frequencies, int c_type, const char *topo) { FILE *fd; int i; @@ -33,29 +33,34 @@ void write_into_legend_file(const char *legfile, const char *icon, /* Title */ fprintf(fd, "||||||%s\n", title); + // Do not show decimal places for integers. + int n_places = c_type == DB_C_TYPE_INT ? 0 : 2; + /* First line */ if (stats_min > breakpoints[0]) { - fprintf(fd, "< %.2f|", breakpoints[0]); + fprintf(fd, "< %.*f|", n_places, breakpoints[0]); } else { - fprintf(fd, "%.2f - %.2f|", stats_min, breakpoints[0]); + fprintf(fd, "%.*f - %.*f|", n_places, stats_min, n_places, + breakpoints[0]); } fprintf(fd, "%s|%d|ps|%d:%d:%d|%d:%d:%d|%d|%s|%d\n", icon, size, colors[0].r, colors[0].g, colors[0].b, bcolor.r, bcolor.g, bcolor.b, default_width, topo, frequencies[0]); /* Middle lines */ for (i = 1; i < nbreaks; i++) { - fprintf(fd, "%.2f - %.2f|%s|%d|ps|%d:%d:%d|%d:%d:%d|%d|%s|%d\n", - breakpoints[i - 1], breakpoints[i], icon, size, colors[i].r, - colors[i].g, colors[i].b, bcolor.r, bcolor.g, bcolor.b, - default_width, topo, frequencies[i]); + fprintf(fd, "%.*f - %.*f|%s|%d|ps|%d:%d:%d|%d:%d:%d|%d|%s|%d\n", + n_places, breakpoints[i - 1], n_places, breakpoints[i], icon, + size, colors[i].r, colors[i].g, colors[i].b, bcolor.r, bcolor.g, + bcolor.b, default_width, topo, frequencies[i]); } /* Last one */ if (stats_max < breakpoints[nbreaks - 1]) { - fprintf(fd, ">%.2f|", breakpoints[nbreaks - 1]); + fprintf(fd, ">%.*f|", n_places, breakpoints[nbreaks - 1]); } else { - fprintf(fd, "%.2f - %.2f|", breakpoints[nbreaks - 1], stats_max); + fprintf(fd, "%.*f - %.*f|", n_places, breakpoints[nbreaks - 1], + n_places, stats_max); } fprintf(fd, "%s|%d|ps|%d:%d:%d|%d:%d:%d|%d|%s|%d\n", icon, size, colors[nbreaks].r, colors[nbreaks].g, colors[nbreaks].b, bcolor.r, diff --git a/display/d.vect.thematic/local_proto.h b/display/d.vect.thematic/local_proto.h index 0f1377e4db6..3eee24ef112 100644 --- a/display/d.vect.thematic/local_proto.h +++ b/display/d.vect.thematic/local_proto.h @@ -28,4 +28,4 @@ int display_lines(struct Map_info *, struct cat_list *, int, const char *, /* legend.c */ void write_into_legend_file(const char *, const char *, const char *, double, double, double *, int, int, struct color_rgb, - struct color_rgb *, int, int *, const char *); + struct color_rgb *, int, int *, int, const char *); diff --git a/display/d.vect.thematic/main.c b/display/d.vect.thematic/main.c index 08bc0d264a3..bf162df0499 100644 --- a/display/d.vect.thematic/main.c +++ b/display/d.vect.thematic/main.c @@ -533,26 +533,26 @@ int main(int argc, char **argv) while (TRUE) { nfeatures = Vect_get_num_primitives(&Map, GV_POINT); if (nfeatures > 0) { - write_into_legend_file("stdout", icon_opt->answer, title, - stats.min, stats.max, breakpoints, - nbreaks, size, bcolor, colors, - default_width, frequencies, "point"); + write_into_legend_file( + "stdout", icon_opt->answer, title, stats.min, stats.max, + breakpoints, nbreaks, size, bcolor, colors, default_width, + frequencies, ctype, "point"); break; } nfeatures = Vect_get_num_primitives(&Map, GV_LINE); if (nfeatures > 0) { - write_into_legend_file("stdout", icon_line_opt->answer, title, - stats.min, stats.max, breakpoints, - nbreaks, size, bcolor, colors, - default_width, frequencies, "line"); + write_into_legend_file( + "stdout", icon_line_opt->answer, title, stats.min, + stats.max, breakpoints, nbreaks, size, bcolor, colors, + default_width, frequencies, ctype, "line"); break; } nfeatures = Vect_get_num_primitives(&Map, GV_BOUNDARY); if (nfeatures > 0) { - write_into_legend_file("stdout", icon_area_opt->answer, title, - stats.min, stats.max, breakpoints, - nbreaks, size, bcolor, colors, - default_width, frequencies, "area"); + write_into_legend_file( + "stdout", icon_area_opt->answer, title, stats.min, + stats.max, breakpoints, nbreaks, size, bcolor, colors, + default_width, frequencies, ctype, "area"); break; } } @@ -564,26 +564,26 @@ int main(int argc, char **argv) while (TRUE) { nfeatures = Vect_get_num_primitives(&Map, GV_POINT); if (nfeatures > 0) { - write_into_legend_file(leg_file, icon_opt->answer, title, - stats.min, stats.max, breakpoints, - nbreaks, size, bcolor, colors, - default_width, frequencies, "point"); + write_into_legend_file( + leg_file, icon_opt->answer, title, stats.min, stats.max, + breakpoints, nbreaks, size, bcolor, colors, default_width, + frequencies, ctype, "point"); break; } nfeatures = Vect_get_num_primitives(&Map, GV_LINE); if (nfeatures > 0) { - write_into_legend_file(leg_file, icon_line_opt->answer, title, - stats.min, stats.max, breakpoints, - nbreaks, size, bcolor, colors, - default_width, frequencies, "line"); + write_into_legend_file( + leg_file, icon_line_opt->answer, title, stats.min, + stats.max, breakpoints, nbreaks, size, bcolor, colors, + default_width, frequencies, ctype, "line"); break; } nfeatures = Vect_get_num_primitives(&Map, GV_BOUNDARY); if (nfeatures > 0) { - write_into_legend_file(leg_file, icon_area_opt->answer, title, - stats.min, stats.max, breakpoints, - nbreaks, size, bcolor, colors, - default_width, frequencies, "area"); + write_into_legend_file( + leg_file, icon_area_opt->answer, title, stats.min, + stats.max, breakpoints, nbreaks, size, bcolor, colors, + default_width, frequencies, ctype, "area"); break; } } @@ -598,7 +598,7 @@ int main(int argc, char **argv) write_into_legend_file( legend_file_opt->answer, icon_opt->answer, title, stats.min, stats.max, breakpoints, nbreaks, size, bcolor, colors, - default_width, frequencies, "point"); + default_width, frequencies, ctype, "point"); break; } nfeatures = Vect_get_num_primitives(&Map, GV_LINE); @@ -606,7 +606,7 @@ int main(int argc, char **argv) write_into_legend_file( legend_file_opt->answer, icon_line_opt->answer, title, stats.min, stats.max, breakpoints, nbreaks, size, bcolor, - colors, default_width, frequencies, "line"); + colors, default_width, frequencies, ctype, "line"); break; } nfeatures = Vect_get_num_primitives(&Map, GV_BOUNDARY); @@ -614,7 +614,7 @@ int main(int argc, char **argv) write_into_legend_file( legend_file_opt->answer, icon_area_opt->answer, title, stats.min, stats.max, breakpoints, nbreaks, size, bcolor, - colors, default_width, frequencies, "area"); + colors, default_width, frequencies, ctype, "area"); break; } }