gfio: add graph axis unit change notification callbacks
authorStephen M. Cameron <stephenmcameron@gmail.com>
Sun, 11 Mar 2012 10:35:10 +0000 (11:35 +0100)
committerJens Axboe <axboe@kernel.dk>
Sun, 11 Mar 2012 10:35:10 +0000 (11:35 +0100)
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 <scameron@beardog.cce.hp.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
graph.c
graph.h
tickmarks.c
tickmarks.h

diff --git a/graph.c b/graph.c
index 1194510e441a111a713bdd6f2202d5af6eed6107..de65055b99c1ffbb0dc00c88b805629c9bfd5bc9 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -59,6 +59,8 @@ struct graph {
        struct graph_label *tail;
        int per_label_limit;
        const char *font;
        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)
 };
 
 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;
 }
 
        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;
 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;
 {
        struct tickmark *tm;
        double tx;
-       int i;
+       int i, power_of_ten;
        static double dash[] = { 1.0, 2.0 };
 
        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;
 
        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;
 {
        struct tickmark *tm;
        double ty;
-       int i;
+       int i, power_of_ten;
        static double dash[] = { 2.0, 2.0 };
 
        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);
 
        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 7c3e862b5f486d7a6024f3405a6494f7f380a615..e04b5fffacf76b9972871cf35f6ffe38260cfd2b 100644 (file)
--- 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);
 
                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
 
 
 #endif
 
index 52fd556817a8102ef95d8d74a21a89db5d17a6a4..2959f0267ccb6116f1f8726c51c37f93057b9a75 100644 (file)
@@ -39,9 +39,10 @@ static double nicenum(double x, int round)
        return 10.0 * pow(10.0, exp);
 }
 
        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 = '?';
 
        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) {
                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) {
                } 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) {
                } 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 {
                } 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)
        }
 
        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;
 {
        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++;
        }
                sprintf((*tm)[i].string, str, x);
                i++;
        }
-       shorten(*tm, i);
+       shorten(*tm, i, power_of_ten, use_KMG_symbols);
        return i;
 }
 
        return i;
 }
 
index ab817a76b77afe445126831bfeaf9e78442d3619..a8c1bf9f16485a1768875dd4a892bdd10a4e6a95 100644 (file)
@@ -6,6 +6,7 @@ struct tickmark {
        char string[20];
 };
 
        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
 
 #endif