Code

added support for a right axis. New rrdgraph options are
authoroetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa>
Thu, 16 Oct 2008 06:58:08 +0000 (06:58 +0000)
committeroetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa>
Thu, 16 Oct 2008 06:58:08 +0000 (06:58 +0000)
 --right-axis scale:shift
 --right-axis-label label

this work was sponsored by voltwerk electronics GmbH

git-svn-id: svn://svn.oetiker.ch/rrdtool/branches/1.2@1611 a5681a0c-68f1-0310-ab6d-d61299d08faa

program/doc/rrdgraph.pod
program/src/Makefile.am
program/src/rrd_graph.c
program/src/rrd_graph.h
program/src/rrd_tool.c

index c98ce9c55062c2f7bd76f07d55d999395b6f50bb..02b9a82f44e107e13ee75fb86265b2ab3f361a19 100644 (file)
@@ -86,6 +86,14 @@ one pixel will silently be ignored.
 A horizontal string at the top of the graph and/or a vertically
 placed string at the left hand side of the graph.
 
+=item Right Axis
+
+[B<--right-axis> I<scale>B<:>I<shift>]
+[B<--right-axis-label> I<label>]
+
+A second axis will be drawn to the right of the graph. It is tied to the
+left axis via the scale and shift parameters. You can also define a label for the right axis.
+
 =item Size
 
 [B<-w>|B<--width> I<pixels>]
index 73f1113844c69c50fe45f08064bb5a6207aec9b2..61af91ca18214412eeccfd3e7a8cad504c7b21f4 100644 (file)
@@ -102,7 +102,7 @@ librrd_la_LIBADD          = librrdupd.la $(ALL_LIBS)
 # your package will not be binary compatible with any other release.
 #
 # see http://www.gnu.org/software/libtool/manual.html#SEC32 for explanation
-librrd_la_LDFLAGS         = -version-info 2:13:0
+librrd_la_LDFLAGS         = -version-info 2:14:0
 
 librrd_th_la_SOURCES         = $(UPD_C_FILES) $(RRD_C_FILES) rrd_thread_safe.c
 librrd_th_la_CFLAGS          = $(MULTITHREAD_CFLAGS)
index 6125c8a0b279b22d4a59377eba4c2a8ebaffcba7..41024eccc3925932b92aaf0802ceb79f248f712c 100644 (file)
@@ -1710,6 +1710,8 @@ int draw_horizontal_grid(image_desc_t *im)
     int sgrid = (int)( im->minval / im->ygrid_scale.gridstep - 1);
     int egrid = (int)( im->maxval / im->ygrid_scale.gridstep + 1);
     double MaxY;
+    double second_axis_magfact = 0;
+    char *second_axis_symb = "";
     scaledstep = im->ygrid_scale.gridstep/(double)im->magfact*(double)im->viewfactor;
     MaxY = scaledstep*(double)egrid;
     for (i = sgrid; i <= egrid; i++){
@@ -1744,6 +1746,28 @@ int draw_horizontal_grid(image_desc_t *im)
                     }
                 }
                 nlabels++;
