New, hopefully better, implementation of PART drawing
[rrdtool.git] / src / rrd_graph.c
index 5597c49..c7ba63e 100644 (file)
@@ -174,6 +174,7 @@ enum gf_en gf_conv(char *string){
     conv_if(DEF,GF_DEF)
     conv_if(CDEF,GF_CDEF)
     conv_if(VDEF,GF_VDEF)
+    conv_if(PART,GF_PART)
     
     return (-1);
 }
@@ -937,6 +938,7 @@ data_proc( image_desc_t *im ){
            case GF_DEF:               
            case GF_CDEF:
            case GF_VDEF:
+           case GF_PART:
                break;
            }
        }
@@ -1209,6 +1211,7 @@ print_calc(image_desc_t *im, char ***prdata)
        case GF_LINE:
        case GF_AREA:
        case GF_TICK:
+       case GF_PART:
        case GF_STACK:
        case GF_HRULE:
        case GF_VRULE:
@@ -1324,9 +1327,6 @@ leg_place(image_desc_t *im)
                    continue;
                im->gdes[ii].leg_x = leg_x;
                im->gdes[ii].leg_y = leg_y;
-printf("DEBUG: using font %s with width %lf\n",
-       im->text_prop[TEXT_PROP_LEGEND].font,
-       im->text_prop[TEXT_PROP_LEGEND].size);
                leg_x += 
                 gfx_get_text_width(leg_x,im->text_prop[TEXT_PROP_LEGEND].font,
                                      im->text_prop[TEXT_PROP_LEGEND].size,
@@ -1851,6 +1851,8 @@ graph_paint(image_desc_t *im, char ***calcpr)
 {
   int i,ii;
   int lazy =     lazy_check(im);
+  int piechart = 0, PieSize, PieCenterX, PieCenterY;
+  double PieStart=0.0;
   FILE  *fo;
   gfx_canvas_t *canvas;
   gfx_node_t *node;
@@ -1907,8 +1909,30 @@ graph_paint(image_desc_t *im, char ***calcpr)
   if(im->title[0] != '\0')
     im->yorigin += im->text_prop[TEXT_PROP_TITLE].size*3+4;
   
-  im->xgif=20+im->xsize + im->xorigin;
+  im->xgif= 20 +im->xsize + im->xorigin;
   im->ygif= im->yorigin+2* im->text_prop[TEXT_PROP_LEGEND].size;
+
+  /* check if we need to draw a piechart */
+  for(i=0;i<im->gdes_c;i++){
+    if (im->gdes[i].gf == GF_PART) {
+      piechart=1;
+      break;
+    }
+  }
+
+  if (piechart) {
+       /* allocate enough space for the piechart itself (PieSize), 20%
+       ** more for the background and an additional 50 pixels spacing.
+       */
+    if (im->xsize < im->ysize)
+       PieSize = im->xsize;
+    else
+       PieSize = im->ysize;
+    im->xgif += PieSize*1.2 + 50;
+
+    PieCenterX = im->xorigin + im->xsize + 50 + PieSize*0.6;
+    PieCenterY = im->yorigin - PieSize*0.5;
+  }
   
   /* determine where to place the legends onto the graphics.
      and set im->ygif to match space requirements for text */
@@ -1936,7 +1960,24 @@ graph_paint(image_desc_t *im, char ***calcpr)
   
   gfx_add_point(node,im->xorigin, im->yorigin - im->ysize);
 
-  
+  if (piechart) {
+#if 1
+    node=gfx_arc_sect (canvas,
+       PieCenterX,PieCenterY,
+       PieSize*0.6, PieSize*0.6,       /* 20% more as background */
+       0,M_PI*2,
+       im->graph_col[GRC_CANVAS]);
+#else
+    node=gfx_new_area ( canvas,
+       PieCenterX-0.6*PieSize, PieCenterY-0.6*PieSize,
+       PieCenterX+0.6*PieSize, PieCenterY-0.6*PieSize,
+       PieCenterX+0.6*PieSize, PieCenterY+0.6*PieSize,
+       im->graph_col[GRC_CANVAS]);
+    gfx_add_point(node,
+       PieCenterX-0.6*PieSize, PieCenterY+0.6*PieSize);
+#endif
+  }
+
   if (im->minval > 0.0)
     areazero = im->minval;
   if (im->maxval < 0.0)
@@ -1945,7 +1986,7 @@ graph_paint(image_desc_t *im, char ***calcpr)
   axis_paint(im,canvas);
 
 
-  for(i=0;i<im->gdes_c;i++){    
+  for(i=0;i<im->gdes_c;i++){
     switch(im->gdes[i].gf){
     case GF_CDEF:
     case GF_VDEF:
@@ -2064,6 +2105,20 @@ graph_paint(image_desc_t *im, char ***calcpr)
       } 
       lastgdes = &(im->gdes[i]);                         
       break;
+    case GF_PART:
+      if(isnan(im->gdes[i].yrule)) /* fetch variable */
+       im->gdes[i].yrule = im->gdes[im->gdes[i].vidx].vf.val;
+     
+      if (finite(im->gdes[i].yrule)) { /* even the fetched var can be NaN */
+       node=gfx_arc_sect(canvas,
+               PieCenterX, PieCenterY,
+               PieSize/2,PieSize/2,
+               M_PI*2.0*PieStart/100.0,
+               M_PI*2.0*(PieStart+im->gdes[i].yrule)/100.0,
+               im->gdes[i].col);
+       PieStart += im->gdes[i].yrule;
+      }
+      break;
     } /* switch */
   }
   grid_paint(im,canvas);
@@ -2073,7 +2128,6 @@ graph_paint(image_desc_t *im, char ***calcpr)
     
     switch(im->gdes[i].gf){
     case GF_HRULE:
-      printf("DEBUG: HRULE at %f\n",im->gdes[i].yrule);
       if(isnan(im->gdes[i].yrule)) { /* fetch variable */
         im->gdes[i].yrule = im->gdes[im->gdes[i].vidx].vf.val;
       };
@@ -2532,8 +2586,6 @@ rrd_graph_options(int argc, char *argv[],image_desc_t *im)
                                prop,&size,font) == 3){
                int sindex;
                if((sindex=text_prop_conv(prop)) != -1){
-printf("DEBUG: setting all to the default of font %s with width %lf\n",
-font,size);
                    im->text_prop[sindex].size=size;              
                    im->text_prop[sindex].font=font;
                    if (sindex==0) { /* the default */
@@ -2636,7 +2688,7 @@ rrd_graph_script(int argc, char *argv[], image_desc_t *im)
        /* function:newvname=string[:ds-name:CF]        for xDEF
        ** function:vname[#color[:string]]              for LINEx,AREA,STACK
        ** function:vname#color[:num[:string]]          for TICK
-       ** function:vname-or-num#color[:string]         for xRULE
+       ** function:vname-or-num#color[:string]         for xRULE,PART
        ** function:vname:CF:string                     for xPRINT
        ** function:string                              for COMMENT
        */
@@ -2665,6 +2717,7 @@ rrd_graph_script(int argc, char *argv[], image_desc_t *im)
                if (rrd_graph_legend(gdp,&line[argstart])==0)
                    rrd_set_error("Cannot parse comment in line: %s",line);
                break;
+           case GF_PART:
            case GF_VRULE:
            case GF_HRULE:
                j=k=l=m=0;