gfio: do not mix K M and G when shortening axis labels
authorStephen M. Cameron <stephenmcameron@gmail.com>
Sun, 11 Mar 2012 10:33:46 +0000 (11:33 +0100)
committerJens Axboe <axboe@kernel.dk>
Sun, 11 Mar 2012 10:33:46 +0000 (11:33 +0100)
That is, do not scale axis labels by differing amounts
like 900K, 1M, 1100K, instead: 900k, 1000k, 1100k.

Signed-off-by: Stephen M. Cameron <stephenmcameron@gmail.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
tickmarks.c

index 21f77da0f7d8b61593b1ea45946a5f11cf95ac31..52fd556817a8102ef95d8d74a21a89db5d17a6a4 100644 (file)
@@ -39,20 +39,43 @@ static double nicenum(double x, int round)
        return 10.0 * pow(10.0, exp);
 }
 
-static void shorten(char *str)
+static void shorten(struct tickmark *tm, int nticks)
 {
-       int l;
-
-       l = strlen(str);
-       if (l > 9 && strcmp(&str[l - 9], "000000000") == 0) {
-               str[l - 9] = 'G';
-               str[l - 8] = '\0';
-       } else if (l > 6 && strcmp(&str[l - 6], "000000") == 0) {
-               str[l - 6] = 'M';
-               str[l - 5] = '\0';
-       } else if (l > 3 && strcmp(&str[l - 3], "000") == 0) {
-               str[l - 3] = 'K';
-               str[l - 2] = '\0';
+       int i, l, minshorten, can_shorten_by;
+       char *str;
+       char shorten_char = '?';
+
+       minshorten = 100;
+       for (i = 0; i < nticks; i++) {
+               str = tm[i].string;
+               l = strlen(str);
+
+               if (l > 9 && strcmp(&str[l - 9], "000000000") == 0) {
+                       can_shorten_by = 9;
+                       shorten_char = 'G';
+               } else if (6 < minshorten && l > 6 &&
+                               strcmp(&str[l - 6], "000000") == 0) {
+                       can_shorten_by = 6;
+                       shorten_char = 'M';
+               } else if (l > 3 && strcmp(&str[l - 3], "000") == 0) {
+                       can_shorten_by = 3;
+                       shorten_char = 'K';
+               } else {
+                       can_shorten_by = 0;
+               }
+
+               if (can_shorten_by < minshorten)
+                       minshorten = can_shorten_by;
+       }
+
+       if (minshorten == 0)
+               return;
+
+       for (i = 0; i < nticks; i++) {
+               str = tm[i].string;
+               l = strlen(str);
+               str[l - minshorten] = shorten_char;
+               str[l - minshorten + 1] = '\0';
        }
 }
 
@@ -80,9 +103,9 @@ int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm)
        for (x = graphmin; x < graphmax + 0.5 * d; x += d) {
                (*tm)[i].value = x;
                sprintf((*tm)[i].string, str, x);
-               shorten((*tm)[i].string);
                i++;
        }
+       shorten(*tm, i);
        return i;
 }