1 /****************************************************************************
2 * RRDtool 1.2.19 Copyright by Tobi Oetiker, 1997-2007
3 ****************************************************************************
4 * rrd__graph.c produce graphs from data in rrdfiles
5 ****************************************************************************/
15 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
28 #include "rrd_graph.h"
30 /* some constant definitions */
34 #ifndef RRD_DEFAULT_FONT
35 /* there is special code later to pick Cour.ttf when running on windows */
36 #define RRD_DEFAULT_FONT "DejaVuSansMono-Roman.ttf"
39 text_prop_t text_prop[] = {
40 { 8.0, RRD_DEFAULT_FONT }, /* default */
41 { 9.0, RRD_DEFAULT_FONT }, /* title */
42 { 7.0, RRD_DEFAULT_FONT }, /* axis */
43 { 8.0, RRD_DEFAULT_FONT }, /* unit */
44 { 8.0, RRD_DEFAULT_FONT } /* legend */
48 {0, 0, TMT_SECOND,30, TMT_MINUTE,5, TMT_MINUTE,5, 0,"%H:%M"},
49 {2, 0, TMT_MINUTE,1, TMT_MINUTE,5, TMT_MINUTE,5, 0,"%H:%M"},
50 {5, 0, TMT_MINUTE,2, TMT_MINUTE,10, TMT_MINUTE,10, 0,"%H:%M"},
51 {10, 0, TMT_MINUTE,5, TMT_MINUTE,20, TMT_MINUTE,20, 0,"%H:%M"},
52 {30, 0, TMT_MINUTE,10, TMT_HOUR,1, TMT_HOUR,1, 0,"%H:%M"},
53 {60, 0, TMT_MINUTE,30, TMT_HOUR,2, TMT_HOUR,2, 0,"%H:%M"},
54 {60, 24*3600, TMT_MINUTE,30, TMT_HOUR,2, TMT_HOUR,4, 0,"%a %H:%M"},
55 {180, 0, TMT_HOUR,1, TMT_HOUR,6, TMT_HOUR,6, 0,"%H:%M"},
56 {180, 24*3600, TMT_HOUR,1, TMT_HOUR,6, TMT_HOUR,12, 0,"%a %H:%M"},
57 /*{300, 0, TMT_HOUR,3, TMT_HOUR,12, TMT_HOUR,12, 12*3600,"%a %p"}, this looks silly*/
58 {600, 0, TMT_HOUR,6, TMT_DAY,1, TMT_DAY,1, 24*3600,"%a"},
59 {1200, 0, TMT_HOUR,6, TMT_DAY,1, TMT_DAY,1, 24*3600,"%d"},
60 {1800, 0, TMT_HOUR,12, TMT_DAY,1, TMT_DAY,2, 24*3600,"%a %d"},
61 {2400, 0, TMT_HOUR,12, TMT_DAY,1, TMT_DAY,2, 24*3600,"%a"},
62 {3600, 0, TMT_DAY,1, TMT_WEEK,1, TMT_WEEK,1, 7*24*3600,"Week %V"},
63 {3*3600, 0, TMT_WEEK,1, TMT_MONTH,1, TMT_WEEK,2, 7*24*3600,"Week %V"},
64 {6*3600, 0, TMT_MONTH,1, TMT_MONTH,1, TMT_MONTH,1, 30*24*3600,"%b"},
65 {48*3600, 0, TMT_MONTH,1, TMT_MONTH,3, TMT_MONTH,3, 30*24*3600,"%b"},
66 {315360, 0, TMT_MONTH,3, TMT_YEAR,1, TMT_YEAR,1, 365*24*3600,"%Y"},
67 {10*24*3600, 0, TMT_YEAR,1, TMT_YEAR,1, TMT_YEAR,1, 365*24*3600,"%y"},
68 {-1,0,TMT_MONTH,0,TMT_MONTH,0,TMT_MONTH,0,0,""}
71 /* sensible y label intervals ...*/
89 gfx_color_t graph_col[] = /* default colors */
90 { 0xFFFFFFFF, /* canvas */
91 0xF0F0F0FF, /* background */
92 0xD0D0D0FF, /* shade A */
93 0xA0A0A0FF, /* shade B */
94 0x90909080, /* grid */
95 0xE0505080, /* major grid */
96 0x000000FF, /* font */
97 0x802020FF, /* arrow */
98 0x202020FF, /* axis */
99 0x000000FF /* frame */
106 # define DPRINT(x) (void)(printf x, printf("\n"))
112 /* initialize with xtr(im,0); */
114 xtr(image_desc_t *im,time_t mytime){
117 pixie = (double) im->xsize / (double)(im->end - im->start);
120 return (int)((double)im->xorigin
121 + pixie * ( mytime - im->start ) );
124 /* translate data values into y coordinates */
126 ytr(image_desc_t *im, double value){
131 pixie = (double) im->ysize / (im->maxval - im->minval);
133 pixie = (double) im->ysize / (log10(im->maxval) - log10(im->minval));
135 } else if(!im->logarithmic) {
136 yval = im->yorigin - pixie * (value - im->minval);
138 if (value < im->minval) {
141 yval = im->yorigin - pixie * (log10(value) - log10(im->minval));
144 /* make sure we don't return anything too unreasonable. GD lib can
145 get terribly slow when drawing lines outside its scope. This is
146 especially problematic in connection with the rigid option */
148 /* keep yval as-is */
149 } else if (yval > im->yorigin) {
150 yval = im->yorigin +0.00001;
151 } else if (yval < im->yorigin - im->ysize){
152 yval = im->yorigin - im->ysize - 0.00001;
159 /* conversion function for symbolic entry names */
162 #define conv_if(VV,VVV) \
163 if (strcmp(#VV, string) == 0) return VVV ;
165 enum gf_en gf_conv(char *string){
167 conv_if(PRINT,GF_PRINT)
168 conv_if(GPRINT,GF_GPRINT)
169 conv_if(COMMENT,GF_COMMENT)
170 conv_if(HRULE,GF_HRULE)
171 conv_if(VRULE,GF_VRULE)
172 conv_if(LINE,GF_LINE)
173 conv_if(AREA,GF_AREA)
174 conv_if(STACK,GF_STACK)
175 conv_if(TICK,GF_TICK)
177 conv_if(CDEF,GF_CDEF)
178 conv_if(VDEF,GF_VDEF)
180 conv_if(PART,GF_PART)
182 conv_if(XPORT,GF_XPORT)
183 conv_if(SHIFT,GF_SHIFT)
188 enum gfx_if_en if_conv(char *string){
198 enum tmt_en tmt_conv(char *string){
200 conv_if(SECOND,TMT_SECOND)
201 conv_if(MINUTE,TMT_MINUTE)
202 conv_if(HOUR,TMT_HOUR)
204 conv_if(WEEK,TMT_WEEK)
205 conv_if(MONTH,TMT_MONTH)
206 conv_if(YEAR,TMT_YEAR)
210 enum grc_en grc_conv(char *string){
212 conv_if(BACK,GRC_BACK)
213 conv_if(CANVAS,GRC_CANVAS)
214 conv_if(SHADEA,GRC_SHADEA)
215 conv_if(SHADEB,GRC_SHADEB)
216 conv_if(GRID,GRC_GRID)
217 conv_if(MGRID,GRC_MGRID)
218 conv_if(FONT,GRC_FONT)
219 conv_if(ARROW,GRC_ARROW)
220 conv_if(AXIS,GRC_AXIS)
221 conv_if(FRAME,GRC_FRAME)
226 enum text_prop_en text_prop_conv(char *string){
228 conv_if(DEFAULT,TEXT_PROP_DEFAULT)
229 conv_if(TITLE,TEXT_PROP_TITLE)
230 conv_if(AXIS,TEXT_PROP_AXIS)
231 conv_if(UNIT,TEXT_PROP_UNIT)
232 conv_if(LEGEND,TEXT_PROP_LEGEND)
240 im_free(image_desc_t *im)
244 if (im == NULL) return 0;
245 for(i=0;i<(unsigned)im->gdes_c;i++){
246 if (im->gdes[i].data_first){
247 /* careful here, because a single pointer can occur several times */
248 free (im->gdes[i].data);
249 if (im->gdes[i].ds_namv){
250 for (ii=0;ii<im->gdes[i].ds_cnt;ii++)
251 free(im->gdes[i].ds_namv[ii]);
252 free(im->gdes[i].ds_namv);
255 free (im->gdes[i].p_data);
256 free (im->gdes[i].rpnp);
259 gfx_destroy(im->canvas);
263 /* find SI magnitude symbol for the given number*/
266 image_desc_t *im, /* image description */
273 char *symbol[] = {"a", /* 10e-18 Atto */
274 "f", /* 10e-15 Femto */
275 "p", /* 10e-12 Pico */
276 "n", /* 10e-9 Nano */
277 "u", /* 10e-6 Micro */
278 "m", /* 10e-3 Milli */
283 "T", /* 10e12 Tera */
284 "P", /* 10e15 Peta */
290 if (*value == 0.0 || isnan(*value) ) {
294 sindex = floor(log(fabs(*value))/log((double)im->base));
295 *magfact = pow((double)im->base, (double)sindex);
296 (*value) /= (*magfact);
298 if ( sindex <= symbcenter && sindex >= -symbcenter) {
299 (*symb_ptr) = symbol[sindex+symbcenter];
307 static char si_symbol[] = {
308 'a', /* 10e-18 Atto */
309 'f', /* 10e-15 Femto */
310 'p', /* 10e-12 Pico */
311 'n', /* 10e-9 Nano */
312 'u', /* 10e-6 Micro */
313 'm', /* 10e-3 Milli */
318 'T', /* 10e12 Tera */
319 'P', /* 10e15 Peta */
322 static const int si_symbcenter = 6;
324 /* find SI magnitude symbol for the numbers on the y-axis*/
327 image_desc_t *im /* image description */
331 double digits,viewdigits=0;
333 digits = floor( log( max( fabs(im->minval),fabs(im->maxval)))/log((double)im->base));
335 if (im->unitsexponent != 9999) {
336 /* unitsexponent = 9, 6, 3, 0, -3, -6, -9, etc */
337 viewdigits = floor(im->unitsexponent / 3);
342 im->magfact = pow((double)im->base , digits);
345 printf("digits %6.3f im->magfact %6.3f\n",digits,im->magfact);
348 im->viewfactor = im->magfact / pow((double)im->base , viewdigits);
350 if ( ((viewdigits+si_symbcenter) < sizeof(si_symbol)) &&
351 ((viewdigits+si_symbcenter) >= 0) )
352 im->symbol = si_symbol[(int)viewdigits+si_symbcenter];
357 /* move min and max values around to become sensible */
360 expand_range(image_desc_t *im)
362 double sensiblevalues[] ={1000.0,900.0,800.0,750.0,700.0,
363 600.0,500.0,400.0,300.0,250.0,
364 200.0,125.0,100.0,90.0,80.0,
365 75.0,70.0,60.0,50.0,40.0,30.0,
366 25.0,20.0,10.0,9.0,8.0,
367 7.0,6.0,5.0,4.0,3.5,3.0,
368 2.5,2.0,1.8,1.5,1.2,1.0,
369 0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0,-1};
371 double scaled_min,scaled_max;
378 printf("Min: %6.2f Max: %6.2f MagFactor: %6.2f\n",
379 im->minval,im->maxval,im->magfact);
382 if (isnan(im->ygridstep)){
383 if(im->extra_flags & ALTAUTOSCALE) {
384 /* measure the amplitude of the function. Make sure that
385 graph boundaries are slightly higher then max/min vals
386 so we can see amplitude on the graph */
389 delt = im->maxval - im->minval;
391 fact = 2.0 * pow(10.0,
392 floor(log10(max(fabs(im->minval), fabs(im->maxval))/im->magfact)) - 2);
394 adj = (fact - delt) * 0.55;
396 printf("Min: %6.2f Max: %6.2f delt: %6.2f fact: %6.2f adj: %6.2f\n", im->minval, im->maxval, delt, fact, adj);
402 else if(im->extra_flags & ALTAUTOSCALE_MAX) {
403 /* measure the amplitude of the function. Make sure that
404 graph boundaries are slightly higher than max vals
405 so we can see amplitude on the graph */
406 adj = (im->maxval - im->minval) * 0.1;
410 scaled_min = im->minval / im->magfact;
411 scaled_max = im->maxval / im->magfact;
413 for (i=1; sensiblevalues[i] > 0; i++){
414 if (sensiblevalues[i-1]>=scaled_min &&
415 sensiblevalues[i]<=scaled_min)
416 im->minval = sensiblevalues[i]*(im->magfact);
418 if (-sensiblevalues[i-1]<=scaled_min &&
419 -sensiblevalues[i]>=scaled_min)
420 im->minval = -sensiblevalues[i-1]*(im->magfact);
422 if (sensiblevalues[i-1] >= scaled_max &&
423 sensiblevalues[i] <= scaled_max)
424 im->maxval = sensiblevalues[i-1]*(im->magfact);
426 if (-sensiblevalues[i-1]<=scaled_max &&
427 -sensiblevalues[i] >=scaled_max)
428 im->maxval = -sensiblevalues[i]*(im->magfact);
432 /* adjust min and max to the grid definition if there is one */
433 im->minval = (double)im->ylabfact * im->ygridstep *
434 floor(im->minval / ((double)im->ylabfact * im->ygridstep));
435 im->maxval = (double)im->ylabfact * im->ygridstep *
436 ceil(im->maxval /( (double)im->ylabfact * im->ygridstep));
440 fprintf(stderr,"SCALED Min: %6.2f Max: %6.2f Factor: %6.2f\n",
441 im->minval,im->maxval,im->magfact);
446 apply_gridfit(image_desc_t *im)
448 if (isnan(im->minval) || isnan(im->maxval))
451 if (im->logarithmic) {
452 double ya, yb, ypix, ypixfrac;
453 double log10_range = log10(im->maxval) - log10(im->minval);
454 ya = pow((double)10, floor(log10(im->minval)));
455 while (ya < im->minval)
458 return; /* don't have y=10^x gridline */
460 if (yb <= im->maxval) {
461 /* we have at least 2 y=10^x gridlines.
462 Make sure distance between them in pixels
463 are an integer by expanding im->maxval */
464 double y_pixel_delta = ytr(im, ya) - ytr(im, yb);
465 double factor = y_pixel_delta / floor(y_pixel_delta);
466 double new_log10_range = factor * log10_range;
467 double new_ymax_log10 = log10(im->minval) + new_log10_range;
468 im->maxval = pow(10, new_ymax_log10);
469 ytr(im,DNAN); /* reset precalc */
470 log10_range = log10(im->maxval) - log10(im->minval);
472 /* make sure first y=10^x gridline is located on
473 integer pixel position by moving scale slightly
474 downwards (sub-pixel movement) */
475 ypix = ytr(im, ya) + im->ysize; /* add im->ysize so it always is positive */
476 ypixfrac = ypix - floor(ypix);
477 if (ypixfrac > 0 && ypixfrac < 1) {
478 double yfrac = ypixfrac / im->ysize;
479 im->minval = pow(10, log10(im->minval) - yfrac * log10_range);
480 im->maxval = pow(10, log10(im->maxval) - yfrac * log10_range);
481 ytr(im,DNAN); /* reset precalc */
484 /* Make sure we have an integer pixel distance between
485 each minor gridline */
486 double ypos1 = ytr(im, im->minval);
487 double ypos2 = ytr(im, im->minval + im->ygrid_scale.gridstep);
488 double y_pixel_delta = ypos1 - ypos2;
489 double factor = y_pixel_delta / floor(y_pixel_delta);
490 double new_range = factor * (im->maxval - im->minval);
491 double gridstep = im->ygrid_scale.gridstep;
492 double minor_y, minor_y_px, minor_y_px_frac;
493 im->maxval = im->minval + new_range;
494 ytr(im,DNAN); /* reset precalc */
495 /* make sure first minor gridline is on integer pixel y coord */
496 minor_y = gridstep * floor(im->minval / gridstep);
497 while (minor_y < im->minval)
499 minor_y_px = ytr(im, minor_y) + im->ysize; /* ensure > 0 by adding ysize */
500 minor_y_px_frac = minor_y_px - floor(minor_y_px);
501 if (minor_y_px_frac > 0 && minor_y_px_frac < 1) {
502 double yfrac = minor_y_px_frac / im->ysize;
503 double range = im->maxval - im->minval;
504 im->minval = im->minval - yfrac * range;
505 im->maxval = im->maxval - yfrac * range;
506 ytr(im,DNAN); /* reset precalc */
508 calc_horizontal_grid(im); /* recalc with changed im->maxval */
512 /* reduce data reimplementation by Alex */
516 enum cf_en cf, /* which consolidation function ?*/
517 unsigned long cur_step, /* step the data currently is in */
518 time_t *start, /* start, end and step as requested ... */
519 time_t *end, /* ... by the application will be ... */
520 unsigned long *step, /* ... adjusted to represent reality */
521 unsigned long *ds_cnt, /* number of data sources in file */
522 rrd_value_t **data) /* two dimensional array containing the data */
524 int i,reduce_factor = ceil((double)(*step) / (double)cur_step);
525 unsigned long col,dst_row,row_cnt,start_offset,end_offset,skiprows=0;
526 rrd_value_t *srcptr,*dstptr;
528 (*step) = cur_step*reduce_factor; /* set new step size for reduced data */
531 row_cnt = ((*end)-(*start))/cur_step;
537 printf("Reducing %lu rows with factor %i time %lu to %lu, step %lu\n",
538 row_cnt,reduce_factor,*start,*end,cur_step);
539 for (col=0;col<row_cnt;col++) {
540 printf("time %10lu: ",*start+(col+1)*cur_step);
541 for (i=0;i<*ds_cnt;i++)
542 printf(" %8.2e",srcptr[*ds_cnt*col+i]);
547 /* We have to combine [reduce_factor] rows of the source
548 ** into one row for the destination. Doing this we also
549 ** need to take care to combine the correct rows. First
550 ** alter the start and end time so that they are multiples
551 ** of the new step time. We cannot reduce the amount of
552 ** time so we have to move the end towards the future and
553 ** the start towards the past.
555 end_offset = (*end) % (*step);
556 start_offset = (*start) % (*step);
558 /* If there is a start offset (which cannot be more than
559 ** one destination row), skip the appropriate number of
560 ** source rows and one destination row. The appropriate
561 ** number is what we do know (start_offset/cur_step) of
562 ** the new interval (*step/cur_step aka reduce_factor).
565 printf("start_offset: %lu end_offset: %lu\n",start_offset,end_offset);
566 printf("row_cnt before: %lu\n",row_cnt);
569 (*start) = (*start)-start_offset;
570 skiprows=reduce_factor-start_offset/cur_step;
571 srcptr+=skiprows* *ds_cnt;
572 for (col=0;col<(*ds_cnt);col++) *dstptr++ = DNAN;
576 printf("row_cnt between: %lu\n",row_cnt);
579 /* At the end we have some rows that are not going to be
580 ** used, the amount is end_offset/cur_step
583 (*end) = (*end)-end_offset+(*step);
584 skiprows = end_offset/cur_step;
588 printf("row_cnt after: %lu\n",row_cnt);
591 /* Sanity check: row_cnt should be multiple of reduce_factor */
592 /* if this gets triggered, something is REALLY WRONG ... we die immediately */
594 if (row_cnt%reduce_factor) {
595 printf("SANITY CHECK: %lu rows cannot be reduced by %i \n",
596 row_cnt,reduce_factor);
597 printf("BUG in reduce_data()\n");
601 /* Now combine reduce_factor intervals at a time
602 ** into one interval for the destination.
605 for (dst_row=0;(long int)row_cnt>=reduce_factor;dst_row++) {
606 for (col=0;col<(*ds_cnt);col++) {
607 rrd_value_t newval=DNAN;
608 unsigned long validval=0;
610 for (i=0;i<reduce_factor;i++) {
611 if (isnan(srcptr[i*(*ds_cnt)+col])) {
615 if (isnan(newval)) newval = srcptr[i*(*ds_cnt)+col];
623 newval += srcptr[i*(*ds_cnt)+col];
626 newval = min (newval,srcptr[i*(*ds_cnt)+col]);
629 /* an interval contains a failure if any subintervals contained a failure */
631 newval = max (newval,srcptr[i*(*ds_cnt)+col]);
634 newval = srcptr[i*(*ds_cnt)+col];
639 if (validval == 0){newval = DNAN;} else{
657 srcptr+=(*ds_cnt)*reduce_factor;
658 row_cnt-=reduce_factor;
660 /* If we had to alter the endtime, we didn't have enough
661 ** source rows to fill the last row. Fill it with NaN.
663 if (end_offset) for (col=0;col<(*ds_cnt);col++) *dstptr++ = DNAN;
665 row_cnt = ((*end)-(*start))/ *step;
667 printf("Done reducing. Currently %lu rows, time %lu to %lu, step %lu\n",
668 row_cnt,*start,*end,*step);
669 for (col=0;col<row_cnt;col++) {
670 printf("time %10lu: ",*start+(col+1)*(*step));
671 for (i=0;i<*ds_cnt;i++)
672 printf(" %8.2e",srcptr[*ds_cnt*col+i]);
679 /* get the data required for the graphs from the
683 data_fetch(image_desc_t *im )
688 /* pull the data from the rrd files ... */
689 for (i=0;i< (int)im->gdes_c;i++){
690 /* only GF_DEF elements fetch data */
691 if (im->gdes[i].gf != GF_DEF)
695 /* do we have it already ?*/
696 for (ii=0;ii<i;ii++) {
697 if (im->gdes[ii].gf != GF_DEF)
699 if ((strcmp(im->gdes[i].rrd, im->gdes[ii].rrd) == 0)
700 && (im->gdes[i].cf == im->gdes[ii].cf)
701 && (im->gdes[i].cf_reduce == im->gdes[ii].cf_reduce)
702 && (im->gdes[i].start_orig == im->gdes[ii].start_orig)
703 && (im->gdes[i].end_orig == im->gdes[ii].end_orig)
704 && (im->gdes[i].step_orig == im->gdes[ii].step_orig)) {
705 /* OK, the data is already there.
706 ** Just copy the header portion
708 im->gdes[i].start = im->gdes[ii].start;
709 im->gdes[i].end = im->gdes[ii].end;
710 im->gdes[i].step = im->gdes[ii].step;
711 im->gdes[i].ds_cnt = im->gdes[ii].ds_cnt;
712 im->gdes[i].ds_namv = im->gdes[ii].ds_namv;
713 im->gdes[i].data = im->gdes[ii].data;
714 im->gdes[i].data_first = 0;
721 unsigned long ft_step = im->gdes[i].step ; /* ft_step will record what we got from fetch */
723 if((rrd_fetch_fn(im->gdes[i].rrd,
729 &im->gdes[i].ds_namv,
730 &im->gdes[i].data)) == -1){
733 im->gdes[i].data_first = 1;
735 if (ft_step < im->gdes[i].step) {
736 reduce_data(im->gdes[i].cf_reduce,
744 im->gdes[i].step = ft_step;
748 /* lets see if the required data source is really there */
749 for(ii=0;ii<(int)im->gdes[i].ds_cnt;ii++){
750 if(strcmp(im->gdes[i].ds_namv[ii],im->gdes[i].ds_nam) == 0){
753 if (im->gdes[i].ds== -1){
754 rrd_set_error("No DS called '%s' in '%s'",
755 im->gdes[i].ds_nam,im->gdes[i].rrd);
763 /* evaluate the expressions in the CDEF functions */
765 /*************************************************************
767 *************************************************************/
770 find_var_wrapper(void *arg1, char *key)
772 return find_var((image_desc_t *) arg1, key);
775 /* find gdes containing var*/
777 find_var(image_desc_t *im, char *key){
779 for(ii=0;ii<im->gdes_c-1;ii++){
780 if((im->gdes[ii].gf == GF_DEF
781 || im->gdes[ii].gf == GF_VDEF
782 || im->gdes[ii].gf == GF_CDEF)
783 && (strcmp(im->gdes[ii].vname,key) == 0)){
790 /* find the largest common denominator for all the numbers
791 in the 0 terminated num array */
796 for (i=0;num[i+1]!=0;i++){
798 rest=num[i] % num[i+1];
799 num[i]=num[i+1]; num[i+1]=rest;
803 /* return i==0?num[i]:num[i-1]; */
807 /* run the rpn calculator on all the VDEF and CDEF arguments */
809 data_calc( image_desc_t *im){
813 long *steparray, rpi;
818 rpnstack_init(&rpnstack);
820 for (gdi=0;gdi<im->gdes_c;gdi++){
821 /* Look for GF_VDEF and GF_CDEF in the same loop,
822 * so CDEFs can use VDEFs and vice versa
824 switch (im->gdes[gdi].gf) {
828 graph_desc_t *vdp = &im->gdes[im->gdes[gdi].vidx];
830 /* remove current shift */
831 vdp->start -= vdp->shift;
832 vdp->end -= vdp->shift;
835 if (im->gdes[gdi].shidx >= 0)
836 vdp->shift = im->gdes[im->gdes[gdi].shidx].vf.val;
839 vdp->shift = im->gdes[gdi].shval;
841 /* normalize shift to multiple of consolidated step */
842 vdp->shift = (vdp->shift / (long)vdp->step) * (long)vdp->step;
845 vdp->start += vdp->shift;
846 vdp->end += vdp->shift;
850 /* A VDEF has no DS. This also signals other parts
851 * of rrdtool that this is a VDEF value, not a CDEF.
853 im->gdes[gdi].ds_cnt = 0;
854 if (vdef_calc(im,gdi)) {
855 rrd_set_error("Error processing VDEF '%s'"
858 rpnstack_free(&rpnstack);
863 im->gdes[gdi].ds_cnt = 1;
864 im->gdes[gdi].ds = 0;
865 im->gdes[gdi].data_first = 1;
866 im->gdes[gdi].start = 0;
867 im->gdes[gdi].end = 0;
872 /* Find the variables in the expression.
873 * - VDEF variables are substituted by their values
874 * and the opcode is changed into OP_NUMBER.
875 * - CDEF variables are analized for their step size,
876 * the lowest common denominator of all the step
877 * sizes of the data sources involved is calculated
878 * and the resulting number is the step size for the
879 * resulting data source.
881 for(rpi=0;im->gdes[gdi].rpnp[rpi].op != OP_END;rpi++){
882 if(im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE ||
883 im->gdes[gdi].rpnp[rpi].op == OP_PREV_OTHER){
884 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
885 if (im->gdes[ptr].ds_cnt == 0) { /* this is a VDEF data source */
887 printf("DEBUG: inside CDEF '%s' processing VDEF '%s'\n",
889 im->gdes[ptr].vname);
890 printf("DEBUG: value from vdef is %f\n",im->gdes[ptr].vf.val);
892 im->gdes[gdi].rpnp[rpi].val = im->gdes[ptr].vf.val;
893 im->gdes[gdi].rpnp[rpi].op = OP_NUMBER;
894 } else { /* normal variables and PREF(variables) */
896 /* add one entry to the array that keeps track of the step sizes of the
897 * data sources going into the CDEF. */
899 rrd_realloc(steparray,
900 (++stepcnt+1)*sizeof(*steparray)))==NULL){
901 rrd_set_error("realloc steparray");
902 rpnstack_free(&rpnstack);
906 steparray[stepcnt-1] = im->gdes[ptr].step;
908 /* adjust start and end of cdef (gdi) so
909 * that it runs from the latest start point
910 * to the earliest endpoint of any of the
911 * rras involved (ptr)
914 if(im->gdes[gdi].start < im->gdes[ptr].start)
915 im->gdes[gdi].start = im->gdes[ptr].start;
917 if(im->gdes[gdi].end == 0 ||
918 im->gdes[gdi].end > im->gdes[ptr].end)
919 im->gdes[gdi].end = im->gdes[ptr].end;
921 /* store pointer to the first element of
922 * the rra providing data for variable,
923 * further save step size and data source
926 im->gdes[gdi].rpnp[rpi].data = im->gdes[ptr].data + im->gdes[ptr].ds;
927 im->gdes[gdi].rpnp[rpi].step = im->gdes[ptr].step;
928 im->gdes[gdi].rpnp[rpi].ds_cnt = im->gdes[ptr].ds_cnt;
930 /* backoff the *.data ptr; this is done so
931 * rpncalc() function doesn't have to treat
932 * the first case differently
934 } /* if ds_cnt != 0 */
935 } /* if OP_VARIABLE */
936 } /* loop through all rpi */
938 /* move the data pointers to the correct period */
939 for(rpi=0;im->gdes[gdi].rpnp[rpi].op != OP_END;rpi++){
940 if(im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE ||
941 im->gdes[gdi].rpnp[rpi].op == OP_PREV_OTHER){
942 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
943 long diff = im->gdes[gdi].start - im->gdes[ptr].start;
946 im->gdes[gdi].rpnp[rpi].data += (diff / im->gdes[ptr].step) * im->gdes[ptr].ds_cnt;
950 if(steparray == NULL){
951 rrd_set_error("rpn expressions without DEF"
952 " or CDEF variables are not supported");
953 rpnstack_free(&rpnstack);
956 steparray[stepcnt]=0;
957 /* Now find the resulting step. All steps in all
958 * used RRAs have to be visited
960 im->gdes[gdi].step = lcd(steparray);
962 if((im->gdes[gdi].data = malloc((
963 (im->gdes[gdi].end-im->gdes[gdi].start)
964 / im->gdes[gdi].step)
965 * sizeof(double)))==NULL){
966 rrd_set_error("malloc im->gdes[gdi].data");
967 rpnstack_free(&rpnstack);
971 /* Step through the new cdef results array and
972 * calculate the values
974 for (now = im->gdes[gdi].start + im->gdes[gdi].step;
975 now<=im->gdes[gdi].end;
976 now += im->gdes[gdi].step)
978 rpnp_t *rpnp = im -> gdes[gdi].rpnp;
980 /* 3rd arg of rpn_calc is for OP_VARIABLE lookups;
981 * in this case we are advancing by timesteps;
982 * we use the fact that time_t is a synonym for long
984 if (rpn_calc(rpnp,&rpnstack,(long) now,
985 im->gdes[gdi].data,++dataidx) == -1) {
986 /* rpn_calc sets the error string */
987 rpnstack_free(&rpnstack);
990 } /* enumerate over time steps within a CDEF */
995 } /* enumerate over CDEFs */
996 rpnstack_free(&rpnstack);
1000 /* massage data so, that we get one value for each x coordinate in the graph */
1002 data_proc( image_desc_t *im ){
1004 double pixstep = (double)(im->end-im->start)
1005 /(double)im->xsize; /* how much time
1006 passes in one pixel */
1008 double minval=DNAN,maxval=DNAN;
1010 unsigned long gr_time;
1012 /* memory for the processed data */
1013 for(i=0;i<im->gdes_c;i++) {
1014 if((im->gdes[i].gf==GF_LINE) ||
1015 (im->gdes[i].gf==GF_AREA) ||
1016 (im->gdes[i].gf==GF_TICK)) {
1017 if((im->gdes[i].p_data = malloc((im->xsize +1)
1018 * sizeof(rrd_value_t)))==NULL){
1019 rrd_set_error("malloc data_proc");
1025 for (i=0;i<im->xsize;i++) { /* for each pixel */
1027 gr_time = im->start+pixstep*i; /* time of the current step */
1030 for (ii=0;ii<im->gdes_c;ii++) {
1032 switch (im->gdes[ii].gf) {
1036 if (!im->gdes[ii].stack)
1038 value = im->gdes[ii].yrule;
1039 if (isnan(value) || (im->gdes[ii].gf == GF_TICK)) {
1040 /* The time of the data doesn't necessarily match
1041 ** the time of the graph. Beware.
1043 vidx = im->gdes[ii].vidx;
1044 if (im->gdes[vidx].gf == GF_VDEF) {
1045 value = im->gdes[vidx].vf.val;
1046 } else if (((long int)gr_time >= (long int)im->gdes[vidx].start) &&
1047 ((long int)gr_time <= (long int)im->gdes[vidx].end) ) {
1048 value = im->gdes[vidx].data[
1049 (unsigned long) floor(
1050 (double)(gr_time - im->gdes[vidx].start)
1051 / im->gdes[vidx].step)
1052 * im->gdes[vidx].ds_cnt
1060 if (! isnan(value)) {
1062 im->gdes[ii].p_data[i] = paintval;
1063 /* GF_TICK: the data values are not
1064 ** relevant for min and max
1066 if (finite(paintval) && im->gdes[ii].gf != GF_TICK ) {
1067 if ((isnan(minval) || paintval < minval ) &&
1068 ! (im->logarithmic && paintval <= 0.0))
1070 if (isnan(maxval) || paintval > maxval)
1074 im->gdes[ii].p_data[i] = DNAN;
1078 rrd_set_error("STACK should already be turned into LINE or AREA here");
1087 /* if min or max have not been asigned a value this is because
1088 there was no data in the graph ... this is not good ...
1089 lets set these to dummy values then ... */
1091 if (im->logarithmic) {
1092 if (isnan(minval)) minval = 0.2;
1093 if (isnan(maxval)) maxval = 5.1;
1096 if (isnan(minval)) minval = 0.0;
1097 if (isnan(maxval)) maxval = 1.0;
1100 /* adjust min and max values */
1101 if (isnan(im->minval)
1102 /* don't adjust low-end with log scale */ /* why not? */
1103 || ((!im->rigid) && im->minval > minval)
1105 if (im->logarithmic)
1106 im->minval = minval * 0.5;
1108 im->minval = minval;
1110 if (isnan(im->maxval)
1111 || (!im->rigid && im->maxval < maxval)
1113 if (im->logarithmic)
1114 im->maxval = maxval * 2.0;
1116 im->maxval = maxval;
1118 /* make sure min is smaller than max */
1119 if (im->minval > im->maxval) {
1120 im->minval = 0.99 * im->maxval;
1123 /* make sure min and max are not equal */
1124 if (im->minval == im->maxval) {
1126 if (! im->logarithmic) {
1129 /* make sure min and max are not both zero */
1130 if (im->maxval == 0.0) {
1139 /* identify the point where the first gridline, label ... gets placed */
1143 time_t start, /* what is the initial time */
1144 enum tmt_en baseint, /* what is the basic interval */
1145 long basestep /* how many if these do we jump a time */
1149 localtime_r(&start, &tm);
1152 tm.tm_sec -= tm.tm_sec % basestep; break;
1155 tm.tm_min -= tm.tm_min % basestep;
1160 tm.tm_hour -= tm.tm_hour % basestep; break;
1162 /* we do NOT look at the basestep for this ... */
1165 tm.tm_hour = 0; break;
1167 /* we do NOT look at the basestep for this ... */
1171 tm.tm_mday -= tm.tm_wday -1; /* -1 because we want the monday */
1172 if (tm.tm_wday==0) tm.tm_mday -= 7; /* we want the *previous* monday */
1179 tm.tm_mon -= tm.tm_mon % basestep; break;
1187 tm.tm_year -= (tm.tm_year+1900) % basestep;
1192 /* identify the point where the next gridline, label ... gets placed */
1195 time_t current, /* what is the initial time */
1196 enum tmt_en baseint, /* what is the basic interval */
1197 long basestep /* how many if these do we jump a time */
1202 localtime_r(¤t, &tm);
1206 tm.tm_sec += basestep; break;
1208 tm.tm_min += basestep; break;
1210 tm.tm_hour += basestep; break;
1212 tm.tm_mday += basestep; break;
1214 tm.tm_mday += 7*basestep; break;
1216 tm.tm_mon += basestep; break;
1218 tm.tm_year += basestep;
1220 madetime = mktime(&tm);
1221 } while (madetime == -1); /* this is necessary to skip impssible times
1222 like the daylight saving time skips */
1228 /* calculate values required for PRINT and GPRINT functions */
1231 print_calc(image_desc_t *im, char ***prdata)
1233 long i,ii,validsteps;
1236 int graphelement = 0;
1239 double magfact = -1;
1243 /* wow initializing tmvdef is quite a task :-) */
1244 time_t now = time(NULL);
1245 localtime_r(&now,&tmvdef);
1246 if (im->imginfo) prlines++;
1247 for(i=0;i<im->gdes_c;i++){
1248 switch(im->gdes[i].gf){
1251 if(((*prdata) = rrd_realloc((*prdata),prlines*sizeof(char *)))==NULL){
1252 rrd_set_error("realloc prdata");
1256 /* PRINT and GPRINT can now print VDEF generated values.
1257 * There's no need to do any calculations on them as these
1258 * calculations were already made.
1260 vidx = im->gdes[i].vidx;
1261 if (im->gdes[vidx].gf==GF_VDEF) { /* simply use vals */
1262 printval = im->gdes[vidx].vf.val;
1263 localtime_r(&im->gdes[vidx].vf.when,&tmvdef);
1264 } else { /* need to calculate max,min,avg etcetera */
1265 max_ii =((im->gdes[vidx].end
1266 - im->gdes[vidx].start)
1267 / im->gdes[vidx].step
1268 * im->gdes[vidx].ds_cnt);
1271 for( ii=im->gdes[vidx].ds;
1273 ii+=im->gdes[vidx].ds_cnt){
1274 if (! finite(im->gdes[vidx].data[ii]))
1276 if (isnan(printval)){
1277 printval = im->gdes[vidx].data[ii];
1282 switch (im->gdes[i].cf){
1285 case CF_DEVSEASONAL:
1289 printval += im->gdes[vidx].data[ii];
1292 printval = min( printval, im->gdes[vidx].data[ii]);
1296 printval = max( printval, im->gdes[vidx].data[ii]);
1299 printval = im->gdes[vidx].data[ii];
1302 if (im->gdes[i].cf==CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
1303 if (validsteps > 1) {
1304 printval = (printval / validsteps);
1307 } /* prepare printval */
1309 if ((percent_s = strstr(im->gdes[i].format,"%S")) != NULL) {
1310 /* Magfact is set to -1 upon entry to print_calc. If it
1311 * is still less than 0, then we need to run auto_scale.
1312 * Otherwise, put the value into the correct units. If
1313 * the value is 0, then do not set the symbol or magnification
1314 * so next the calculation will be performed again. */
1315 if (magfact < 0.0) {
1316 auto_scale(im,&printval,&si_symb,&magfact);
1317 if (printval == 0.0)
1320 printval /= magfact;
1322 *(++percent_s) = 's';
1323 } else if (strstr(im->gdes[i].format,"%s") != NULL) {
1324 auto_scale(im,&printval,&si_symb,&magfact);
1327 if (im->gdes[i].gf == GF_PRINT){
1328 (*prdata)[prlines-2] = malloc((FMT_LEG_LEN+2)*sizeof(char));
1329 (*prdata)[prlines-1] = NULL;
1330 if (im->gdes[i].strftm){
1331 strftime((*prdata)[prlines-2],FMT_LEG_LEN,im->gdes[i].format,&tmvdef);
1333 if (bad_format(im->gdes[i].format)) {
1334 rrd_set_error("bad format for PRINT in '%s'", im->gdes[i].format);
1338 #ifdef HAVE_SNPRINTF
1339 snprintf((*prdata)[prlines-2],FMT_LEG_LEN,im->gdes[i].format,printval,si_symb);
1341 sprintf((*prdata)[prlines-2],im->gdes[i].format,printval,si_symb);
1347 if (im->gdes[i].strftm){
1348 strftime(im->gdes[i].legend,FMT_LEG_LEN,im->gdes[i].format,&tmvdef);
1350 if (bad_format(im->gdes[i].format)) {
1351 rrd_set_error("bad format for GPRINT in '%s'", im->gdes[i].format);
1354 #ifdef HAVE_SNPRINTF
1355 snprintf(im->gdes[i].legend,FMT_LEG_LEN-2,im->gdes[i].format,printval,si_symb);
1357 sprintf(im->gdes[i].legend,im->gdes[i].format,printval,si_symb);
1369 if(isnan(im->gdes[i].yrule)) { /* we must set this here or the legend printer can not decide to print the legend */
1370 im->gdes[i].yrule=im->gdes[im->gdes[i].vidx].vf.val;
1375 if(im->gdes[i].xrule == 0) { /* again ... the legend printer needs it*/
1376 im->gdes[i].xrule = im->gdes[im->gdes[i].vidx].vf.when;
1384 #ifdef WITH_PIECHART
1391 rrd_set_error("STACK should already be turned into LINE or AREA here");
1396 return graphelement;
1400 /* place legends with color spots */
1402 leg_place(image_desc_t *im)
1405 int interleg = im->text_prop[TEXT_PROP_LEGEND].size*2.0;
1406 int border = im->text_prop[TEXT_PROP_LEGEND].size*2.0;
1407 int fill=0, fill_last;
1409 int leg_x = border, leg_y = im->yimg;
1410 int leg_y_prev = im->yimg;
1414 char prt_fctn; /*special printfunctions */
1417 if( !(im->extra_flags & NOLEGEND) & !(im->extra_flags & ONLY_GRAPH) ) {
1418 if ((legspace = malloc(im->gdes_c*sizeof(int)))==NULL){
1419 rrd_set_error("malloc for legspace");
1423 for(i=0;i<im->gdes_c;i++){
1426 /* hid legends for rules which are not displayed */
1428 if(!(im->extra_flags & FORCE_RULES_LEGEND)) {
1429 if (im->gdes[i].gf == GF_HRULE &&
1430 (im->gdes[i].yrule < im->minval || im->gdes[i].yrule > im->maxval))
1431 im->gdes[i].legend[0] = '\0';
1433 if (im->gdes[i].gf == GF_VRULE &&
1434 (im->gdes[i].xrule < im->start || im->gdes[i].xrule > im->end))
1435 im->gdes[i].legend[0] = '\0';
1438 leg_cc = strlen(im->gdes[i].legend);
1440 /* is there a controle code ant the end of the legend string ? */
1441 /* and it is not a tab \\t */
1442 if (leg_cc >= 2 && im->gdes[i].legend[leg_cc-2] == '\\' && im->gdes[i].legend[leg_cc-1] != 't') {
1443 prt_fctn = im->gdes[i].legend[leg_cc-1];
1445 im->gdes[i].legend[leg_cc] = '\0';
1449 /* only valid control codes */
1450 if (prt_fctn != 'l' &&
1451 prt_fctn != 'n' && /* a synonym for l */
1460 rrd_set_error("Unknown control code at the end of '%s\\%c'",im->gdes[i].legend,prt_fctn);
1465 /* remove exess space */
1466 if ( prt_fctn == 'n' ){
1470 while (prt_fctn=='g' &&
1472 im->gdes[i].legend[leg_cc-1]==' '){
1474 im->gdes[i].legend[leg_cc]='\0';
1477 legspace[i]=(prt_fctn=='g' ? 0 : interleg);
1480 /* no interleg space if string ends in \g */
1481 fill += legspace[i];
1483 fill += gfx_get_text_width(im->canvas, fill+border,
1484 im->text_prop[TEXT_PROP_LEGEND].font,
1485 im->text_prop[TEXT_PROP_LEGEND].size,
1487 im->gdes[i].legend, 0);
1492 /* who said there was a special tag ... ?*/
1493 if (prt_fctn=='g') {
1496 if (prt_fctn == '\0') {
1497 if (i == im->gdes_c -1 ) prt_fctn ='l';
1499 /* is it time to place the legends ? */
1500 if (fill > im->ximg - 2*border){
1515 if (prt_fctn != '\0'){
1517 if (leg_c >= 2 && prt_fctn == 'j') {
1518 glue = (im->ximg - fill - 2* border) / (leg_c-1);
1522 if (prt_fctn =='c') leg_x = (im->ximg - fill) / 2.0;
1523 if (prt_fctn =='r') leg_x = im->ximg - fill - border;
1525 for(ii=mark;ii<=i;ii++){
1526 if(im->gdes[ii].legend[0]=='\0')
1527 continue; /* skip empty legends */
1528 im->gdes[ii].leg_x = leg_x;
1529 im->gdes[ii].leg_y = leg_y;
1531 gfx_get_text_width(im->canvas, leg_x,
1532 im->text_prop[TEXT_PROP_LEGEND].font,
1533 im->text_prop[TEXT_PROP_LEGEND].size,
1535 im->gdes[ii].legend, 0)
1540 /* only add y space if there was text on the line */
1541 if (leg_x > border || prt_fctn == 's')
1542 leg_y += im->text_prop[TEXT_PROP_LEGEND].size*1.8;
1543 if (prt_fctn == 's')
1544 leg_y -= im->text_prop[TEXT_PROP_LEGEND].size;
1550 im->yimg = leg_y_prev;
1551 /* if we did place some legends we have to add vertical space */
1552 if (leg_y != im->yimg){
1553 im->yimg += im->text_prop[TEXT_PROP_LEGEND].size*1.8;
1560 /* create a grid on the graph. it determines what to do
1561 from the values of xsize, start and end */
1563 /* the xaxis labels are determined from the number of seconds per pixel
1564 in the requested graph */
1569 calc_horizontal_grid(image_desc_t *im)
1575 int decimals, fractionals;
1577 im->ygrid_scale.labfact=2;
1578 range = im->maxval - im->minval;
1579 scaledrange = range / im->magfact;
1581 /* does the scale of this graph make it impossible to put lines
1582 on it? If so, give up. */
1583 if (isnan(scaledrange)) {
1587 /* find grid spaceing */
1589 if(isnan(im->ygridstep)){
1590 if(im->extra_flags & ALTYGRID) {
1591 /* find the value with max number of digits. Get number of digits */
1592 decimals = ceil(log10(max(fabs(im->maxval), fabs(im->minval))*im->viewfactor/im->magfact));
1593 if(decimals <= 0) /* everything is small. make place for zero */
1596 im->ygrid_scale.gridstep = pow((double)10, floor(log10(range*im->viewfactor/im->magfact)))/im->viewfactor*im->magfact;
1598 if(im->ygrid_scale.gridstep == 0) /* range is one -> 0.1 is reasonable scale */
1599 im->ygrid_scale.gridstep = 0.1;
1600 /* should have at least 5 lines but no more then 15 */
1601 if(range/im->ygrid_scale.gridstep < 5)
1602 im->ygrid_scale.gridstep /= 10;
1603 if(range/im->ygrid_scale.gridstep > 15)
1604 im->ygrid_scale.gridstep *= 10;
1605 if(range/im->ygrid_scale.gridstep > 5) {
1606 im->ygrid_scale.labfact = 1;
1607 if(range/im->ygrid_scale.gridstep > 8)
1608 im->ygrid_scale.labfact = 2;
1611 im->ygrid_scale.gridstep /= 5;
1612 im->ygrid_scale.labfact = 5;
1614 fractionals = floor(log10(im->ygrid_scale.gridstep*(double)im->ygrid_scale.labfact*im->viewfactor/im->magfact));
1615 if(fractionals < 0) { /* small amplitude. */
1616 int len = decimals - fractionals + 1;
1617 if (im->unitslength < len+2) im->unitslength = len+2;
1618 sprintf(im->ygrid_scale.labfmt, "%%%d.%df%s", len, -fractionals,(im->symbol != ' ' ? " %c" : ""));
1620 int len = decimals + 1;
1621 if (im->unitslength < len+2) im->unitslength = len+2;
1622 sprintf(im->ygrid_scale.labfmt, "%%%d.0f%s", len, ( im->symbol != ' ' ? " %c" : "" ));
1626 for(i=0;ylab[i].grid > 0;i++){
1627 pixel = im->ysize / (scaledrange / ylab[i].grid);
1634 if (pixel * ylab[gridind].lfac[i] >= 2.5 * im->text_prop[TEXT_PROP_AXIS].size) {
1635 im->ygrid_scale.labfact = ylab[gridind].lfac[i];
1640 im->ygrid_scale.gridstep = ylab[gridind].grid * im->magfact;
1643 im->ygrid_scale.gridstep = im->ygridstep;
1644 im->ygrid_scale.labfact = im->ylabfact;
1649 int draw_horizontal_grid(image_desc_t *im)
1653 char graph_label[100];
1655 double X0=im->xorigin;
1656 double X1=im->xorigin+im->xsize;
1658 int sgrid = (int)( im->minval / im->ygrid_scale.gridstep - 1);
1659 int egrid = (int)( im->maxval / im->ygrid_scale.gridstep + 1);
1661 scaledstep = im->ygrid_scale.gridstep/(double)im->magfact*(double)im->viewfactor;
1662 MaxY = scaledstep*(double)egrid;
1663 for (i = sgrid; i <= egrid; i++){
1664 double Y0=ytr(im,im->ygrid_scale.gridstep*i);
1665 double YN=ytr(im,im->ygrid_scale.gridstep*(i+1));
1666 if ( Y0 >= im->yorigin-im->ysize
1667 && Y0 <= im->yorigin){
1668 /* Make sure at least 2 grid labels are shown, even if it doesn't agree
1669 with the chosen settings. Add a label if required by settings, or if
1670 there is only one label so far and the next grid line is out of bounds. */
1671 if(i % im->ygrid_scale.labfact == 0 || ( nlabels==1 && (YN < im->yorigin-im->ysize || YN > im->yorigin) )){
1672 if (im->symbol == ' ') {
1673 if(im->extra_flags & ALTYGRID) {
1674 sprintf(graph_label,im->ygrid_scale.labfmt,scaledstep*(double)i);
1677 sprintf(graph_label,"%4.1f",scaledstep*(double)i);
1679 sprintf(graph_label,"%4.0f",scaledstep*(double)i);
1683 char sisym = ( i == 0 ? ' ' : im->symbol);
1684 if(im->extra_flags & ALTYGRID) {
1685 sprintf(graph_label,im->ygrid_scale.labfmt,scaledstep*(double)i,sisym);
1688 sprintf(graph_label,"%4.1f %c",scaledstep*(double)i, sisym);
1690 sprintf(graph_label,"%4.0f %c",scaledstep*(double)i, sisym);
1696 gfx_new_text ( im->canvas,
1697 X0-im->text_prop[TEXT_PROP_AXIS].size, Y0,
1698 im->graph_col[GRC_FONT],
1699 im->text_prop[TEXT_PROP_AXIS].font,
1700 im->text_prop[TEXT_PROP_AXIS].size,
1701 im->tabwidth, 0.0, GFX_H_RIGHT, GFX_V_CENTER,
1703 gfx_new_dashed_line ( im->canvas,
1706 MGRIDWIDTH, im->graph_col[GRC_MGRID],
1707 im->grid_dash_on, im->grid_dash_off);
1709 } else if (!(im->extra_flags & NOMINOR)) {
1710 gfx_new_dashed_line ( im->canvas,
1713 GRIDWIDTH, im->graph_col[GRC_GRID],
1714 im->grid_dash_on, im->grid_dash_off);
1722 /* this is frexp for base 10 */
1723 double frexp10(double, double *);
1724 double frexp10(double x, double *e) {
1728 iexp = floor(log(fabs(x)) / log(10));
1729 mnt = x / pow(10.0, iexp);
1732 mnt = x / pow(10.0, iexp);
1738 static int AlmostEqual2sComplement (float A, float B, int maxUlps)
1741 int aInt = *(int*)&A;
1742 int bInt = *(int*)&B;
1744 /* Make sure maxUlps is non-negative and small enough that the
1745 default NAN won't compare as equal to anything. */
1747 /* assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024); */
1749 /* Make aInt lexicographically ordered as a twos-complement int */
1752 aInt = 0x80000000l - aInt;
1754 /* Make bInt lexicographically ordered as a twos-complement int */
1757 bInt = 0x80000000l - bInt;
1759 intDiff = abs(aInt - bInt);
1761 if (intDiff <= maxUlps)
1767 /* logaritmic horizontal grid */
1769 horizontal_log_grid(image_desc_t *im)
1771 double yloglab[][10] = {
1772 {1.0, 10., 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
1773 {1.0, 5.0, 10., 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
1774 {1.0, 2.0, 5.0, 7.0, 10., 0.0, 0.0, 0.0, 0.0, 0.0},
1775 {1.0, 2.0, 4.0, 6.0, 8.0, 10., 0.0, 0.0, 0.0, 0.0},
1776 {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.},
1777 {0,0,0,0,0, 0,0,0,0,0} /* last line */ };
1779 int i, j, val_exp, min_exp;
1780 double nex; /* number of decades in data */
1781 double logscale; /* scale in logarithmic space */
1782 int exfrac = 1; /* decade spacing */
1783 int mid = -1; /* row in yloglab for major grid */
1784 double mspac; /* smallest major grid spacing (pixels) */
1785 int flab; /* first value in yloglab to use */
1786 double value, tmp, pre_value;
1788 char graph_label[100];
1790 nex = log10(im->maxval / im->minval);
1791 logscale = im->ysize / nex;
1793 /* major spacing for data with high dynamic range */
1794 while(logscale * exfrac < 3 * im->text_prop[TEXT_PROP_LEGEND].size) {
1795 if(exfrac == 1) exfrac = 3;
1799 /* major spacing for less dynamic data */
1801 /* search best row in yloglab */
1803 for(i = 0; yloglab[mid][i + 1] < 10.0; i++);
1804 mspac = logscale * log10(10.0 / yloglab[mid][i]);
1805 } while(mspac > 2 * im->text_prop[TEXT_PROP_LEGEND].size && yloglab[mid][0] > 0);
1808 /* find first value in yloglab */
1809 for(flab = 0; yloglab[mid][flab] < 10 && frexp10(im->minval, &tmp) > yloglab[mid][flab] ; flab++);
1810 if(yloglab[mid][flab] == 10.0) {
1815 if(val_exp % exfrac) val_exp += abs(-val_exp % exfrac);
1818 X1=im->xorigin+im->xsize;
1824 value = yloglab[mid][flab] * pow(10.0, val_exp);
1825 if ( AlmostEqual2sComplement(value,pre_value,4) ) break; /* it seems we are not converging */
1829 Y0 = ytr(im, value);
1830 if(Y0 <= im->yorigin - im->ysize) break;
1832 /* major grid line */
1833 gfx_new_dashed_line ( im->canvas,
1836 MGRIDWIDTH, im->graph_col[GRC_MGRID],
1837 im->grid_dash_on, im->grid_dash_off);
1840 if (im->extra_flags & FORCE_UNITS_SI) {
1845 scale = floor(val_exp / 3.0);
1846 if( value >= 1.0 ) pvalue = pow(10.0, val_exp % 3);
1847 else pvalue = pow(10.0, ((val_exp + 1) % 3) + 2);
1848 pvalue *= yloglab[mid][flab];
1850 if ( ((scale+si_symbcenter) < (int)sizeof(si_symbol)) &&
1851 ((scale+si_symbcenter) >= 0) )
1852 symbol = si_symbol[scale+si_symbcenter];
1856 sprintf(graph_label,"%3.0f %c", pvalue, symbol);
1858 sprintf(graph_label,"%3.0e", value);
1859 gfx_new_text ( im->canvas,
1860 X0-im->text_prop[TEXT_PROP_AXIS].size, Y0,
1861 im->graph_col[GRC_FONT],
1862 im->text_prop[TEXT_PROP_AXIS].font,
1863 im->text_prop[TEXT_PROP_AXIS].size,
1864 im->tabwidth,0.0, GFX_H_RIGHT, GFX_V_CENTER,
1868 if(mid < 4 && exfrac == 1) {
1869 /* find first and last minor line behind current major line
1870 * i is the first line and j tha last */
1872 min_exp = val_exp - 1;
1873 for(i = 1; yloglab[mid][i] < 10.0; i++);
1874 i = yloglab[mid][i - 1] + 1;
1879 i = yloglab[mid][flab - 1] + 1;
1880 j = yloglab[mid][flab];
1883 /* draw minor lines below current major line */
1886 value = i * pow(10.0, min_exp);
1887 if(value < im->minval) continue;
1889 Y0 = ytr(im, value);
1890 if(Y0 <= im->yorigin - im->ysize) break;
1893 gfx_new_dashed_line ( im->canvas,
1896 GRIDWIDTH, im->graph_col[GRC_GRID],
1897 im->grid_dash_on, im->grid_dash_off);
1900 else if(exfrac > 1) {
1901 for(i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
1902 value = pow(10.0, i);
1903 if(value < im->minval) continue;
1905 Y0 = ytr(im, value);
1906 if(Y0 <= im->yorigin - im->ysize) break;
1909 gfx_new_dashed_line ( im->canvas,
1912 GRIDWIDTH, im->graph_col[GRC_GRID],
1913 im->grid_dash_on, im->grid_dash_off);
1918 if(yloglab[mid][++flab] == 10.0) {
1924 /* draw minor lines after highest major line */
1925 if(mid < 4 && exfrac == 1) {
1926 /* find first and last minor line below current major line
1927 * i is the first line and j tha last */
1929 min_exp = val_exp - 1;
1930 for(i = 1; yloglab[mid][i] < 10.0; i++);
1931 i = yloglab[mid][i - 1] + 1;
1936 i = yloglab[mid][flab - 1] + 1;
1937 j = yloglab[mid][flab];
1940 /* draw minor lines below current major line */
1943 value = i * pow(10.0, min_exp);
1944 if(value < im->minval) continue;
1946 Y0 = ytr(im, value);
1947 if(Y0 <= im->yorigin - im->ysize) break;
1950 gfx_new_dashed_line ( im->canvas,
1953 GRIDWIDTH, im->graph_col[GRC_GRID],
1954 im->grid_dash_on, im->grid_dash_off);
1957 /* fancy minor gridlines */
1958 else if(exfrac > 1) {
1959 for(i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
1960 value = pow(10.0, i);
1961 if(value < im->minval) continue;
1963 Y0 = ytr(im, value);
1964 if(Y0 <= im->yorigin - im->ysize) break;
1967 gfx_new_dashed_line ( im->canvas,
1970 GRIDWIDTH, im->graph_col[GRC_GRID],
1971 im->grid_dash_on, im->grid_dash_off);
1983 int xlab_sel; /* which sort of label and grid ? */
1984 time_t ti, tilab, timajor;
1986 char graph_label[100];
1987 double X0,Y0,Y1; /* points for filled graph and more*/
1990 /* the type of time grid is determined by finding
1991 the number of seconds per pixel in the graph */
1994 if(im->xlab_user.minsec == -1){
1995 factor=(im->end - im->start)/im->xsize;
1997 while ( xlab[xlab_sel+1].minsec != -1
1998 && xlab[xlab_sel+1].minsec <= factor) { xlab_sel++; } /* pick the last one */
1999 while ( xlab[xlab_sel-1].minsec == xlab[xlab_sel].minsec
2000 && xlab[xlab_sel].length > (im->end - im->start)) { xlab_sel--; } /* go back to the smallest size */
2001 im->xlab_user.gridtm = xlab[xlab_sel].gridtm;
2002 im->xlab_user.gridst = xlab[xlab_sel].gridst;
2003 im->xlab_user.mgridtm = xlab[xlab_sel].mgridtm;
2004 im->xlab_user.mgridst = xlab[xlab_sel].mgridst;
2005 im->xlab_user.labtm = xlab[xlab_sel].labtm;
2006 im->xlab_user.labst = xlab[xlab_sel].labst;
2007 im->xlab_user.precis = xlab[xlab_sel].precis;
2008 im->xlab_user.stst = xlab[xlab_sel].stst;
2011 /* y coords are the same for every line ... */
2013 Y1 = im->yorigin-im->ysize;
2016 /* paint the minor grid */
2017 if (!(im->extra_flags & NOMINOR))
2019 for(ti = find_first_time(im->start,
2020 im->xlab_user.gridtm,
2021 im->xlab_user.gridst),
2022 timajor = find_first_time(im->start,
2023 im->xlab_user.mgridtm,
2024 im->xlab_user.mgridst);
2026 ti = find_next_time(ti,im->xlab_user.gridtm,im->xlab_user.gridst)
2028 /* are we inside the graph ? */
2029 if (ti < im->start || ti > im->end) continue;
2030 while (timajor < ti) {
2031 timajor = find_next_time(timajor,
2032 im->xlab_user.mgridtm, im->xlab_user.mgridst);
2034 if (ti == timajor) continue; /* skip as falls on major grid line */
2036 gfx_new_dashed_line(im->canvas,X0,Y0+1, X0,Y1-1,GRIDWIDTH,
2037 im->graph_col[GRC_GRID],
2038 im->grid_dash_on, im->grid_dash_off);
2043 /* paint the major grid */
2044 for(ti = find_first_time(im->start,
2045 im->xlab_user.mgridtm,
2046 im->xlab_user.mgridst);
2048 ti = find_next_time(ti,im->xlab_user.mgridtm,im->xlab_user.mgridst)
2050 /* are we inside the graph ? */
2051 if (ti < im->start || ti > im->end) continue;
2053 gfx_new_dashed_line(im->canvas,X0,Y0+3, X0,Y1-2,MGRIDWIDTH,
2054 im->graph_col[GRC_MGRID],
2055 im->grid_dash_on, im->grid_dash_off);
2058 /* paint the labels below the graph */
2059 for(ti = find_first_time(im->start - im->xlab_user.precis/2,
2060 im->xlab_user.labtm,
2061 im->xlab_user.labst);
2062 ti <= im->end - im->xlab_user.precis/2;
2063 ti = find_next_time(ti,im->xlab_user.labtm,im->xlab_user.labst)
2065 tilab= ti + im->xlab_user.precis/2; /* correct time for the label */
2066 /* are we inside the graph ? */
2067 if (tilab < im->start || tilab > im->end) continue;
2070 localtime_r(&tilab, &tm);
2071 strftime(graph_label,99,im->xlab_user.stst, &tm);
2073 # error "your libc has no strftime I guess we'll abort the exercise here."
2075 gfx_new_text ( im->canvas,
2076 xtr(im,tilab), Y0+im->text_prop[TEXT_PROP_AXIS].size*1.4+5,
2077 im->graph_col[GRC_FONT],
2078 im->text_prop[TEXT_PROP_AXIS].font,
2079 im->text_prop[TEXT_PROP_AXIS].size,
2080 im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_BOTTOM,
2093 /* draw x and y axis */
2094 /* gfx_new_line ( im->canvas, im->xorigin+im->xsize,im->yorigin,
2095 im->xorigin+im->xsize,im->yorigin-im->ysize,
2096 GRIDWIDTH, im->graph_col[GRC_AXIS]);
2098 gfx_new_line ( im->canvas, im->xorigin,im->yorigin-im->ysize,
2099 im->xorigin+im->xsize,im->yorigin-im->ysize,
2100 GRIDWIDTH, im->graph_col[GRC_AXIS]); */
2102 gfx_new_line ( im->canvas, im->xorigin-4,im->yorigin,
2103 im->xorigin+im->xsize+4,im->yorigin,
2104 MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2106 gfx_new_line ( im->canvas, im->xorigin,im->yorigin+4,
2107 im->xorigin,im->yorigin-im->ysize-4,
2108 MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2111 /* arrow for X and Y axis direction */
2112 gfx_new_area ( im->canvas,
2113 im->xorigin+im->xsize+2, im->yorigin-2,
2114 im->xorigin+im->xsize+2, im->yorigin+3,
2115 im->xorigin+im->xsize+7, im->yorigin+0.5, /* LINEOFFSET */
2116 im->graph_col[GRC_ARROW]);
2118 gfx_new_area ( im->canvas,
2119 im->xorigin-2, im->yorigin-im->ysize-2,
2120 im->xorigin+3, im->yorigin-im->ysize-2,
2121 im->xorigin+0.5, im->yorigin-im->ysize-7, /* LINEOFFSET */
2122 im->graph_col[GRC_ARROW]);
2127 grid_paint(image_desc_t *im)
2131 double X0,Y0; /* points for filled graph and more*/
2134 /* draw 3d border */
2135 node = gfx_new_area (im->canvas, 0,im->yimg,
2137 2,2,im->graph_col[GRC_SHADEA]);
2138 gfx_add_point( node , im->ximg - 2, 2 );
2139 gfx_add_point( node , im->ximg, 0 );
2140 gfx_add_point( node , 0,0 );
2141 /* gfx_add_point( node , 0,im->yimg ); */
2143 node = gfx_new_area (im->canvas, 2,im->yimg-2,
2144 im->ximg-2,im->yimg-2,
2146 im->graph_col[GRC_SHADEB]);
2147 gfx_add_point( node , im->ximg,0);
2148 gfx_add_point( node , im->ximg,im->yimg);
2149 gfx_add_point( node , 0,im->yimg);
2150 /* gfx_add_point( node , 0,im->yimg ); */
2153 if (im->draw_x_grid == 1 )
2156 if (im->draw_y_grid == 1){
2157 if(im->logarithmic){
2158 res = horizontal_log_grid(im);
2160 res = draw_horizontal_grid(im);
2163 /* dont draw horizontal grid if there is no min and max val */
2165 char *nodata = "No Data found";
2166 gfx_new_text(im->canvas,im->ximg/2, (2*im->yorigin-im->ysize) / 2,
2167 im->graph_col[GRC_FONT],
2168 im->text_prop[TEXT_PROP_AXIS].font,
2169 im->text_prop[TEXT_PROP_AXIS].size,
2170 im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_CENTER,
2175 /* yaxis unit description */
2176 gfx_new_text( im->canvas,
2177 10, (im->yorigin - im->ysize/2),
2178 im->graph_col[GRC_FONT],
2179 im->text_prop[TEXT_PROP_UNIT].font,
2180 im->text_prop[TEXT_PROP_UNIT].size, im->tabwidth,
2181 RRDGRAPH_YLEGEND_ANGLE,
2182 GFX_H_LEFT, GFX_V_CENTER,
2186 gfx_new_text( im->canvas,
2187 im->ximg/2, im->text_prop[TEXT_PROP_TITLE].size*1.3+4,
2188 im->graph_col[GRC_FONT],
2189 im->text_prop[TEXT_PROP_TITLE].font,
2190 im->text_prop[TEXT_PROP_TITLE].size, im->tabwidth, 0.0,
2191 GFX_H_CENTER, GFX_V_CENTER,
2193 /* rrdtool 'logo' */
2194 gfx_new_text( im->canvas,
2196 ( im->graph_col[GRC_FONT] & 0xffffff00 ) | 0x00000044,
2197 im->text_prop[TEXT_PROP_AXIS].font,
2198 5.5, im->tabwidth, 270,
2199 GFX_H_RIGHT, GFX_V_TOP,
2200 "RRDTOOL / TOBI OETIKER");
2202 /* graph watermark */
2203 if(im->watermark[0] != '\0') {
2204 gfx_new_text( im->canvas,
2205 im->ximg/2, im->yimg-6,
2206 ( im->graph_col[GRC_FONT] & 0xffffff00 ) | 0x00000044,
2207 im->text_prop[TEXT_PROP_AXIS].font,
2208 5.5, im->tabwidth, 0,
2209 GFX_H_CENTER, GFX_V_BOTTOM,
2214 if( !(im->extra_flags & NOLEGEND) & !(im->extra_flags & ONLY_GRAPH) ) {
2215 for(i=0;i<im->gdes_c;i++){
2216 if(im->gdes[i].legend[0] =='\0')
2219 /* im->gdes[i].leg_y is the bottom of the legend */
2220 X0 = im->gdes[i].leg_x;
2221 Y0 = im->gdes[i].leg_y;
2222 gfx_new_text ( im->canvas, X0, Y0,
2223 im->graph_col[GRC_FONT],
2224 im->text_prop[TEXT_PROP_LEGEND].font,
2225 im->text_prop[TEXT_PROP_LEGEND].size,
2226 im->tabwidth,0.0, GFX_H_LEFT, GFX_V_BOTTOM,
2227 im->gdes[i].legend );
2228 /* The legend for GRAPH items starts with "M " to have
2229 enough space for the box */
2230 if ( im->gdes[i].gf != GF_PRINT &&
2231 im->gdes[i].gf != GF_GPRINT &&
2232 im->gdes[i].gf != GF_COMMENT) {
2235 boxH = gfx_get_text_width(im->canvas, 0,
2236 im->text_prop[TEXT_PROP_LEGEND].font,
2237 im->text_prop[TEXT_PROP_LEGEND].size,
2238 im->tabwidth,"o", 0) * 1.2;
2241 /* make sure transparent colors show up the same way as in the graph */
2242 node = gfx_new_area(im->canvas,
2246 im->graph_col[GRC_BACK]);
2247 gfx_add_point ( node, X0+boxH, Y0-boxV );
2249 node = gfx_new_area(im->canvas,
2254 gfx_add_point ( node, X0+boxH, Y0-boxV );
2255 node = gfx_new_line(im->canvas,
2258 1.0,im->graph_col[GRC_FRAME]);
2259 gfx_add_point(node,X0+boxH,Y0);
2260 gfx_add_point(node,X0+boxH,Y0-boxV);
2261 gfx_close_path(node);
2268 /*****************************************************
2269 * lazy check make sure we rely need to create this graph
2270 *****************************************************/
2272 int lazy_check(image_desc_t *im){
2275 struct stat imgstat;
2277 if (im->lazy == 0) return 0; /* no lazy option */
2278 if (stat(im->graphfile,&imgstat) != 0)
2279 return 0; /* can't stat */
2280 /* one pixel in the existing graph is more then what we would
2282 if (time(NULL) - imgstat.st_mtime >
2283 (im->end - im->start) / im->xsize)
2285 if ((fd = fopen(im->graphfile,"rb")) == NULL)
2286 return 0; /* the file does not exist */
2287 switch (im->canvas->imgformat) {
2289 size = PngSize(fd,&(im->ximg),&(im->yimg));
2298 #ifdef WITH_PIECHART
2300 pie_part(image_desc_t *im, gfx_color_t color,
2301 double PieCenterX, double PieCenterY, double Radius,
2302 double startangle, double endangle)
2306 double step=M_PI/50; /* Number of iterations for the circle;
2307 ** 10 is definitely too low, more than
2308 ** 50 seems to be overkill
2311 /* Strange but true: we have to work clockwise or else
2312 ** anti aliasing nor transparency don't work.
2314 ** This test is here to make sure we do it right, also
2315 ** this makes the for...next loop more easy to implement.
2316 ** The return will occur if the user enters a negative number
2317 ** (which shouldn't be done according to the specs) or if the
2318 ** programmers do something wrong (which, as we all know, never
2319 ** happens anyway :)
2321 if (endangle<startangle) return;
2323 /* Hidden feature: Radius decreases each full circle */
2325 while (angle>=2*M_PI) {
2330 node=gfx_new_area(im->canvas,
2331 PieCenterX+sin(startangle)*Radius,
2332 PieCenterY-cos(startangle)*Radius,
2335 PieCenterX+sin(endangle)*Radius,
2336 PieCenterY-cos(endangle)*Radius,
2338 for (angle=endangle;angle-startangle>=step;angle-=step) {
2340 PieCenterX+sin(angle)*Radius,
2341 PieCenterY-cos(angle)*Radius );
2348 graph_size_location(image_desc_t *im, int elements
2350 #ifdef WITH_PIECHART
2356 /* The actual size of the image to draw is determined from
2357 ** several sources. The size given on the command line is
2358 ** the graph area but we need more as we have to draw labels
2359 ** and other things outside the graph area
2362 /* +-+-------------------------------------------+
2363 ** |l|.................title.....................|
2364 ** |e+--+-------------------------------+--------+
2367 ** |l| l| main graph area | chart |
2370 ** |r+--+-------------------------------+--------+
2371 ** |e| | x-axis labels | |
2372 ** |v+--+-------------------------------+--------+
2373 ** | |..............legends......................|
2374 ** +-+-------------------------------------------+
2376 ** +---------------------------------------------+
2382 #ifdef WITH_PIECHART
2387 Xlegend =0, Ylegend =0,
2389 Xspacing =15, Yspacing =15,
2393 if (im->extra_flags & ONLY_GRAPH) {
2395 im->ximg = im->xsize;
2396 im->yimg = im->ysize;
2397 im->yorigin = im->ysize;
2402 if (im->ylegend[0] != '\0' ) {
2403 Xvertical = im->text_prop[TEXT_PROP_UNIT].size *2;
2407 if (im->title[0] != '\0') {
2408 /* The title is placed "inbetween" two text lines so it
2409 ** automatically has some vertical spacing. The horizontal
2410 ** spacing is added here, on each side.
2412 /* don't care for the with of the title
2413 Xtitle = gfx_get_text_width(im->canvas, 0,
2414 im->text_prop[TEXT_PROP_TITLE].font,
2415 im->text_prop[TEXT_PROP_TITLE].size,
2417 im->title, 0) + 2*Xspacing; */
2418 Ytitle = im->text_prop[TEXT_PROP_TITLE].size*2.6+10;
2424 if (im->draw_x_grid) {
2425 Yxlabel=im->text_prop[TEXT_PROP_AXIS].size *2.5;
2427 if (im->draw_y_grid || im->forceleftspace ) {
2428 Xylabel=gfx_get_text_width(im->canvas, 0,
2429 im->text_prop[TEXT_PROP_AXIS].font,
2430 im->text_prop[TEXT_PROP_AXIS].size,
2432 "0", 0) * im->unitslength;
2436 #ifdef WITH_PIECHART
2438 im->piesize=im->xsize<im->ysize?im->xsize:im->ysize;
2444 /* Now calculate the total size. Insert some spacing where
2445 desired. im->xorigin and im->yorigin need to correspond
2446 with the lower left corner of the main graph area or, if
2447 this one is not set, the imaginary box surrounding the
2450 /* The legend width cannot yet be determined, as a result we
2451 ** have problems adjusting the image to it. For now, we just
2452 ** forget about it at all; the legend will have to fit in the
2453 ** size already allocated.
2455 im->ximg = Xylabel + Xmain + 2 * Xspacing;
2457 #ifdef WITH_PIECHART
2461 if (Xmain) im->ximg += Xspacing;
2462 #ifdef WITH_PIECHART
2463 if (Xpie) im->ximg += Xspacing;
2466 im->xorigin = Xspacing + Xylabel;
2468 /* the length of the title should not influence with width of the graph
2469 if (Xtitle > im->ximg) im->ximg = Xtitle; */
2471 if (Xvertical) { /* unit description */
2472 im->ximg += Xvertical;
2473 im->xorigin += Xvertical;
2477 /* The vertical size is interesting... we need to compare
2478 ** the sum of {Ytitle, Ymain, Yxlabel, Ylegend, Ywatermark} with
2479 ** Yvertical however we need to know {Ytitle+Ymain+Yxlabel}
2480 ** in order to start even thinking about Ylegend or Ywatermark.
2482 ** Do it in three portions: First calculate the inner part,
2483 ** then do the legend, then adjust the total height of the img,
2484 ** adding space for a watermark if one exists;
2487 /* reserve space for main and/or pie */
2489 im->yimg = Ymain + Yxlabel;
2491 #ifdef WITH_PIECHART
2492 if (im->yimg < Ypie) im->yimg = Ypie;
2495 im->yorigin = im->yimg - Yxlabel;
2497 /* reserve space for the title *or* some padding above the graph */
2500 im->yorigin += Ytitle;
2502 im->yimg += 1.5*Yspacing;
2503 im->yorigin += 1.5*Yspacing;
2505 /* reserve space for padding below the graph */
2506 im->yimg += Yspacing;
2508 /* Determine where to place the legends onto the image.
2509 ** Adjust im->yimg to match the space requirements.
2511 if(leg_place(im)==-1)
2514 if (im->watermark[0] != '\0') {
2515 im->yimg += Ywatermark;
2519 if (Xlegend > im->ximg) {
2521 /* reposition Pie */
2525 #ifdef WITH_PIECHART
2526 /* The pie is placed in the upper right hand corner,
2527 ** just below the title (if any) and with sufficient
2531 im->pie_x = im->ximg - Xspacing - Xpie/2;
2532 im->pie_y = im->yorigin-Ymain+Ypie/2;
2534 im->pie_x = im->ximg/2;
2535 im->pie_y = im->yorigin-Ypie/2;
2543 /* from http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm */
2544 /* yes we are loosing precision by doing tos with floats instead of doubles
2545 but it seems more stable this way. */
2548 /* draw that picture thing ... */
2550 graph_paint(image_desc_t *im, char ***calcpr)
2553 int lazy = lazy_check(im);
2554 #ifdef WITH_PIECHART
2556 double PieStart=0.0;
2561 double areazero = 0.0;
2562 graph_desc_t *lastgdes = NULL;
2564 /* if we are lazy and there is nothing to PRINT ... quit now */
2565 if (lazy && im->prt_c==0) return 0;
2567 /* pull the data from the rrd files ... */
2569 if(data_fetch(im)==-1)
2572 /* evaluate VDEF and CDEF operations ... */
2573 if(data_calc(im)==-1)
2576 #ifdef WITH_PIECHART
2577 /* check if we need to draw a piechart */
2578 for(i=0;i<im->gdes_c;i++){
2579 if (im->gdes[i].gf == GF_PART) {
2586 /* calculate and PRINT and GPRINT definitions. We have to do it at
2587 * this point because it will affect the length of the legends
2588 * if there are no graph elements we stop here ...
2589 * if we are lazy, try to quit ...
2591 i=print_calc(im,calcpr);
2594 #ifdef WITH_PIECHART
2597 ) || lazy) return 0;
2599 #ifdef WITH_PIECHART
2600 /* If there's only the pie chart to draw, signal this */
2601 if (i==0) piechart=2;
2604 /* get actual drawing data and find min and max values*/
2605 if(data_proc(im)==-1)
2608 if(!im->logarithmic){si_unit(im);} /* identify si magnitude Kilo, Mega Giga ? */
2610 if(!im->rigid && ! im->logarithmic)
2611 expand_range(im); /* make sure the upper and lower limit are
2614 if (!calc_horizontal_grid(im))
2621 /**************************************************************
2622 *** Calculating sizes and locations became a bit confusing ***
2623 *** so I moved this into a separate function. ***
2624 **************************************************************/
2625 if(graph_size_location(im,i
2626 #ifdef WITH_PIECHART
2632 /* the actual graph is created by going through the individual
2633 graph elements and then drawing them */
2635 node=gfx_new_area ( im->canvas,
2639 im->graph_col[GRC_BACK]);
2641 gfx_add_point(node,im->ximg, 0);
2643 #ifdef WITH_PIECHART
2644 if (piechart != 2) {
2646 node=gfx_new_area ( im->canvas,
2647 im->xorigin, im->yorigin,
2648 im->xorigin + im->xsize, im->yorigin,
2649 im->xorigin + im->xsize, im->yorigin-im->ysize,
2650 im->graph_col[GRC_CANVAS]);
2652 gfx_add_point(node,im->xorigin, im->yorigin - im->ysize);
2654 if (im->minval > 0.0)
2655 areazero = im->minval;
2656 if (im->maxval < 0.0)
2657 areazero = im->maxval;
2658 #ifdef WITH_PIECHART
2662 #ifdef WITH_PIECHART
2664 pie_part(im,im->graph_col[GRC_CANVAS],im->pie_x,im->pie_y,im->piesize*0.5,0,2*M_PI);
2668 for(i=0;i<im->gdes_c;i++){
2669 switch(im->gdes[i].gf){
2682 for (ii = 0; ii < im->xsize; ii++)
2684 if (!isnan(im->gdes[i].p_data[ii]) &&
2685 im->gdes[i].p_data[ii] != 0.0)
2687 if (im -> gdes[i].yrule > 0 ) {
2688 gfx_new_line(im->canvas,
2689 im -> xorigin + ii, im->yorigin,
2690 im -> xorigin + ii, im->yorigin - im -> gdes[i].yrule * im -> ysize,
2692 im -> gdes[i].col );
2693 } else if ( im -> gdes[i].yrule < 0 ) {
2694 gfx_new_line(im->canvas,
2695 im -> xorigin + ii, im->yorigin - im -> ysize,
2696 im -> xorigin + ii, im->yorigin - ( 1 - im -> gdes[i].yrule ) * im -> ysize,
2698 im -> gdes[i].col );
2706 /* fix data points at oo and -oo */
2707 for(ii=0;ii<im->xsize;ii++){
2708 if (isinf(im->gdes[i].p_data[ii])){
2709 if (im->gdes[i].p_data[ii] > 0) {
2710 im->gdes[i].p_data[ii] = im->maxval ;
2712 im->gdes[i].p_data[ii] = im->minval ;
2718 /* *******************************************************
2723 -------|--t-1--t--------------------------------
2725 if we know the value at time t was a then
2726 we draw a square from t-1 to t with the value a.
2728 ********************************************************* */
2729 if (im->gdes[i].col != 0x0){
2730 /* GF_LINE and friend */
2731 if(im->gdes[i].gf == GF_LINE ){
2734 for(ii=1;ii<im->xsize;ii++){
2735 if (isnan(im->gdes[i].p_data[ii]) || (im->slopemode==1 && isnan(im->gdes[i].p_data[ii-1]))){
2739 if ( node == NULL ) {
2740 last_y = ytr(im,im->gdes[i].p_data[ii]);
2741 if ( im->slopemode == 0 ){
2742 node = gfx_new_line(im->canvas,
2743 ii-1+im->xorigin,last_y,
2744 ii+im->xorigin,last_y,
2745 im->gdes[i].linewidth,
2748 node = gfx_new_line(im->canvas,
2749 ii-1+im->xorigin,ytr(im,im->gdes[i].p_data[ii-1]),
2750 ii+im->xorigin,last_y,
2751 im->gdes[i].linewidth,
2755 double new_y = ytr(im,im->gdes[i].p_data[ii]);
2756 if ( im->slopemode==0 && ! AlmostEqual2sComplement(new_y,last_y,4)){
2757 gfx_add_point(node,ii-1+im->xorigin,new_y);
2760 gfx_add_point(node,ii+im->xorigin,new_y);
2766 double *foreY=malloc(sizeof(double)*im->xsize*2);
2767 double *foreX=malloc(sizeof(double)*im->xsize*2);
2768 double *backY=malloc(sizeof(double)*im->xsize*2);
2769 double *backX=malloc(sizeof(double)*im->xsize*2);
2771 for(ii=0;ii<=im->xsize;ii++){
2773 if ( idxI > 0 && ( drawem != 0 || ii==im->xsize)){
2776 while (cntI < idxI && AlmostEqual2sComplement(foreY[lastI],foreY[cntI],4) && AlmostEqual2sComplement(foreY[lastI],foreY[cntI+1],4)){cntI++;}
2777 node = gfx_new_area(im->canvas,
2780 foreX[cntI],foreY[cntI], im->gdes[i].col);
2781 while (cntI < idxI) {
2784 while ( cntI < idxI && AlmostEqual2sComplement(foreY[lastI],foreY[cntI],4) && AlmostEqual2sComplement(foreY[lastI],foreY[cntI+1],4)){cntI++;}
2785 gfx_add_point(node,foreX[cntI],foreY[cntI]);
2787 gfx_add_point(node,backX[idxI],backY[idxI]);
2791 while ( idxI > 1 && AlmostEqual2sComplement(backY[lastI], backY[idxI],4) && AlmostEqual2sComplement(backY[lastI],backY[idxI-1],4)){idxI--;}
2792 gfx_add_point(node,backX[idxI],backY[idxI]);
2801 if (ii == im->xsize) break;
2803 /* keep things simple for now, just draw these bars
2804 do not try to build a big and complex area */
2807 if ( im->slopemode == 0 && ii==0){
2810 if ( isnan(im->gdes[i].p_data[ii]) ) {
2814 ytop = ytr(im,im->gdes[i].p_data[ii]);
2815 if ( lastgdes && im->gdes[i].stack ) {
2816 ybase = ytr(im,lastgdes->p_data[ii]);
2818 ybase = ytr(im,areazero);
2820 if ( ybase == ytop ){
2824 /* every area has to be wound clock-wise,
2825 so we have to make sur base remains base */
2827 double extra = ytop;
2831 if ( im->slopemode == 0 ){
2832 backY[++idxI] = ybase-0.2;
2833 backX[idxI] = ii+im->xorigin-1;
2834 foreY[idxI] = ytop+0.2;
2835 foreX[idxI] = ii+im->xorigin-1;
2837 backY[++idxI] = ybase-0.2;
2838 backX[idxI] = ii+im->xorigin;
2839 foreY[idxI] = ytop+0.2;
2840 foreX[idxI] = ii+im->xorigin;
2842 /* close up any remaining area */
2847 } /* else GF_LINE */
2848 } /* if color != 0x0 */
2849 /* make sure we do not run into trouble when stacking on NaN */
2850 for(ii=0;ii<im->xsize;ii++){
2851 if (isnan(im->gdes[i].p_data[ii])) {
2852 if (lastgdes && (im->gdes[i].stack)) {
2853 im->gdes[i].p_data[ii] = lastgdes->p_data[ii];
2855 im->gdes[i].p_data[ii] = areazero;
2859 lastgdes = &(im->gdes[i]);
2861 #ifdef WITH_PIECHART
2863 if(isnan(im->gdes[i].yrule)) /* fetch variable */
2864 im->gdes[i].yrule = im->gdes[im->gdes[i].vidx].vf.val;
2866 if (finite(im->gdes[i].yrule)) { /* even the fetched var can be NaN */
2867 pie_part(im,im->gdes[i].col,
2868 im->pie_x,im->pie_y,im->piesize*0.4,
2869 M_PI*2.0*PieStart/100.0,
2870 M_PI*2.0*(PieStart+im->gdes[i].yrule)/100.0);
2871 PieStart += im->gdes[i].yrule;
2876 rrd_set_error("STACK should already be turned into LINE or AREA here");
2882 #ifdef WITH_PIECHART
2890 /* grid_paint also does the text */
2891 if( !(im->extra_flags & ONLY_GRAPH) )
2895 if( !(im->extra_flags & ONLY_GRAPH) )
2898 /* the RULES are the last thing to paint ... */
2899 for(i=0;i<im->gdes_c;i++){
2901 switch(im->gdes[i].gf){
2903 if(im->gdes[i].yrule >= im->minval
2904 && im->gdes[i].yrule <= im->maxval)
2905 gfx_new_line(im->canvas,
2906 im->xorigin,ytr(im,im->gdes[i].yrule),
2907 im->xorigin+im->xsize,ytr(im,im->gdes[i].yrule),
2908 1.0,im->gdes[i].col);
2911 if(im->gdes[i].xrule >= im->start
2912 && im->gdes[i].xrule <= im->end)
2913 gfx_new_line(im->canvas,
2914 xtr(im,im->gdes[i].xrule),im->yorigin,
2915 xtr(im,im->gdes[i].xrule),im->yorigin-im->ysize,
2916 1.0,im->gdes[i].col);
2924 if (strcmp(im->graphfile,"-")==0) {
2925 fo = im->graphhandle ? im->graphhandle : stdout;
2926 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
2927 /* Change translation mode for stdout to BINARY */
2928 _setmode( _fileno( fo ), O_BINARY );
2931 if ((fo = fopen(im->graphfile,"wb")) == NULL) {
2932 rrd_set_error("Opening '%s' for write: %s",im->graphfile,
2933 rrd_strerror(errno));
2937 gfx_render (im->canvas,im->ximg,im->yimg,0x00000000,fo);
2938 if (strcmp(im->graphfile,"-") != 0)
2944 /*****************************************************
2946 *****************************************************/
2949 gdes_alloc(image_desc_t *im){
2952 if ((im->gdes = (graph_desc_t *) rrd_realloc(im->gdes, (im->gdes_c)
2953 * sizeof(graph_desc_t)))==NULL){
2954 rrd_set_error("realloc graph_descs");
2959 im->gdes[im->gdes_c-1].step=im->step;
2960 im->gdes[im->gdes_c-1].step_orig=im->step;
2961 im->gdes[im->gdes_c-1].stack=0;
2962 im->gdes[im->gdes_c-1].linewidth=0;
2963 im->gdes[im->gdes_c-1].debug=0;
2964 im->gdes[im->gdes_c-1].start=im->start;
2965 im->gdes[im->gdes_c-1].start_orig=im->start;
2966 im->gdes[im->gdes_c-1].end=im->end;
2967 im->gdes[im->gdes_c-1].end_orig=im->end;
2968 im->gdes[im->gdes_c-1].vname[0]='\0';
2969 im->gdes[im->gdes_c-1].data=NULL;
2970 im->gdes[im->gdes_c-1].ds_namv=NULL;
2971 im->gdes[im->gdes_c-1].data_first=0;
2972 im->gdes[im->gdes_c-1].p_data=NULL;
2973 im->gdes[im->gdes_c-1].rpnp=NULL;
2974 im->gdes[im->gdes_c-1].shift=0;
2975 im->gdes[im->gdes_c-1].col = 0x0;
2976 im->gdes[im->gdes_c-1].legend[0]='\0';
2977 im->gdes[im->gdes_c-1].format[0]='\0';
2978 im->gdes[im->gdes_c-1].strftm=0;
2979 im->gdes[im->gdes_c-1].rrd[0]='\0';
2980 im->gdes[im->gdes_c-1].ds=-1;
2981 im->gdes[im->gdes_c-1].cf_reduce=CF_AVERAGE;
2982 im->gdes[im->gdes_c-1].cf=CF_AVERAGE;
2983 im->gdes[im->gdes_c-1].p_data=NULL;
2984 im->gdes[im->gdes_c-1].yrule=DNAN;
2985 im->gdes[im->gdes_c-1].xrule=0;
2989 /* copies input untill the first unescaped colon is found
2990 or until input ends. backslashes have to be escaped as well */
2992 scan_for_col(const char *const input, int len, char *const output)
2997 input[inp] != ':' &&
3000 if (input[inp] == '\\' &&
3001 input[inp+1] != '\0' &&
3002 (input[inp+1] == '\\' ||
3003 input[inp+1] == ':')){
3004 output[outp++] = input[++inp];
3007 output[outp++] = input[inp];
3010 output[outp] = '\0';
3013 /* Some surgery done on this function, it became ridiculously big.
3015 ** - initializing now in rrd_graph_init()
3016 ** - options parsing now in rrd_graph_options()
3017 ** - script parsing now in rrd_graph_script()
3020 rrd_graph(int argc, char **argv, char ***prdata, int *xsize, int *ysize, FILE *stream, double *ymin, double *ymax)
3023 rrd_graph_init(&im);
3024 im.graphhandle = stream;
3026 rrd_graph_options(argc,argv,&im);
3027 if (rrd_test_error()) {
3032 if (strlen(argv[optind])>=MAXPATH) {
3033 rrd_set_error("filename (including path) too long");
3037 strncpy(im.graphfile,argv[optind],MAXPATH-1);
3038 im.graphfile[MAXPATH-1]='\0';
3040 rrd_graph_script(argc,argv,&im,1);
3041 if (rrd_test_error()) {
3046 /* Everything is now read and the actual work can start */
3049 if (graph_paint(&im,prdata)==-1){
3054 /* The image is generated and needs to be output.
3055 ** Also, if needed, print a line with information about the image.
3065 /* maybe prdata is not allocated yet ... lets do it now */
3066 if ((*prdata = calloc(2,sizeof(char *)))==NULL) {
3067 rrd_set_error("malloc imginfo");
3071 if(((*prdata)[0] = malloc((strlen(im.imginfo)+200+strlen(im.graphfile))*sizeof(char)))
3073 rrd_set_error("malloc imginfo");
3076 filename=im.graphfile+strlen(im.graphfile);
3077 while(filename > im.graphfile) {
3078 if (*(filename-1)=='/' || *(filename-1)=='\\' ) break;
3082 sprintf((*prdata)[0],im.imginfo,filename,(long)(im.canvas->zoom*im.ximg),(long)(im.canvas->zoom*im.yimg));
3089 rrd_graph_init(image_desc_t *im)
3096 #ifdef HAVE_SETLOCALE
3097 setlocale(LC_TIME,"");
3098 #ifdef HAVE_MBSTOWCS
3099 setlocale(LC_CTYPE,"");
3105 im->xlab_user.minsec = -1;
3111 im->ylegend[0] = '\0';
3112 im->title[0] = '\0';
3113 im->watermark[0] = '\0';
3116 im->unitsexponent= 9999;
3118 im->forceleftspace = 0;
3120 im->viewfactor = 1.0;
3127 im->logarithmic = 0;
3128 im->ygridstep = DNAN;
3129 im->draw_x_grid = 1;
3130 im->draw_y_grid = 1;
3135 im->canvas = gfx_new_canvas();
3136 im->grid_dash_on = 1;
3137 im->grid_dash_off = 1;
3138 im->tabwidth = 40.0;
3140 for(i=0;i<DIM(graph_col);i++)
3141 im->graph_col[i]=graph_col[i];
3143 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
3146 char rrd_win_default_font[1000];
3147 windir = getenv("windir");
3148 /* %windir% is something like D:\windows or C:\winnt */
3149 if (windir != NULL) {
3150 strncpy(rrd_win_default_font,windir,500);
3151 rrd_win_default_font[500] = '\0';
3152 strcat(rrd_win_default_font,"\\fonts\\");
3153 strcat(rrd_win_default_font,RRD_DEFAULT_FONT);
3154 for(i=0;i<DIM(text_prop);i++){
3155 strncpy(text_prop[i].font,rrd_win_default_font,sizeof(text_prop[i].font)-1);
3156 text_prop[i].font[sizeof(text_prop[i].font)-1] = '\0';
3163 deffont = getenv("RRD_DEFAULT_FONT");
3164 if (deffont != NULL) {
3165 for(i=0;i<DIM(text_prop);i++){
3166 strncpy(text_prop[i].font,deffont,sizeof(text_prop[i].font)-1);
3167 text_prop[i].font[sizeof(text_prop[i].font)-1] = '\0';
3171 for(i=0;i<DIM(text_prop);i++){
3172 im->text_prop[i].size = text_prop[i].size;
3173 strcpy(im->text_prop[i].font,text_prop[i].font);
3178 rrd_graph_options(int argc, char *argv[],image_desc_t *im)
3181 char *parsetime_error = NULL;
3182 char scan_gtm[12],scan_mtm[12],scan_ltm[12],col_nam[12];
3183 time_t start_tmp=0,end_tmp=0;
3185 struct rrd_time_value start_tv, end_tv;
3187 optind = 0; opterr = 0; /* initialize getopt */
3189 parsetime("end-24h", &start_tv);
3190 parsetime("now", &end_tv);
3192 /* defines for long options without a short equivalent. should be bytes,
3193 and may not collide with (the ASCII value of) short options */
3194 #define LONGOPT_UNITS_SI 255
3197 static struct option long_options[] =
3199 {"start", required_argument, 0, 's'},
3200 {"end", required_argument, 0, 'e'},
3201 {"x-grid", required_argument, 0, 'x'},
3202 {"y-grid", required_argument, 0, 'y'},
3203 {"vertical-label",required_argument,0,'v'},
3204 {"width", required_argument, 0, 'w'},
3205 {"height", required_argument, 0, 'h'},
3206 {"interlaced", no_argument, 0, 'i'},
3207 {"upper-limit",required_argument, 0, 'u'},
3208 {"lower-limit",required_argument, 0, 'l'},
3209 {"rigid", no_argument, 0, 'r'},
3210 {"base", required_argument, 0, 'b'},
3211 {"logarithmic",no_argument, 0, 'o'},
3212 {"color", required_argument, 0, 'c'},
3213 {"font", required_argument, 0, 'n'},
3214 {"title", required_argument, 0, 't'},
3215 {"imginfo", required_argument, 0, 'f'},
3216 {"imgformat", required_argument, 0, 'a'},
3217 {"lazy", no_argument, 0, 'z'},
3218 {"zoom", required_argument, 0, 'm'},
3219 {"no-legend", no_argument, 0, 'g'},
3220 {"force-rules-legend",no_argument,0, 'F'},
3221 {"only-graph", no_argument, 0, 'j'},
3222 {"alt-y-grid", no_argument, 0, 'Y'},
3223 {"no-minor", no_argument, 0, 'I'},
3224 {"slope-mode", no_argument, 0, 'E'},
3225 {"alt-autoscale", no_argument, 0, 'A'},
3226 {"alt-autoscale-max", no_argument, 0, 'M'},
3227 {"no-gridfit", no_argument, 0, 'N'},
3228 {"units-exponent",required_argument, 0, 'X'},
3229 {"units-length",required_argument, 0, 'L'},
3230 {"units", required_argument, 0, LONGOPT_UNITS_SI },
3231 {"step", required_argument, 0, 'S'},
3232 {"tabwidth", required_argument, 0, 'T'},
3233 {"font-render-mode", required_argument, 0, 'R'},
3234 {"font-smoothing-threshold", required_argument, 0, 'B'},
3235 {"watermark", required_argument, 0, 'W'},
3236 {"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 */
3238 int option_index = 0;
3240 int col_start,col_end;
3242 opt = getopt_long(argc, argv,
3243 "s:e:x:y:v:w:h:iu:l:rb:oc:n:m:t:f:a:I:zgjFYAMEX:L:S:T:NR:B:W:",
3244 long_options, &option_index);
3251 im->extra_flags |= NOMINOR;
3254 im->extra_flags |= ALTYGRID;
3257 im->extra_flags |= ALTAUTOSCALE;
3260 im->extra_flags |= ALTAUTOSCALE_MAX;
3263 im->extra_flags |= ONLY_GRAPH;
3266 im->extra_flags |= NOLEGEND;
3269 im->extra_flags |= FORCE_RULES_LEGEND;
3271 case LONGOPT_UNITS_SI:
3272 if(im->extra_flags & FORCE_UNITS) {
3273 rrd_set_error("--units can only be used once!");
3276 if(strcmp(optarg,"si")==0)
3277 im->extra_flags |= FORCE_UNITS_SI;
3279 rrd_set_error("invalid argument for --units: %s", optarg );
3284 im->unitsexponent = atoi(optarg);
3287 im->unitslength = atoi(optarg);
3288 im->forceleftspace = 1;
3291 im->tabwidth = atof(optarg);
3294 im->step = atoi(optarg);
3300 if ((parsetime_error = parsetime(optarg, &start_tv))) {
3301 rrd_set_error( "start time: %s", parsetime_error );
3306 if ((parsetime_error = parsetime(optarg, &end_tv))) {
3307 rrd_set_error( "end time: %s", parsetime_error );
3312 if(strcmp(optarg,"none") == 0){
3318 "%10[A-Z]:%ld:%10[A-Z]:%ld:%10[A-Z]:%ld:%ld:%n",
3320 &im->xlab_user.gridst,
3322 &im->xlab_user.mgridst,
3324 &im->xlab_user.labst,
3325 &im->xlab_user.precis,
3326 &stroff) == 7 && stroff != 0){
3327 strncpy(im->xlab_form, optarg+stroff, sizeof(im->xlab_form) - 1);
3328 im->xlab_form[sizeof(im->xlab_form)-1] = '\0';
3329 if((int)(im->xlab_user.gridtm = tmt_conv(scan_gtm)) == -1){
3330 rrd_set_error("unknown keyword %s",scan_gtm);
3332 } else if ((int)(im->xlab_user.mgridtm = tmt_conv(scan_mtm)) == -1){
3333 rrd_set_error("unknown keyword %s",scan_mtm);
3335 } else if ((int)(im->xlab_user.labtm = tmt_conv(scan_ltm)) == -1){
3336 rrd_set_error("unknown keyword %s",scan_ltm);
3339 im->xlab_user.minsec = 1;
3340 im->xlab_user.stst = im->xlab_form;
3342 rrd_set_error("invalid x-grid format");
3348 if(strcmp(optarg,"none") == 0){
3356 &im->ylabfact) == 2) {
3357 if(im->ygridstep<=0){
3358 rrd_set_error("grid step must be > 0");
3360 } else if (im->ylabfact < 1){
3361 rrd_set_error("label factor must be > 0");
3365 rrd_set_error("invalid y-grid format");
3370 strncpy(im->ylegend,optarg,150);
3371 im->ylegend[150]='\0';
3374 im->maxval = atof(optarg);
3377 im->minval = atof(optarg);
3380 im->base = atol(optarg);
3381 if(im->base != 1024 && im->base != 1000 ){
3382 rrd_set_error("the only sensible value for base apart from 1000 is 1024");
3387 long_tmp = atol(optarg);
3388 if (long_tmp < 10) {
3389 rrd_set_error("width below 10 pixels");
3392 im->xsize = long_tmp;
3395 long_tmp = atol(optarg);
3396 if (long_tmp < 10) {
3397 rrd_set_error("height below 10 pixels");
3400 im->ysize = long_tmp;
3403 im->canvas->interlaced = 1;
3409 im->imginfo = optarg;
3412 if((int)(im->canvas->imgformat = if_conv(optarg)) == -1) {
3413 rrd_set_error("unsupported graphics format '%s'",optarg);
3425 im->logarithmic = 1;
3429 "%10[A-Z]#%n%8lx%n",
3430 col_nam,&col_start,&color,&col_end) == 2){
3432 int col_len = col_end - col_start;
3436 ((color & 0xF00) * 0x110000) |
3437 ((color & 0x0F0) * 0x011000) |
3438 ((color & 0x00F) * 0x001100) |
3444 ((color & 0xF000) * 0x11000) |
3445 ((color & 0x0F00) * 0x01100) |
3446 ((color & 0x00F0) * 0x00110) |
3447 ((color & 0x000F) * 0x00011)
3451 color = (color << 8) + 0xff /* shift left by 8 */;
3456 rrd_set_error("the color format is #RRGGBB[AA]");
3459 if((ci=grc_conv(col_nam)) != -1){
3460 im->graph_col[ci]=color;
3462 rrd_set_error("invalid color name '%s'",col_nam);
3466 rrd_set_error("invalid color def format");
3473 char font[1024] = "";
3476 "%10[A-Z]:%lf:%1000s",
3477 prop,&size,font) >= 2){
3479 if((sindex=text_prop_conv(prop)) != -1){
3480 for (propidx=sindex;propidx<TEXT_PROP_LAST;propidx++){
3482 im->text_prop[propidx].size=size;
3484 if (strlen(font) > 0){
3485 strcpy(im->text_prop[propidx].font,font);
3487 if (propidx==sindex && sindex != 0) break;
3490 rrd_set_error("invalid fonttag '%s'",prop);
3494 rrd_set_error("invalid text property format");
3500 im->canvas->zoom = atof(optarg);
3501 if (im->canvas->zoom <= 0.0) {
3502 rrd_set_error("zoom factor must be > 0");
3507 strncpy(im->title,optarg,150);
3508 im->title[150]='\0';
3512 if ( strcmp( optarg, "normal" ) == 0 )
3513 im->canvas->aa_type = AA_NORMAL;
3514 else if ( strcmp( optarg, "light" ) == 0 )
3515 im->canvas->aa_type = AA_LIGHT;
3516 else if ( strcmp( optarg, "mono" ) == 0 )
3517 im->canvas->aa_type = AA_NONE;
3520 rrd_set_error("unknown font-render-mode '%s'", optarg );
3526 im->canvas->font_aa_threshold = atof(optarg);
3530 strncpy(im->watermark,optarg,100);
3531 im->watermark[99]='\0';
3536 rrd_set_error("unknown option '%c'", optopt);
3538 rrd_set_error("unknown option '%s'",argv[optind-1]);
3543 if (optind >= argc) {
3544 rrd_set_error("missing filename");
3548 if (im->logarithmic == 1 && im->minval <= 0){
3549 rrd_set_error("for a logarithmic yaxis you must specify a lower-limit > 0");
3553 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
3554 /* error string is set in parsetime.c */
3558 if (start_tmp < 3600*24*365*10){
3559 rrd_set_error("the first entry to fetch should be after 1980 (%ld)",start_tmp);
3563 if (end_tmp < start_tmp) {
3564 rrd_set_error("start (%ld) should be less than end (%ld)",
3565 start_tmp, end_tmp);
3569 im->start = start_tmp;
3571 im->step = max((long)im->step, (im->end-im->start)/im->xsize);
3575 rrd_graph_check_vname(image_desc_t *im, char *varname, char *err)
3577 if ((im->gdes[im->gdes_c-1].vidx=find_var(im,varname))==-1) {
3578 rrd_set_error("Unknown variable '%s' in %s",varname,err);
3584 rrd_graph_color(image_desc_t *im, char *var, char *err, int optional)
3587 graph_desc_t *gdp=&im->gdes[im->gdes_c-1];
3589 color=strstr(var,"#");
3592 rrd_set_error("Found no color in %s",err);
3601 rest=strstr(color,":");
3609 sscanf(color,"#%6lx%n",&col,&n);
3610 col = (col << 8) + 0xff /* shift left by 8 */;
3611 if (n!=7) rrd_set_error("Color problem in %s",err);
3614 sscanf(color,"#%8lx%n",&col,&n);
3617 rrd_set_error("Color problem in %s",err);
3619 if (rrd_test_error()) return 0;
3626 int bad_format(char *fmt) {
3630 while (*ptr != '\0')
3631 if (*ptr++ == '%') {
3633 /* line cannot end with percent char */
3634 if (*ptr == '\0') return 1;
3636 /* '%s', '%S' and '%%' are allowed */
3637 if (*ptr == 's' || *ptr == 'S' || *ptr == '%') ptr++;
3639 /* %c is allowed (but use only with vdef!) */
3640 else if (*ptr == 'c') {
3645 /* or else '% 6.2lf' and such are allowed */
3647 /* optional padding character */
3648 if (*ptr == ' ' || *ptr == '+' || *ptr == '-') ptr++;
3650 /* This should take care of 'm.n' with all three optional */
3651 while (*ptr >= '0' && *ptr <= '9') ptr++;
3652 if (*ptr == '.') ptr++;
3653 while (*ptr >= '0' && *ptr <= '9') ptr++;
3655 /* Either 'le', 'lf' or 'lg' must follow here */
3656 if (*ptr++ != 'l') return 1;
3657 if (*ptr == 'e' || *ptr == 'f' || *ptr == 'g') ptr++;
3668 vdef_parse(gdes,str)
3669 struct graph_desc_t *gdes;
3670 const char *const str;
3672 /* A VDEF currently is either "func" or "param,func"
3673 * so the parsing is rather simple. Change if needed.
3680 sscanf(str,"%le,%29[A-Z]%n",¶m,func,&n);
3681 if (n== (int)strlen(str)) { /* matched */
3685 sscanf(str,"%29[A-Z]%n",func,&n);
3686 if (n== (int)strlen(str)) { /* matched */
3689 rrd_set_error("Unknown function string '%s' in VDEF '%s'"
3696 if (!strcmp("PERCENT",func)) gdes->vf.op = VDEF_PERCENT;
3697 else if (!strcmp("MAXIMUM",func)) gdes->vf.op = VDEF_MAXIMUM;
3698 else if (!strcmp("AVERAGE",func)) gdes->vf.op = VDEF_AVERAGE;
3699 else if (!strcmp("MINIMUM",func)) gdes->vf.op = VDEF_MINIMUM;
3700 else if (!strcmp("TOTAL", func)) gdes->vf.op = VDEF_TOTAL;
3701 else if (!strcmp("FIRST", func)) gdes->vf.op = VDEF_FIRST;
3702 else if (!strcmp("LAST", func)) gdes->vf.op = VDEF_LAST;
3703 else if (!strcmp("LSLSLOPE", func)) gdes->vf.op = VDEF_LSLSLOPE;
3704 else if (!strcmp("LSLINT", func)) gdes->vf.op = VDEF_LSLINT;
3705 else if (!strcmp("LSLCORREL",func)) gdes->vf.op = VDEF_LSLCORREL;
3707 rrd_set_error("Unknown function '%s' in VDEF '%s'\n"
3714 switch (gdes->vf.op) {
3716 if (isnan(param)) { /* no parameter given */
3717 rrd_set_error("Function '%s' needs parameter in VDEF '%s'\n"
3723 if (param>=0.0 && param<=100.0) {
3724 gdes->vf.param = param;
3725 gdes->vf.val = DNAN; /* undefined */
3726 gdes->vf.when = 0; /* undefined */
3728 rrd_set_error("Parameter '%f' out of range in VDEF '%s'\n"
3743 case VDEF_LSLCORREL:
3745 gdes->vf.param = DNAN;
3746 gdes->vf.val = DNAN;
3749 rrd_set_error("Function '%s' needs no parameter in VDEF '%s'\n"
3766 graph_desc_t *src,*dst;
3770 dst = &im->gdes[gdi];
3771 src = &im->gdes[dst->vidx];
3772 data = src->data + src->ds;
3773 steps = (src->end - src->start) / src->step;
3776 printf("DEBUG: start == %lu, end == %lu, %lu steps\n"
3783 switch (dst->vf.op) {
3784 case VDEF_PERCENT: {
3785 rrd_value_t * array;
3789 if ((array = malloc(steps*sizeof(double)))==NULL) {
3790 rrd_set_error("malloc VDEV_PERCENT");
3793 for (step=0;step < steps; step++) {
3794 array[step]=data[step*src->ds_cnt];
3796 qsort(array,step,sizeof(double),vdef_percent_compar);
3798 field = (steps-1)*dst->vf.param/100;
3799 dst->vf.val = array[field];
3800 dst->vf.when = 0; /* no time component */
3803 for(step=0;step<steps;step++)
3804 printf("DEBUG: %3li:%10.2f %c\n",step,array[step],step==field?'*':' ');
3810 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3811 if (step == steps) {
3815 dst->vf.val = data[step*src->ds_cnt];
3816 dst->vf.when = src->start + (step+1)*src->step;
3818 while (step != steps) {
3819 if (finite(data[step*src->ds_cnt])) {
3820 if (data[step*src->ds_cnt] > dst->vf.val) {
3821 dst->vf.val = data[step*src->ds_cnt];
3822 dst->vf.when = src->start + (step+1)*src->step;
3829 case VDEF_AVERAGE: {
3832 for (step=0;step<steps;step++) {
3833 if (finite(data[step*src->ds_cnt])) {
3834 sum += data[step*src->ds_cnt];
3839 if (dst->vf.op == VDEF_TOTAL) {
3840 dst->vf.val = sum*src->step;
3841 dst->vf.when = 0; /* no time component */
3843 dst->vf.val = sum/cnt;
3844 dst->vf.when = 0; /* no time component */
3854 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3855 if (step == steps) {
3859 dst->vf.val = data[step*src->ds_cnt];
3860 dst->vf.when = src->start + (step+1)*src->step;
3862 while (step != steps) {
3863 if (finite(data[step*src->ds_cnt])) {
3864 if (data[step*src->ds_cnt] < dst->vf.val) {
3865 dst->vf.val = data[step*src->ds_cnt];
3866 dst->vf.when = src->start + (step+1)*src->step;
3873 /* The time value returned here is one step before the
3874 * actual time value. This is the start of the first
3878 while (step != steps && isnan(data[step*src->ds_cnt])) step++;
3879 if (step == steps) { /* all entries were NaN */
3883 dst->vf.val = data[step*src->ds_cnt];
3884 dst->vf.when = src->start + step*src->step;
3888 /* The time value returned here is the
3889 * actual time value. This is the end of the last
3893 while (step >= 0 && isnan(data[step*src->ds_cnt])) step--;
3894 if (step < 0) { /* all entries were NaN */
3898 dst->vf.val = data[step*src->ds_cnt];
3899 dst->vf.when = src->start + (step+1)*src->step;
3904 case VDEF_LSLCORREL:{
3905 /* Bestfit line by linear least squares method */
3908 double SUMx, SUMy, SUMxy, SUMxx, SUMyy, slope, y_intercept, correl ;
3909 SUMx = 0; SUMy = 0; SUMxy = 0; SUMxx = 0; SUMyy = 0;
3911 for (step=0;step<steps;step++) {
3912 if (finite(data[step*src->ds_cnt])) {
3915 SUMxx += step * step;
3916 SUMxy += step * data[step*src->ds_cnt];
3917 SUMy += data[step*src->ds_cnt];
3918 SUMyy += data[step*src->ds_cnt]*data[step*src->ds_cnt];
3922 slope = ( SUMx*SUMy - cnt*SUMxy ) / ( SUMx*SUMx - cnt*SUMxx );
3923 y_intercept = ( SUMy - slope*SUMx ) / cnt;
3924 correl = (SUMxy - (SUMx*SUMy)/cnt) / sqrt((SUMxx - (SUMx*SUMx)/cnt)*(SUMyy - (SUMy*SUMy)/cnt));
3927 if (dst->vf.op == VDEF_LSLSLOPE) {
3928 dst->vf.val = slope;
3930 } else if (dst->vf.op == VDEF_LSLINT) {
3931 dst->vf.val = y_intercept;
3933 } else if (dst->vf.op == VDEF_LSLCORREL) {
3934 dst->vf.val = correl;
3948 /* NaN < -INF < finite_values < INF */
3950 vdef_percent_compar(a,b)
3953 /* Equality is not returned; this doesn't hurt except
3954 * (maybe) for a little performance.
3957 /* First catch NaN values. They are smallest */
3958 if (isnan( *(double *)a )) return -1;
3959 if (isnan( *(double *)b )) return 1;
3961 /* NaN doesn't reach this part so INF and -INF are extremes.
3962 * The sign from isinf() is compatible with the sign we return
3964 if (isinf( *(double *)a )) return isinf( *(double *)a );
3965 if (isinf( *(double *)b )) return isinf( *(double *)b );
3967 /* If we reach this, both values must be finite */
3968 if ( *(double *)a < *(double *)b ) return -1; else return 1;