1 /****************************************************************************
2 * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008
3 ****************************************************************************
4 * rrd__graph.c produce graphs from data in rrdfiles
5 ****************************************************************************/
16 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
29 #include "rrd_graph.h"
30 #include "rrd_client.h"
32 /* some constant definitions */
36 #ifndef RRD_DEFAULT_FONT
37 /* there is special code later to pick Cour.ttf when running on windows */
38 #define RRD_DEFAULT_FONT "DejaVu Sans Mono,Bitstream Vera Sans Mono,monospace,Courier"
41 text_prop_t text_prop[] = {
42 {8.0, RRD_DEFAULT_FONT,NULL}
44 {9.0, RRD_DEFAULT_FONT,NULL}
46 {7.0, RRD_DEFAULT_FONT,NULL}
48 {8.0, RRD_DEFAULT_FONT,NULL}
50 {8.0, RRD_DEFAULT_FONT,NULL} /* legend */
52 {5.5, RRD_DEFAULT_FONT,NULL} /* watermark */
56 {0, 0, TMT_SECOND, 30, TMT_MINUTE, 5, TMT_MINUTE, 5, 0, "%H:%M"}
58 {2, 0, TMT_MINUTE, 1, TMT_MINUTE, 5, TMT_MINUTE, 5, 0, "%H:%M"}
60 {5, 0, TMT_MINUTE, 2, TMT_MINUTE, 10, TMT_MINUTE, 10, 0, "%H:%M"}
62 {10, 0, TMT_MINUTE, 5, TMT_MINUTE, 20, TMT_MINUTE, 20, 0, "%H:%M"}
64 {30, 0, TMT_MINUTE, 10, TMT_HOUR, 1, TMT_HOUR, 1, 0, "%H:%M"}
66 {60, 0, TMT_MINUTE, 30, TMT_HOUR, 2, TMT_HOUR, 2, 0, "%H:%M"}
68 {60, 24 * 3600, TMT_MINUTE, 30, TMT_HOUR, 2, TMT_HOUR, 6, 0, "%a %H:%M"}
70 {180, 0, TMT_HOUR, 1, TMT_HOUR, 6, TMT_HOUR, 6, 0, "%H:%M"}
72 {180, 24 * 3600, TMT_HOUR, 1, TMT_HOUR, 6, TMT_HOUR, 12, 0, "%a %H:%M"}
74 /*{300, 0, TMT_HOUR,3, TMT_HOUR,12, TMT_HOUR,12, 12*3600,"%a %p"}, this looks silly */
75 {600, 0, TMT_HOUR, 6, TMT_DAY, 1, TMT_DAY, 1, 24 * 3600, "%a"}
77 {1200, 0, TMT_HOUR, 6, TMT_DAY, 1, TMT_DAY, 1, 24 * 3600, "%d"}
79 {1800, 0, TMT_HOUR, 12, TMT_DAY, 1, TMT_DAY, 2, 24 * 3600, "%a %d"}
81 {2400, 0, TMT_HOUR, 12, TMT_DAY, 1, TMT_DAY, 2, 24 * 3600, "%a"}
83 {3600, 0, TMT_DAY, 1, TMT_WEEK, 1, TMT_WEEK, 1, 7 * 24 * 3600, "Week %V"}
85 {3 * 3600, 0, TMT_WEEK, 1, TMT_MONTH, 1, TMT_WEEK, 2, 7 * 24 * 3600,
88 {6 * 3600, 0, TMT_MONTH, 1, TMT_MONTH, 1, TMT_MONTH, 1, 30 * 24 * 3600,
91 {48 * 3600, 0, TMT_MONTH, 1, TMT_MONTH, 3, TMT_MONTH, 3, 30 * 24 * 3600,
94 {315360, 0, TMT_MONTH, 3, TMT_YEAR, 1, TMT_YEAR, 1, 365 * 24 * 3600, "%Y"}
96 {10 * 24 * 3600, 0, TMT_YEAR, 1, TMT_YEAR, 1, TMT_YEAR, 1,
97 365 * 24 * 3600, "%y"}
99 {-1, 0, TMT_MONTH, 0, TMT_MONTH, 0, TMT_MONTH, 0, 0, ""}
102 /* sensible y label intervals ...*/
126 {20.0, {1, 5, 10, 20}
132 {100.0, {1, 2, 5, 10}
135 {200.0, {1, 5, 10, 20}
138 {500.0, {1, 2, 4, 10}
146 gfx_color_t graph_col[] = /* default colors */
148 {1.00, 1.00, 1.00, 1.00}, /* canvas */
149 {0.95, 0.95, 0.95, 1.00}, /* background */
150 {0.81, 0.81, 0.81, 1.00}, /* shade A */
151 {0.62, 0.62, 0.62, 1.00}, /* shade B */
152 {0.56, 0.56, 0.56, 0.75}, /* grid */
153 {0.87, 0.31, 0.31, 0.60}, /* major grid */
154 {0.00, 0.00, 0.00, 1.00}, /* font */
155 {0.50, 0.12, 0.12, 1.00}, /* arrow */
156 {0.12, 0.12, 0.12, 1.00}, /* axis */
157 {0.00, 0.00, 0.00, 1.00} /* frame */
164 # define DPRINT(x) (void)(printf x, printf("\n"))
170 /* initialize with xtr(im,0); */
178 pixie = (double) im->xsize / (double) (im->end - im->start);
181 return (int) ((double) im->xorigin + pixie * (mytime - im->start));
184 /* translate data values into y coordinates */
193 if (!im->logarithmic)
194 pixie = (double) im->ysize / (im->maxval - im->minval);
197 (double) im->ysize / (log10(im->maxval) - log10(im->minval));
199 } else if (!im->logarithmic) {
200 yval = im->yorigin - pixie * (value - im->minval);
202 if (value < im->minval) {
205 yval = im->yorigin - pixie * (log10(value) - log10(im->minval));
213 /* conversion function for symbolic entry names */
216 #define conv_if(VV,VVV) \
217 if (strcmp(#VV, string) == 0) return VVV ;
223 conv_if(PRINT, GF_PRINT);
224 conv_if(GPRINT, GF_GPRINT);
225 conv_if(COMMENT, GF_COMMENT);
226 conv_if(HRULE, GF_HRULE);
227 conv_if(VRULE, GF_VRULE);
228 conv_if(LINE, GF_LINE);
229 conv_if(AREA, GF_AREA);
230 conv_if(STACK, GF_STACK);
231 conv_if(TICK, GF_TICK);
232 conv_if(TEXTALIGN, GF_TEXTALIGN);
233 conv_if(DEF, GF_DEF);
234 conv_if(CDEF, GF_CDEF);
235 conv_if(VDEF, GF_VDEF);
236 conv_if(XPORT, GF_XPORT);
237 conv_if(SHIFT, GF_SHIFT);
239 return (enum gf_en)(-1);
242 enum gfx_if_en if_conv(
246 conv_if(PNG, IF_PNG);
247 conv_if(SVG, IF_SVG);
248 conv_if(EPS, IF_EPS);
249 conv_if(PDF, IF_PDF);
251 return (enum gfx_if_en)(-1);
254 enum tmt_en tmt_conv(
258 conv_if(SECOND, TMT_SECOND);
259 conv_if(MINUTE, TMT_MINUTE);
260 conv_if(HOUR, TMT_HOUR);
261 conv_if(DAY, TMT_DAY);
262 conv_if(WEEK, TMT_WEEK);
263 conv_if(MONTH, TMT_MONTH);
264 conv_if(YEAR, TMT_YEAR);
265 return (enum tmt_en)(-1);
268 enum grc_en grc_conv(
272 conv_if(BACK, GRC_BACK);
273 conv_if(CANVAS, GRC_CANVAS);
274 conv_if(SHADEA, GRC_SHADEA);
275 conv_if(SHADEB, GRC_SHADEB);
276 conv_if(GRID, GRC_GRID);
277 conv_if(MGRID, GRC_MGRID);
278 conv_if(FONT, GRC_FONT);
279 conv_if(ARROW, GRC_ARROW);
280 conv_if(AXIS, GRC_AXIS);
281 conv_if(FRAME, GRC_FRAME);
283 return (enum grc_en)(-1);
286 enum text_prop_en text_prop_conv(
290 conv_if(DEFAULT, TEXT_PROP_DEFAULT);
291 conv_if(TITLE, TEXT_PROP_TITLE);
292 conv_if(AXIS, TEXT_PROP_AXIS);
293 conv_if(UNIT, TEXT_PROP_UNIT);
294 conv_if(LEGEND, TEXT_PROP_LEGEND);
295 conv_if(WATERMARK, TEXT_PROP_WATERMARK);
296 return (enum text_prop_en)(-1);
306 cairo_status_t status = (cairo_status_t) 0;
311 if (im->daemon_addr != NULL)
312 free(im->daemon_addr);
314 for (i = 0; i < (unsigned) im->gdes_c; i++) {
315 if (im->gdes[i].data_first) {
316 /* careful here, because a single pointer can occur several times */
317 free(im->gdes[i].data);
318 if (im->gdes[i].ds_namv) {
319 for (ii = 0; ii < im->gdes[i].ds_cnt; ii++)
320 free(im->gdes[i].ds_namv[ii]);
321 free(im->gdes[i].ds_namv);
324 /* free allocated memory used for dashed lines */
325 if (im->gdes[i].p_dashes != NULL)
326 free(im->gdes[i].p_dashes);
328 free(im->gdes[i].p_data);
329 free(im->gdes[i].rpnp);
332 if (im->font_options)
333 cairo_font_options_destroy(im->font_options);
336 status = cairo_status(im->cr);
337 cairo_destroy(im->cr);
339 if (im->rendered_image) {
340 free(im->rendered_image);
344 g_object_unref (im->layout);
348 cairo_surface_destroy(im->surface);
351 fprintf(stderr, "OOPS: Cairo has issues it can't even die: %s\n",
352 cairo_status_to_string(status));
357 /* find SI magnitude symbol for the given number*/
359 image_desc_t *im, /* image description */
365 char *symbol[] = { "a", /* 10e-18 Atto */
366 "f", /* 10e-15 Femto */
367 "p", /* 10e-12 Pico */
368 "n", /* 10e-9 Nano */
369 "u", /* 10e-6 Micro */
370 "m", /* 10e-3 Milli */
375 "T", /* 10e12 Tera */
376 "P", /* 10e15 Peta */
383 if (*value == 0.0 || isnan(*value)) {
387 sindex = floor(log(fabs(*value)) / log((double) im->base));
388 *magfact = pow((double) im->base, (double) sindex);
389 (*value) /= (*magfact);
391 if (sindex <= symbcenter && sindex >= -symbcenter) {
392 (*symb_ptr) = symbol[sindex + symbcenter];
399 static char si_symbol[] = {
400 'a', /* 10e-18 Atto */
401 'f', /* 10e-15 Femto */
402 'p', /* 10e-12 Pico */
403 'n', /* 10e-9 Nano */
404 'u', /* 10e-6 Micro */
405 'm', /* 10e-3 Milli */
410 'T', /* 10e12 Tera */
411 'P', /* 10e15 Peta */
414 static const int si_symbcenter = 6;
416 /* find SI magnitude symbol for the numbers on the y-axis*/
418 image_desc_t *im /* image description */
422 double digits, viewdigits = 0;
425 floor(log(max(fabs(im->minval), fabs(im->maxval))) /
426 log((double) im->base));
428 if (im->unitsexponent != 9999) {
429 /* unitsexponent = 9, 6, 3, 0, -3, -6, -9, etc */
430 viewdigits = floor((double)(im->unitsexponent / 3));
435 im->magfact = pow((double) im->base, digits);
438 printf("digits %6.3f im->magfact %6.3f\n", digits, im->magfact);
441 im->viewfactor = im->magfact / pow((double) im->base, viewdigits);
443 if (((viewdigits + si_symbcenter) < sizeof(si_symbol)) &&
444 ((viewdigits + si_symbcenter) >= 0))
445 im->symbol = si_symbol[(int) viewdigits + si_symbcenter];
450 /* move min and max values around to become sensible */
455 double sensiblevalues[] = { 1000.0, 900.0, 800.0, 750.0, 700.0,
456 600.0, 500.0, 400.0, 300.0, 250.0,
457 200.0, 125.0, 100.0, 90.0, 80.0,
458 75.0, 70.0, 60.0, 50.0, 40.0, 30.0,
459 25.0, 20.0, 10.0, 9.0, 8.0,
460 7.0, 6.0, 5.0, 4.0, 3.5, 3.0,
461 2.5, 2.0, 1.8, 1.5, 1.2, 1.0,
462 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0, -1
465 double scaled_min, scaled_max;
472 printf("Min: %6.2f Max: %6.2f MagFactor: %6.2f\n",
473 im->minval, im->maxval, im->magfact);
476 if (isnan(im->ygridstep)) {
477 if (im->extra_flags & ALTAUTOSCALE) {
478 /* measure the amplitude of the function. Make sure that
479 graph boundaries are slightly higher then max/min vals
480 so we can see amplitude on the graph */
483 delt = im->maxval - im->minval;
485 fact = 2.0 * pow(10.0,
487 (max(fabs(im->minval), fabs(im->maxval)) /
490 adj = (fact - delt) * 0.55;
493 ("Min: %6.2f Max: %6.2f delt: %6.2f fact: %6.2f adj: %6.2f\n",
494 im->minval, im->maxval, delt, fact, adj);
499 } else if (im->extra_flags & ALTAUTOSCALE_MIN) {
500 /* measure the amplitude of the function. Make sure that
501 graph boundaries are slightly lower than min vals
502 so we can see amplitude on the graph */
503 adj = (im->maxval - im->minval) * 0.1;
505 } else if (im->extra_flags & ALTAUTOSCALE_MAX) {
506 /* measure the amplitude of the function. Make sure that
507 graph boundaries are slightly higher than max vals
508 so we can see amplitude on the graph */
509 adj = (im->maxval - im->minval) * 0.1;
512 scaled_min = im->minval / im->magfact;
513 scaled_max = im->maxval / im->magfact;
515 for (i = 1; sensiblevalues[i] > 0; i++) {
516 if (sensiblevalues[i - 1] >= scaled_min &&
517 sensiblevalues[i] <= scaled_min)
518 im->minval = sensiblevalues[i] * (im->magfact);
520 if (-sensiblevalues[i - 1] <= scaled_min &&
521 -sensiblevalues[i] >= scaled_min)
522 im->minval = -sensiblevalues[i - 1] * (im->magfact);
524 if (sensiblevalues[i - 1] >= scaled_max &&
525 sensiblevalues[i] <= scaled_max)
526 im->maxval = sensiblevalues[i - 1] * (im->magfact);
528 if (-sensiblevalues[i - 1] <= scaled_max &&
529 -sensiblevalues[i] >= scaled_max)
530 im->maxval = -sensiblevalues[i] * (im->magfact);
534 /* adjust min and max to the grid definition if there is one */
535 im->minval = (double) im->ylabfact * im->ygridstep *
536 floor(im->minval / ((double) im->ylabfact * im->ygridstep));
537 im->maxval = (double) im->ylabfact * im->ygridstep *
538 ceil(im->maxval / ((double) im->ylabfact * im->ygridstep));
542 fprintf(stderr, "SCALED Min: %6.2f Max: %6.2f Factor: %6.2f\n",
543 im->minval, im->maxval, im->magfact);
551 if (isnan(im->minval) || isnan(im->maxval))
554 if (im->logarithmic) {
555 double ya, yb, ypix, ypixfrac;
556 double log10_range = log10(im->maxval) - log10(im->minval);
558 ya = pow((double) 10, floor(log10(im->minval)));
559 while (ya < im->minval)
562 return; /* don't have y=10^x gridline */
564 if (yb <= im->maxval) {
565 /* we have at least 2 y=10^x gridlines.
566 Make sure distance between them in pixels
567 are an integer by expanding im->maxval */
568 double y_pixel_delta = ytr(im, ya) - ytr(im, yb);
569 double factor = y_pixel_delta / floor(y_pixel_delta);
570 double new_log10_range = factor * log10_range;
571 double new_ymax_log10 = log10(im->minval) + new_log10_range;
573 im->maxval = pow(10, new_ymax_log10);
574 ytr(im, DNAN); /* reset precalc */
575 log10_range = log10(im->maxval) - log10(im->minval);
577 /* make sure first y=10^x gridline is located on
578 integer pixel position by moving scale slightly
579 downwards (sub-pixel movement) */
580 ypix = ytr(im, ya) + im->ysize; /* add im->ysize so it always is positive */
581 ypixfrac = ypix - floor(ypix);
582 if (ypixfrac > 0 && ypixfrac < 1) {
583 double yfrac = ypixfrac / im->ysize;
585 im->minval = pow(10, log10(im->minval) - yfrac * log10_range);
586 im->maxval = pow(10, log10(im->maxval) - yfrac * log10_range);
587 ytr(im, DNAN); /* reset precalc */
590 /* Make sure we have an integer pixel distance between
591 each minor gridline */
592 double ypos1 = ytr(im, im->minval);
593 double ypos2 = ytr(im, im->minval + im->ygrid_scale.gridstep);
594 double y_pixel_delta = ypos1 - ypos2;
595 double factor = y_pixel_delta / floor(y_pixel_delta);
596 double new_range = factor * (im->maxval - im->minval);
597 double gridstep = im->ygrid_scale.gridstep;
598 double minor_y, minor_y_px, minor_y_px_frac;
600 if (im->maxval > 0.0)
601 im->maxval = im->minval + new_range;
603 im->minval = im->maxval - new_range;
604 ytr(im, DNAN); /* reset precalc */
605 /* make sure first minor gridline is on integer pixel y coord */
606 minor_y = gridstep * floor(im->minval / gridstep);
607 while (minor_y < im->minval)
609 minor_y_px = ytr(im, minor_y) + im->ysize; /* ensure > 0 by adding ysize */
610 minor_y_px_frac = minor_y_px - floor(minor_y_px);
611 if (minor_y_px_frac > 0 && minor_y_px_frac < 1) {
612 double yfrac = minor_y_px_frac / im->ysize;
613 double range = im->maxval - im->minval;
615 im->minval = im->minval - yfrac * range;
616 im->maxval = im->maxval - yfrac * range;
617 ytr(im, DNAN); /* reset precalc */
619 calc_horizontal_grid(im); /* recalc with changed im->maxval */
623 /* reduce data reimplementation by Alex */
626 enum cf_en cf, /* which consolidation function ? */
627 unsigned long cur_step, /* step the data currently is in */
628 time_t *start, /* start, end and step as requested ... */
629 time_t *end, /* ... by the application will be ... */
630 unsigned long *step, /* ... adjusted to represent reality */
631 unsigned long *ds_cnt, /* number of data sources in file */
633 { /* two dimensional array containing the data */
634 int i, reduce_factor = ceil((double) (*step) / (double) cur_step);
635 unsigned long col, dst_row, row_cnt, start_offset, end_offset, skiprows =
637 rrd_value_t *srcptr, *dstptr;
639 (*step) = cur_step * reduce_factor; /* set new step size for reduced data */
642 row_cnt = ((*end) - (*start)) / cur_step;
648 printf("Reducing %lu rows with factor %i time %lu to %lu, step %lu\n",
649 row_cnt, reduce_factor, *start, *end, cur_step);
650 for (col = 0; col < row_cnt; col++) {
651 printf("time %10lu: ", *start + (col + 1) * cur_step);
652 for (i = 0; i < *ds_cnt; i++)
653 printf(" %8.2e", srcptr[*ds_cnt * col + i]);
658 /* We have to combine [reduce_factor] rows of the source
659 ** into one row for the destination. Doing this we also
660 ** need to take care to combine the correct rows. First
661 ** alter the start and end time so that they are multiples
662 ** of the new step time. We cannot reduce the amount of
663 ** time so we have to move the end towards the future and
664 ** the start towards the past.
666 end_offset = (*end) % (*step);
667 start_offset = (*start) % (*step);
669 /* If there is a start offset (which cannot be more than
670 ** one destination row), skip the appropriate number of
671 ** source rows and one destination row. The appropriate
672 ** number is what we do know (start_offset/cur_step) of
673 ** the new interval (*step/cur_step aka reduce_factor).
676 printf("start_offset: %lu end_offset: %lu\n", start_offset, end_offset);
677 printf("row_cnt before: %lu\n", row_cnt);
680 (*start) = (*start) - start_offset;
681 skiprows = reduce_factor - start_offset / cur_step;
682 srcptr += skiprows * *ds_cnt;
683 for (col = 0; col < (*ds_cnt); col++)
688 printf("row_cnt between: %lu\n", row_cnt);
691 /* At the end we have some rows that are not going to be
692 ** used, the amount is end_offset/cur_step
695 (*end) = (*end) - end_offset + (*step);
696 skiprows = end_offset / cur_step;
700 printf("row_cnt after: %lu\n", row_cnt);
703 /* Sanity check: row_cnt should be multiple of reduce_factor */
704 /* if this gets triggered, something is REALLY WRONG ... we die immediately */
706 if (row_cnt % reduce_factor) {
707 printf("SANITY CHECK: %lu rows cannot be reduced by %i \n",
708 row_cnt, reduce_factor);
709 printf("BUG in reduce_data()\n");
713 /* Now combine reduce_factor intervals at a time
714 ** into one interval for the destination.
717 for (dst_row = 0; (long int) row_cnt >= reduce_factor; dst_row++) {
718 for (col = 0; col < (*ds_cnt); col++) {
719 rrd_value_t newval = DNAN;
720 unsigned long validval = 0;
722 for (i = 0; i < reduce_factor; i++) {
723 if (isnan(srcptr[i * (*ds_cnt) + col])) {
728 newval = srcptr[i * (*ds_cnt) + col];
737 newval += srcptr[i * (*ds_cnt) + col];
740 newval = min(newval, srcptr[i * (*ds_cnt) + col]);
743 /* an interval contains a failure if any subintervals contained a failure */
745 newval = max(newval, srcptr[i * (*ds_cnt) + col]);
748 newval = srcptr[i * (*ds_cnt) + col];
774 srcptr += (*ds_cnt) * reduce_factor;
775 row_cnt -= reduce_factor;
777 /* If we had to alter the endtime, we didn't have enough
778 ** source rows to fill the last row. Fill it with NaN.
781 for (col = 0; col < (*ds_cnt); col++)
784 row_cnt = ((*end) - (*start)) / *step;
786 printf("Done reducing. Currently %lu rows, time %lu to %lu, step %lu\n",
787 row_cnt, *start, *end, *step);
788 for (col = 0; col < row_cnt; col++) {
789 printf("time %10lu: ", *start + (col + 1) * (*step));
790 for (i = 0; i < *ds_cnt; i++)
791 printf(" %8.2e", srcptr[*ds_cnt * col + i]);
798 /* get the data required for the graphs from the
807 /* pull the data from the rrd files ... */
808 for (i = 0; i < (int) im->gdes_c; i++) {
809 /* only GF_DEF elements fetch data */
810 if (im->gdes[i].gf != GF_DEF)
814 /* do we have it already ? */
815 for (ii = 0; ii < i; ii++) {
816 if (im->gdes[ii].gf != GF_DEF)
818 if ((strcmp(im->gdes[i].rrd, im->gdes[ii].rrd) == 0)
819 && (im->gdes[i].cf == im->gdes[ii].cf)
820 && (im->gdes[i].cf_reduce == im->gdes[ii].cf_reduce)
821 && (im->gdes[i].start_orig == im->gdes[ii].start_orig)
822 && (im->gdes[i].end_orig == im->gdes[ii].end_orig)
823 && (im->gdes[i].step_orig == im->gdes[ii].step_orig)) {
824 /* OK, the data is already there.
825 ** Just copy the header portion
827 im->gdes[i].start = im->gdes[ii].start;
828 im->gdes[i].end = im->gdes[ii].end;
829 im->gdes[i].step = im->gdes[ii].step;
830 im->gdes[i].ds_cnt = im->gdes[ii].ds_cnt;
831 im->gdes[i].ds_namv = im->gdes[ii].ds_namv;
832 im->gdes[i].data = im->gdes[ii].data;
833 im->gdes[i].data_first = 0;
840 unsigned long ft_step = im->gdes[i].step; /* ft_step will record what we got from fetch */
843 * - a connection to the daemon has been established
844 * - this is the first occurrence of that RRD file
846 if (rrdc_is_connected(im->daemon_addr))
851 for (ii = 0; ii < i; ii++)
853 if (strcmp (im->gdes[i].rrd, im->gdes[ii].rrd) == 0)
862 status = rrdc_flush (im->gdes[i].rrd);
865 rrd_set_error ("rrdc_flush (%s) failed with status %i.",
866 im->gdes[i].rrd, status);
870 } /* if (rrdc_is_connected()) */
872 if ((rrd_fetch_fn(im->gdes[i].rrd,
878 &im->gdes[i].ds_namv,
879 &im->gdes[i].data)) == -1) {
882 im->gdes[i].data_first = 1;
884 if (ft_step < im->gdes[i].step) {
885 reduce_data(im->gdes[i].cf_reduce,
890 &im->gdes[i].ds_cnt, &im->gdes[i].data);
892 im->gdes[i].step = ft_step;
896 /* lets see if the required data source is really there */
897 for (ii = 0; ii < (int) im->gdes[i].ds_cnt; ii++) {
898 if (strcmp(im->gdes[i].ds_namv[ii], im->gdes[i].ds_nam) == 0) {
902 if (im->gdes[i].ds == -1) {
903 rrd_set_error("No DS called '%s' in '%s'",
904 im->gdes[i].ds_nam, im->gdes[i].rrd);
912 /* evaluate the expressions in the CDEF functions */
914 /*************************************************************
916 *************************************************************/
918 long find_var_wrapper(
922 return find_var((image_desc_t *) arg1, key);
925 /* find gdes containing var*/
932 for (ii = 0; ii < im->gdes_c - 1; ii++) {
933 if ((im->gdes[ii].gf == GF_DEF
934 || im->gdes[ii].gf == GF_VDEF || im->gdes[ii].gf == GF_CDEF)
935 && (strcmp(im->gdes[ii].vname, key) == 0)) {
942 /* find the greatest common divisor for all the numbers
943 in the 0 terminated num array */
950 for (i = 0; num[i + 1] != 0; i++) {
952 rest = num[i] % num[i + 1];
958 /* return i==0?num[i]:num[i-1]; */
962 /* run the rpn calculator on all the VDEF and CDEF arguments */
969 long *steparray, rpi;
974 rpnstack_init(&rpnstack);
976 for (gdi = 0; gdi < im->gdes_c; gdi++) {
977 /* Look for GF_VDEF and GF_CDEF in the same loop,
978 * so CDEFs can use VDEFs and vice versa
980 switch (im->gdes[gdi].gf) {
984 graph_desc_t *vdp = &im->gdes[im->gdes[gdi].vidx];
986 /* remove current shift */
987 vdp->start -= vdp->shift;
988 vdp->end -= vdp->shift;
991 if (im->gdes[gdi].shidx >= 0)
992 vdp->shift = im->gdes[im->gdes[gdi].shidx].vf.val;
995 vdp->shift = im->gdes[gdi].shval;
997 /* normalize shift to multiple of consolidated step */
998 vdp->shift = (vdp->shift / (long) vdp->step) * (long) vdp->step;
1001 vdp->start += vdp->shift;
1002 vdp->end += vdp->shift;
1006 /* A VDEF has no DS. This also signals other parts
1007 * of rrdtool that this is a VDEF value, not a CDEF.
1009 im->gdes[gdi].ds_cnt = 0;
1010 if (vdef_calc(im, gdi)) {
1011 rrd_set_error("Error processing VDEF '%s'",
1012 im->gdes[gdi].vname);
1013 rpnstack_free(&rpnstack);
1018 im->gdes[gdi].ds_cnt = 1;
1019 im->gdes[gdi].ds = 0;
1020 im->gdes[gdi].data_first = 1;
1021 im->gdes[gdi].start = 0;
1022 im->gdes[gdi].end = 0;
1027 /* Find the variables in the expression.
1028 * - VDEF variables are substituted by their values
1029 * and the opcode is changed into OP_NUMBER.
1030 * - CDEF variables are analized for their step size,
1031 * the lowest common denominator of all the step
1032 * sizes of the data sources involved is calculated
1033 * and the resulting number is the step size for the
1034 * resulting data source.
1036 for (rpi = 0; im->gdes[gdi].rpnp[rpi].op != OP_END; rpi++) {
1037 if (im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE ||
1038 im->gdes[gdi].rpnp[rpi].op == OP_PREV_OTHER) {
1039 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
1041 if (im->gdes[ptr].ds_cnt == 0) { /* this is a VDEF data source */
1044 ("DEBUG: inside CDEF '%s' processing VDEF '%s'\n",
1045 im->gdes[gdi].vname, im->gdes[ptr].vname);
1046 printf("DEBUG: value from vdef is %f\n",
1047 im->gdes[ptr].vf.val);
1049 im->gdes[gdi].rpnp[rpi].val = im->gdes[ptr].vf.val;
1050 im->gdes[gdi].rpnp[rpi].op = OP_NUMBER;
1051 } else { /* normal variables and PREF(variables) */
1053 /* add one entry to the array that keeps track of the step sizes of the
1054 * data sources going into the CDEF. */
1056 (long*)rrd_realloc(steparray,
1058 1) * sizeof(*steparray))) == NULL) {
1059 rrd_set_error("realloc steparray");
1060 rpnstack_free(&rpnstack);
1064 steparray[stepcnt - 1] = im->gdes[ptr].step;
1066 /* adjust start and end of cdef (gdi) so
1067 * that it runs from the latest start point
1068 * to the earliest endpoint of any of the
1069 * rras involved (ptr)
1072 if (im->gdes[gdi].start < im->gdes[ptr].start)
1073 im->gdes[gdi].start = im->gdes[ptr].start;
1075 if (im->gdes[gdi].end == 0 ||
1076 im->gdes[gdi].end > im->gdes[ptr].end)
1077 im->gdes[gdi].end = im->gdes[ptr].end;
1079 /* store pointer to the first element of
1080 * the rra providing data for variable,
1081 * further save step size and data source
1084 im->gdes[gdi].rpnp[rpi].data =
1085 im->gdes[ptr].data + im->gdes[ptr].ds;
1086 im->gdes[gdi].rpnp[rpi].step = im->gdes[ptr].step;
1087 im->gdes[gdi].rpnp[rpi].ds_cnt = im->gdes[ptr].ds_cnt;
1089 /* backoff the *.data ptr; this is done so
1090 * rpncalc() function doesn't have to treat
1091 * the first case differently
1093 } /* if ds_cnt != 0 */
1094 } /* if OP_VARIABLE */
1095 } /* loop through all rpi */
1097 /* move the data pointers to the correct period */
1098 for (rpi = 0; im->gdes[gdi].rpnp[rpi].op != OP_END; rpi++) {
1099 if (im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE ||
1100 im->gdes[gdi].rpnp[rpi].op == OP_PREV_OTHER) {
1101 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
1103 im->gdes[gdi].start - im->gdes[ptr].start;
1106 im->gdes[gdi].rpnp[rpi].data +=
1107 (diff / im->gdes[ptr].step) *
1108 im->gdes[ptr].ds_cnt;
1112 if (steparray == NULL) {
1113 rrd_set_error("rpn expressions without DEF"
1114 " or CDEF variables are not supported");
1115 rpnstack_free(&rpnstack);
1118 steparray[stepcnt] = 0;
1119 /* Now find the resulting step. All steps in all
1120 * used RRAs have to be visited
1122 im->gdes[gdi].step = lcd(steparray);
1124 if ((im->gdes[gdi].data = (rrd_value_t*)malloc(((im->gdes[gdi].end -
1125 im->gdes[gdi].start)
1126 / im->gdes[gdi].step)
1127 * sizeof(double))) == NULL) {
1128 rrd_set_error("malloc im->gdes[gdi].data");
1129 rpnstack_free(&rpnstack);
1133 /* Step through the new cdef results array and
1134 * calculate the values
1136 for (now = im->gdes[gdi].start + im->gdes[gdi].step;
1137 now <= im->gdes[gdi].end; now += im->gdes[gdi].step) {
1138 rpnp_t *rpnp = im->gdes[gdi].rpnp;
1140 /* 3rd arg of rpn_calc is for OP_VARIABLE lookups;
1141 * in this case we are advancing by timesteps;
1142 * we use the fact that time_t is a synonym for long
1144 if (rpn_calc(rpnp, &rpnstack, (long) now,
1145 im->gdes[gdi].data, ++dataidx) == -1) {
1146 /* rpn_calc sets the error string */
1147 rpnstack_free(&rpnstack);
1150 } /* enumerate over time steps within a CDEF */
1155 } /* enumerate over CDEFs */
1156 rpnstack_free(&rpnstack);
1160 /* from http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm */
1161 /* yes we are loosing precision by doing tos with floats instead of doubles
1162 but it seems more stable this way. */
1164 static int AlmostEqual2sComplement(
1170 int aInt = *(int *) &A;
1171 int bInt = *(int *) &B;
1174 /* Make sure maxUlps is non-negative and small enough that the
1175 default NAN won't compare as equal to anything. */
1177 /* assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024); */
1179 /* Make aInt lexicographically ordered as a twos-complement int */
1182 aInt = 0x80000000l - aInt;
1184 /* Make bInt lexicographically ordered as a twos-complement int */
1187 bInt = 0x80000000l - bInt;
1189 intDiff = abs(aInt - bInt);
1191 if (intDiff <= maxUlps)
1197 /* massage data so, that we get one value for each x coordinate in the graph */
1202 double pixstep = (double) (im->end - im->start)
1203 / (double) im->xsize; /* how much time
1204 passes in one pixel */
1206 double minval = DNAN, maxval = DNAN;
1208 unsigned long gr_time;
1210 /* memory for the processed data */
1211 for (i = 0; i < im->gdes_c; i++) {
1212 if ((im->gdes[i].gf == GF_LINE) ||
1213 (im->gdes[i].gf == GF_AREA) || (im->gdes[i].gf == GF_TICK)) {
1214 if ((im->gdes[i].p_data = (rrd_value_t*)malloc((im->xsize + 1)
1215 * sizeof(rrd_value_t))) == NULL) {
1216 rrd_set_error("malloc data_proc");
1222 for (i = 0; i < im->xsize; i++) { /* for each pixel */
1225 gr_time = im->start + pixstep * i; /* time of the current step */
1228 for (ii = 0; ii < im->gdes_c; ii++) {
1231 switch (im->gdes[ii].gf) {
1235 if (!im->gdes[ii].stack)
1237 value = im->gdes[ii].yrule;
1238 if (isnan(value) || (im->gdes[ii].gf == GF_TICK)) {
1239 /* The time of the data doesn't necessarily match
1240 ** the time of the graph. Beware.
1242 vidx = im->gdes[ii].vidx;
1243 if (im->gdes[vidx].gf == GF_VDEF) {
1244 value = im->gdes[vidx].vf.val;
1246 if (((long int) gr_time >=
1247 (long int) im->gdes[vidx].start)
1248 && ((long int) gr_time <=
1249 (long int) im->gdes[vidx].end)) {
1250 value = im->gdes[vidx].data[(unsigned long)
1256 im->gdes[vidx].step)
1257 * im->gdes[vidx].ds_cnt +
1264 if (!isnan(value)) {
1266 im->gdes[ii].p_data[i] = paintval;
1267 /* GF_TICK: the data values are not
1268 ** relevant for min and max
1270 if (finite(paintval) && im->gdes[ii].gf != GF_TICK) {
1271 if ((isnan(minval) || paintval < minval) &&
1272 !(im->logarithmic && paintval <= 0.0))
1274 if (isnan(maxval) || paintval > maxval)
1278 im->gdes[ii].p_data[i] = DNAN;
1283 ("STACK should already be turned into LINE or AREA here");
1292 /* if min or max have not been asigned a value this is because
1293 there was no data in the graph ... this is not good ...
1294 lets set these to dummy values then ... */
1296 if (im->logarithmic) {
1297 if (isnan(minval) || isnan(maxval) || maxval <= 0) {
1298 minval = 0.0; /* catching this right away below */
1301 /* in logarithm mode, where minval is smaller or equal
1302 to 0 make the beast just way smaller than maxval */
1304 minval = maxval / 10e8;
1307 if (isnan(minval) || isnan(maxval)) {
1313 /* adjust min and max values given by the user */
1314 /* for logscale we add something on top */
1315 if (isnan(im->minval)
1316 || ((!im->rigid) && im->minval > minval)
1318 if (im->logarithmic)
1319 im->minval = minval / 2.0;
1321 im->minval = minval;
1323 if (isnan(im->maxval)
1324 || (!im->rigid && im->maxval < maxval)
1326 if (im->logarithmic)
1327 im->maxval = maxval * 2.0;
1329 im->maxval = maxval;
1332 /* make sure min is smaller than max */
1333 if (im->minval > im->maxval) {
1335 im->minval = 0.99 * im->maxval;
1337 im->minval = 1.01 * im->maxval;
1340 /* make sure min and max are not equal */
1341 if (AlmostEqual2sComplement(im->minval, im->maxval, 4)) {
1347 /* make sure min and max are not both zero */
1348 if (AlmostEqual2sComplement(im->maxval, 0, 4)) {
1357 /* identify the point where the first gridline, label ... gets placed */
1359 time_t find_first_time(
1360 time_t start, /* what is the initial time */
1361 enum tmt_en baseint, /* what is the basic interval */
1362 long basestep /* how many if these do we jump a time */
1367 localtime_r(&start, &tm);
1371 tm. tm_sec -= tm.tm_sec % basestep;
1376 tm. tm_min -= tm.tm_min % basestep;
1382 tm. tm_hour -= tm.tm_hour % basestep;
1386 /* we do NOT look at the basestep for this ... */
1393 /* we do NOT look at the basestep for this ... */
1397 tm. tm_mday -= tm.tm_wday - 1; /* -1 because we want the monday */
1399 if (tm.tm_wday == 0)
1400 tm. tm_mday -= 7; /* we want the *previous* monday */
1408 tm. tm_mon -= tm.tm_mon % basestep;
1419 tm.tm_year + 1900) %basestep;
1425 /* identify the point where the next gridline, label ... gets placed */
1426 time_t find_next_time(
1427 time_t current, /* what is the initial time */
1428 enum tmt_en baseint, /* what is the basic interval */
1429 long basestep /* how many if these do we jump a time */
1435 localtime_r(¤t, &tm);
1440 tm. tm_sec += basestep;
1444 tm. tm_min += basestep;
1448 tm. tm_hour += basestep;
1452 tm. tm_mday += basestep;
1456 tm. tm_mday += 7 * basestep;
1460 tm. tm_mon += basestep;
1464 tm. tm_year += basestep;
1466 madetime = mktime(&tm);
1467 } while (madetime == -1); /* this is necessary to skip impssible times
1468 like the daylight saving time skips */
1474 /* calculate values required for PRINT and GPRINT functions */
1479 long i, ii, validsteps;
1482 int graphelement = 0;
1485 double magfact = -1;
1490 /* wow initializing tmvdef is quite a task :-) */
1491 time_t now = time(NULL);
1493 localtime_r(&now, &tmvdef);
1494 for (i = 0; i < im->gdes_c; i++) {
1495 vidx = im->gdes[i].vidx;
1496 switch (im->gdes[i].gf) {
1499 /* PRINT and GPRINT can now print VDEF generated values.
1500 * There's no need to do any calculations on them as these
1501 * calculations were already made.
1503 if (im->gdes[vidx].gf == GF_VDEF) { /* simply use vals */
1504 printval = im->gdes[vidx].vf.val;
1505 localtime_r(&im->gdes[vidx].vf.when, &tmvdef);
1506 } else { /* need to calculate max,min,avg etcetera */
1507 max_ii = ((im->gdes[vidx].end - im->gdes[vidx].start)
1508 / im->gdes[vidx].step * im->gdes[vidx].ds_cnt);
1511 for (ii = im->gdes[vidx].ds;
1512 ii < max_ii; ii += im->gdes[vidx].ds_cnt) {
1513 if (!finite(im->gdes[vidx].data[ii]))
1515 if (isnan(printval)) {
1516 printval = im->gdes[vidx].data[ii];
1521 switch (im->gdes[i].cf) {
1525 case CF_DEVSEASONAL:
1529 printval += im->gdes[vidx].data[ii];
1532 printval = min(printval, im->gdes[vidx].data[ii]);
1536 printval = max(printval, im->gdes[vidx].data[ii]);
1539 printval = im->gdes[vidx].data[ii];
1542 if (im->gdes[i].cf == CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
1543 if (validsteps > 1) {
1544 printval = (printval / validsteps);
1547 } /* prepare printval */
1549 if ((percent_s = strstr(im->gdes[i].format, "%S")) != NULL) {
1550 /* Magfact is set to -1 upon entry to print_calc. If it
1551 * is still less than 0, then we need to run auto_scale.
1552 * Otherwise, put the value into the correct units. If
1553 * the value is 0, then do not set the symbol or magnification
1554 * so next the calculation will be performed again. */
1555 if (magfact < 0.0) {
1556 auto_scale(im, &printval, &si_symb, &magfact);
1557 if (printval == 0.0)
1560 printval /= magfact;
1562 *(++percent_s) = 's';
1563 } else if (strstr(im->gdes[i].format, "%s") != NULL) {
1564 auto_scale(im, &printval, &si_symb, &magfact);
1567 if (im->gdes[i].gf == GF_PRINT) {
1568 rrd_infoval_t prline;
1570 if (im->gdes[i].strftm) {
1571 prline.u_str = (char*)malloc((FMT_LEG_LEN + 2) * sizeof(char));
1572 strftime(prline.u_str,
1573 FMT_LEG_LEN, im->gdes[i].format, &tmvdef);
1574 } else if (bad_format(im->gdes[i].format)) {
1576 ("bad format for PRINT in '%s'", im->gdes[i].format);
1580 sprintf_alloc(im->gdes[i].format, printval, si_symb);
1584 ("print[%ld]", prline_cnt++), RD_I_STR, prline);
1589 if (im->gdes[i].strftm) {
1590 strftime(im->gdes[i].legend,
1591 FMT_LEG_LEN, im->gdes[i].format, &tmvdef);
1593 if (bad_format(im->gdes[i].format)) {
1595 ("bad format for GPRINT in '%s'",
1596 im->gdes[i].format);
1599 #ifdef HAVE_SNPRINTF
1600 snprintf(im->gdes[i].legend,
1602 im->gdes[i].format, printval, si_symb);
1604 sprintf(im->gdes[i].legend,
1605 im->gdes[i].format, printval, si_symb);
1617 if (isnan(im->gdes[i].yrule)) { /* we must set this here or the legend printer can not decide to print the legend */
1618 im->gdes[i].yrule = im->gdes[vidx].vf.val;
1623 if (im->gdes[i].xrule == 0) { /* again ... the legend printer needs it */
1624 im->gdes[i].xrule = im->gdes[vidx].vf.when;
1633 #ifdef WITH_PIECHART
1641 ("STACK should already be turned into LINE or AREA here");
1646 return graphelement;
1650 /* place legends with color spots */
1656 int interleg = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
1657 int border = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
1658 int fill = 0, fill_last;
1660 double leg_x = border;
1661 int leg_y = im->yimg;
1662 int leg_y_prev = im->yimg;
1665 int i, ii, mark = 0;
1666 char default_txtalign = TXA_JUSTIFIED; /*default line orientation */
1670 if (!(im->extra_flags & NOLEGEND) & !(im->extra_flags & ONLY_GRAPH)) {
1671 if ((legspace = (int*)malloc(im->gdes_c * sizeof(int))) == NULL) {
1672 rrd_set_error("malloc for legspace");
1676 for (i = 0; i < im->gdes_c; i++) {
1677 char prt_fctn; /*special printfunctions */
1679 /* hide legends for rules which are not displayed */
1680 if (im->gdes[i].gf == GF_TEXTALIGN) {
1681 default_txtalign = im->gdes[i].txtalign;
1684 if (!(im->extra_flags & FORCE_RULES_LEGEND)) {
1685 if (im->gdes[i].gf == GF_HRULE
1686 && (im->gdes[i].yrule <
1687 im->minval || im->gdes[i].yrule > im->maxval))
1688 im->gdes[i].legend[0] = '\0';
1689 if (im->gdes[i].gf == GF_VRULE
1690 && (im->gdes[i].xrule <
1691 im->start || im->gdes[i].xrule > im->end))
1692 im->gdes[i].legend[0] = '\0';
1695 /* turn \\t into tab */
1696 while ((tab = strstr(im->gdes[i].legend, "\\t"))) {
1697 memmove(tab, tab + 1, strlen(tab));
1700 leg_cc = strlen(im->gdes[i].legend);
1701 /* is there a controle code at the end of the legend string ? */
1702 if (leg_cc >= 2 && im->gdes[i].legend[leg_cc - 2] == '\\') {
1703 prt_fctn = im->gdes[i].legend[leg_cc - 1];
1705 im->gdes[i].legend[leg_cc] = '\0';
1709 /* only valid control codes */
1710 if (prt_fctn != 'l' && prt_fctn != 'n' && /* a synonym for l */
1714 prt_fctn != 's' && prt_fctn != '\0' && prt_fctn != 'g') {
1717 ("Unknown control code at the end of '%s\\%c'",
1718 im->gdes[i].legend, prt_fctn);
1722 if (prt_fctn == 'n') {
1726 /* remove exess space from the end of the legend for \g */
1727 while (prt_fctn == 'g' &&
1728 leg_cc > 0 && im->gdes[i].legend[leg_cc - 1] == ' ') {
1730 im->gdes[i].legend[leg_cc] = '\0';
1735 /* no interleg space if string ends in \g */
1736 legspace[i] = (prt_fctn == 'g' ? 0 : interleg);
1738 fill += legspace[i];
1741 gfx_get_text_width(im,
1747 im->tabwidth, im->gdes[i].legend);
1752 /* who said there was a special tag ... ? */
1753 if (prt_fctn == 'g') {
1757 if (prt_fctn == '\0') {
1758 if (i == im->gdes_c - 1 || fill > im->ximg - 2 * border) {
1759 /* just one legend item is left right or center */
1760 switch (default_txtalign) {
1775 /* is it time to place the legends ? */
1776 if (fill > im->ximg - 2 * border) {
1784 if (leg_c == 1 && prt_fctn == 'j') {
1790 if (prt_fctn != '\0') {
1792 if (leg_c >= 2 && prt_fctn == 'j') {
1793 glue = (double)(im->ximg - fill - 2 * border) / (double)(leg_c - 1);
1797 if (prt_fctn == 'c')
1798 leg_x = (double)(im->ximg - fill) / 2.0;
1799 if (prt_fctn == 'r')
1800 leg_x = im->ximg - fill - border;
1801 for (ii = mark; ii <= i; ii++) {
1802 if (im->gdes[ii].legend[0] == '\0')
1803 continue; /* skip empty legends */
1804 im->gdes[ii].leg_x = leg_x;
1805 im->gdes[ii].leg_y = leg_y;
1807 (double)gfx_get_text_width(im, leg_x,
1812 im->tabwidth, im->gdes[ii].legend)
1813 +(double)legspace[ii]
1817 if (leg_x > border || prt_fctn == 's')
1818 leg_y += im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
1819 if (prt_fctn == 's')
1820 leg_y -= im->text_prop[TEXT_PROP_LEGEND].size;
1827 if (im->extra_flags & FULL_SIZE_MODE) {
1828 /* now for some backpaddeling. We have to shift up all the
1829 legend items into the graph and tell the caller about the
1830 space we used up. */
1831 long shift_up = leg_y - im->yimg - im->text_prop[TEXT_PROP_LEGEND].size * 1.8 + border * 0.7;
1832 for (i = 0; i < im->gdes_c; i++) {
1833 im->gdes[i].leg_y -= shift_up;
1835 im->yorigin = im->yorigin - leg_y + im->yimg - im->text_prop[TEXT_PROP_LEGEND].size * 1.8 - border;
1839 leg_y - im->text_prop[TEXT_PROP_LEGEND].size * 1.8 +
1847 /* create a grid on the graph. it determines what to do
1848 from the values of xsize, start and end */
1850 /* the xaxis labels are determined from the number of seconds per pixel
1851 in the requested graph */
1853 int calc_horizontal_grid(
1861 int decimals, fractionals;
1863 im->ygrid_scale.labfact = 2;
1864 range = im->maxval - im->minval;
1865 scaledrange = range / im->magfact;
1866 /* does the scale of this graph make it impossible to put lines
1867 on it? If so, give up. */
1868 if (isnan(scaledrange)) {
1872 /* find grid spaceing */
1874 if (isnan(im->ygridstep)) {
1875 if (im->extra_flags & ALTYGRID) {
1876 /* find the value with max number of digits. Get number of digits */
1879 (max(fabs(im->maxval), fabs(im->minval)) *
1880 im->viewfactor / im->magfact));
1881 if (decimals <= 0) /* everything is small. make place for zero */
1883 im->ygrid_scale.gridstep =
1885 floor(log10(range * im->viewfactor / im->magfact))) /
1886 im->viewfactor * im->magfact;
1887 if (im->ygrid_scale.gridstep == 0) /* range is one -> 0.1 is reasonable scale */
1888 im->ygrid_scale.gridstep = 0.1;
1889 /* should have at least 5 lines but no more then 15 */
1890 if (range / im->ygrid_scale.gridstep < 5
1891 && im->ygrid_scale.gridstep >= 30)
1892 im->ygrid_scale.gridstep /= 10;
1893 if (range / im->ygrid_scale.gridstep > 15)
1894 im->ygrid_scale.gridstep *= 10;
1895 if (range / im->ygrid_scale.gridstep > 5) {
1896 im->ygrid_scale.labfact = 1;
1897 if (range / im->ygrid_scale.gridstep > 8
1898 || im->ygrid_scale.gridstep <
1899 1.8 * im->text_prop[TEXT_PROP_AXIS].size)
1900 im->ygrid_scale.labfact = 2;
1902 im->ygrid_scale.gridstep /= 5;
1903 im->ygrid_scale.labfact = 5;
1907 (im->ygrid_scale.gridstep *
1908 (double) im->ygrid_scale.labfact * im->viewfactor /
1910 if (fractionals < 0) { /* small amplitude. */
1911 int len = decimals - fractionals + 1;
1913 if (im->unitslength < len + 2)
1914 im->unitslength = len + 2;
1915 sprintf(im->ygrid_scale.labfmt,
1917 -fractionals, (im->symbol != ' ' ? " %c" : ""));
1919 int len = decimals + 1;
1921 if (im->unitslength < len + 2)
1922 im->unitslength = len + 2;
1923 sprintf(im->ygrid_scale.labfmt,
1924 "%%%d.0f%s", len, (im->symbol != ' ' ? " %c" : ""));
1926 } else { /* classic rrd grid */
1927 for (i = 0; ylab[i].grid > 0; i++) {
1928 pixel = im->ysize / (scaledrange / ylab[i].grid);
1934 for (i = 0; i < 4; i++) {
1935 if (pixel * ylab[gridind].lfac[i] >=
1936 1.8 * im->text_prop[TEXT_PROP_AXIS].size) {
1937 im->ygrid_scale.labfact = ylab[gridind].lfac[i];
1942 im->ygrid_scale.gridstep = ylab[gridind].grid * im->magfact;
1945 im->ygrid_scale.gridstep = im->ygridstep;
1946 im->ygrid_scale.labfact = im->ylabfact;
1951 int draw_horizontal_grid(
1957 char graph_label[100];
1959 double X0 = im->xorigin;
1960 double X1 = im->xorigin + im->xsize;
1961 int sgrid = (int) (im->minval / im->ygrid_scale.gridstep - 1);
1962 int egrid = (int) (im->maxval / im->ygrid_scale.gridstep + 1);
1966 im->ygrid_scale.gridstep /
1967 (double) im->magfact * (double) im->viewfactor;
1968 MaxY = scaledstep * (double) egrid;
1969 for (i = sgrid; i <= egrid; i++) {
1971 im->ygrid_scale.gridstep * i);
1973 im->ygrid_scale.gridstep * (i + 1));
1975 if (floor(Y0 + 0.5) >=
1976 im->yorigin - im->ysize && floor(Y0 + 0.5) <= im->yorigin) {
1977 /* Make sure at least 2 grid labels are shown, even if it doesn't agree
1978 with the chosen settings. Add a label if required by settings, or if
1979 there is only one label so far and the next grid line is out of bounds. */
1980 if (i % im->ygrid_scale.labfact == 0
1982 && (YN < im->yorigin - im->ysize || YN > im->yorigin))) {
1983 if (im->symbol == ' ') {
1984 if (im->extra_flags & ALTYGRID) {
1985 sprintf(graph_label,
1986 im->ygrid_scale.labfmt,
1987 scaledstep * (double) i);
1990 sprintf(graph_label, "%4.1f",
1991 scaledstep * (double) i);
1993 sprintf(graph_label, "%4.0f",
1994 scaledstep * (double) i);
1998 char sisym = (i == 0 ? ' ' : im->symbol);
2000 if (im->extra_flags & ALTYGRID) {
2001 sprintf(graph_label,
2002 im->ygrid_scale.labfmt,
2003 scaledstep * (double) i, sisym);
2006 sprintf(graph_label, "%4.1f %c",
2007 scaledstep * (double) i, sisym);
2009 sprintf(graph_label, "%4.0f %c",
2010 scaledstep * (double) i, sisym);
2018 text_prop[TEXT_PROP_AXIS].
2020 im->graph_col[GRC_FONT],
2022 text_prop[TEXT_PROP_AXIS].
2025 GFX_H_RIGHT, GFX_V_CENTER, graph_label);
2026 gfx_line(im, X0 - 2, Y0, X0, Y0,
2027 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2028 gfx_line(im, X1, Y0, X1 + 2, Y0,
2029 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2030 gfx_dashed_line(im, X0 - 2, Y0,
2036 im->grid_dash_on, im->grid_dash_off);
2037 } else if (!(im->extra_flags & NOMINOR)) {
2040 X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2041 gfx_line(im, X1, Y0, X1 + 2, Y0,
2042 GRIDWIDTH, im->graph_col[GRC_GRID]);
2043 gfx_dashed_line(im, X0 - 1, Y0,
2047 graph_col[GRC_GRID],
2048 im->grid_dash_on, im->grid_dash_off);
2055 /* this is frexp for base 10 */
2066 iexp = floor(log((double)fabs(x)) / log((double)10));
2067 mnt = x / pow(10.0, iexp);
2070 mnt = x / pow(10.0, iexp);
2077 /* logaritmic horizontal grid */
2078 int horizontal_log_grid(
2082 double yloglab[][10] = {
2084 1.0, 10., 0.0, 0.0, 0.0, 0.0, 0.0,
2086 1.0, 5.0, 10., 0.0, 0.0, 0.0, 0.0,
2088 1.0, 2.0, 5.0, 7.0, 10., 0.0, 0.0,
2105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* last line */
2107 int i, j, val_exp, min_exp;
2108 double nex; /* number of decades in data */
2109 double logscale; /* scale in logarithmic space */
2110 int exfrac = 1; /* decade spacing */
2111 int mid = -1; /* row in yloglab for major grid */
2112 double mspac; /* smallest major grid spacing (pixels) */
2113 int flab; /* first value in yloglab to use */
2114 double value, tmp, pre_value;
2116 char graph_label[100];
2118 nex = log10(im->maxval / im->minval);
2119 logscale = im->ysize / nex;
2120 /* major spacing for data with high dynamic range */
2121 while (logscale * exfrac < 3 * im->text_prop[TEXT_PROP_LEGEND].size) {
2128 /* major spacing for less dynamic data */
2130 /* search best row in yloglab */
2132 for (i = 0; yloglab[mid][i + 1] < 10.0; i++);
2133 mspac = logscale * log10(10.0 / yloglab[mid][i]);
2136 2 * im->text_prop[TEXT_PROP_LEGEND].size && yloglab[mid][0] > 0);
2139 /* find first value in yloglab */
2141 yloglab[mid][flab] < 10
2142 && frexp10(im->minval, &tmp) > yloglab[mid][flab]; flab++);
2143 if (yloglab[mid][flab] == 10.0) {
2148 if (val_exp % exfrac)
2149 val_exp += abs(-val_exp % exfrac);
2151 X1 = im->xorigin + im->xsize;
2156 value = yloglab[mid][flab] * pow(10.0, val_exp);
2157 if (AlmostEqual2sComplement(value, pre_value, 4))
2158 break; /* it seems we are not converging */
2160 Y0 = ytr(im, value);
2161 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2163 /* major grid line */
2165 X0 - 2, Y0, X0, Y0, MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2166 gfx_line(im, X1, Y0, X1 + 2, Y0,
2167 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2168 gfx_dashed_line(im, X0 - 2, Y0,
2173 [GRC_MGRID], im->grid_dash_on, im->grid_dash_off);
2175 if (im->extra_flags & FORCE_UNITS_SI) {
2180 scale = floor(val_exp / 3.0);
2182 pvalue = pow(10.0, val_exp % 3);
2184 pvalue = pow(10.0, ((val_exp + 1) % 3) + 2);
2185 pvalue *= yloglab[mid][flab];
2186 if (((scale + si_symbcenter) < (int) sizeof(si_symbol))
2187 && ((scale + si_symbcenter) >= 0))
2188 symbol = si_symbol[scale + si_symbcenter];
2191 sprintf(graph_label, "%3.0f %c", pvalue, symbol);
2193 sprintf(graph_label, "%3.0e", value);
2197 text_prop[TEXT_PROP_AXIS].
2199 im->graph_col[GRC_FONT],
2201 text_prop[TEXT_PROP_AXIS].
2204 GFX_H_RIGHT, GFX_V_CENTER, graph_label);
2206 if (mid < 4 && exfrac == 1) {
2207 /* find first and last minor line behind current major line
2208 * i is the first line and j tha last */
2210 min_exp = val_exp - 1;
2211 for (i = 1; yloglab[mid][i] < 10.0; i++);
2212 i = yloglab[mid][i - 1] + 1;
2216 i = yloglab[mid][flab - 1] + 1;
2217 j = yloglab[mid][flab];
2220 /* draw minor lines below current major line */
2221 for (; i < j; i++) {
2223 value = i * pow(10.0, min_exp);
2224 if (value < im->minval)
2226 Y0 = ytr(im, value);
2227 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2232 X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2233 gfx_line(im, X1, Y0, X1 + 2, Y0,
2234 GRIDWIDTH, im->graph_col[GRC_GRID]);
2235 gfx_dashed_line(im, X0 - 1, Y0,
2239 graph_col[GRC_GRID],
2240 im->grid_dash_on, im->grid_dash_off);
2242 } else if (exfrac > 1) {
2243 for (i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
2244 value = pow(10.0, i);
2245 if (value < im->minval)
2247 Y0 = ytr(im, value);
2248 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2253 X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2254 gfx_line(im, X1, Y0, X1 + 2, Y0,
2255 GRIDWIDTH, im->graph_col[GRC_GRID]);
2256 gfx_dashed_line(im, X0 - 1, Y0,
2260 graph_col[GRC_GRID],
2261 im->grid_dash_on, im->grid_dash_off);
2266 if (yloglab[mid][++flab] == 10.0) {
2272 /* draw minor lines after highest major line */
2273 if (mid < 4 && exfrac == 1) {
2274 /* find first and last minor line below current major line
2275 * i is the first line and j tha last */
2277 min_exp = val_exp - 1;
2278 for (i = 1; yloglab[mid][i] < 10.0; i++);
2279 i = yloglab[mid][i - 1] + 1;
2283 i = yloglab[mid][flab - 1] + 1;
2284 j = yloglab[mid][flab];
2287 /* draw minor lines below current major line */
2288 for (; i < j; i++) {
2290 value = i * pow(10.0, min_exp);
2291 if (value < im->minval)
2293 Y0 = ytr(im, value);
2294 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2298 X0 - 2, Y0, X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2299 gfx_line(im, X1, Y0, X1 + 2, Y0,
2300 GRIDWIDTH, im->graph_col[GRC_GRID]);
2301 gfx_dashed_line(im, X0 - 1, Y0,
2305 graph_col[GRC_GRID],
2306 im->grid_dash_on, im->grid_dash_off);
2309 /* fancy minor gridlines */
2310 else if (exfrac > 1) {
2311 for (i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
2312 value = pow(10.0, i);
2313 if (value < im->minval)
2315 Y0 = ytr(im, value);
2316 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2320 X0 - 2, Y0, X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2321 gfx_line(im, X1, Y0, X1 + 2, Y0,
2322 GRIDWIDTH, im->graph_col[GRC_GRID]);
2323 gfx_dashed_line(im, X0 - 1, Y0,
2327 graph_col[GRC_GRID],
2328 im->grid_dash_on, im->grid_dash_off);
2339 int xlab_sel; /* which sort of label and grid ? */
2340 time_t ti, tilab, timajor;
2342 char graph_label[100];
2343 double X0, Y0, Y1; /* points for filled graph and more */
2346 /* the type of time grid is determined by finding
2347 the number of seconds per pixel in the graph */
2348 if (im->xlab_user.minsec == -1) {
2349 factor = (im->end - im->start) / im->xsize;
2351 while (xlab[xlab_sel + 1].minsec !=
2352 -1 && xlab[xlab_sel + 1].minsec <= factor) {
2354 } /* pick the last one */
2355 while (xlab[xlab_sel - 1].minsec ==
2356 xlab[xlab_sel].minsec
2357 && xlab[xlab_sel].length > (im->end - im->start)) {
2359 } /* go back to the smallest size */
2360 im->xlab_user.gridtm = xlab[xlab_sel].gridtm;
2361 im->xlab_user.gridst = xlab[xlab_sel].gridst;
2362 im->xlab_user.mgridtm = xlab[xlab_sel].mgridtm;
2363 im->xlab_user.mgridst = xlab[xlab_sel].mgridst;
2364 im->xlab_user.labtm = xlab[xlab_sel].labtm;
2365 im->xlab_user.labst = xlab[xlab_sel].labst;
2366 im->xlab_user.precis = xlab[xlab_sel].precis;
2367 im->xlab_user.stst = xlab[xlab_sel].stst;
2370 /* y coords are the same for every line ... */
2372 Y1 = im->yorigin - im->ysize;
2373 /* paint the minor grid */
2374 if (!(im->extra_flags & NOMINOR)) {
2375 for (ti = find_first_time(im->start,
2383 find_first_time(im->start,
2390 find_next_time(ti, im->xlab_user.gridtm, im->xlab_user.gridst)
2392 /* are we inside the graph ? */
2393 if (ti < im->start || ti > im->end)
2395 while (timajor < ti) {
2396 timajor = find_next_time(timajor,
2399 mgridtm, im->xlab_user.mgridst);
2402 continue; /* skip as falls on major grid line */
2404 gfx_line(im, X0, Y1 - 2, X0, Y1,
2405 GRIDWIDTH, im->graph_col[GRC_GRID]);
2406 gfx_line(im, X0, Y0, X0, Y0 + 2,
2407 GRIDWIDTH, im->graph_col[GRC_GRID]);
2408 gfx_dashed_line(im, X0, Y0 + 1, X0,
2411 graph_col[GRC_GRID],
2412 im->grid_dash_on, im->grid_dash_off);
2416 /* paint the major grid */
2417 for (ti = find_first_time(im->start,
2425 ti = find_next_time(ti, im->xlab_user.mgridtm, im->xlab_user.mgridst)
2427 /* are we inside the graph ? */
2428 if (ti < im->start || ti > im->end)
2431 gfx_line(im, X0, Y1 - 2, X0, Y1,
2432 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2433 gfx_line(im, X0, Y0, X0, Y0 + 3,
2434 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2435 gfx_dashed_line(im, X0, Y0 + 3, X0,
2439 [GRC_MGRID], im->grid_dash_on, im->grid_dash_off);
2441 /* paint the labels below the graph */
2443 find_first_time(im->start -
2452 im->xlab_user.precis / 2;
2453 ti = find_next_time(ti, im->xlab_user.labtm, im->xlab_user.labst)
2455 tilab = ti + im->xlab_user.precis / 2; /* correct time for the label */
2456 /* are we inside the graph ? */
2457 if (tilab < im->start || tilab > im->end)
2460 localtime_r(&tilab, &tm);
2461 strftime(graph_label, 99, im->xlab_user.stst, &tm);
2463 # error "your libc has no strftime I guess we'll abort the exercise here."
2468 im->graph_col[GRC_FONT],
2470 text_prop[TEXT_PROP_AXIS].
2473 GFX_H_CENTER, GFX_V_TOP, graph_label);
2482 /* draw x and y axis */
2483 /* gfx_line ( im->canvas, im->xorigin+im->xsize,im->yorigin,
2484 im->xorigin+im->xsize,im->yorigin-im->ysize,
2485 GRIDWIDTH, im->graph_col[GRC_AXIS]);
2487 gfx_line ( im->canvas, im->xorigin,im->yorigin-im->ysize,
2488 im->xorigin+im->xsize,im->yorigin-im->ysize,
2489 GRIDWIDTH, im->graph_col[GRC_AXIS]); */
2491 gfx_line(im, im->xorigin - 4,
2493 im->xorigin + im->xsize +
2494 4, im->yorigin, MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2495 gfx_line(im, im->xorigin,
2498 im->yorigin - im->ysize -
2499 4, MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2500 /* arrow for X and Y axis direction */
2501 gfx_new_area(im, im->xorigin + im->xsize + 2, im->yorigin - 3, im->xorigin + im->xsize + 2, im->yorigin + 3, im->xorigin + im->xsize + 7, im->yorigin, /* horyzontal */
2502 im->graph_col[GRC_ARROW]);
2504 gfx_new_area(im, im->xorigin - 3, im->yorigin - im->ysize - 2, im->xorigin + 3, im->yorigin - im->ysize - 2, im->xorigin, im->yorigin - im->ysize - 7, /* vertical */
2505 im->graph_col[GRC_ARROW]);
2514 double X0, Y0; /* points for filled graph and more */
2515 struct gfx_color_t water_color;
2517 /* draw 3d border */
2518 gfx_new_area(im, 0, im->yimg,
2519 2, im->yimg - 2, 2, 2, im->graph_col[GRC_SHADEA]);
2520 gfx_add_point(im, im->ximg - 2, 2);
2521 gfx_add_point(im, im->ximg, 0);
2522 gfx_add_point(im, 0, 0);
2524 gfx_new_area(im, 2, im->yimg - 2,
2526 im->yimg - 2, im->ximg - 2, 2, im->graph_col[GRC_SHADEB]);
2527 gfx_add_point(im, im->ximg, 0);
2528 gfx_add_point(im, im->ximg, im->yimg);
2529 gfx_add_point(im, 0, im->yimg);
2531 if (im->draw_x_grid == 1)
2533 if (im->draw_y_grid == 1) {
2534 if (im->logarithmic) {
2535 res = horizontal_log_grid(im);
2537 res = draw_horizontal_grid(im);
2540 /* dont draw horizontal grid if there is no min and max val */
2542 char *nodata = "No Data found";
2544 gfx_text(im, im->ximg / 2,
2547 im->graph_col[GRC_FONT],
2549 text_prop[TEXT_PROP_AXIS].
2552 GFX_H_CENTER, GFX_V_CENTER, nodata);
2556 /* yaxis unit description */
2561 im->graph_col[GRC_FONT],
2563 text_prop[TEXT_PROP_UNIT].
2566 RRDGRAPH_YLEGEND_ANGLE, GFX_H_CENTER, GFX_V_CENTER, im->ylegend);
2570 im->graph_col[GRC_FONT],
2572 text_prop[TEXT_PROP_TITLE].
2574 im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_TOP, im->title);
2575 /* rrdtool 'logo' */
2576 water_color = im->graph_col[GRC_FONT];
2577 water_color.alpha = 0.3;
2578 gfx_text(im, im->ximg - 4, 5,
2581 text_prop[TEXT_PROP_WATERMARK].
2582 font_desc, im->tabwidth,
2583 -90, GFX_H_LEFT, GFX_V_TOP, "RRDTOOL / TOBI OETIKER");
2584 /* graph watermark */
2585 if (im->watermark[0] != '\0') {
2587 im->ximg / 2, im->yimg - 6,
2590 text_prop[TEXT_PROP_WATERMARK].
2591 font_desc, im->tabwidth, 0,
2592 GFX_H_CENTER, GFX_V_BOTTOM, im->watermark);
2596 if (!(im->extra_flags & NOLEGEND) & !(im->extra_flags & ONLY_GRAPH)) {
2597 for (i = 0; i < im->gdes_c; i++) {
2598 if (im->gdes[i].legend[0] == '\0')
2600 /* im->gdes[i].leg_y is the bottom of the legend */
2601 X0 = im->gdes[i].leg_x;
2602 Y0 = im->gdes[i].leg_y;
2603 gfx_text(im, X0, Y0,
2604 im->graph_col[GRC_FONT],
2607 [TEXT_PROP_LEGEND].font_desc,
2609 GFX_H_LEFT, GFX_V_BOTTOM, im->gdes[i].legend);
2610 /* The legend for GRAPH items starts with "M " to have
2611 enough space for the box */
2612 if (im->gdes[i].gf != GF_PRINT &&
2613 im->gdes[i].gf != GF_GPRINT && im->gdes[i].gf != GF_COMMENT) {
2617 boxH = gfx_get_text_width(im, 0,
2622 im->tabwidth, "o") * 1.2;
2624 /* shift the box up a bit */
2626 /* make sure transparent colors show up the same way as in the graph */
2629 X0, Y0, X0 + boxH, Y0, im->graph_col[GRC_BACK]);
2630 gfx_add_point(im, X0 + boxH, Y0 - boxV);
2632 gfx_new_area(im, X0, Y0 - boxV, X0,
2633 Y0, X0 + boxH, Y0, im->gdes[i].col);
2634 gfx_add_point(im, X0 + boxH, Y0 - boxV);
2637 cairo_new_path(im->cr);
2638 cairo_set_line_width(im->cr, 1.0);
2641 gfx_line_fit(im, &X0, &Y0);
2642 gfx_line_fit(im, &X1, &Y1);
2643 cairo_move_to(im->cr, X0, Y0);
2644 cairo_line_to(im->cr, X1, Y0);
2645 cairo_line_to(im->cr, X1, Y1);
2646 cairo_line_to(im->cr, X0, Y1);
2647 cairo_close_path(im->cr);
2648 cairo_set_source_rgba(im->cr,
2660 blue, im->graph_col[GRC_FRAME].alpha);
2661 if (im->gdes[i].dash) {
2662 /* make box borders in legend dashed if the graph is dashed */
2666 cairo_set_dash(im->cr, dashes, 1, 0.0);
2668 cairo_stroke(im->cr);
2669 cairo_restore(im->cr);
2676 /*****************************************************
2677 * lazy check make sure we rely need to create this graph
2678 *****************************************************/
2685 struct stat imgstat;
2688 return 0; /* no lazy option */
2689 if (strlen(im->graphfile) == 0)
2690 return 0; /* inmemory option */
2691 if (stat(im->graphfile, &imgstat) != 0)
2692 return 0; /* can't stat */
2693 /* one pixel in the existing graph is more then what we would
2695 if (time(NULL) - imgstat.st_mtime > (im->end - im->start) / im->xsize)
2697 if ((fd = fopen(im->graphfile, "rb")) == NULL)
2698 return 0; /* the file does not exist */
2699 switch (im->imgformat) {
2701 size = PngSize(fd, &(im->ximg), &(im->yimg));
2711 int graph_size_location(
2716 /* The actual size of the image to draw is determined from
2717 ** several sources. The size given on the command line is
2718 ** the graph area but we need more as we have to draw labels
2719 ** and other things outside the graph area
2722 int Xvertical = 0, Ytitle =
2723 0, Xylabel = 0, Xmain = 0, Ymain =
2724 0, Yxlabel = 0, Xspacing = 15, Yspacing = 15, Ywatermark = 4;
2726 if (im->extra_flags & ONLY_GRAPH) {
2728 im->ximg = im->xsize;
2729 im->yimg = im->ysize;
2730 im->yorigin = im->ysize;
2735 /** +---+-----------------------------------+
2736 ** | y |...............graph title.........|
2737 ** | +---+-------------------------------+
2741 ** | s | x | main graph area |
2746 ** | l | b +-------------------------------+
2747 ** | e | l | x axis labels |
2748 ** +---+---+-------------------------------+
2749 ** |....................legends............|
2750 ** +---------------------------------------+
2752 ** +---------------------------------------+
2755 if (im->ylegend[0] != '\0') {
2756 Xvertical = im->text_prop[TEXT_PROP_UNIT].size * 2;
2759 if (im->title[0] != '\0') {
2760 /* The title is placed "inbetween" two text lines so it
2761 ** automatically has some vertical spacing. The horizontal
2762 ** spacing is added here, on each side.
2764 /* if necessary, reduce the font size of the title until it fits the image width */
2765 Ytitle = im->text_prop[TEXT_PROP_TITLE].size * 2.6 + 10;
2769 if (im->draw_x_grid) {
2770 Yxlabel = im->text_prop[TEXT_PROP_AXIS].size * 2.5;
2772 if (im->draw_y_grid || im->forceleftspace) {
2774 gfx_get_text_width(im, 0,
2779 im->tabwidth, "0") * im->unitslength;
2783 if (im->extra_flags & FULL_SIZE_MODE) {
2784 /* The actual size of the image to draw has been determined by the user.
2785 ** The graph area is the space remaining after accounting for the legend,
2786 ** the watermark, the axis labels, and the title.
2789 im->ximg = im->xsize;
2790 im->yimg = im->ysize;
2791 im->yorigin = im->ysize;
2794 im->yorigin += Ytitle;
2795 /* Now calculate the total size. Insert some spacing where
2796 desired. im->xorigin and im->yorigin need to correspond
2797 with the lower left corner of the main graph area or, if
2798 this one is not set, the imaginary box surrounding the
2800 /* Initial size calculation for the main graph area */
2801 Xmain = im->ximg - (Xylabel + 2 * Xspacing);
2803 Xmain -= Xspacing; /* put space between main graph area and right edge */
2804 im->xorigin = Xspacing + Xylabel;
2805 /* the length of the title should not influence with width of the graph
2806 if (Xtitle > im->ximg) im->ximg = Xtitle; */
2807 if (Xvertical) { /* unit description */
2809 im->xorigin += Xvertical;
2813 /* The vertical size of the image is known in advance. The main graph area
2814 ** (Ymain) and im->yorigin must be set according to the space requirements
2815 ** of the legend and the axis labels.
2817 if (im->extra_flags & NOLEGEND) {
2818 im->yorigin = im->yimg -
2819 im->text_prop[TEXT_PROP_AXIS].size * 2.5 - Yspacing;
2820 Ymain = im->yorigin;
2823 /* Determine where to place the legends onto the image.
2824 ** Set Ymain and adjust im->yorigin to match the space requirements.
2826 if (leg_place(im, &Ymain) == -1)
2831 /* remove title space *or* some padding above the graph from the main graph area */
2835 Ymain -= 1.5 * Yspacing;
2838 /* watermark doesn't seem to effect the vertical size of the main graph area, oh well! */
2839 if (im->watermark[0] != '\0') {
2840 Ymain -= Ywatermark;
2844 } else { /* dimension options -width and -height refer to the dimensions of the main graph area */
2846 /* The actual size of the image to draw is determined from
2847 ** several sources. The size given on the command line is
2848 ** the graph area but we need more as we have to draw labels
2849 ** and other things outside the graph area.
2852 if (im->ylegend[0] != '\0') {
2853 Xvertical = im->text_prop[TEXT_PROP_UNIT].size * 2;
2857 if (im->title[0] != '\0') {
2858 /* The title is placed "inbetween" two text lines so it
2859 ** automatically has some vertical spacing. The horizontal
2860 ** spacing is added here, on each side.
2862 /* don't care for the with of the title
2863 Xtitle = gfx_get_text_width(im->canvas, 0,
2864 im->text_prop[TEXT_PROP_TITLE].font,
2865 im->text_prop[TEXT_PROP_TITLE].size,
2867 im->title, 0) + 2*Xspacing; */
2868 Ytitle = im->text_prop[TEXT_PROP_TITLE].size * 2.6 + 10;
2875 /* Now calculate the total size. Insert some spacing where
2876 desired. im->xorigin and im->yorigin need to correspond
2877 with the lower left corner of the main graph area or, if
2878 this one is not set, the imaginary box surrounding the
2881 /* The legend width cannot yet be determined, as a result we
2882 ** have problems adjusting the image to it. For now, we just
2883 ** forget about it at all; the legend will have to fit in the
2884 ** size already allocated.
2886 im->ximg = Xylabel + Xmain + 2 * Xspacing;
2888 im->ximg += Xspacing;
2889 im->xorigin = Xspacing + Xylabel;
2890 /* the length of the title should not influence with width of the graph
2891 if (Xtitle > im->ximg) im->ximg = Xtitle; */
2892 if (Xvertical) { /* unit description */
2893 im->ximg += Xvertical;
2894 im->xorigin += Xvertical;
2897 /* The vertical size is interesting... we need to compare
2898 ** the sum of {Ytitle, Ymain, Yxlabel, Ylegend, Ywatermark} with
2899 ** Yvertical however we need to know {Ytitle+Ymain+Yxlabel}
2900 ** in order to start even thinking about Ylegend or Ywatermark.
2902 ** Do it in three portions: First calculate the inner part,
2903 ** then do the legend, then adjust the total height of the img,
2904 ** adding space for a watermark if one exists;
2906 /* reserve space for main and/or pie */
2907 im->yimg = Ymain + Yxlabel;
2908 im->yorigin = im->yimg - Yxlabel;
2909 /* reserve space for the title *or* some padding above the graph */
2912 im->yorigin += Ytitle;
2914 im->yimg += 1.5 * Yspacing;
2915 im->yorigin += 1.5 * Yspacing;
2917 /* reserve space for padding below the graph */
2918 im->yimg += Yspacing;
2919 /* Determine where to place the legends onto the image.
2920 ** Adjust im->yimg to match the space requirements.
2922 if (leg_place(im, 0) == -1)
2924 if (im->watermark[0] != '\0') {
2925 im->yimg += Ywatermark;
2933 static cairo_status_t cairo_output(
2937 unsigned int length)
2939 image_desc_t *im = (image_desc_t*)closure;
2941 im->rendered_image =
2942 (unsigned char*)realloc(im->rendered_image, im->rendered_image_size + length);
2943 if (im->rendered_image == NULL)
2944 return CAIRO_STATUS_WRITE_ERROR;
2945 memcpy(im->rendered_image + im->rendered_image_size, data, length);
2946 im->rendered_image_size += length;
2947 return CAIRO_STATUS_SUCCESS;
2950 /* draw that picture thing ... */
2955 int lazy = lazy_check(im);
2956 double areazero = 0.0;
2957 graph_desc_t *lastgdes = NULL;
2960 // PangoFontMap *font_map = pango_cairo_font_map_get_default();
2962 /* if we want and can be lazy ... quit now */
2964 info.u_cnt = im->ximg;
2965 grinfo_push(im, sprintf_alloc("image_width"), RD_I_CNT, info);
2966 info.u_cnt = im->yimg;
2967 grinfo_push(im, sprintf_alloc("image_height"), RD_I_CNT, info);
2970 /* pull the data from the rrd files ... */
2971 if (data_fetch(im) == -1)
2973 /* evaluate VDEF and CDEF operations ... */
2974 if (data_calc(im) == -1)
2976 /* calculate and PRINT and GPRINT definitions. We have to do it at
2977 * this point because it will affect the length of the legends
2978 * if there are no graph elements (i==0) we stop here ...
2979 * if we are lazy, try to quit ...
2985 if ((i == 0) || lazy)
2988 /**************************************************************
2989 *** Calculating sizes and locations became a bit confusing ***
2990 *** so I moved this into a separate function. ***
2991 **************************************************************/
2992 if (graph_size_location(im, i) == -1)
2995 info.u_cnt = im->xorigin;
2996 grinfo_push(im, sprintf_alloc("graph_left"), RD_I_CNT, info);
2997 info.u_cnt = im->yorigin - im->ysize;
2998 grinfo_push(im, sprintf_alloc("graph_top"), RD_I_CNT, info);
2999 info.u_cnt = im->xsize;
3000 grinfo_push(im, sprintf_alloc("graph_width"), RD_I_CNT, info);
3001 info.u_cnt = im->ysize;
3002 grinfo_push(im, sprintf_alloc("graph_height"), RD_I_CNT, info);
3003 info.u_cnt = im->ximg;
3004 grinfo_push(im, sprintf_alloc("image_width"), RD_I_CNT, info);
3005 info.u_cnt = im->yimg;
3006 grinfo_push(im, sprintf_alloc("image_height"), RD_I_CNT, info);
3008 /* get actual drawing data and find min and max values */
3009 if (data_proc(im) == -1)
3011 if (!im->logarithmic) {
3015 /* identify si magnitude Kilo, Mega Giga ? */
3016 if (!im->rigid && !im->logarithmic)
3017 expand_range(im); /* make sure the upper and lower limit are
3020 info.u_val = im->minval;
3021 grinfo_push(im, sprintf_alloc("value_min"), RD_I_VAL, info);
3022 info.u_val = im->maxval;
3023 grinfo_push(im, sprintf_alloc("value_max"), RD_I_VAL, info);
3025 if (!calc_horizontal_grid(im))
3030 apply_gridfit(im); */
3031 /* the actual graph is created by going through the individual
3032 graph elements and then drawing them */
3033 cairo_surface_destroy(im->surface);
3034 switch (im->imgformat) {
3037 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
3038 im->ximg * im->zoom,
3039 im->yimg * im->zoom);
3043 im->surface = strlen(im->graphfile)
3044 ? cairo_pdf_surface_create(im->graphfile, im->ximg * im->zoom,
3045 im->yimg * im->zoom)
3046 : cairo_pdf_surface_create_for_stream
3047 (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3051 im->surface = strlen(im->graphfile)
3053 cairo_ps_surface_create(im->graphfile, im->ximg * im->zoom,
3054 im->yimg * im->zoom)
3055 : cairo_ps_surface_create_for_stream
3056 (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3060 im->surface = strlen(im->graphfile)
3062 cairo_svg_surface_create(im->
3064 im->ximg * im->zoom, im->yimg * im->zoom)
3065 : cairo_svg_surface_create_for_stream
3066 (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3067 cairo_svg_surface_restrict_to_version
3068 (im->surface, CAIRO_SVG_VERSION_1_1);
3071 cairo_destroy(im->cr);
3072 im->cr = cairo_create(im->surface);
3073 cairo_set_antialias(im->cr, im->graph_antialias);
3074 cairo_scale(im->cr, im->zoom, im->zoom);
3075 // pango_cairo_font_map_set_resolution(PANGO_CAIRO_FONT_MAP(font_map), 100);
3076 gfx_new_area(im, 0, 0, 0, im->yimg,
3077 im->ximg, im->yimg, im->graph_col[GRC_BACK]);
3078 gfx_add_point(im, im->ximg, 0);
3080 gfx_new_area(im, im->xorigin,
3083 im->xsize, im->yorigin,
3086 im->yorigin - im->ysize, im->graph_col[GRC_CANVAS]);
3087 gfx_add_point(im, im->xorigin, im->yorigin - im->ysize);
3089 cairo_rectangle(im->cr, im->xorigin, im->yorigin - im->ysize - 1.0,
3090 im->xsize, im->ysize + 2.0);
3092 if (im->minval > 0.0)
3093 areazero = im->minval;
3094 if (im->maxval < 0.0)
3095 areazero = im->maxval;
3096 for (i = 0; i < im->gdes_c; i++) {
3097 switch (im->gdes[i].gf) {
3111 for (ii = 0; ii < im->xsize; ii++) {
3112 if (!isnan(im->gdes[i].p_data[ii])
3113 && im->gdes[i].p_data[ii] != 0.0) {
3114 if (im->gdes[i].yrule > 0) {
3121 im->ysize, 1.0, im->gdes[i].col);
3122 } else if (im->gdes[i].yrule < 0) {
3125 im->yorigin - im->ysize,
3130 im->ysize, 1.0, im->gdes[i].col);
3137 /* fix data points at oo and -oo */
3138 for (ii = 0; ii < im->xsize; ii++) {
3139 if (isinf(im->gdes[i].p_data[ii])) {
3140 if (im->gdes[i].p_data[ii] > 0) {
3141 im->gdes[i].p_data[ii] = im->maxval;
3143 im->gdes[i].p_data[ii] = im->minval;
3149 /* *******************************************************
3154 -------|--t-1--t--------------------------------
3156 if we know the value at time t was a then
3157 we draw a square from t-1 to t with the value a.
3159 ********************************************************* */
3160 if (im->gdes[i].col.alpha != 0.0) {
3161 /* GF_LINE and friend */
3162 if (im->gdes[i].gf == GF_LINE) {
3163 double last_y = 0.0;
3167 cairo_new_path(im->cr);
3168 cairo_set_line_width(im->cr, im->gdes[i].linewidth);
3169 if (im->gdes[i].dash) {
3170 cairo_set_dash(im->cr,
3171 im->gdes[i].p_dashes,
3172 im->gdes[i].ndash, im->gdes[i].offset);
3175 for (ii = 1; ii < im->xsize; ii++) {
3176 if (isnan(im->gdes[i].p_data[ii])
3177 || (im->slopemode == 1
3178 && isnan(im->gdes[i].p_data[ii - 1]))) {
3183 last_y = ytr(im, im->gdes[i].p_data[ii]);
3184 if (im->slopemode == 0) {
3185 double x = ii - 1 + im->xorigin;
3188 gfx_line_fit(im, &x, &y);
3189 cairo_move_to(im->cr, x, y);
3190 x = ii + im->xorigin;
3192 gfx_line_fit(im, &x, &y);
3193 cairo_line_to(im->cr, x, y);
3195 double x = ii - 1 + im->xorigin;
3197 ytr(im, im->gdes[i].p_data[ii - 1]);
3198 gfx_line_fit(im, &x, &y);
3199 cairo_move_to(im->cr, x, y);
3200 x = ii + im->xorigin;
3202 gfx_line_fit(im, &x, &y);
3203 cairo_line_to(im->cr, x, y);
3207 double x1 = ii + im->xorigin;
3208 double y1 = ytr(im, im->gdes[i].p_data[ii]);
3210 if (im->slopemode == 0
3211 && !AlmostEqual2sComplement(y1, last_y, 4)) {
3212 double x = ii - 1 + im->xorigin;
3215 gfx_line_fit(im, &x, &y);
3216 cairo_line_to(im->cr, x, y);
3219 gfx_line_fit(im, &x1, &y1);
3220 cairo_line_to(im->cr, x1, y1);
3223 cairo_set_source_rgba(im->cr,
3229 col.blue, im->gdes[i].col.alpha);
3230 cairo_set_line_cap(im->cr, CAIRO_LINE_CAP_ROUND);
3231 cairo_set_line_join(im->cr, CAIRO_LINE_JOIN_ROUND);
3232 cairo_stroke(im->cr);
3233 cairo_restore(im->cr);
3237 (double *) malloc(sizeof(double) * im->xsize * 2);
3239 (double *) malloc(sizeof(double) * im->xsize * 2);
3241 (double *) malloc(sizeof(double) * im->xsize * 2);
3243 (double *) malloc(sizeof(double) * im->xsize * 2);
3246 for (ii = 0; ii <= im->xsize; ii++) {
3249 if (idxI > 0 && (drawem != 0 || ii == im->xsize)) {
3255 AlmostEqual2sComplement(foreY
3259 AlmostEqual2sComplement(foreY
3269 foreY[cntI], im->gdes[i].col);
3270 while (cntI < idxI) {
3275 AlmostEqual2sComplement(foreY
3279 AlmostEqual2sComplement(foreY
3286 gfx_add_point(im, foreX[cntI], foreY[cntI]);
3288 gfx_add_point(im, backX[idxI], backY[idxI]);
3294 AlmostEqual2sComplement(backY
3298 AlmostEqual2sComplement(backY
3305 gfx_add_point(im, backX[idxI], backY[idxI]);
3315 if (ii == im->xsize)
3317 if (im->slopemode == 0 && ii == 0) {
3320 if (isnan(im->gdes[i].p_data[ii])) {
3324 ytop = ytr(im, im->gdes[i].p_data[ii]);
3325 if (lastgdes && im->gdes[i].stack) {
3326 ybase = ytr(im, lastgdes->p_data[ii]);
3328 ybase = ytr(im, areazero);
3330 if (ybase == ytop) {
3336 double extra = ytop;
3341 if (im->slopemode == 0) {
3342 backY[++idxI] = ybase - 0.2;
3343 backX[idxI] = ii + im->xorigin - 1;
3344 foreY[idxI] = ytop + 0.2;
3345 foreX[idxI] = ii + im->xorigin - 1;
3347 backY[++idxI] = ybase - 0.2;
3348 backX[idxI] = ii + im->xorigin;
3349 foreY[idxI] = ytop + 0.2;
3350 foreX[idxI] = ii + im->xorigin;
3352 /* close up any remaining area */
3357 } /* else GF_LINE */
3359 /* if color != 0x0 */
3360 /* make sure we do not run into trouble when stacking on NaN */
3361 for (ii = 0; ii < im->xsize; ii++) {
3362 if (isnan(im->gdes[i].p_data[ii])) {
3363 if (lastgdes && (im->gdes[i].stack)) {
3364 im->gdes[i].p_data[ii] = lastgdes->p_data[ii];
3366 im->gdes[i].p_data[ii] = areazero;
3370 lastgdes = &(im->gdes[i]);
3374 ("STACK should already be turned into LINE or AREA here");
3379 cairo_reset_clip(im->cr);
3381 /* grid_paint also does the text */
3382 if (!(im->extra_flags & ONLY_GRAPH))
3384 if (!(im->extra_flags & ONLY_GRAPH))
3386 /* the RULES are the last thing to paint ... */
3387 for (i = 0; i < im->gdes_c; i++) {
3389 switch (im->gdes[i].gf) {
3391 if (im->gdes[i].yrule >= im->minval
3392 && im->gdes[i].yrule <= im->maxval) {
3394 if (im->gdes[i].dash) {
3395 cairo_set_dash(im->cr,
3396 im->gdes[i].p_dashes,
3397 im->gdes[i].ndash, im->gdes[i].offset);
3399 gfx_line(im, im->xorigin,
3400 ytr(im, im->gdes[i].yrule),
3401 im->xorigin + im->xsize,
3402 ytr(im, im->gdes[i].yrule), 1.0, im->gdes[i].col);
3403 cairo_stroke(im->cr);
3404 cairo_restore(im->cr);
3408 if (im->gdes[i].xrule >= im->start
3409 && im->gdes[i].xrule <= im->end) {
3411 if (im->gdes[i].dash) {
3412 cairo_set_dash(im->cr,
3413 im->gdes[i].p_dashes,
3414 im->gdes[i].ndash, im->gdes[i].offset);
3417 xtr(im, im->gdes[i].xrule),
3418 im->yorigin, xtr(im,
3422 im->yorigin - im->ysize, 1.0, im->gdes[i].col);
3423 cairo_stroke(im->cr);
3424 cairo_restore(im->cr);
3433 switch (im->imgformat) {
3436 cairo_status_t status;
3438 status = strlen(im->graphfile) ?
3439 cairo_surface_write_to_png(im->surface, im->graphfile)
3440 : cairo_surface_write_to_png_stream(im->surface, &cairo_output,
3443 if (status != CAIRO_STATUS_SUCCESS) {
3444 rrd_set_error("Could not save png to '%s'", im->graphfile);
3450 if (strlen(im->graphfile)) {
3451 cairo_show_page(im->cr);
3453 cairo_surface_finish(im->surface);
3462 /*****************************************************
3464 *****************************************************/
3471 if ((im->gdes = (graph_desc_t *)
3472 rrd_realloc(im->gdes, (im->gdes_c)
3473 * sizeof(graph_desc_t))) == NULL) {
3474 rrd_set_error("realloc graph_descs");
3479 im->gdes[im->gdes_c - 1].step = im->step;
3480 im->gdes[im->gdes_c - 1].step_orig = im->step;
3481 im->gdes[im->gdes_c - 1].stack = 0;
3482 im->gdes[im->gdes_c - 1].linewidth = 0;
3483 im->gdes[im->gdes_c - 1].debug = 0;
3484 im->gdes[im->gdes_c - 1].start = im->start;
3485 im->gdes[im->gdes_c - 1].start_orig = im->start;
3486 im->gdes[im->gdes_c - 1].end = im->end;
3487 im->gdes[im->gdes_c - 1].end_orig = im->end;
3488 im->gdes[im->gdes_c - 1].vname[0] = '\0';
3489 im->gdes[im->gdes_c - 1].data = NULL;
3490 im->gdes[im->gdes_c - 1].ds_namv = NULL;
3491 im->gdes[im->gdes_c - 1].data_first = 0;
3492 im->gdes[im->gdes_c - 1].p_data = NULL;
3493 im->gdes[im->gdes_c - 1].rpnp = NULL;
3494 im->gdes[im->gdes_c - 1].p_dashes = NULL;
3495 im->gdes[im->gdes_c - 1].shift = 0.0;
3496 im->gdes[im->gdes_c - 1].dash = 0;
3497 im->gdes[im->gdes_c - 1].ndash = 0;
3498 im->gdes[im->gdes_c - 1].offset = 0;
3499 im->gdes[im->gdes_c - 1].col.red = 0.0;
3500 im->gdes[im->gdes_c - 1].col.green = 0.0;
3501 im->gdes[im->gdes_c - 1].col.blue = 0.0;
3502 im->gdes[im->gdes_c - 1].col.alpha = 0.0;
3503 im->gdes[im->gdes_c - 1].legend[0] = '\0';
3504 im->gdes[im->gdes_c - 1].format[0] = '\0';
3505 im->gdes[im->gdes_c - 1].strftm = 0;
3506 im->gdes[im->gdes_c - 1].rrd[0] = '\0';
3507 im->gdes[im->gdes_c - 1].ds = -1;
3508 im->gdes[im->gdes_c - 1].cf_reduce = CF_AVERAGE;
3509 im->gdes[im->gdes_c - 1].cf = CF_AVERAGE;
3510 im->gdes[im->gdes_c - 1].yrule = DNAN;
3511 im->gdes[im->gdes_c - 1].xrule = 0;
3515 /* copies input untill the first unescaped colon is found
3516 or until input ends. backslashes have to be escaped as well */
3518 const char *const input,
3524 for (inp = 0; inp < len && input[inp] != ':' && input[inp] != '\0'; inp++) {
3525 if (input[inp] == '\\'
3526 && input[inp + 1] != '\0'
3527 && (input[inp + 1] == '\\' || input[inp + 1] == ':')) {
3528 output[outp++] = input[++inp];
3530 output[outp++] = input[inp];
3533 output[outp] = '\0';
3537 /* Now just a wrapper around rrd_graph_v */
3549 rrd_info_t *grinfo = NULL;
3552 grinfo = rrd_graph_v(argc, argv);
3558 if (strcmp(walker->key, "image_info") == 0) {
3561 (char**)rrd_realloc((*prdata),
3562 (prlines + 1) * sizeof(char *))) == NULL) {
3563 rrd_set_error("realloc prdata");
3566 /* imginfo goes to position 0 in the prdata array */
3567 (*prdata)[prlines - 1] = (char*)malloc((strlen(walker->value.u_str)
3568 + 2) * sizeof(char));
3569 strcpy((*prdata)[prlines - 1], walker->value.u_str);
3570 (*prdata)[prlines] = NULL;
3572 /* skip anything else */
3573 walker = walker->next;
3581 if (strcmp(walker->key, "image_width") == 0) {
3582 *xsize = walker->value.u_int;
3583 } else if (strcmp(walker->key, "image_height") == 0) {
3584 *ysize = walker->value.u_int;
3585 } else if (strcmp(walker->key, "value_min") == 0) {
3586 *ymin = walker->value.u_val;
3587 } else if (strcmp(walker->key, "value_max") == 0) {
3588 *ymax = walker->value.u_val;
3589 } else if (strncmp(walker->key, "print", 5) == 0) { /* keys are prdate[0..] */
3592 (char**)rrd_realloc((*prdata),
3593 (prlines + 1) * sizeof(char *))) == NULL) {
3594 rrd_set_error("realloc prdata");
3597 (*prdata)[prlines - 1] = (char*)malloc((strlen(walker->value.u_str)
3598 + 2) * sizeof(char));
3599 (*prdata)[prlines] = NULL;
3600 strcpy((*prdata)[prlines - 1], walker->value.u_str);
3601 } else if (strcmp(walker->key, "image") == 0) {
3602 fwrite(walker->value.u_blo.ptr, walker->value.u_blo.size, 1,
3603 (stream ? stream : stdout));
3605 /* skip anything else */
3606 walker = walker->next;
3608 rrd_info_free(grinfo);
3613 /* Some surgery done on this function, it became ridiculously big.
3615 ** - initializing now in rrd_graph_init()
3616 ** - options parsing now in rrd_graph_options()
3617 ** - script parsing now in rrd_graph_script()
3619 rrd_info_t *rrd_graph_v(
3625 rrd_graph_init(&im);
3626 /* a dummy surface so that we can measure text sizes for placements */
3628 rrd_graph_options(argc, argv, &im);
3629 if (rrd_test_error()) {
3630 rrd_info_free(im.grinfo);
3635 if (optind >= argc) {
3636 rrd_info_free(im.grinfo);
3638 rrd_set_error("missing filename");
3642 if (strlen(argv[optind]) >= MAXPATH) {
3643 rrd_set_error("filename (including path) too long");
3644 rrd_info_free(im.grinfo);
3649 strncpy(im.graphfile, argv[optind], MAXPATH - 1);
3650 im.graphfile[MAXPATH - 1] = '\0';
3652 if (strcmp(im.graphfile, "-") == 0) {
3653 im.graphfile[0] = '\0';
3656 rrd_graph_script(argc, argv, &im, 1);
3657 if (rrd_test_error()) {
3658 rrd_info_free(im.grinfo);
3663 /* Everything is now read and the actual work can start */
3665 if (graph_paint(&im) == -1) {
3666 rrd_info_free(im.grinfo);
3672 /* The image is generated and needs to be output.
3673 ** Also, if needed, print a line with information about the image.
3681 path = strdup(im.graphfile);
3682 filename = basename(path);
3684 sprintf_alloc(im.imginfo,
3687 im.ximg), (long) (im.zoom * im.yimg));
3688 grinfo_push(&im, sprintf_alloc("image_info"), RD_I_STR, info);
3692 if (im.rendered_image) {
3695 img.u_blo.size = im.rendered_image_size;
3696 img.u_blo.ptr = im.rendered_image;
3697 grinfo_push(&im, sprintf_alloc("image"), RD_I_BLO, img);
3706 image_desc_t *im,int prop,char *font, double size ){
3708 strncpy(im->text_prop[prop].font, font, sizeof(text_prop[prop].font) - 1);
3709 im->text_prop[prop].font[sizeof(text_prop[prop].font) - 1] = '\0';
3710 im->text_prop[prop].font_desc = pango_font_description_from_string( font );
3713 im->text_prop[prop].size = size;
3715 if (im->text_prop[prop].font_desc && im->text_prop[prop].size ){
3716 pango_font_description_set_size(im->text_prop[prop].font_desc, im->text_prop[prop].size * PANGO_SCALE);
3720 void rrd_graph_init(
3725 char *deffont = getenv("RRD_DEFAULT_FONT");
3726 static PangoFontMap *fontmap = NULL;
3727 PangoContext *context;
3732 #ifdef HAVE_SETLOCALE
3733 setlocale(LC_TIME, "");
3734 #ifdef HAVE_MBSTOWCS
3735 setlocale(LC_CTYPE, "");
3739 im->daemon_addr = NULL;
3740 im->draw_x_grid = 1;
3741 im->draw_y_grid = 1;
3742 im->extra_flags = 0;
3743 im->font_options = cairo_font_options_create();
3744 im->forceleftspace = 0;
3747 im->graph_antialias = CAIRO_ANTIALIAS_GRAY;
3748 im->grid_dash_off = 1;
3749 im->grid_dash_on = 1;
3751 im->grinfo = (rrd_info_t *) NULL;
3752 im->grinfo_current = (rrd_info_t *) NULL;
3753 im->imgformat = IF_PNG;
3756 im->logarithmic = 0;
3762 im->rendered_image_size = 0;
3763 im->rendered_image = NULL;
3767 im->tabwidth = 40.0;
3768 im->title[0] = '\0';
3769 im->unitsexponent = 9999;
3770 im->unitslength = 6;
3771 im->viewfactor = 1.0;
3772 im->watermark[0] = '\0';
3773 im->with_markup = 0;
3775 im->xlab_user.minsec = -1;
3778 im->ygridstep = DNAN;
3780 im->ylegend[0] = '\0';
3785 im->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
3786 im->cr = cairo_create(im->surface);
3788 for (i = 0; i < DIM(text_prop); i++) {
3789 im->text_prop[i].size = -1;
3790 rrd_set_font_desc(im,i, deffont ? deffont : text_prop[i].font,text_prop[i].size);
3793 if (fontmap == NULL){
3794 fontmap = pango_cairo_font_map_get_default();
3797 context = pango_cairo_font_map_create_context((PangoCairoFontMap*)fontmap);
3799 pango_cairo_context_set_resolution(context, 100);
3801 pango_cairo_update_context(im->cr,context);
3803 im->layout = pango_layout_new(context);
3805 // im->layout = pango_cairo_create_layout(im->cr);
3808 cairo_font_options_set_hint_style
3809 (im->font_options, CAIRO_HINT_STYLE_FULL);
3810 cairo_font_options_set_hint_metrics
3811 (im->font_options, CAIRO_HINT_METRICS_ON);
3812 cairo_font_options_set_antialias(im->font_options, CAIRO_ANTIALIAS_GRAY);
3816 for (i = 0; i < DIM(graph_col); i++)
3817 im->graph_col[i] = graph_col[i];
3823 void rrd_graph_options(
3830 char *parsetime_error = NULL;
3831 char scan_gtm[12], scan_mtm[12], scan_ltm[12], col_nam[12];
3832 time_t start_tmp = 0, end_tmp = 0;
3834 rrd_time_value_t start_tv, end_tv;
3835 long unsigned int color;
3836 char *old_locale = "";
3838 /* defines for long options without a short equivalent. should be bytes,
3839 and may not collide with (the ASCII value of) short options */
3840 #define LONGOPT_UNITS_SI 255
3843 struct option long_options[] = {
3844 { "start", required_argument, 0, 's'},
3845 { "end", required_argument, 0, 'e'},
3846 { "x-grid", required_argument, 0, 'x'},
3847 { "y-grid", required_argument, 0, 'y'},
3848 { "vertical-label", required_argument, 0, 'v'},
3849 { "width", required_argument, 0, 'w'},
3850 { "height", required_argument, 0, 'h'},
3851 { "full-size-mode", no_argument, 0, 'D'},
3852 { "interlaced", no_argument, 0, 'i'},
3853 { "upper-limit", required_argument, 0, 'u'},
3854 { "lower-limit", required_argument, 0, 'l'},
3855 { "rigid", no_argument, 0, 'r'},
3856 { "base", required_argument, 0, 'b'},
3857 { "logarithmic", no_argument, 0, 'o'},
3858 { "color", required_argument, 0, 'c'},
3859 { "font", required_argument, 0, 'n'},
3860 { "title", required_argument, 0, 't'},
3861 { "imginfo", required_argument, 0, 'f'},
3862 { "imgformat", required_argument, 0, 'a'},
3863 { "lazy", no_argument, 0, 'z'},
3864 { "zoom", required_argument, 0, 'm'},
3865 { "no-legend", no_argument, 0, 'g'},
3866 { "force-rules-legend", no_argument, 0, 'F'},
3867 { "only-graph", no_argument, 0, 'j'},
3868 { "alt-y-grid", no_argument, 0, 'Y'},
3869 { "no-minor", no_argument, 0, 'I'},
3870 { "slope-mode", no_argument, 0, 'E'},
3871 { "alt-autoscale", no_argument, 0, 'A'},
3872 { "alt-autoscale-min", no_argument, 0, 'J'},
3873 { "alt-autoscale-max", no_argument, 0, 'M'},
3874 { "no-gridfit", no_argument, 0, 'N'},
3875 { "units-exponent", required_argument, 0, 'X'},
3876 { "units-length", required_argument, 0, 'L'},
3877 { "units", required_argument, 0, LONGOPT_UNITS_SI},
3878 { "step", required_argument, 0, 'S'},
3879 { "tabwidth", required_argument, 0, 'T'},
3880 { "font-render-mode", required_argument, 0, 'R'},
3881 { "graph-render-mode", required_argument, 0, 'G'},
3882 { "font-smoothing-threshold", required_argument, 0, 'B'},
3883 { "watermark", required_argument, 0, 'W'},
3884 { "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 */
3885 { "pango-markup", no_argument, 0, 'P'},
3886 { "daemon", required_argument, 0, 'd'},
3892 opterr = 0; /* initialize getopt */
3893 rrd_parsetime("end-24h", &start_tv);
3894 rrd_parsetime("now", &end_tv);
3896 int option_index = 0;
3898 int col_start, col_end;
3900 opt = getopt_long(argc, argv,
3901 "s:e:x:y:v:w:h:D:iu:l:rb:oc:n:m:t:f:a:I:zgjFYAMEX:L:S:T:NR:B:W:kPd:",
3902 long_options, &option_index);
3907 im->extra_flags |= NOMINOR;
3910 im->extra_flags |= ALTYGRID;
3913 im->extra_flags |= ALTAUTOSCALE;
3916 im->extra_flags |= ALTAUTOSCALE_MIN;
3919 im->extra_flags |= ALTAUTOSCALE_MAX;
3922 im->extra_flags |= ONLY_GRAPH;
3925 im->extra_flags |= NOLEGEND;
3928 im->extra_flags |= FORCE_RULES_LEGEND;
3930 case LONGOPT_UNITS_SI:
3931 if (im->extra_flags & FORCE_UNITS) {
3932 rrd_set_error("--units can only be used once!");
3933 setlocale(LC_NUMERIC, old_locale);
3936 if (strcmp(optarg, "si") == 0)
3937 im->extra_flags |= FORCE_UNITS_SI;
3939 rrd_set_error("invalid argument for --units: %s", optarg);
3944 im->unitsexponent = atoi(optarg);
3947 im->unitslength = atoi(optarg);
3948 im->forceleftspace = 1;
3951 old_locale = setlocale(LC_NUMERIC, "C");
3952 im->tabwidth = atof(optarg);
3953 setlocale(LC_NUMERIC, old_locale);
3956 old_locale = setlocale(LC_NUMERIC, "C");
3957 im->step = atoi(optarg);
3958 setlocale(LC_NUMERIC, old_locale);
3964 im->with_markup = 1;
3967 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
3968 rrd_set_error("start time: %s", parsetime_error);
3973 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
3974 rrd_set_error("end time: %s", parsetime_error);
3979 if (strcmp(optarg, "none") == 0) {
3980 im->draw_x_grid = 0;
3984 "%10[A-Z]:%ld:%10[A-Z]:%ld:%10[A-Z]:%ld:%ld:%n",
3986 &im->xlab_user.gridst,
3988 &im->xlab_user.mgridst,
3990 &im->xlab_user.labst,
3991 &im->xlab_user.precis, &stroff) == 7 && stroff != 0) {
3992 strncpy(im->xlab_form, optarg + stroff,
3993 sizeof(im->xlab_form) - 1);
3994 im->xlab_form[sizeof(im->xlab_form) - 1] = '\0';
3996 (im->xlab_user.gridtm = tmt_conv(scan_gtm)) == -1) {
3997 rrd_set_error("unknown keyword %s", scan_gtm);
4000 (im->xlab_user.mgridtm = tmt_conv(scan_mtm))
4002 rrd_set_error("unknown keyword %s", scan_mtm);
4005 (im->xlab_user.labtm = tmt_conv(scan_ltm)) == -1) {
4006 rrd_set_error("unknown keyword %s", scan_ltm);
4009 im->xlab_user.minsec = 1;
4010 im->xlab_user.stst = im->xlab_form;
4012 rrd_set_error("invalid x-grid format");
4018 if (strcmp(optarg, "none") == 0) {
4019 im->draw_y_grid = 0;
4022 old_locale = setlocale(LC_NUMERIC, "C");
4023 if (sscanf(optarg, "%lf:%d", &im->ygridstep, &im->ylabfact) == 2) {
4024 setlocale(LC_NUMERIC, old_locale);
4025 if (im->ygridstep <= 0) {
4026 rrd_set_error("grid step must be > 0");
4028 } else if (im->ylabfact < 1) {
4029 rrd_set_error("label factor must be > 0");
4033 setlocale(LC_NUMERIC, old_locale);
4034 rrd_set_error("invalid y-grid format");
4039 strncpy(im->ylegend, optarg, 150);
4040 im->ylegend[150] = '\0';
4043 old_locale = setlocale(LC_NUMERIC, "C");
4044 im->maxval = atof(optarg);
4045 setlocale(LC_NUMERIC, old_locale);
4048 old_locale = setlocale(LC_NUMERIC, "C");
4049 im->minval = atof(optarg);
4050 setlocale(LC_NUMERIC, old_locale);
4053 im->base = atol(optarg);
4054 if (im->base != 1024 && im->base != 1000) {
4056 ("the only sensible value for base apart from 1000 is 1024");
4061 long_tmp = atol(optarg);
4062 if (long_tmp < 10) {
4063 rrd_set_error("width below 10 pixels");
4066 im->xsize = long_tmp;
4069 long_tmp = atol(optarg);
4070 if (long_tmp < 10) {
4071 rrd_set_error("height below 10 pixels");
4074 im->ysize = long_tmp;
4077 im->extra_flags |= FULL_SIZE_MODE;
4080 /* interlaced png not supported at the moment */
4086 im->imginfo = optarg;
4090 (im->imgformat = if_conv(optarg)) == -1) {
4091 rrd_set_error("unsupported graphics format '%s'", optarg);
4102 im->logarithmic = 1;
4106 "%10[A-Z]#%n%8lx%n",
4107 col_nam, &col_start, &color, &col_end) == 2) {
4109 int col_len = col_end - col_start;
4114 (((color & 0xF00) * 0x110000) | ((color & 0x0F0) *
4122 (((color & 0xF000) *
4123 0x11000) | ((color & 0x0F00) *
4124 0x01100) | ((color &
4127 ((color & 0x000F) * 0x00011)
4131 color = (color << 8) + 0xff /* shift left by 8 */ ;
4136 rrd_set_error("the color format is #RRGGBB[AA]");
4139 if ((ci = grc_conv(col_nam)) != -1) {
4140 im->graph_col[ci] = gfx_hex_to_col(color);
4142 rrd_set_error("invalid color name '%s'", col_nam);
4146 rrd_set_error("invalid color def format");
4155 old_locale = setlocale(LC_NUMERIC, "C");
4156 if (sscanf(optarg, "%10[A-Z]:%lf%n", prop, &size, &end) >= 2) {
4157 int sindex, propidx;
4159 setlocale(LC_NUMERIC, old_locale);
4160 if ((sindex = text_prop_conv(prop)) != -1) {
4161 for (propidx = sindex;
4162 propidx < TEXT_PROP_LAST; propidx++) {
4164 rrd_set_font_desc(im,propidx,NULL,size);
4166 if ((int) strlen(optarg) > end+2) {
4167 if (optarg[end] == ':') {
4168 rrd_set_font_desc(im,propidx,optarg + end + 1,0);
4171 ("expected : after font size in '%s'",
4176 /* only run the for loop for DEFAULT (0) for
4177 all others, we break here. woodo programming */
4178 if (propidx == sindex && sindex != 0)
4182 rrd_set_error("invalid fonttag '%s'", prop);
4186 setlocale(LC_NUMERIC, old_locale);
4187 rrd_set_error("invalid text property format");
4193 old_locale = setlocale(LC_NUMERIC, "C");
4194 im->zoom = atof(optarg);
4195 setlocale(LC_NUMERIC, old_locale);
4196 if (im->zoom <= 0.0) {
4197 rrd_set_error("zoom factor must be > 0");
4202 strncpy(im->title, optarg, 150);
4203 im->title[150] = '\0';
4206 if (strcmp(optarg, "normal") == 0) {
4207 cairo_font_options_set_antialias
4208 (im->font_options, CAIRO_ANTIALIAS_GRAY);
4209 cairo_font_options_set_hint_style
4210 (im->font_options, CAIRO_HINT_STYLE_FULL);
4211 } else if (strcmp(optarg, "light") == 0) {
4212 cairo_font_options_set_antialias
4213 (im->font_options, CAIRO_ANTIALIAS_GRAY);
4214 cairo_font_options_set_hint_style
4215 (im->font_options, CAIRO_HINT_STYLE_SLIGHT);
4216 } else if (strcmp(optarg, "mono") == 0) {
4217 cairo_font_options_set_antialias
4218 (im->font_options, CAIRO_ANTIALIAS_NONE);
4219 cairo_font_options_set_hint_style
4220 (im->font_options, CAIRO_HINT_STYLE_FULL);
4222 rrd_set_error("unknown font-render-mode '%s'", optarg);
4227 if (strcmp(optarg, "normal") == 0)
4228 im->graph_antialias = CAIRO_ANTIALIAS_GRAY;
4229 else if (strcmp(optarg, "mono") == 0)
4230 im->graph_antialias = CAIRO_ANTIALIAS_NONE;
4232 rrd_set_error("unknown graph-render-mode '%s'", optarg);
4237 /* not supported curently */
4240 strncpy(im->watermark, optarg, 100);
4241 im->watermark[99] = '\0';
4245 if (im->daemon_addr != NULL)
4247 rrd_set_error ("You cannot specify --daemon "
4252 im->daemon_addr = strdup(optarg);
4253 if (im->daemon_addr == NULL)
4255 rrd_set_error("strdup failed");
4263 rrd_set_error("unknown option '%c'", optopt);
4265 rrd_set_error("unknown option '%s'", argv[optind - 1]);
4270 { /* try to connect to rrdcached */
4271 int status = rrdc_connect(im->daemon_addr);
4272 if (status != 0) return;
4275 pango_cairo_context_set_font_options(pango_layout_get_context(im->layout), im->font_options);
4276 pango_layout_context_changed(im->layout);
4280 if (im->logarithmic && im->minval <= 0) {
4282 ("for a logarithmic yaxis you must specify a lower-limit > 0");
4286 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
4287 /* error string is set in rrd_parsetime.c */
4291 if (start_tmp < 3600 * 24 * 365 * 10) {
4293 ("the first entry to fetch should be after 1980 (%ld)",
4298 if (end_tmp < start_tmp) {
4300 ("start (%ld) should be less than end (%ld)", start_tmp, end_tmp);
4304 im->start = start_tmp;
4306 im->step = max((long) im->step, (im->end - im->start) / im->xsize);
4309 int rrd_graph_color(
4317 graph_desc_t *gdp = &im->gdes[im->gdes_c - 1];
4319 color = strstr(var, "#");
4320 if (color == NULL) {
4321 if (optional == 0) {
4322 rrd_set_error("Found no color in %s", err);
4329 long unsigned int col;
4331 rest = strstr(color, ":");
4338 sscanf(color, "#%6lx%n", &col, &n);
4339 col = (col << 8) + 0xff /* shift left by 8 */ ;
4341 rrd_set_error("Color problem in %s", err);
4344 sscanf(color, "#%8lx%n", &col, &n);
4348 rrd_set_error("Color problem in %s", err);
4350 if (rrd_test_error())
4352 gdp->col = gfx_hex_to_col(col);
4365 while (*ptr != '\0')
4366 if (*ptr++ == '%') {
4368 /* line cannot end with percent char */
4371 /* '%s', '%S' and '%%' are allowed */
4372 if (*ptr == 's' || *ptr == 'S' || *ptr == '%')
4374 /* %c is allowed (but use only with vdef!) */
4375 else if (*ptr == 'c') {
4380 /* or else '% 6.2lf' and such are allowed */
4382 /* optional padding character */
4383 if (*ptr == ' ' || *ptr == '+' || *ptr == '-')
4385 /* This should take care of 'm.n' with all three optional */
4386 while (*ptr >= '0' && *ptr <= '9')
4390 while (*ptr >= '0' && *ptr <= '9')
4392 /* Either 'le', 'lf' or 'lg' must follow here */
4395 if (*ptr == 'e' || *ptr == 'f' || *ptr == 'g')
4410 const char *const str)
4412 /* A VDEF currently is either "func" or "param,func"
4413 * so the parsing is rather simple. Change if needed.
4421 old_locale = setlocale(LC_NUMERIC, "C");
4422 sscanf(str, "%le,%29[A-Z]%n", ¶m, func, &n);
4423 setlocale(LC_NUMERIC, old_locale);
4424 if (n == (int) strlen(str)) { /* matched */
4428 sscanf(str, "%29[A-Z]%n", func, &n);
4429 if (n == (int) strlen(str)) { /* matched */
4433 ("Unknown function string '%s' in VDEF '%s'",
4438 if (!strcmp("PERCENT", func))
4439 gdes->vf.op = VDEF_PERCENT;
4440 else if (!strcmp("MAXIMUM", func))
4441 gdes->vf.op = VDEF_MAXIMUM;
4442 else if (!strcmp("AVERAGE", func))
4443 gdes->vf.op = VDEF_AVERAGE;
4444 else if (!strcmp("STDEV", func))
4445 gdes->vf.op = VDEF_STDEV;
4446 else if (!strcmp("MINIMUM", func))
4447 gdes->vf.op = VDEF_MINIMUM;
4448 else if (!strcmp("TOTAL", func))
4449 gdes->vf.op = VDEF_TOTAL;
4450 else if (!strcmp("FIRST", func))
4451 gdes->vf.op = VDEF_FIRST;
4452 else if (!strcmp("LAST", func))
4453 gdes->vf.op = VDEF_LAST;
4454 else if (!strcmp("LSLSLOPE", func))
4455 gdes->vf.op = VDEF_LSLSLOPE;
4456 else if (!strcmp("LSLINT", func))
4457 gdes->vf.op = VDEF_LSLINT;
4458 else if (!strcmp("LSLCORREL", func))
4459 gdes->vf.op = VDEF_LSLCORREL;
4462 ("Unknown function '%s' in VDEF '%s'\n", func, gdes->vname);
4465 switch (gdes->vf.op) {
4467 if (isnan(param)) { /* no parameter given */
4469 ("Function '%s' needs parameter in VDEF '%s'\n",
4473 if (param >= 0.0 && param <= 100.0) {
4474 gdes->vf.param = param;
4475 gdes->vf.val = DNAN; /* undefined */
4476 gdes->vf.when = 0; /* undefined */
4479 ("Parameter '%f' out of range in VDEF '%s'\n",
4480 param, gdes->vname);
4493 case VDEF_LSLCORREL:
4495 gdes->vf.param = DNAN;
4496 gdes->vf.val = DNAN;
4500 ("Function '%s' needs no parameter in VDEF '%s'\n",
4514 graph_desc_t *src, *dst;
4518 dst = &im->gdes[gdi];
4519 src = &im->gdes[dst->vidx];
4520 data = src->data + src->ds;
4522 steps = (src->end - src->start) / src->step;
4525 ("DEBUG: start == %lu, end == %lu, %lu steps\n",
4526 src->start, src->end, steps);
4528 switch (dst->vf.op) {
4532 if ((array = (rrd_value_t*)malloc(steps * sizeof(double))) == NULL) {
4533 rrd_set_error("malloc VDEV_PERCENT");
4536 for (step = 0; step < steps; step++) {
4537 array[step] = data[step * src->ds_cnt];
4539 qsort(array, step, sizeof(double), vdef_percent_compar);
4540 field = (steps - 1) * dst->vf.param / 100;
4541 dst->vf.val = array[field];
4542 dst->vf.when = 0; /* no time component */
4545 for (step = 0; step < steps; step++)
4546 printf("DEBUG: %3li:%10.2f %c\n",
4547 step, array[step], step == field ? '*' : ' ');
4553 while (step != steps && isnan(data[step * src->ds_cnt]))
4555 if (step == steps) {
4559 dst->vf.val = data[step * src->ds_cnt];
4560 dst->vf.when = src->start + (step + 1) * src->step;
4562 while (step != steps) {
4563 if (finite(data[step * src->ds_cnt])) {
4564 if (data[step * src->ds_cnt] > dst->vf.val) {
4565 dst->vf.val = data[step * src->ds_cnt];
4566 dst->vf.when = src->start + (step + 1) * src->step;
4577 double average = 0.0;
4579 for (step = 0; step < steps; step++) {
4580 if (finite(data[step * src->ds_cnt])) {
4581 sum += data[step * src->ds_cnt];
4586 if (dst->vf.op == VDEF_TOTAL) {
4587 dst->vf.val = sum * src->step;
4588 dst->vf.when = 0; /* no time component */
4589 } else if (dst->vf.op == VDEF_AVERAGE) {
4590 dst->vf.val = sum / cnt;
4591 dst->vf.when = 0; /* no time component */
4593 average = sum / cnt;
4595 for (step = 0; step < steps; step++) {
4596 if (finite(data[step * src->ds_cnt])) {
4597 sum += pow((data[step * src->ds_cnt] - average), 2.0);
4600 dst->vf.val = pow(sum / cnt, 0.5);
4601 dst->vf.when = 0; /* no time component */
4611 while (step != steps && isnan(data[step * src->ds_cnt]))
4613 if (step == steps) {
4617 dst->vf.val = data[step * src->ds_cnt];
4618 dst->vf.when = src->start + (step + 1) * src->step;
4620 while (step != steps) {
4621 if (finite(data[step * src->ds_cnt])) {
4622 if (data[step * src->ds_cnt] < dst->vf.val) {
4623 dst->vf.val = data[step * src->ds_cnt];
4624 dst->vf.when = src->start + (step + 1) * src->step;
4631 /* The time value returned here is one step before the
4632 * actual time value. This is the start of the first
4636 while (step != steps && isnan(data[step * src->ds_cnt]))
4638 if (step == steps) { /* all entries were NaN */
4642 dst->vf.val = data[step * src->ds_cnt];
4643 dst->vf.when = src->start + step * src->step;
4647 /* The time value returned here is the
4648 * actual time value. This is the end of the last
4652 while (step >= 0 && isnan(data[step * src->ds_cnt]))
4654 if (step < 0) { /* all entries were NaN */
4658 dst->vf.val = data[step * src->ds_cnt];
4659 dst->vf.when = src->start + (step + 1) * src->step;
4664 case VDEF_LSLCORREL:{
4665 /* Bestfit line by linear least squares method */
4668 double SUMx, SUMy, SUMxy, SUMxx, SUMyy, slope, y_intercept, correl;
4675 for (step = 0; step < steps; step++) {
4676 if (finite(data[step * src->ds_cnt])) {
4679 SUMxx += step * step;
4680 SUMxy += step * data[step * src->ds_cnt];
4681 SUMy += data[step * src->ds_cnt];
4682 SUMyy += data[step * src->ds_cnt] * data[step * src->ds_cnt];
4686 slope = (SUMx * SUMy - cnt * SUMxy) / (SUMx * SUMx - cnt * SUMxx);
4687 y_intercept = (SUMy - slope * SUMx) / cnt;
4690 (SUMx * SUMy) / cnt) /
4692 (SUMx * SUMx) / cnt) * (SUMyy - (SUMy * SUMy) / cnt));
4694 if (dst->vf.op == VDEF_LSLSLOPE) {
4695 dst->vf.val = slope;
4697 } else if (dst->vf.op == VDEF_LSLINT) {
4698 dst->vf.val = y_intercept;
4700 } else if (dst->vf.op == VDEF_LSLCORREL) {
4701 dst->vf.val = correl;
4714 /* NaN < -INF < finite_values < INF */
4715 int vdef_percent_compar(
4721 /* Equality is not returned; this doesn't hurt except
4722 * (maybe) for a little performance.
4725 /* First catch NaN values. They are smallest */
4726 if (isnan(*(double *) a))
4728 if (isnan(*(double *) b))
4730 /* NaN doesn't reach this part so INF and -INF are extremes.
4731 * The sign from isinf() is compatible with the sign we return
4733 if (isinf(*(double *) a))
4734 return isinf(*(double *) a);
4735 if (isinf(*(double *) b))
4736 return isinf(*(double *) b);
4737 /* If we reach this, both values must be finite */
4738 if (*(double *) a < *(double *) b)
4747 rrd_info_type_t type,
4748 rrd_infoval_t value)
4750 im->grinfo_current = rrd_info_push(im->grinfo_current, key, type, value);
4751 if (im->grinfo == NULL) {
4752 im->grinfo = im->grinfo_current;