+                if (im->second_axis_scale != 0){
+                        char graph_label_right[100];
+                        double sval = im->ygrid_scale.gridstep*(double)i/im->second_axis_scale+im->second_axis_shift;
+                        if (!second_axis_magfact){
+                                double dummy = im->ygrid_scale.gridstep*(double)(sgrid+egrid)/2.0/im->second_axis_scale+im->second_axis_shift;
+                                auto_scale(im,&dummy,&second_axis_symb,&second_axis_magfact);
+                        }
+                        sval /= second_axis_magfact;
+                       if(MaxY < 10) { 
+                           sprintf(graph_label_right,"%5.1f %s",sval,second_axis_symb);
+                          } else {
+                           sprintf(graph_label_right,"%5.0f %s",sval,second_axis_symb);
+                        }
+                        gfx_new_text ( im->canvas,
+                               X1+7, Y0,
+                               im->graph_col[GRC_FONT],
+                               im->text_prop[TEXT_PROP_AXIS].font,
+                               im->text_prop[TEXT_PROP_AXIS].size,
+                               im->tabwidth,0.0, GFX_H_LEFT, GFX_V_CENTER,
+                               graph_label_right );
+                }
 
                gfx_new_text ( im->canvas,
                               X0-im->text_prop[TEXT_PROP_AXIS].size, Y0,
@@ -1787,7 +1811,6 @@ double frexp10(double x, double *e) {
     return mnt;
 }
 
-
 /* logaritmic horizontal grid */
 int
 horizontal_log_grid(image_desc_t   *im)   
@@ -1877,16 +1900,38 @@ horizontal_log_grid(image_desc_t   *im)
             else
                 symbol = '?';
 
-                sprintf(graph_label,"%3.0f %c", pvalue, symbol);
-        } else
+            sprintf(graph_label,"%3.0f %c", pvalue, symbol);
+        } else {
             sprintf(graph_label,"%3.0e", value);
+        }
+        if (im->second_axis_scale != 0){
+                char graph_label_right[100];
+                if (im->extra_flags & FORCE_UNITS_SI) {
+                        double sval = value/im->second_axis_scale+im->second_axis_shift;
+                        double mfac = 1;
+                        char   *symb = "";
+                        auto_scale(im,&sval,&symb,&mfac);
+                        sprintf(graph_label_right,"%4.0f %s", sval,symb);
+                }
+                else {        
+                        sprintf(graph_label_right,"%3.0e", value/im->second_axis_scale+im->second_axis_shift);
+                }
+                gfx_new_text ( im->canvas,
+                               X1+7, Y0,
+                               im->graph_col[GRC_FONT],
+                               im->text_prop[TEXT_PROP_AXIS].font,
+                               im->text_prop[TEXT_PROP_AXIS].size,
+                               im->tabwidth,0.0, GFX_H_LEFT, GFX_V_CENTER,
+                               graph_label_right );
+        }
+            
         gfx_new_text ( im->canvas,
-            X0-im->text_prop[TEXT_PROP_AXIS].size, Y0,
-            im->graph_col[GRC_FONT],
-            im->text_prop[TEXT_PROP_AXIS].font,
-            im->text_prop[TEXT_PROP_AXIS].size,
-            im->tabwidth,0.0, GFX_H_RIGHT, GFX_V_CENTER,
-            graph_label );
+                       X0-im->text_prop[TEXT_PROP_AXIS].size, Y0,
+                       im->graph_col[GRC_FONT],
+                       im->text_prop[TEXT_PROP_AXIS].font,
+                       im->text_prop[TEXT_PROP_AXIS].size,
+                       im->tabwidth,0.0, GFX_H_RIGHT, GFX_V_CENTER,
+                       graph_label );
 
         /* minor grid */
         if(mid < 4 && exfrac == 1) {
@@ -2145,6 +2190,16 @@ axis_paint(
                    im->xorigin+0.5,    im->yorigin-im->ysize-7, /* LINEOFFSET */
                    im->graph_col[GRC_ARROW]);
 
+    if (im->second_axis_scale != 0){
+       gfx_new_line ( im->canvas, im->xorigin+im->xsize,im->yorigin+4,
+                         im->xorigin+im->xsize,im->yorigin-im->ysize-4,
+                         MGRIDWIDTH, im->graph_col[GRC_AXIS]);
+       gfx_new_area ( im->canvas, 
+                   im->xorigin+im->xsize-2,  im->yorigin-im->ysize-2,
+                   im->xorigin+im->xsize+3,  im->yorigin-im->ysize-2,
+                   im->xorigin+im->xsize+0.5,    im->yorigin-im->ysize-7, /* LINEOFFSET */
+                   im->graph_col[GRC_ARROW]);
+    }
 }
 
 void
@@ -2197,7 +2252,8 @@ grid_paint(image_desc_t   *im)
     }
 
     /* yaxis unit description */
