gfio: add graph axis unit change notification callbacks
[fio.git] / tickmarks.c
index 7766acd29a6e13af8d795451518383e9aa709408..2959f0267ccb6116f1f8726c51c37f93057b9a75 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <math.h>
 #include <malloc.h>
+#include <string.h>
 
 /* 
  * adapted from Paul Heckbert's algorithm on p 657-659 of
@@ -38,7 +39,49 @@ static double nicenum(double x, int round)
        return 10.0 * pow(10.0, exp);
 }
 
-int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm)
+static void shorten(struct tickmark *tm, int nticks, int *power_of_ten,
+                       int use_KMG_symbols)
+{
+       int i, l, minshorten;
+       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) {
+                       *power_of_ten = 9;
+                       shorten_char = use_KMG_symbols ? 'G' : '\0';
+               } else if (6 < minshorten && l > 6 &&
+                               strcmp(&str[l - 6], "000000") == 0) {
+                       *power_of_ten = 6;
+                       shorten_char = use_KMG_symbols ? 'M' : '\0';
+               } else if (l > 3 && strcmp(&str[l - 3], "000") == 0) {
+                       *power_of_ten = 3;
+                       shorten_char = use_KMG_symbols ? 'K': '\0';
+               } else {
+                       *power_of_ten = 0;
+               }
+
+               if (*power_of_ten < minshorten)
+                       minshorten = *power_of_ten;
+       }
+
+       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';
+       }
+}
+
+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;
@@ -64,6 +107,7 @@ int calc_tickmarks(double min, double max, int nticks, struct tickmark **tm)
                sprintf((*tm)[i].string, str, x);
                i++;
        }
+       shorten(*tm, i, power_of_ten, use_KMG_symbols);
        return i;
 }