From: Stephen M. Cameron Date: Sun, 11 Mar 2012 10:35:10 +0000 (+0100) Subject: gfio: add graph axis unit change notification callbacks X-Git-Tag: gfio-0.1~188 X-Git-Url: https://git.kernel.dk/?p=fio.git;a=commitdiff_plain;h=7175d91deff20b1408450c231b2b445ea28f7f29;hp=09ad20ff60eb8c11edf407c4060062dae187f5e7 gfio: add graph axis unit change notification callbacks This enables code that uses the graph functions to change the axis titles on the fly to display unit information which matches how the tick labels are shortened. Signed-off-by: Stephen M. Cameron Signed-off-by: Jens Axboe --- diff --git a/graph.c b/graph.c index 1194510e..de65055b 100644 --- a/graph.c +++ b/graph.c @@ -59,6 +59,8 @@ struct graph { struct graph_label *tail; int per_label_limit; const char *font; + graph_axis_unit_change_callback x_axis_unit_change_callback; + graph_axis_unit_change_callback y_axis_unit_change_callback; }; void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim) @@ -80,6 +82,16 @@ struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font) return g; } +void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) +{ + g->x_axis_unit_change_callback = f; +} + +void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) +{ + g->y_axis_unit_change_callback = f; +} + static int count_labels(struct graph_label *labels) { int count = 0; @@ -303,10 +315,13 @@ static void graph_draw_x_ticks(struct graph *g, cairo_t *cr, { struct tickmark *tm; double tx; - int i; + int i, power_of_ten; static double dash[] = { 1.0, 2.0 }; - nticks = calc_tickmarks(minx, maxx, nticks, &tm); + nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten, + g->x_axis_unit_change_callback == NULL); + if (g->x_axis_unit_change_callback) + g->x_axis_unit_change_callback(g, power_of_ten); for (i = 0; i < nticks; i++) { tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1; @@ -341,10 +356,13 @@ static void graph_draw_y_ticks(struct graph *g, cairo_t *cr, { struct tickmark *tm; double ty; - int i; + int i, power_of_ten; static double dash[] = { 2.0, 2.0 }; - nticks = calc_tickmarks(miny, maxy, nticks, &tm); + nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten, + g->y_axis_unit_change_callback == NULL); + if (g->y_axis_unit_change_callback) + g->y_axis_unit_change_callback(g, power_of_ten); for (i = 0; i < nticks; i++) { ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1); diff --git a/graph.h b/graph.h index 7c3e862b..e04b5fff 100644 --- a/graph.h +++ b/graph.h @@ -21,6 +21,9 @@ void graph_set_color(struct graph *g, const char *label, double red, double green, double blue); void graph_free(struct graph *bg); +typedef void (*graph_axis_unit_change_callback)(struct graph *g, int power_of_ten); +void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f); +void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f); #endif diff --git a/tickmarks.c b/tickmarks.c index 52fd5568..2959f026 100644 --- a/tickmarks.c +++ b/tickmarks.c @@ -39,9 +39,10 @@ static double nicenum(double x, int round) return 10.0 * pow(10.0, exp); } -static void shorten(struct tickmark *tm, int nticks) +static void shorten(struct tickmark *tm, int nticks, int *power_of_ten, + int use_KMG_symbols) { - int i, l, minshorten, can_shorten_by; + int i, l, minshorten; char *str; char shorten_char = '?'; @@ -51,21 +52,21 @@ static void shorten(struct tickmark *tm, int nticks) l = strlen(str); if (l > 9 && strcmp(&str[l - 9], "000000000") == 0) { - can_shorten_by = 9; - shorten_char = 'G'; + *power_of_ten = 9; + shorten_char = use_KMG_symbols ? 'G' : '\0'; } else if (6 < minshorten && l > 6 && strcmp(&str[l - 6], "000000") == 0) { - can_shorten_by = 6; - shorten_char = 'M'; + *power_of_ten = 6; + shorten_char = use_KMG_symbols ? 'M' : '\0'; } else if (l > 3 && strcmp(&str[l - 3], "000") == 0) { - can_shorten_by = 3; - shorten_char = 'K'; + *power_of_ten = 3; + shorten_char = use_KMG_symbols ? 'K': '\0'; } else { - can_shorten_by = 0; + *power_of_ten = 0; } - if (can_shorten_by < minshorten) - minshorten = can_shorten_by; + if (*power_of_ten < minshorten) + minshorten = *power_of_ten; } if (minshorten == 0) @@ -79,7 +80,8 @@ static void shorten(struct tickmark *tm, int nticks) } } -int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm) +int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm, + int *power_of_ten, int use_KMG_symbols) { char str[100]; int nfrac; @@ -105,7 +107,7 @@ int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm) sprintf((*tm)[i].string, str, x); i++; } - shorten(*tm, i); + shorten(*tm, i, power_of_ten, use_KMG_symbols); return i; } diff --git a/tickmarks.h b/tickmarks.h index ab817a76..a8c1bf9f 100644 --- a/tickmarks.h +++ b/tickmarks.h @@ -6,6 +6,7 @@ struct tickmark { char string[20]; }; -int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm); +int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm, + int *power_of_ten, int use_KMG_symbols); #endif