-    gfx_new_text( im->canvas,
+    if (im->ylegend[0] != '\0'){
+            gfx_new_text( im->canvas,
                   10, (im->yorigin - im->ysize/2),
                   im->graph_col[GRC_FONT],
                   im->text_prop[TEXT_PROP_UNIT].font,
@@ -2205,7 +2261,23 @@ grid_paint(image_desc_t   *im)
                   RRDGRAPH_YLEGEND_ANGLE,
                   GFX_H_LEFT, GFX_V_CENTER,
                   im->ylegend);
-
+    }
+    if (im->second_axis_legend[0] != '\0'){
+            double Xylabel=gfx_get_text_width(im->canvas, 0,
+                        im->text_prop[TEXT_PROP_AXIS].font,
+                        im->text_prop[TEXT_PROP_AXIS].size,
+                        im->tabwidth,
+                        "0", 0) * im->unitslength
+                    + im->text_prop[TEXT_PROP_UNIT].size *2;
+            gfx_new_text( im->canvas,
+                  im->xorigin+im->xsize+Xylabel+4, (im->yorigin - im->ysize/2),
+                  im->graph_col[GRC_FONT],
+                  im->text_prop[TEXT_PROP_UNIT].font,
+                  im->text_prop[TEXT_PROP_UNIT].size, im->tabwidth, 
+                  RRDGRAPH_YLEGEND_ANGLE,
+                  GFX_H_LEFT, GFX_V_CENTER,
+                  im->second_axis_legend);
+    }        
     /* graph title */
     gfx_new_text( im->canvas,
                   im->ximg/2, im->text_prop[TEXT_PROP_TITLE].size*1.3+4,
@@ -2215,14 +2287,15 @@ grid_paint(image_desc_t   *im)
                   GFX_H_CENTER, GFX_V_CENTER,
                   im->title);
     /* rrdtool 'logo' */
-    gfx_new_text( im->canvas,
+    if (!(im->extra_flags & NO_RRDTOOL_TAG)){
+            gfx_new_text( im->canvas,
                   im->ximg-7, 7,
                   ( im->graph_col[GRC_FONT] & 0xffffff00 ) | 0x00000044,
                   im->text_prop[TEXT_PROP_AXIS].font,
                   5.5, im->tabwidth, 270,
                   GFX_H_RIGHT, GFX_V_TOP,
                   "RRDTOOL / TOBI OETIKER");
-
+    }
     /* graph watermark */
     if(im->watermark[0] != '\0') {
         gfx_new_text( im->canvas,
@@ -2478,6 +2551,13 @@ graph_size_location(image_desc_t *im, int elements
     */
     im->ximg = Xylabel + Xmain + 2 * Xspacing;
 
+    if (im->second_axis_scale != 0){
+        im->ximg += Xylabel + Xspacing;
+    }
+    if (im->extra_flags & NO_RRDTOOL_TAG){
+        im->ximg -= Xspacing;
+    }
+
 #ifdef WITH_PIECHART
     im->ximg  += Xpie;
 #endif
@@ -2496,6 +2576,10 @@ graph_size_location(image_desc_t *im, int elements
         im->ximg += Xvertical;
         im->xorigin += Xvertical;
     }
+    if (im->second_axis_legend[0] != '\0' ) {
+        im->ximg += Xvertical;
+    }
+
     xtr(im,0);
 
     /* The vertical size is interesting... we need to compare
@@ -3133,6 +3217,9 @@ rrd_graph_init(image_desc_t *im)
     im->ysize = 100;
     im->step = 0;
     im->ylegend[0] = '\0';
+    im->second_axis_scale = 0; /* 0 disables it */
+    im->second_axis_shift = 0; /* no shift by default */
+    im->second_axis_legend[0] = '\0';
     im->title[0] = '\0';
     im->watermark[0] = '\0';
     im->minval = DNAN;
@@ -3259,6 +3346,9 @@ rrd_graph_options(int argc, char *argv[],image_desc_t *im)
             {"font-smoothing-threshold", required_argument, 0, 'B'},
             {"watermark",  required_argument, 0,  'W'},
             {"alt-y-mrtg", no_argument,       0,  1000}, /* this has no effect it is just here to save old apps from crashing when they use it */
+            {"disable-rrdtool-tag", no_argument,  0,  1001},
+            {"right-axis", required_argument,  0,  1002},
+            {"right-axis-label", required_argument,  0,  1003},
             {0,0,0,0}};
         int option_index = 0;
         int opt;
@@ -3296,6 +3386,9 @@ rrd_graph_options(int argc, char *argv[],image_desc_t *im)
         case 'F':
             im->extra_flags |= FORCE_RULES_LEGEND;
             break;
+        case 1001:
+            im->extra_flags |= NO_RRDTOOL_TAG;
+            break;
         case LONGOPT_UNITS_SI:
             if(im->extra_flags & FORCE_UNITS) {
                 rrd_set_error("--units can only be used once!");
@@ -3394,6 +3487,25 @@ rrd_graph_options(int argc, char *argv[],image_desc_t *im)
                 return;
             }
             break;
+        case 1002: /* right y axis */
+
+            if(sscanf(optarg,
+                      "%lf:%lf",
+                      &im->second_axis_scale,
+                      &im->second_axis_shift) == 2) {
+                if(im->second_axis_scale==0){
+                    rrd_set_error("the second_axis_scale  must not be 0");
+                    return;
+                }
+            } else {
+                rrd_set_error("invalid right-axis format expected scale:shift");
+                return;
+            }
+            break;
+        case 1003:
+            strncpy(im->second_axis_legend,optarg,150);
+            im->second_axis_legend[150]='\0';
+            break;
         case 'v':
             strncpy(im->ylegend,optarg,150);
             im->ylegend[150]='\0';
index e83b442aab6e0d6bb71ac1574ec858f5bd10359b..b39552a7ab8b951b67b35d023103b0fcefa145a8 100644 (file)
@@ -19,6 +19,7 @@
 
 #define FORCE_UNITS 0x100        /* mask for all FORCE_UNITS_* flags */
 #define FORCE_UNITS_SI 0x100     /* force use of SI units in Y axis (no effect in linear graph, SI instead of E in log graph) */
+#define NO_RRDTOOL_TAG 0x200    /* disable the rrdtool tag */
 
 enum tmt_en {TMT_SECOND=0,TMT_MINUTE,TMT_HOUR,TMT_DAY,
             TMT_WEEK,TMT_MONTH,TMT_YEAR};
@@ -165,6 +166,10 @@ typedef struct image_desc_t {
     xlab_t         xlab_user;      /* user defined labeling for xaxis */
     char           xlab_form[210]; /* format for the label on the xaxis */
 
+    double         second_axis_scale; /* relative to the first axis (0 to disable) */
+    double         second_axis_shift; /* how much is it shifted vs the first axis */
+    char           second_axis_legend[210]; /* label to put on the seond axis */
+
     double         ygridstep;      /* user defined step for y grid */
     int            ylabfact;       /* every how many y grid shall a label be written ? */
     double         tabwidth;       /* tabwdith */
index dfd9890ff2157ac9b333eeb6b664fbc3787b66a5..bcb1d07cb8751700d4db7b7a3ccb35cee60a3ca2 100644 (file)
@@ -99,6 +99,7 @@ void PrintUsage(char *cmd)
           "\t\t[-Y|--alt-y-grid]\n"
           "\t\t[-y|--y-grid y-axis grid and label]\n"
           "\t\t[-v|--vertical-label string] [-w|--width pixels]\n"
+          "\t\t[--right-axis scale:shift] [--right-axis-label label]\n"
           "\t\t[-h|--height pixels] [-o|--logarithmic]\n"
           "\t\t[-u|--upper-limit value] [-z|--lazy]\n"
           "\t\t[-l|--lower-limit value] [-r|--rigid]\n"