1 /****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 ****************************************************************************
4 * rrd__graph.c produce graphs from data in rrdfiles
5 ****************************************************************************/
20 #include "plbasename.h"
23 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
32 #ifdef HAVE_LANGINFO_H
36 #include "rrd_graph.h"
37 #include "rrd_client.h"
39 /* some constant definitions */
43 #ifndef RRD_DEFAULT_FONT
44 /* there is special code later to pick Cour.ttf when running on windows */
45 #define RRD_DEFAULT_FONT "DejaVu Sans Mono,Bitstream Vera Sans Mono,monospace,Courier"
48 text_prop_t text_prop[] = {
49 {8.0, RRD_DEFAULT_FONT,NULL}
51 {9.0, RRD_DEFAULT_FONT,NULL}
53 {7.0, RRD_DEFAULT_FONT,NULL}
55 {8.0, RRD_DEFAULT_FONT,NULL}
57 {8.0, RRD_DEFAULT_FONT,NULL} /* legend */
59 {5.5, RRD_DEFAULT_FONT,NULL} /* watermark */
63 {0, 0, TMT_SECOND, 30, TMT_MINUTE, 5, TMT_MINUTE, 5, 0, "%H:%M"}
65 {2, 0, TMT_MINUTE, 1, TMT_MINUTE, 5, TMT_MINUTE, 5, 0, "%H:%M"}
67 {5, 0, TMT_MINUTE, 2, TMT_MINUTE, 10, TMT_MINUTE, 10, 0, "%H:%M"}
69 {10, 0, TMT_MINUTE, 5, TMT_MINUTE, 20, TMT_MINUTE, 20, 0, "%H:%M"}
71 {30, 0, TMT_MINUTE, 10, TMT_HOUR, 1, TMT_HOUR, 1, 0, "%H:%M"}
73 {60, 0, TMT_MINUTE, 30, TMT_HOUR, 2, TMT_HOUR, 2, 0, "%H:%M"}
75 {60, 24 * 3600, TMT_MINUTE, 30, TMT_HOUR, 2, TMT_HOUR, 6, 0, "%a %H:%M"}
77 {180, 0, TMT_HOUR, 1, TMT_HOUR, 6, TMT_HOUR, 6, 0, "%H:%M"}
79 {180, 24 * 3600, TMT_HOUR, 1, TMT_HOUR, 6, TMT_HOUR, 12, 0, "%a %H:%M"}
81 /*{300, 0, TMT_HOUR,3, TMT_HOUR,12, TMT_HOUR,12, 12*3600,"%a %p"}, this looks silly */
82 {600, 0, TMT_HOUR, 6, TMT_DAY, 1, TMT_DAY, 1, 24 * 3600, "%a"}
84 {1200, 0, TMT_HOUR, 6, TMT_DAY, 1, TMT_DAY, 1, 24 * 3600, "%d"}
86 {1800, 0, TMT_HOUR, 12, TMT_DAY, 1, TMT_DAY, 2, 24 * 3600, "%a %d"}
88 {2400, 0, TMT_HOUR, 12, TMT_DAY, 1, TMT_DAY, 2, 24 * 3600, "%a"}
90 {3600, 0, TMT_DAY, 1, TMT_WEEK, 1, TMT_WEEK, 1, 7 * 24 * 3600, "Week %V"}
92 {3 * 3600, 0, TMT_WEEK, 1, TMT_MONTH, 1, TMT_WEEK, 2, 7 * 24 * 3600,
95 {6 * 3600, 0, TMT_MONTH, 1, TMT_MONTH, 1, TMT_MONTH, 1, 30 * 24 * 3600,
98 {48 * 3600, 0, TMT_MONTH, 1, TMT_MONTH, 3, TMT_MONTH, 3, 30 * 24 * 3600,
101 {315360, 0, TMT_MONTH, 3, TMT_YEAR, 1, TMT_YEAR, 1, 365 * 24 * 3600, "%Y"}
103 {10 * 24 * 3600, 0, TMT_YEAR, 1, TMT_YEAR, 1, TMT_YEAR, 1,
104 365 * 24 * 3600, "%y"}
106 {-1, 0, TMT_MONTH, 0, TMT_MONTH, 0, TMT_MONTH, 0, 0, ""}
109 /* sensible y label intervals ...*/
133 {20.0, {1, 5, 10, 20}
139 {100.0, {1, 2, 5, 10}
142 {200.0, {1, 5, 10, 20}
145 {500.0, {1, 2, 4, 10}
153 gfx_color_t graph_col[] = /* default colors */
155 {1.00, 1.00, 1.00, 1.00}, /* canvas */
156 {0.95, 0.95, 0.95, 1.00}, /* background */
157 {0.81, 0.81, 0.81, 1.00}, /* shade A */
158 {0.62, 0.62, 0.62, 1.00}, /* shade B */
159 {0.56, 0.56, 0.56, 0.75}, /* grid */
160 {0.87, 0.31, 0.31, 0.60}, /* major grid */
161 {0.00, 0.00, 0.00, 1.00}, /* font */
162 {0.50, 0.12, 0.12, 1.00}, /* arrow */
163 {0.12, 0.12, 0.12, 1.00}, /* axis */
164 {0.00, 0.00, 0.00, 1.00} /* frame */
171 # define DPRINT(x) (void)(printf x, printf("\n"))
177 /* initialize with xtr(im,0); */
185 pixie = (double) im->xsize / (double) (im->end - im->start);
188 return (int) ((double) im->xorigin + pixie * (mytime - im->start));
191 /* translate data values into y coordinates */
200 if (!im->logarithmic)
201 pixie = (double) im->ysize / (im->maxval - im->minval);
204 (double) im->ysize / (log10(im->maxval) - log10(im->minval));
206 } else if (!im->logarithmic) {
207 yval = im->yorigin - pixie * (value - im->minval);
209 if (value < im->minval) {
212 yval = im->yorigin - pixie * (log10(value) - log10(im->minval));
220 /* conversion function for symbolic entry names */
223 #define conv_if(VV,VVV) \
224 if (strcmp(#VV, string) == 0) return VVV ;
230 conv_if(PRINT, GF_PRINT);
231 conv_if(GPRINT, GF_GPRINT);
232 conv_if(COMMENT, GF_COMMENT);
233 conv_if(HRULE, GF_HRULE);
234 conv_if(VRULE, GF_VRULE);
235 conv_if(LINE, GF_LINE);
236 conv_if(AREA, GF_AREA);
237 conv_if(GRAD, GF_GRAD);
238 conv_if(STACK, GF_STACK);
239 conv_if(TICK, GF_TICK);
240 conv_if(TEXTALIGN, GF_TEXTALIGN);
241 conv_if(DEF, GF_DEF);
242 conv_if(CDEF, GF_CDEF);
243 conv_if(VDEF, GF_VDEF);
244 conv_if(XPORT, GF_XPORT);
245 conv_if(SHIFT, GF_SHIFT);
247 return (enum gf_en)(-1);
250 enum gfx_if_en if_conv(
254 conv_if(PNG, IF_PNG);
255 conv_if(SVG, IF_SVG);
256 conv_if(EPS, IF_EPS);
257 conv_if(PDF, IF_PDF);
259 return (enum gfx_if_en)(-1);
262 enum tmt_en tmt_conv(
266 conv_if(SECOND, TMT_SECOND);
267 conv_if(MINUTE, TMT_MINUTE);
268 conv_if(HOUR, TMT_HOUR);
269 conv_if(DAY, TMT_DAY);
270 conv_if(WEEK, TMT_WEEK);
271 conv_if(MONTH, TMT_MONTH);
272 conv_if(YEAR, TMT_YEAR);
273 return (enum tmt_en)(-1);
276 enum grc_en grc_conv(
280 conv_if(BACK, GRC_BACK);
281 conv_if(CANVAS, GRC_CANVAS);
282 conv_if(SHADEA, GRC_SHADEA);
283 conv_if(SHADEB, GRC_SHADEB);
284 conv_if(GRID, GRC_GRID);
285 conv_if(MGRID, GRC_MGRID);
286 conv_if(FONT, GRC_FONT);
287 conv_if(ARROW, GRC_ARROW);
288 conv_if(AXIS, GRC_AXIS);
289 conv_if(FRAME, GRC_FRAME);
291 return (enum grc_en)(-1);
294 enum text_prop_en text_prop_conv(
298 conv_if(DEFAULT, TEXT_PROP_DEFAULT);
299 conv_if(TITLE, TEXT_PROP_TITLE);
300 conv_if(AXIS, TEXT_PROP_AXIS);
301 conv_if(UNIT, TEXT_PROP_UNIT);
302 conv_if(LEGEND, TEXT_PROP_LEGEND);
303 conv_if(WATERMARK, TEXT_PROP_WATERMARK);
304 return (enum text_prop_en)(-1);
314 cairo_status_t status = (cairo_status_t) 0;
319 if (im->daemon_addr != NULL)
320 free(im->daemon_addr);
322 for (i = 0; i < (unsigned) im->gdes_c; i++) {
323 if (im->gdes[i].data_first) {
324 /* careful here, because a single pointer can occur several times */
325 free(im->gdes[i].data);
326 if (im->gdes[i].ds_namv) {
327 for (ii = 0; ii < im->gdes[i].ds_cnt; ii++)
328 free(im->gdes[i].ds_namv[ii]);
329 free(im->gdes[i].ds_namv);
332 /* free allocated memory used for dashed lines */
333 if (im->gdes[i].p_dashes != NULL)
334 free(im->gdes[i].p_dashes);
336 free(im->gdes[i].p_data);
337 free(im->gdes[i].rpnp);
340 if (im->font_options)
341 cairo_font_options_destroy(im->font_options);
344 status = cairo_status(im->cr);
345 cairo_destroy(im->cr);
347 if (im->rendered_image) {
348 free(im->rendered_image);
352 g_object_unref (im->layout);
356 cairo_surface_destroy(im->surface);
359 fprintf(stderr, "OOPS: Cairo has issues it can't even die: %s\n",
360 cairo_status_to_string(status));
365 /* find SI magnitude symbol for the given number*/
367 image_desc_t *im, /* image description */
373 char *symbol[] = { "a", /* 10e-18 Atto */
374 "f", /* 10e-15 Femto */
375 "p", /* 10e-12 Pico */
376 "n", /* 10e-9 Nano */
377 "u", /* 10e-6 Micro */
378 "m", /* 10e-3 Milli */
383 "T", /* 10e12 Tera */
384 "P", /* 10e15 Peta */
391 if (*value == 0.0 || isnan(*value)) {
395 sindex = floor(log(fabs(*value)) / log((double) im->base));
396 *magfact = pow((double) im->base, (double) sindex);
397 (*value) /= (*magfact);
399 if (sindex <= symbcenter && sindex >= -symbcenter) {
400 (*symb_ptr) = symbol[sindex + symbcenter];
407 static char si_symbol[] = {
408 'a', /* 10e-18 Atto */
409 'f', /* 10e-15 Femto */
410 'p', /* 10e-12 Pico */
411 'n', /* 10e-9 Nano */
412 'u', /* 10e-6 Micro */
413 'm', /* 10e-3 Milli */
418 'T', /* 10e12 Tera */
419 'P', /* 10e15 Peta */
422 static const int si_symbcenter = 6;
424 /* find SI magnitude symbol for the numbers on the y-axis*/
426 image_desc_t *im /* image description */
430 double digits, viewdigits = 0;
433 floor(log(max(fabs(im->minval), fabs(im->maxval))) /
434 log((double) im->base));
436 if (im->unitsexponent != 9999) {
437 /* unitsexponent = 9, 6, 3, 0, -3, -6, -9, etc */
438 viewdigits = floor((double)(im->unitsexponent / 3));
443 im->magfact = pow((double) im->base, digits);
446 printf("digits %6.3f im->magfact %6.3f\n", digits, im->magfact);
449 im->viewfactor = im->magfact / pow((double) im->base, viewdigits);
451 if (((viewdigits + si_symbcenter) < sizeof(si_symbol)) &&
452 ((viewdigits + si_symbcenter) >= 0))
453 im->symbol = si_symbol[(int) viewdigits + si_symbcenter];
458 /* move min and max values around to become sensible */
463 double sensiblevalues[] = { 1000.0, 900.0, 800.0, 750.0, 700.0,
464 600.0, 500.0, 400.0, 300.0, 250.0,
465 200.0, 125.0, 100.0, 90.0, 80.0,
466 75.0, 70.0, 60.0, 50.0, 40.0, 30.0,
467 25.0, 20.0, 10.0, 9.0, 8.0,
468 7.0, 6.0, 5.0, 4.0, 3.5, 3.0,
469 2.5, 2.0, 1.8, 1.5, 1.2, 1.0,
470 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0, -1
473 double scaled_min, scaled_max;
480 printf("Min: %6.2f Max: %6.2f MagFactor: %6.2f\n",
481 im->minval, im->maxval, im->magfact);
484 if (isnan(im->ygridstep)) {
485 if (im->extra_flags & ALTAUTOSCALE) {
486 /* measure the amplitude of the function. Make sure that
487 graph boundaries are slightly higher then max/min vals
488 so we can see amplitude on the graph */
491 delt = im->maxval - im->minval;
493 fact = 2.0 * pow(10.0,
495 (max(fabs(im->minval), fabs(im->maxval)) /
498 adj = (fact - delt) * 0.55;
501 ("Min: %6.2f Max: %6.2f delt: %6.2f fact: %6.2f adj: %6.2f\n",
502 im->minval, im->maxval, delt, fact, adj);
507 } else if (im->extra_flags & ALTAUTOSCALE_MIN) {
508 /* measure the amplitude of the function. Make sure that
509 graph boundaries are slightly lower than min vals
510 so we can see amplitude on the graph */
511 adj = (im->maxval - im->minval) * 0.1;
513 } else if (im->extra_flags & ALTAUTOSCALE_MAX) {
514 /* measure the amplitude of the function. Make sure that
515 graph boundaries are slightly higher than max vals
516 so we can see amplitude on the graph */
517 adj = (im->maxval - im->minval) * 0.1;
520 scaled_min = im->minval / im->magfact;
521 scaled_max = im->maxval / im->magfact;
523 for (i = 1; sensiblevalues[i] > 0; i++) {
524 if (sensiblevalues[i - 1] >= scaled_min &&
525 sensiblevalues[i] <= scaled_min)
526 im->minval = sensiblevalues[i] * (im->magfact);
528 if (-sensiblevalues[i - 1] <= scaled_min &&
529 -sensiblevalues[i] >= scaled_min)
530 im->minval = -sensiblevalues[i - 1] * (im->magfact);
532 if (sensiblevalues[i - 1] >= scaled_max &&
533 sensiblevalues[i] <= scaled_max)
534 im->maxval = sensiblevalues[i - 1] * (im->magfact);
536 if (-sensiblevalues[i - 1] <= scaled_max &&
537 -sensiblevalues[i] >= scaled_max)
538 im->maxval = -sensiblevalues[i] * (im->magfact);
542 /* adjust min and max to the grid definition if there is one */
543 im->minval = (double) im->ylabfact * im->ygridstep *
544 floor(im->minval / ((double) im->ylabfact * im->ygridstep));
545 im->maxval = (double) im->ylabfact * im->ygridstep *
546 ceil(im->maxval / ((double) im->ylabfact * im->ygridstep));
550 fprintf(stderr, "SCALED Min: %6.2f Max: %6.2f Factor: %6.2f\n",
551 im->minval, im->maxval, im->magfact);
559 if (isnan(im->minval) || isnan(im->maxval))
562 if (im->logarithmic) {
563 double ya, yb, ypix, ypixfrac;
564 double log10_range = log10(im->maxval) - log10(im->minval);
566 ya = pow((double) 10, floor(log10(im->minval)));
567 while (ya < im->minval)
570 return; /* don't have y=10^x gridline */
572 if (yb <= im->maxval) {
573 /* we have at least 2 y=10^x gridlines.
574 Make sure distance between them in pixels
575 are an integer by expanding im->maxval */
576 double y_pixel_delta = ytr(im, ya) - ytr(im, yb);
577 double factor = y_pixel_delta / floor(y_pixel_delta);
578 double new_log10_range = factor * log10_range;
579 double new_ymax_log10 = log10(im->minval) + new_log10_range;
581 im->maxval = pow(10, new_ymax_log10);
582 ytr(im, DNAN); /* reset precalc */
583 log10_range = log10(im->maxval) - log10(im->minval);
585 /* make sure first y=10^x gridline is located on
586 integer pixel position by moving scale slightly
587 downwards (sub-pixel movement) */
588 ypix = ytr(im, ya) + im->ysize; /* add im->ysize so it always is positive */
589 ypixfrac = ypix - floor(ypix);
590 if (ypixfrac > 0 && ypixfrac < 1) {
591 double yfrac = ypixfrac / im->ysize;
593 im->minval = pow(10, log10(im->minval) - yfrac * log10_range);
594 im->maxval = pow(10, log10(im->maxval) - yfrac * log10_range);
595 ytr(im, DNAN); /* reset precalc */
598 /* Make sure we have an integer pixel distance between
599 each minor gridline */
600 double ypos1 = ytr(im, im->minval);
601 double ypos2 = ytr(im, im->minval + im->ygrid_scale.gridstep);
602 double y_pixel_delta = ypos1 - ypos2;
603 double factor = y_pixel_delta / floor(y_pixel_delta);
604 double new_range = factor * (im->maxval - im->minval);
605 double gridstep = im->ygrid_scale.gridstep;
606 double minor_y, minor_y_px, minor_y_px_frac;
608 if (im->maxval > 0.0)
609 im->maxval = im->minval + new_range;
611 im->minval = im->maxval - new_range;
612 ytr(im, DNAN); /* reset precalc */
613 /* make sure first minor gridline is on integer pixel y coord */
614 minor_y = gridstep * floor(im->minval / gridstep);
615 while (minor_y < im->minval)
617 minor_y_px = ytr(im, minor_y) + im->ysize; /* ensure > 0 by adding ysize */
618 minor_y_px_frac = minor_y_px - floor(minor_y_px);
619 if (minor_y_px_frac > 0 && minor_y_px_frac < 1) {
620 double yfrac = minor_y_px_frac / im->ysize;
621 double range = im->maxval - im->minval;
623 im->minval = im->minval - yfrac * range;
624 im->maxval = im->maxval - yfrac * range;
625 ytr(im, DNAN); /* reset precalc */
627 calc_horizontal_grid(im); /* recalc with changed im->maxval */
631 /* reduce data reimplementation by Alex */
634 enum cf_en cf, /* which consolidation function ? */
635 unsigned long cur_step, /* step the data currently is in */
636 time_t *start, /* start, end and step as requested ... */
637 time_t *end, /* ... by the application will be ... */
638 unsigned long *step, /* ... adjusted to represent reality */
639 unsigned long *ds_cnt, /* number of data sources in file */
641 { /* two dimensional array containing the data */
642 int i, reduce_factor = ceil((double) (*step) / (double) cur_step);
643 unsigned long col, dst_row, row_cnt, start_offset, end_offset, skiprows =
645 rrd_value_t *srcptr, *dstptr;
647 (*step) = cur_step * reduce_factor; /* set new step size for reduced data */
650 row_cnt = ((*end) - (*start)) / cur_step;
656 printf("Reducing %lu rows with factor %i time %lu to %lu, step %lu\n",
657 row_cnt, reduce_factor, *start, *end, cur_step);
658 for (col = 0; col < row_cnt; col++) {
659 printf("time %10lu: ", *start + (col + 1) * cur_step);
660 for (i = 0; i < *ds_cnt; i++)
661 printf(" %8.2e", srcptr[*ds_cnt * col + i]);
666 /* We have to combine [reduce_factor] rows of the source
667 ** into one row for the destination. Doing this we also
668 ** need to take care to combine the correct rows. First
669 ** alter the start and end time so that they are multiples
670 ** of the new step time. We cannot reduce the amount of
671 ** time so we have to move the end towards the future and
672 ** the start towards the past.
674 end_offset = (*end) % (*step);
675 start_offset = (*start) % (*step);
677 /* If there is a start offset (which cannot be more than
678 ** one destination row), skip the appropriate number of
679 ** source rows and one destination row. The appropriate
680 ** number is what we do know (start_offset/cur_step) of
681 ** the new interval (*step/cur_step aka reduce_factor).
684 printf("start_offset: %lu end_offset: %lu\n", start_offset, end_offset);
685 printf("row_cnt before: %lu\n", row_cnt);
688 (*start) = (*start) - start_offset;
689 skiprows = reduce_factor - start_offset / cur_step;
690 srcptr += skiprows * *ds_cnt;
691 for (col = 0; col < (*ds_cnt); col++)
696 printf("row_cnt between: %lu\n", row_cnt);
699 /* At the end we have some rows that are not going to be
700 ** used, the amount is end_offset/cur_step
703 (*end) = (*end) - end_offset + (*step);
704 skiprows = end_offset / cur_step;
708 printf("row_cnt after: %lu\n", row_cnt);
711 /* Sanity check: row_cnt should be multiple of reduce_factor */
712 /* if this gets triggered, something is REALLY WRONG ... we die immediately */
714 if (row_cnt % reduce_factor) {
715 printf("SANITY CHECK: %lu rows cannot be reduced by %i \n",
716 row_cnt, reduce_factor);
717 printf("BUG in reduce_data()\n");
721 /* Now combine reduce_factor intervals at a time
722 ** into one interval for the destination.
725 for (dst_row = 0; (long int) row_cnt >= reduce_factor; dst_row++) {
726 for (col = 0; col < (*ds_cnt); col++) {
727 rrd_value_t newval = DNAN;
728 unsigned long validval = 0;
730 for (i = 0; i < reduce_factor; i++) {
731 if (isnan(srcptr[i * (*ds_cnt) + col])) {
736 newval = srcptr[i * (*ds_cnt) + col];
745 newval += srcptr[i * (*ds_cnt) + col];
748 newval = min(newval, srcptr[i * (*ds_cnt) + col]);
751 /* an interval contains a failure if any subintervals contained a failure */
753 newval = max(newval, srcptr[i * (*ds_cnt) + col]);
756 newval = srcptr[i * (*ds_cnt) + col];
782 srcptr += (*ds_cnt) * reduce_factor;
783 row_cnt -= reduce_factor;
785 /* If we had to alter the endtime, we didn't have enough
786 ** source rows to fill the last row. Fill it with NaN.
789 for (col = 0; col < (*ds_cnt); col++)
792 row_cnt = ((*end) - (*start)) / *step;
794 printf("Done reducing. Currently %lu rows, time %lu to %lu, step %lu\n",
795 row_cnt, *start, *end, *step);
796 for (col = 0; col < row_cnt; col++) {
797 printf("time %10lu: ", *start + (col + 1) * (*step));
798 for (i = 0; i < *ds_cnt; i++)
799 printf(" %8.2e", srcptr[*ds_cnt * col + i]);
806 /* get the data required for the graphs from the
815 /* pull the data from the rrd files ... */
816 for (i = 0; i < (int) im->gdes_c; i++) {
817 /* only GF_DEF elements fetch data */
818 if (im->gdes[i].gf != GF_DEF)
822 /* do we have it already ? */
823 for (ii = 0; ii < i; ii++) {
824 if (im->gdes[ii].gf != GF_DEF)
826 if ((strcmp(im->gdes[i].rrd, im->gdes[ii].rrd) == 0)
827 && (im->gdes[i].cf == im->gdes[ii].cf)
828 && (im->gdes[i].cf_reduce == im->gdes[ii].cf_reduce)
829 && (im->gdes[i].start_orig == im->gdes[ii].start_orig)
830 && (im->gdes[i].end_orig == im->gdes[ii].end_orig)
831 && (im->gdes[i].step_orig == im->gdes[ii].step_orig)) {
832 /* OK, the data is already there.
833 ** Just copy the header portion
835 im->gdes[i].start = im->gdes[ii].start;
836 im->gdes[i].end = im->gdes[ii].end;
837 im->gdes[i].step = im->gdes[ii].step;
838 im->gdes[i].ds_cnt = im->gdes[ii].ds_cnt;
839 im->gdes[i].ds_namv = im->gdes[ii].ds_namv;
840 im->gdes[i].data = im->gdes[ii].data;
841 im->gdes[i].data_first = 0;
848 unsigned long ft_step = im->gdes[i].step; /* ft_step will record what we got from fetch */
849 const char *rrd_daemon;
852 if (im->gdes[i].daemon[0] != 0)
853 rrd_daemon = im->gdes[i].daemon;
855 rrd_daemon = im->daemon_addr;
857 /* "daemon" may be NULL. ENV_RRDCACHED_ADDRESS is evaluated in that
858 * case. If "daemon" holds the same value as in the previous
859 * iteration, no actual new connection is established - the
860 * existing connection is re-used. */
861 rrdc_connect (rrd_daemon);
863 /* If connecting was successfull, use the daemon to query the data.
864 * If there is no connection, for example because no daemon address
865 * was specified, (try to) use the local file directly. */
866 if (rrdc_is_connected (rrd_daemon))
868 status = rrdc_fetch (im->gdes[i].rrd,
869 cf_to_string (im->gdes[i].cf),
874 &im->gdes[i].ds_namv,
881 if ((rrd_fetch_fn(im->gdes[i].rrd,
887 &im->gdes[i].ds_namv,
888 &im->gdes[i].data)) == -1) {
892 im->gdes[i].data_first = 1;
894 if (ft_step < im->gdes[i].step) {
895 reduce_data(im->gdes[i].cf_reduce,
900 &im->gdes[i].ds_cnt, &im->gdes[i].data);
902 im->gdes[i].step = ft_step;
906 /* lets see if the required data source is really there */
907 for (ii = 0; ii < (int) im->gdes[i].ds_cnt; ii++) {
908 if (strcmp(im->gdes[i].ds_namv[ii], im->gdes[i].ds_nam) == 0) {
912 if (im->gdes[i].ds == -1) {
913 rrd_set_error("No DS called '%s' in '%s'",
914 im->gdes[i].ds_nam, im->gdes[i].rrd);
922 /* evaluate the expressions in the CDEF functions */
924 /*************************************************************
926 *************************************************************/
928 long find_var_wrapper(
932 return find_var((image_desc_t *) arg1, key);
935 /* find gdes containing var*/
942 for (ii = 0; ii < im->gdes_c - 1; ii++) {
943 if ((im->gdes[ii].gf == GF_DEF
944 || im->gdes[ii].gf == GF_VDEF || im->gdes[ii].gf == GF_CDEF)
945 && (strcmp(im->gdes[ii].vname, key) == 0)) {
952 /* find the greatest common divisor for all the numbers
953 in the 0 terminated num array */
960 for (i = 0; num[i + 1] != 0; i++) {
962 rest = num[i] % num[i + 1];
968 /* return i==0?num[i]:num[i-1]; */
972 /* run the rpn calculator on all the VDEF and CDEF arguments */
979 long *steparray, rpi;
984 rpnstack_init(&rpnstack);
986 for (gdi = 0; gdi < im->gdes_c; gdi++) {
987 /* Look for GF_VDEF and GF_CDEF in the same loop,
988 * so CDEFs can use VDEFs and vice versa
990 switch (im->gdes[gdi].gf) {
994 graph_desc_t *vdp = &im->gdes[im->gdes[gdi].vidx];
996 /* remove current shift */
997 vdp->start -= vdp->shift;
998 vdp->end -= vdp->shift;
1001 if (im->gdes[gdi].shidx >= 0)
1002 vdp->shift = im->gdes[im->gdes[gdi].shidx].vf.val;
1005 vdp->shift = im->gdes[gdi].shval;
1007 /* normalize shift to multiple of consolidated step */
1008 vdp->shift = (vdp->shift / (long) vdp->step) * (long) vdp->step;
1011 vdp->start += vdp->shift;
1012 vdp->end += vdp->shift;
1016 /* A VDEF has no DS. This also signals other parts
1017 * of rrdtool that this is a VDEF value, not a CDEF.
1019 im->gdes[gdi].ds_cnt = 0;
1020 if (vdef_calc(im, gdi)) {
1021 rrd_set_error("Error processing VDEF '%s'",
1022 im->gdes[gdi].vname);
1023 rpnstack_free(&rpnstack);
1028 im->gdes[gdi].ds_cnt = 1;
1029 im->gdes[gdi].ds = 0;
1030 im->gdes[gdi].data_first = 1;
1031 im->gdes[gdi].start = 0;
1032 im->gdes[gdi].end = 0;
1037 /* Find the variables in the expression.
1038 * - VDEF variables are substituted by their values
1039 * and the opcode is changed into OP_NUMBER.
1040 * - CDEF variables are analized for their step size,
1041 * the lowest common denominator of all the step
1042 * sizes of the data sources involved is calculated
1043 * and the resulting number is the step size for the
1044 * resulting data source.
1046 for (rpi = 0; im->gdes[gdi].rpnp[rpi].op != OP_END; rpi++) {
1047 if (im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE ||
1048 im->gdes[gdi].rpnp[rpi].op == OP_PREV_OTHER) {
1049 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
1051 if (im->gdes[ptr].ds_cnt == 0) { /* this is a VDEF data source */
1054 ("DEBUG: inside CDEF '%s' processing VDEF '%s'\n",
1055 im->gdes[gdi].vname, im->gdes[ptr].vname);
1056 printf("DEBUG: value from vdef is %f\n",
1057 im->gdes[ptr].vf.val);
1059 im->gdes[gdi].rpnp[rpi].val = im->gdes[ptr].vf.val;
1060 im->gdes[gdi].rpnp[rpi].op = OP_NUMBER;
1061 } else { /* normal variables and PREF(variables) */
1063 /* add one entry to the array that keeps track of the step sizes of the
1064 * data sources going into the CDEF. */
1066 (long*)rrd_realloc(steparray,
1068 1) * sizeof(*steparray))) == NULL) {
1069 rrd_set_error("realloc steparray");
1070 rpnstack_free(&rpnstack);
1074 steparray[stepcnt - 1] = im->gdes[ptr].step;
1076 /* adjust start and end of cdef (gdi) so
1077 * that it runs from the latest start point
1078 * to the earliest endpoint of any of the
1079 * rras involved (ptr)
1082 if (im->gdes[gdi].start < im->gdes[ptr].start)
1083 im->gdes[gdi].start = im->gdes[ptr].start;
1085 if (im->gdes[gdi].end == 0 ||
1086 im->gdes[gdi].end > im->gdes[ptr].end)
1087 im->gdes[gdi].end = im->gdes[ptr].end;
1089 /* store pointer to the first element of
1090 * the rra providing data for variable,
1091 * further save step size and data source
1094 im->gdes[gdi].rpnp[rpi].data =
1095 im->gdes[ptr].data + im->gdes[ptr].ds;
1096 im->gdes[gdi].rpnp[rpi].step = im->gdes[ptr].step;
1097 im->gdes[gdi].rpnp[rpi].ds_cnt = im->gdes[ptr].ds_cnt;
1099 /* backoff the *.data ptr; this is done so
1100 * rpncalc() function doesn't have to treat
1101 * the first case differently
1103 } /* if ds_cnt != 0 */
1104 } /* if OP_VARIABLE */
1105 } /* loop through all rpi */
1107 /* move the data pointers to the correct period */
1108 for (rpi = 0; im->gdes[gdi].rpnp[rpi].op != OP_END; rpi++) {
1109 if (im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE ||
1110 im->gdes[gdi].rpnp[rpi].op == OP_PREV_OTHER) {
1111 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
1113 im->gdes[gdi].start - im->gdes[ptr].start;
1116 im->gdes[gdi].rpnp[rpi].data +=
1117 (diff / im->gdes[ptr].step) *
1118 im->gdes[ptr].ds_cnt;
1122 if (steparray == NULL) {
1123 rrd_set_error("rpn expressions without DEF"
1124 " or CDEF variables are not supported");
1125 rpnstack_free(&rpnstack);
1128 steparray[stepcnt] = 0;
1129 /* Now find the resulting step. All steps in all
1130 * used RRAs have to be visited
1132 im->gdes[gdi].step = lcd(steparray);
1134 if ((im->gdes[gdi].data = (rrd_value_t*)malloc(((im->gdes[gdi].end -
1135 im->gdes[gdi].start)
1136 / im->gdes[gdi].step)
1137 * sizeof(double))) == NULL) {
1138 rrd_set_error("malloc im->gdes[gdi].data");
1139 rpnstack_free(&rpnstack);
1143 /* Step through the new cdef results array and
1144 * calculate the values
1146 for (now = im->gdes[gdi].start + im->gdes[gdi].step;
1147 now <= im->gdes[gdi].end; now += im->gdes[gdi].step) {
1148 rpnp_t *rpnp = im->gdes[gdi].rpnp;
1150 /* 3rd arg of rpn_calc is for OP_VARIABLE lookups;
1151 * in this case we are advancing by timesteps;
1152 * we use the fact that time_t is a synonym for long
1154 if (rpn_calc(rpnp, &rpnstack, (long) now,
1155 im->gdes[gdi].data, ++dataidx) == -1) {
1156 /* rpn_calc sets the error string */
1157 rpnstack_free(&rpnstack);
1160 } /* enumerate over time steps within a CDEF */
1165 } /* enumerate over CDEFs */
1166 rpnstack_free(&rpnstack);
1170 /* from http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm */
1171 /* yes we are loosing precision by doing tos with floats instead of doubles
1172 but it seems more stable this way. */
1174 static int AlmostEqual2sComplement(
1180 int aInt = *(int *) &A;
1181 int bInt = *(int *) &B;
1184 /* Make sure maxUlps is non-negative and small enough that the
1185 default NAN won't compare as equal to anything. */
1187 /* assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024); */
1189 /* Make aInt lexicographically ordered as a twos-complement int */
1192 aInt = 0x80000000l - aInt;
1194 /* Make bInt lexicographically ordered as a twos-complement int */
1197 bInt = 0x80000000l - bInt;
1199 intDiff = abs(aInt - bInt);
1201 if (intDiff <= maxUlps)
1207 /* massage data so, that we get one value for each x coordinate in the graph */
1212 double pixstep = (double) (im->end - im->start)
1213 / (double) im->xsize; /* how much time
1214 passes in one pixel */
1216 double minval = DNAN, maxval = DNAN;
1218 unsigned long gr_time;
1220 /* memory for the processed data */
1221 for (i = 0; i < im->gdes_c; i++) {
1222 if ((im->gdes[i].gf == GF_LINE)
1223 || (im->gdes[i].gf == GF_AREA)
1224 || (im->gdes[i].gf == GF_TICK)
1225 || (im->gdes[i].gf == GF_GRAD)
1227 if ((im->gdes[i].p_data = (rrd_value_t*)malloc((im->xsize + 1)
1228 * sizeof(rrd_value_t))) == NULL) {
1229 rrd_set_error("malloc data_proc");
1235 for (i = 0; i < im->xsize; i++) { /* for each pixel */
1238 gr_time = im->start + pixstep * i; /* time of the current step */
1241 for (ii = 0; ii < im->gdes_c; ii++) {
1244 switch (im->gdes[ii].gf) {
1249 if (!im->gdes[ii].stack)
1251 value = im->gdes[ii].yrule;
1252 if (isnan(value) || (im->gdes[ii].gf == GF_TICK)) {
1253 /* The time of the data doesn't necessarily match
1254 ** the time of the graph. Beware.
1256 vidx = im->gdes[ii].vidx;
1257 if (im->gdes[vidx].gf == GF_VDEF) {
1258 value = im->gdes[vidx].vf.val;
1260 if (((long int) gr_time >=
1261 (long int) im->gdes[vidx].start)
1262 && ((long int) gr_time <
1263 (long int) im->gdes[vidx].end)) {
1264 value = im->gdes[vidx].data[(unsigned long)
1270 im->gdes[vidx].step)
1271 * im->gdes[vidx].ds_cnt +
1278 if (!isnan(value)) {
1280 im->gdes[ii].p_data[i] = paintval;
1281 /* GF_TICK: the data values are not
1282 ** relevant for min and max
1284 if (finite(paintval) && im->gdes[ii].gf != GF_TICK) {
1285 if ((isnan(minval) || paintval < minval) &&
1286 !(im->logarithmic && paintval <= 0.0))
1288 if (isnan(maxval) || paintval > maxval)
1292 im->gdes[ii].p_data[i] = DNAN;
1297 ("STACK should already be turned into LINE or AREA here");
1306 /* if min or max have not been asigned a value this is because
1307 there was no data in the graph ... this is not good ...
1308 lets set these to dummy values then ... */
1310 if (im->logarithmic) {
1311 if (isnan(minval) || isnan(maxval) || maxval <= 0) {
1312 minval = 0.0; /* catching this right away below */
1315 /* in logarithm mode, where minval is smaller or equal
1316 to 0 make the beast just way smaller than maxval */
1318 minval = maxval / 10e8;
1321 if (isnan(minval) || isnan(maxval)) {
1327 /* adjust min and max values given by the user */
1328 /* for logscale we add something on top */
1329 if (isnan(im->minval)
1330 || ((!im->rigid) && im->minval > minval)
1332 if (im->logarithmic)
1333 im->minval = minval / 2.0;
1335 im->minval = minval;
1337 if (isnan(im->maxval)
1338 || (!im->rigid && im->maxval < maxval)
1340 if (im->logarithmic)
1341 im->maxval = maxval * 2.0;
1343 im->maxval = maxval;
1346 /* make sure min is smaller than max */
1347 if (im->minval > im->maxval) {
1349 im->minval = 0.99 * im->maxval;
1351 im->minval = 1.01 * im->maxval;
1354 /* make sure min and max are not equal */
1355 if (AlmostEqual2sComplement(im->minval, im->maxval, 4)) {
1361 /* make sure min and max are not both zero */
1362 if (AlmostEqual2sComplement(im->maxval, 0, 4)) {
1369 static int find_first_weekday(void){
1370 static int first_weekday = -1;
1371 if (first_weekday == -1){
1372 #ifdef HAVE__NL_TIME_WEEK_1STDAY
1373 /* according to http://sourceware.org/ml/libc-locales/2009-q1/msg00011.html */
1374 long week_1stday_l = (long) nl_langinfo (_NL_TIME_WEEK_1STDAY);
1375 if (week_1stday_l == 19971130) first_weekday = 0; /* Sun */
1376 else if (week_1stday_l == 19971201) first_weekday = 1; /* Mon */
1377 else first_weekday = 1; /* we go for a monday default */
1382 return first_weekday;
1385 /* identify the point where the first gridline, label ... gets placed */
1387 time_t find_first_time(
1388 time_t start, /* what is the initial time */
1389 enum tmt_en baseint, /* what is the basic interval */
1390 long basestep /* how many if these do we jump a time */
1395 localtime_r(&start, &tm);
1399 tm. tm_sec -= tm.tm_sec % basestep;
1404 tm. tm_min -= tm.tm_min % basestep;
1410 tm. tm_hour -= tm.tm_hour % basestep;
1414 /* we do NOT look at the basestep for this ... */
1421 /* we do NOT look at the basestep for this ... */
1425 tm. tm_mday -= tm.tm_wday - find_first_weekday();
1427 if (tm.tm_wday == 0 && find_first_weekday() > 0)
1428 tm. tm_mday -= 7; /* we want the *previous* week */
1436 tm. tm_mon -= tm.tm_mon % basestep;
1447 tm.tm_year + 1900) %basestep;
1453 /* identify the point where the next gridline, label ... gets placed */
1454 time_t find_next_time(
1455 time_t current, /* what is the initial time */
1456 enum tmt_en baseint, /* what is the basic interval */
1457 long basestep /* how many if these do we jump a time */
1463 localtime_r(¤t, &tm);
1468 tm. tm_sec += basestep;
1472 tm. tm_min += basestep;
1476 tm. tm_hour += basestep;
1480 tm. tm_mday += basestep;
1484 tm. tm_mday += 7 * basestep;
1488 tm. tm_mon += basestep;
1492 tm. tm_year += basestep;
1494 madetime = mktime(&tm);
1495 } while (madetime == -1); /* this is necessary to skip impssible times
1496 like the daylight saving time skips */
1502 /* calculate values required for PRINT and GPRINT functions */
1507 long i, ii, validsteps;
1510 int graphelement = 0;
1513 double magfact = -1;
1518 /* wow initializing tmvdef is quite a task :-) */
1519 time_t now = time(NULL);
1521 localtime_r(&now, &tmvdef);
1522 for (i = 0; i < im->gdes_c; i++) {
1523 vidx = im->gdes[i].vidx;
1524 switch (im->gdes[i].gf) {
1527 /* PRINT and GPRINT can now print VDEF generated values.
1528 * There's no need to do any calculations on them as these
1529 * calculations were already made.
1531 if (im->gdes[vidx].gf == GF_VDEF) { /* simply use vals */
1532 printval = im->gdes[vidx].vf.val;
1533 localtime_r(&im->gdes[vidx].vf.when, &tmvdef);
1534 } else { /* need to calculate max,min,avg etcetera */
1535 max_ii = ((im->gdes[vidx].end - im->gdes[vidx].start)
1536 / im->gdes[vidx].step * im->gdes[vidx].ds_cnt);
1539 for (ii = im->gdes[vidx].ds;
1540 ii < max_ii; ii += im->gdes[vidx].ds_cnt) {
1541 if (!finite(im->gdes[vidx].data[ii]))
1543 if (isnan(printval)) {
1544 printval = im->gdes[vidx].data[ii];
1549 switch (im->gdes[i].cf) {
1553 case CF_DEVSEASONAL:
1557 printval += im->gdes[vidx].data[ii];
1560 printval = min(printval, im->gdes[vidx].data[ii]);
1564 printval = max(printval, im->gdes[vidx].data[ii]);
1567 printval = im->gdes[vidx].data[ii];
1570 if (im->gdes[i].cf == CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
1571 if (validsteps > 1) {
1572 printval = (printval / validsteps);
1575 } /* prepare printval */
1577 if ((percent_s = strstr(im->gdes[i].format, "%S")) != NULL) {
1578 /* Magfact is set to -1 upon entry to print_calc. If it
1579 * is still less than 0, then we need to run auto_scale.
1580 * Otherwise, put the value into the correct units. If
1581 * the value is 0, then do not set the symbol or magnification
1582 * so next the calculation will be performed again. */
1583 if (magfact < 0.0) {
1584 auto_scale(im, &printval, &si_symb, &magfact);
1585 if (printval == 0.0)
1588 printval /= magfact;
1590 *(++percent_s) = 's';
1591 } else if (strstr(im->gdes[i].format, "%s") != NULL) {
1592 auto_scale(im, &printval, &si_symb, &magfact);
1595 if (im->gdes[i].gf == GF_PRINT) {
1596 rrd_infoval_t prline;
1598 if (im->gdes[i].strftm) {
1599 prline.u_str = (char*)malloc((FMT_LEG_LEN + 2) * sizeof(char));
1600 strftime(prline.u_str,
1601 FMT_LEG_LEN, im->gdes[i].format, &tmvdef);
1602 } else if (bad_format(im->gdes[i].format)) {
1604 ("bad format for PRINT in '%s'", im->gdes[i].format);
1608 sprintf_alloc(im->gdes[i].format, printval, si_symb);
1612 ("print[%ld]", prline_cnt++), RD_I_STR, prline);
1617 if (im->gdes[i].strftm) {
1618 strftime(im->gdes[i].legend,
1619 FMT_LEG_LEN, im->gdes[i].format, &tmvdef);
1621 if (bad_format(im->gdes[i].format)) {
1623 ("bad format for GPRINT in '%s'",
1624 im->gdes[i].format);
1627 #ifdef HAVE_SNPRINTF
1628 snprintf(im->gdes[i].legend,
1630 im->gdes[i].format, printval, si_symb);
1632 sprintf(im->gdes[i].legend,
1633 im->gdes[i].format, printval, si_symb);
1646 if (isnan(im->gdes[i].yrule)) { /* we must set this here or the legend printer can not decide to print the legend */
1647 im->gdes[i].yrule = im->gdes[vidx].vf.val;
1652 if (im->gdes[i].xrule == 0) { /* again ... the legend printer needs it */
1653 im->gdes[i].xrule = im->gdes[vidx].vf.when;
1662 #ifdef WITH_PIECHART
1670 ("STACK should already be turned into LINE or AREA here");
1675 return graphelement;
1680 /* place legends with color spots */
1686 int interleg = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
1687 int border = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
1688 int fill = 0, fill_last;
1689 double legendwidth; // = im->ximg - 2 * border;
1691 double leg_x = border;
1692 int leg_y = 0; //im->yimg;
1693 int leg_y_prev = 0; // im->yimg;
1696 int i, ii, mark = 0;
1697 char default_txtalign = TXA_JUSTIFIED; /*default line orientation */
1700 char saved_legend[FMT_LEG_LEN + 5];
1706 legendwidth = im->legendwidth - 2 * border;
1710 if (!(im->extra_flags & NOLEGEND) && !(im->extra_flags & ONLY_GRAPH)) {
1711 if ((legspace = (int*)malloc(im->gdes_c * sizeof(int))) == NULL) {
1712 rrd_set_error("malloc for legspace");
1716 for (i = 0; i < im->gdes_c; i++) {
1717 char prt_fctn; /*special printfunctions */
1719 strcpy(saved_legend, im->gdes[i].legend);
1723 /* hide legends for rules which are not displayed */
1724 if (im->gdes[i].gf == GF_TEXTALIGN) {
1725 default_txtalign = im->gdes[i].txtalign;
1728 if (!(im->extra_flags & FORCE_RULES_LEGEND)) {
1729 if (im->gdes[i].gf == GF_HRULE
1730 && (im->gdes[i].yrule <
1731 im->minval || im->gdes[i].yrule > im->maxval))
1732 im->gdes[i].legend[0] = '\0';
1733 if (im->gdes[i].gf == GF_VRULE
1734 && (im->gdes[i].xrule <
1735 im->start || im->gdes[i].xrule > im->end))
1736 im->gdes[i].legend[0] = '\0';
1739 /* turn \\t into tab */
1740 while ((tab = strstr(im->gdes[i].legend, "\\t"))) {
1741 memmove(tab, tab + 1, strlen(tab));
1745 leg_cc = strlen(im->gdes[i].legend);
1746 /* is there a controle code at the end of the legend string ? */
1747 if (leg_cc >= 2 && im->gdes[i].legend[leg_cc - 2] == '\\') {
1748 prt_fctn = im->gdes[i].legend[leg_cc - 1];
1750 im->gdes[i].legend[leg_cc] = '\0';
1754 /* only valid control codes */
1755 if (prt_fctn != 'l' && prt_fctn != 'n' && /* a synonym for l */
1760 prt_fctn != 's' && prt_fctn != '\0' && prt_fctn != 'g') {
1763 ("Unknown control code at the end of '%s\\%c'",
1764 im->gdes[i].legend, prt_fctn);
1768 if (prt_fctn == 'n') {
1772 /* remove exess space from the end of the legend for \g */
1773 while (prt_fctn == 'g' &&
1774 leg_cc > 0 && im->gdes[i].legend[leg_cc - 1] == ' ') {
1776 im->gdes[i].legend[leg_cc] = '\0';
1781 /* no interleg space if string ends in \g */
1782 legspace[i] = (prt_fctn == 'g' ? 0 : interleg);
1784 fill += legspace[i];
1787 gfx_get_text_width(im,
1793 im->tabwidth, im->gdes[i].legend);
1798 /* who said there was a special tag ... ? */
1799 if (prt_fctn == 'g') {
1803 if (prt_fctn == '\0') {
1804 if(calc_width && (fill > legendwidth)){
1807 if (i == im->gdes_c - 1 || fill > legendwidth) {
1808 /* just one legend item is left right or center */
1809 switch (default_txtalign) {
1824 /* is it time to place the legends ? */
1825 if (fill > legendwidth) {
1833 if (leg_c == 1 && prt_fctn == 'j') {
1838 if (prt_fctn != '\0') {
1840 if (leg_c >= 2 && prt_fctn == 'j') {
1841 glue = (double)(legendwidth - fill) / (double)(leg_c - 1);
1845 if (prt_fctn == 'c')
1846 leg_x = (double)(legendwidth - fill) / 2.0;
1847 if (prt_fctn == 'r')
1848 leg_x = legendwidth - fill + border;
1849 for (ii = mark; ii <= i; ii++) {
1850 if (im->gdes[ii].legend[0] == '\0')
1851 continue; /* skip empty legends */
1852 im->gdes[ii].leg_x = leg_x;
1853 im->gdes[ii].leg_y = leg_y + border;
1855 (double)gfx_get_text_width(im, leg_x,
1860 im->tabwidth, im->gdes[ii].legend)
1861 +(double)legspace[ii]
1865 if (leg_x > border || prt_fctn == 's')
1866 leg_y += im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
1867 if (prt_fctn == 's')
1868 leg_y -= im->text_prop[TEXT_PROP_LEGEND].size;
1869 if (prt_fctn == 'u')
1870 leg_y -= im->text_prop[TEXT_PROP_LEGEND].size *1.8;
1872 if(calc_width && (fill > legendwidth)){
1881 strcpy(im->gdes[i].legend, saved_legend);
1886 im->legendwidth = legendwidth + 2 * border;
1889 im->legendheight = leg_y + border * 0.6;
1896 /* create a grid on the graph. it determines what to do
1897 from the values of xsize, start and end */
1899 /* the xaxis labels are determined from the number of seconds per pixel
1900 in the requested graph */
1902 int calc_horizontal_grid(
1910 int decimals, fractionals;
1912 im->ygrid_scale.labfact = 2;
1913 range = im->maxval - im->minval;
1914 scaledrange = range / im->magfact;
1915 /* does the scale of this graph make it impossible to put lines
1916 on it? If so, give up. */
1917 if (isnan(scaledrange)) {
1921 /* find grid spaceing */
1923 if (isnan(im->ygridstep)) {
1924 if (im->extra_flags & ALTYGRID) {
1925 /* find the value with max number of digits. Get number of digits */
1928 (max(fabs(im->maxval), fabs(im->minval)) *
1929 im->viewfactor / im->magfact));
1930 if (decimals <= 0) /* everything is small. make place for zero */
1932 im->ygrid_scale.gridstep =
1934 floor(log10(range * im->viewfactor / im->magfact))) /
1935 im->viewfactor * im->magfact;
1936 if (im->ygrid_scale.gridstep == 0) /* range is one -> 0.1 is reasonable scale */
1937 im->ygrid_scale.gridstep = 0.1;
1938 /* should have at least 5 lines but no more then 15 */
1939 if (range / im->ygrid_scale.gridstep < 5
1940 && im->ygrid_scale.gridstep >= 30)
1941 im->ygrid_scale.gridstep /= 10;
1942 if (range / im->ygrid_scale.gridstep > 15)
1943 im->ygrid_scale.gridstep *= 10;
1944 if (range / im->ygrid_scale.gridstep > 5) {
1945 im->ygrid_scale.labfact = 1;
1946 if (range / im->ygrid_scale.gridstep > 8
1947 || im->ygrid_scale.gridstep <
1948 1.8 * im->text_prop[TEXT_PROP_AXIS].size)
1949 im->ygrid_scale.labfact = 2;
1951 im->ygrid_scale.gridstep /= 5;
1952 im->ygrid_scale.labfact = 5;
1956 (im->ygrid_scale.gridstep *
1957 (double) im->ygrid_scale.labfact * im->viewfactor /
1959 if (fractionals < 0) { /* small amplitude. */
1960 int len = decimals - fractionals + 1;
1962 if (im->unitslength < len + 2)
1963 im->unitslength = len + 2;
1964 sprintf(im->ygrid_scale.labfmt,
1966 -fractionals, (im->symbol != ' ' ? " %c" : ""));
1968 int len = decimals + 1;
1970 if (im->unitslength < len + 2)
1971 im->unitslength = len + 2;
1972 sprintf(im->ygrid_scale.labfmt,
1973 "%%%d.0f%s", len, (im->symbol != ' ' ? " %c" : ""));
1975 } else { /* classic rrd grid */
1976 for (i = 0; ylab[i].grid > 0; i++) {
1977 pixel = im->ysize / (scaledrange / ylab[i].grid);
1983 for (i = 0; i < 4; i++) {
1984 if (pixel * ylab[gridind].lfac[i] >=
1985 1.8 * im->text_prop[TEXT_PROP_AXIS].size) {
1986 im->ygrid_scale.labfact = ylab[gridind].lfac[i];
1991 im->ygrid_scale.gridstep = ylab[gridind].grid * im->magfact;
1994 im->ygrid_scale.gridstep = im->ygridstep;
1995 im->ygrid_scale.labfact = im->ylabfact;
2000 int draw_horizontal_grid(
2006 char graph_label[100];
2008 double X0 = im->xorigin;
2009 double X1 = im->xorigin + im->xsize;
2010 int sgrid = (int) (im->minval / im->ygrid_scale.gridstep - 1);
2011 int egrid = (int) (im->maxval / im->ygrid_scale.gridstep + 1);
2013 double second_axis_magfact = 0;
2014 char *second_axis_symb = "";
2017 im->ygrid_scale.gridstep /
2018 (double) im->magfact * (double) im->viewfactor;
2019 MaxY = scaledstep * (double) egrid;
2020 for (i = sgrid; i <= egrid; i++) {
2022 im->ygrid_scale.gridstep * i);
2024 im->ygrid_scale.gridstep * (i + 1));
2026 if (floor(Y0 + 0.5) >=
2027 im->yorigin - im->ysize && floor(Y0 + 0.5) <= im->yorigin) {
2028 /* Make sure at least 2 grid labels are shown, even if it doesn't agree
2029 with the chosen settings. Add a label if required by settings, or if
2030 there is only one label so far and the next grid line is out of bounds. */
2031 if (i % im->ygrid_scale.labfact == 0
2033 && (YN < im->yorigin - im->ysize || YN > im->yorigin))) {
2034 if (im->symbol == ' ') {
2035 if (im->extra_flags & ALTYGRID) {
2036 sprintf(graph_label,
2037 im->ygrid_scale.labfmt,
2038 scaledstep * (double) i);
2041 sprintf(graph_label, "%4.1f",
2042 scaledstep * (double) i);
2044 sprintf(graph_label, "%4.0f",
2045 scaledstep * (double) i);
2049 char sisym = (i == 0 ? ' ' : im->symbol);
2051 if (im->extra_flags & ALTYGRID) {
2052 sprintf(graph_label,
2053 im->ygrid_scale.labfmt,
2054 scaledstep * (double) i, sisym);
2057 sprintf(graph_label, "%4.1f %c",
2058 scaledstep * (double) i, sisym);
2060 sprintf(graph_label, "%4.0f %c",
2061 scaledstep * (double) i, sisym);
2066 if (im->second_axis_scale != 0){
2067 char graph_label_right[100];
2068 double sval = im->ygrid_scale.gridstep*(double)i*im->second_axis_scale+im->second_axis_shift;
2069 if (im->second_axis_format[0] == '\0'){
2070 if (!second_axis_magfact){
2071 double dummy = im->ygrid_scale.gridstep*(double)(sgrid+egrid)/2.0*im->second_axis_scale+im->second_axis_shift;
2072 auto_scale(im,&dummy,&second_axis_symb,&second_axis_magfact);
2074 sval /= second_axis_magfact;
2077 sprintf(graph_label_right,"%5.1f %s",sval,second_axis_symb);
2079 sprintf(graph_label_right,"%5.0f %s",sval,second_axis_symb);
2083 sprintf(graph_label_right,im->second_axis_format,sval);
2087 im->graph_col[GRC_FONT],
2088 im->text_prop[TEXT_PROP_AXIS].font_desc,
2089 im->tabwidth,0.0, GFX_H_LEFT, GFX_V_CENTER,
2090 graph_label_right );
2096 text_prop[TEXT_PROP_AXIS].
2098 im->graph_col[GRC_FONT],
2100 text_prop[TEXT_PROP_AXIS].
2103 GFX_H_RIGHT, GFX_V_CENTER, graph_label);
2104 gfx_line(im, X0 - 2, Y0, X0, Y0,
2105 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2106 gfx_line(im, X1, Y0, X1 + 2, Y0,
2107 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2108 gfx_dashed_line(im, X0 - 2, Y0,
2114 im->grid_dash_on, im->grid_dash_off);
2115 } else if (!(im->extra_flags & NOMINOR)) {
2118 X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2119 gfx_line(im, X1, Y0, X1 + 2, Y0,
2120 GRIDWIDTH, im->graph_col[GRC_GRID]);
2121 gfx_dashed_line(im, X0 - 1, Y0,
2125 graph_col[GRC_GRID],
2126 im->grid_dash_on, im->grid_dash_off);
2133 /* this is frexp for base 10 */
2144 iexp = floor(log((double)fabs(x)) / log((double)10));
2145 mnt = x / pow(10.0, iexp);
2148 mnt = x / pow(10.0, iexp);
2155 /* logaritmic horizontal grid */
2156 int horizontal_log_grid(
2160 double yloglab[][10] = {
2162 1.0, 10., 0.0, 0.0, 0.0, 0.0, 0.0,
2164 1.0, 5.0, 10., 0.0, 0.0, 0.0, 0.0,
2166 1.0, 2.0, 5.0, 7.0, 10., 0.0, 0.0,
2183 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* last line */
2185 int i, j, val_exp, min_exp;
2186 double nex; /* number of decades in data */
2187 double logscale; /* scale in logarithmic space */
2188 int exfrac = 1; /* decade spacing */
2189 int mid = -1; /* row in yloglab for major grid */
2190 double mspac; /* smallest major grid spacing (pixels) */
2191 int flab; /* first value in yloglab to use */
2192 double value, tmp, pre_value;
2194 char graph_label[100];
2196 nex = log10(im->maxval / im->minval);
2197 logscale = im->ysize / nex;
2198 /* major spacing for data with high dynamic range */
2199 while (logscale * exfrac < 3 * im->text_prop[TEXT_PROP_LEGEND].size) {
2206 /* major spacing for less dynamic data */
2208 /* search best row in yloglab */
2210 for (i = 0; yloglab[mid][i + 1] < 10.0; i++);
2211 mspac = logscale * log10(10.0 / yloglab[mid][i]);
2214 2 * im->text_prop[TEXT_PROP_LEGEND].size && yloglab[mid][0] > 0);
2217 /* find first value in yloglab */
2219 yloglab[mid][flab] < 10
2220 && frexp10(im->minval, &tmp) > yloglab[mid][flab]; flab++);
2221 if (yloglab[mid][flab] == 10.0) {
2226 if (val_exp % exfrac)
2227 val_exp += abs(-val_exp % exfrac);
2229 X1 = im->xorigin + im->xsize;
2234 value = yloglab[mid][flab] * pow(10.0, val_exp);
2235 if (AlmostEqual2sComplement(value, pre_value, 4))
2236 break; /* it seems we are not converging */
2238 Y0 = ytr(im, value);
2239 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2241 /* major grid line */
2243 X0 - 2, Y0, X0, Y0, MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2244 gfx_line(im, X1, Y0, X1 + 2, Y0,
2245 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2246 gfx_dashed_line(im, X0 - 2, Y0,
2251 [GRC_MGRID], im->grid_dash_on, im->grid_dash_off);
2253 if (im->extra_flags & FORCE_UNITS_SI) {
2258 scale = floor(val_exp / 3.0);
2260 pvalue = pow(10.0, val_exp % 3);
2262 pvalue = pow(10.0, ((val_exp + 1) % 3) + 2);
2263 pvalue *= yloglab[mid][flab];
2264 if (((scale + si_symbcenter) < (int) sizeof(si_symbol))
2265 && ((scale + si_symbcenter) >= 0))
2266 symbol = si_symbol[scale + si_symbcenter];
2269 sprintf(graph_label, "%3.0f %c", pvalue, symbol);
2271 sprintf(graph_label, "%3.0e", value);
2273 if (im->second_axis_scale != 0){
2274 char graph_label_right[100];
2275 double sval = value*im->second_axis_scale+im->second_axis_shift;
2276 if (im->second_axis_format[0] == '\0'){
2277 if (im->extra_flags & FORCE_UNITS_SI) {
2280 auto_scale(im,&sval,&symb,&mfac);
2281 sprintf(graph_label_right,"%4.0f %s", sval,symb);
2284 sprintf(graph_label_right,"%3.0e", sval);
2288 sprintf(graph_label_right,im->second_axis_format,sval);
2293 im->graph_col[GRC_FONT],
2294 im->text_prop[TEXT_PROP_AXIS].font_desc,
2295 im->tabwidth,0.0, GFX_H_LEFT, GFX_V_CENTER,
2296 graph_label_right );
2302 text_prop[TEXT_PROP_AXIS].
2304 im->graph_col[GRC_FONT],
2306 text_prop[TEXT_PROP_AXIS].
2309 GFX_H_RIGHT, GFX_V_CENTER, graph_label);
2311 if (mid < 4 && exfrac == 1) {
2312 /* find first and last minor line behind current major line
2313 * i is the first line and j tha last */
2315 min_exp = val_exp - 1;
2316 for (i = 1; yloglab[mid][i] < 10.0; i++);
2317 i = yloglab[mid][i - 1] + 1;
2321 i = yloglab[mid][flab - 1] + 1;
2322 j = yloglab[mid][flab];
2325 /* draw minor lines below current major line */
2326 for (; i < j; i++) {
2328 value = i * pow(10.0, min_exp);
2329 if (value < im->minval)
2331 Y0 = ytr(im, value);
2332 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2337 X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2338 gfx_line(im, X1, Y0, X1 + 2, Y0,
2339 GRIDWIDTH, im->graph_col[GRC_GRID]);
2340 gfx_dashed_line(im, X0 - 1, Y0,
2344 graph_col[GRC_GRID],
2345 im->grid_dash_on, im->grid_dash_off);
2347 } else if (exfrac > 1) {
2348 for (i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
2349 value = pow(10.0, i);
2350 if (value < im->minval)
2352 Y0 = ytr(im, value);
2353 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2358 X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2359 gfx_line(im, X1, Y0, X1 + 2, Y0,
2360 GRIDWIDTH, im->graph_col[GRC_GRID]);
2361 gfx_dashed_line(im, X0 - 1, Y0,
2365 graph_col[GRC_GRID],
2366 im->grid_dash_on, im->grid_dash_off);
2371 if (yloglab[mid][++flab] == 10.0) {
2377 /* draw minor lines after highest major line */
2378 if (mid < 4 && exfrac == 1) {
2379 /* find first and last minor line below current major line
2380 * i is the first line and j tha last */
2382 min_exp = val_exp - 1;
2383 for (i = 1; yloglab[mid][i] < 10.0; i++);
2384 i = yloglab[mid][i - 1] + 1;
2388 i = yloglab[mid][flab - 1] + 1;
2389 j = yloglab[mid][flab];
2392 /* draw minor lines below current major line */
2393 for (; i < j; i++) {
2395 value = i * pow(10.0, min_exp);
2396 if (value < im->minval)
2398 Y0 = ytr(im, value);
2399 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2403 X0 - 2, Y0, X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2404 gfx_line(im, X1, Y0, X1 + 2, Y0,
2405 GRIDWIDTH, im->graph_col[GRC_GRID]);
2406 gfx_dashed_line(im, X0 - 1, Y0,
2410 graph_col[GRC_GRID],
2411 im->grid_dash_on, im->grid_dash_off);
2414 /* fancy minor gridlines */
2415 else if (exfrac > 1) {
2416 for (i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
2417 value = pow(10.0, i);
2418 if (value < im->minval)
2420 Y0 = ytr(im, value);
2421 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2425 X0 - 2, Y0, X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2426 gfx_line(im, X1, Y0, X1 + 2, Y0,
2427 GRIDWIDTH, im->graph_col[GRC_GRID]);
2428 gfx_dashed_line(im, X0 - 1, Y0,
2432 graph_col[GRC_GRID],
2433 im->grid_dash_on, im->grid_dash_off);
2444 int xlab_sel; /* which sort of label and grid ? */
2445 time_t ti, tilab, timajor;
2447 char graph_label[100];
2448 double X0, Y0, Y1; /* points for filled graph and more */
2451 /* the type of time grid is determined by finding
2452 the number of seconds per pixel in the graph */
2453 if (im->xlab_user.minsec == -1) {
2454 factor = (im->end - im->start) / im->xsize;
2456 while (xlab[xlab_sel + 1].minsec !=
2457 -1 && xlab[xlab_sel + 1].minsec <= factor) {
2459 } /* pick the last one */
2460 while (xlab[xlab_sel - 1].minsec ==
2461 xlab[xlab_sel].minsec
2462 && xlab[xlab_sel].length > (im->end - im->start)) {
2464 } /* go back to the smallest size */
2465 im->xlab_user.gridtm = xlab[xlab_sel].gridtm;
2466 im->xlab_user.gridst = xlab[xlab_sel].gridst;
2467 im->xlab_user.mgridtm = xlab[xlab_sel].mgridtm;
2468 im->xlab_user.mgridst = xlab[xlab_sel].mgridst;
2469 im->xlab_user.labtm = xlab[xlab_sel].labtm;
2470 im->xlab_user.labst = xlab[xlab_sel].labst;
2471 im->xlab_user.precis = xlab[xlab_sel].precis;
2472 im->xlab_user.stst = xlab[xlab_sel].stst;
2475 /* y coords are the same for every line ... */
2477 Y1 = im->yorigin - im->ysize;
2478 /* paint the minor grid */
2479 if (!(im->extra_flags & NOMINOR)) {
2480 for (ti = find_first_time(im->start,
2488 find_first_time(im->start,
2495 find_next_time(ti, im->xlab_user.gridtm, im->xlab_user.gridst)
2497 /* are we inside the graph ? */
2498 if (ti < im->start || ti > im->end)
2500 while (timajor < ti) {
2501 timajor = find_next_time(timajor,
2504 mgridtm, im->xlab_user.mgridst);
2507 continue; /* skip as falls on major grid line */
2509 gfx_line(im, X0, Y1 - 2, X0, Y1,
2510 GRIDWIDTH, im->graph_col[GRC_GRID]);
2511 gfx_line(im, X0, Y0, X0, Y0 + 2,
2512 GRIDWIDTH, im->graph_col[GRC_GRID]);
2513 gfx_dashed_line(im, X0, Y0 + 1, X0,
2516 graph_col[GRC_GRID],
2517 im->grid_dash_on, im->grid_dash_off);
2521 /* paint the major grid */
2522 for (ti = find_first_time(im->start,
2530 ti = find_next_time(ti, im->xlab_user.mgridtm, im->xlab_user.mgridst)
2532 /* are we inside the graph ? */
2533 if (ti < im->start || ti > im->end)
2536 gfx_line(im, X0, Y1 - 2, X0, Y1,
2537 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2538 gfx_line(im, X0, Y0, X0, Y0 + 3,
2539 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2540 gfx_dashed_line(im, X0, Y0 + 3, X0,
2544 [GRC_MGRID], im->grid_dash_on, im->grid_dash_off);
2546 /* paint the labels below the graph */
2548 find_first_time(im->start -
2557 im->xlab_user.precis / 2;
2558 ti = find_next_time(ti, im->xlab_user.labtm, im->xlab_user.labst)
2560 tilab = ti + im->xlab_user.precis / 2; /* correct time for the label */
2561 /* are we inside the graph ? */
2562 if (tilab < im->start || tilab > im->end)
2565 localtime_r(&tilab, &tm);
2566 strftime(graph_label, 99, im->xlab_user.stst, &tm);
2568 # error "your libc has no strftime I guess we'll abort the exercise here."
2573 im->graph_col[GRC_FONT],
2575 text_prop[TEXT_PROP_AXIS].
2578 GFX_H_CENTER, GFX_V_TOP, graph_label);
2587 /* draw x and y axis */
2588 /* gfx_line ( im->canvas, im->xorigin+im->xsize,im->yorigin,
2589 im->xorigin+im->xsize,im->yorigin-im->ysize,
2590 GRIDWIDTH, im->graph_col[GRC_AXIS]);
2592 gfx_line ( im->canvas, im->xorigin,im->yorigin-im->ysize,
2593 im->xorigin+im->xsize,im->yorigin-im->ysize,
2594 GRIDWIDTH, im->graph_col[GRC_AXIS]); */
2596 gfx_line(im, im->xorigin - 4,
2598 im->xorigin + im->xsize +
2599 4, im->yorigin, MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2600 gfx_line(im, im->xorigin,
2603 im->yorigin - im->ysize -
2604 4, MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2605 /* arrow for X and Y axis direction */
2606 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 */
2607 im->graph_col[GRC_ARROW]);
2609 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 */
2610 im->graph_col[GRC_ARROW]);
2612 if (im->second_axis_scale != 0){
2613 gfx_line ( im, im->xorigin+im->xsize,im->yorigin+4,
2614 im->xorigin+im->xsize,im->yorigin-im->ysize-4,
2615 MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2617 im->xorigin+im->xsize-2, im->yorigin-im->ysize-2,
2618 im->xorigin+im->xsize+3, im->yorigin-im->ysize-2,
2619 im->xorigin+im->xsize, im->yorigin-im->ysize-7, /* LINEOFFSET */
2620 im->graph_col[GRC_ARROW]);
2631 double X0, Y0; /* points for filled graph and more */
2632 struct gfx_color_t water_color;
2634 if (im->draw_3d_border > 0) {
2635 /* draw 3d border */
2636 i = im->draw_3d_border;
2637 gfx_new_area(im, 0, im->yimg,
2638 i, im->yimg - i, i, i, im->graph_col[GRC_SHADEA]);
2639 gfx_add_point(im, im->ximg - i, i);
2640 gfx_add_point(im, im->ximg, 0);
2641 gfx_add_point(im, 0, 0);
2643 gfx_new_area(im, i, im->yimg - i,
2645 im->yimg - i, im->ximg - i, i, im->graph_col[GRC_SHADEB]);
2646 gfx_add_point(im, im->ximg, 0);
2647 gfx_add_point(im, im->ximg, im->yimg);
2648 gfx_add_point(im, 0, im->yimg);
2651 if (im->draw_x_grid == 1)
2653 if (im->draw_y_grid == 1) {
2654 if (im->logarithmic) {
2655 res = horizontal_log_grid(im);
2657 res = draw_horizontal_grid(im);
2660 /* dont draw horizontal grid if there is no min and max val */
2662 char *nodata = "No Data found";
2664 gfx_text(im, im->ximg / 2,
2667 im->graph_col[GRC_FONT],
2669 text_prop[TEXT_PROP_AXIS].
2672 GFX_H_CENTER, GFX_V_CENTER, nodata);
2676 /* yaxis unit description */
2677 if (im->ylegend[0] != '\0'){
2679 im->xOriginLegendY+10,
2681 im->graph_col[GRC_FONT],
2683 text_prop[TEXT_PROP_UNIT].
2686 RRDGRAPH_YLEGEND_ANGLE, GFX_H_CENTER, GFX_V_CENTER, im->ylegend);
2689 if (im->second_axis_legend[0] != '\0'){
2691 im->xOriginLegendY2+10,
2692 im->yOriginLegendY2,
2693 im->graph_col[GRC_FONT],
2694 im->text_prop[TEXT_PROP_UNIT].font_desc,
2696 RRDGRAPH_YLEGEND_ANGLE,
2697 GFX_H_CENTER, GFX_V_CENTER,
2698 im->second_axis_legend);
2703 im->xOriginTitle, im->yOriginTitle+6,
2704 im->graph_col[GRC_FONT],
2706 text_prop[TEXT_PROP_TITLE].
2708 im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_TOP, im->title);
2709 /* rrdtool 'logo' */
2710 if (!(im->extra_flags & NO_RRDTOOL_TAG)){
2711 water_color = im->graph_col[GRC_FONT];
2712 water_color.alpha = 0.3;
2713 double xpos = im->legendposition == EAST ? im->xOriginLegendY : im->ximg - 4;
2714 gfx_text(im, xpos, 5,
2717 text_prop[TEXT_PROP_WATERMARK].
2718 font_desc, im->tabwidth,
2719 -90, GFX_H_LEFT, GFX_V_TOP, "RRDTOOL / TOBI OETIKER");
2721 /* graph watermark */
2722 if (im->watermark[0] != '\0') {
2723 water_color = im->graph_col[GRC_FONT];
2724 water_color.alpha = 0.3;
2726 im->ximg / 2, im->yimg - 6,
2729 text_prop[TEXT_PROP_WATERMARK].
2730 font_desc, im->tabwidth, 0,
2731 GFX_H_CENTER, GFX_V_BOTTOM, im->watermark);
2735 if (!(im->extra_flags & NOLEGEND) && !(im->extra_flags & ONLY_GRAPH)) {
2736 for (i = 0; i < im->gdes_c; i++) {
2737 if (im->gdes[i].legend[0] == '\0')
2739 /* im->gdes[i].leg_y is the bottom of the legend */
2740 X0 = im->xOriginLegend + im->gdes[i].leg_x;
2741 Y0 = im->legenddirection == TOP_DOWN ? im->yOriginLegend + im->gdes[i].leg_y : im->yOriginLegend + im->legendheight - im->gdes[i].leg_y;
2742 gfx_text(im, X0, Y0,
2743 im->graph_col[GRC_FONT],
2746 [TEXT_PROP_LEGEND].font_desc,
2748 GFX_H_LEFT, GFX_V_BOTTOM, im->gdes[i].legend);
2749 /* The legend for GRAPH items starts with "M " to have
2750 enough space for the box */
2751 if (im->gdes[i].gf != GF_PRINT &&
2752 im->gdes[i].gf != GF_GPRINT && im->gdes[i].gf != GF_COMMENT) {
2756 boxH = gfx_get_text_width(im, 0,
2761 im->tabwidth, "o") * 1.2;
2763 /* shift the box up a bit */
2766 if (im->dynamic_labels && im->gdes[i].gf == GF_HRULE) { /* [-] */
2768 cairo_new_path(im->cr);
2769 cairo_set_line_width(im->cr, 1.0);
2772 X0 + boxH, Y0 - boxV / 2,
2773 1.0, im->gdes[i].col);
2775 } else if (im->dynamic_labels && im->gdes[i].gf == GF_VRULE) { /* [|] */
2777 cairo_new_path(im->cr);
2778 cairo_set_line_width(im->cr, 1.0);
2781 X0 + boxH / 2, Y0 - boxV,
2782 1.0, im->gdes[i].col);
2784 } else if (im->dynamic_labels && im->gdes[i].gf == GF_LINE) { /* [/] */
2786 cairo_new_path(im->cr);
2787 cairo_set_line_width(im->cr, im->gdes[i].linewidth);
2790 X0 + boxH, Y0 - boxV,
2791 im->gdes[i].linewidth, im->gdes[i].col);
2794 /* make sure transparent colors show up the same way as in the graph */
2797 X0, Y0, X0 + boxH, Y0, im->graph_col[GRC_BACK]);
2798 gfx_add_point(im, X0 + boxH, Y0 - boxV);
2800 gfx_new_area(im, X0, Y0 - boxV, X0,
2801 Y0, X0 + boxH, Y0, im->gdes[i].col);
2802 gfx_add_point(im, X0 + boxH, Y0 - boxV);
2805 cairo_new_path(im->cr);
2806 cairo_set_line_width(im->cr, 1.0);
2809 gfx_line_fit(im, &X0, &Y0);
2810 gfx_line_fit(im, &X1, &Y1);
2811 cairo_move_to(im->cr, X0, Y0);
2812 cairo_line_to(im->cr, X1, Y0);
2813 cairo_line_to(im->cr, X1, Y1);
2814 cairo_line_to(im->cr, X0, Y1);
2815 cairo_close_path(im->cr);
2816 cairo_set_source_rgba(im->cr,
2817 im->graph_col[GRC_FRAME].red,
2818 im->graph_col[GRC_FRAME].green,
2819 im->graph_col[GRC_FRAME].blue,
2820 im->graph_col[GRC_FRAME].alpha);
2822 if (im->gdes[i].dash) {
2823 /* make box borders in legend dashed if the graph is dashed */
2827 cairo_set_dash(im->cr, dashes, 1, 0.0);
2829 cairo_stroke(im->cr);
2830 cairo_restore(im->cr);
2837 /*****************************************************
2838 * lazy check make sure we rely need to create this graph
2839 *****************************************************/
2846 struct stat imgstat;
2849 return 0; /* no lazy option */
2850 if (strlen(im->graphfile) == 0)
2851 return 0; /* inmemory option */
2852 if (stat(im->graphfile, &imgstat) != 0)
2853 return 0; /* can't stat */
2854 /* one pixel in the existing graph is more then what we would
2856 if (time(NULL) - imgstat.st_mtime > (im->end - im->start) / im->xsize)
2858 if ((fd = fopen(im->graphfile, "rb")) == NULL)
2859 return 0; /* the file does not exist */
2860 switch (im->imgformat) {
2862 size = PngSize(fd, &(im->ximg), &(im->yimg));
2872 int graph_size_location(
2877 /* The actual size of the image to draw is determined from
2878 ** several sources. The size given on the command line is
2879 ** the graph area but we need more as we have to draw labels
2880 ** and other things outside the graph area. If the option
2881 ** --full-size-mode is selected the size defines the total
2882 ** image size and the size available for the graph is
2886 /** +---+-----------------------------------+
2887 ** | y |...............graph title.........|
2888 ** | +---+-------------------------------+
2892 ** | s | x | main graph area |
2897 ** | l | b +-------------------------------+
2898 ** | e | l | x axis labels |
2899 ** +---+---+-------------------------------+
2900 ** |....................legends............|
2901 ** +---------------------------------------+
2903 ** +---------------------------------------+
2906 int Xvertical = 0, Xvertical2 = 0, Ytitle =
2907 0, Xylabel = 0, Xmain = 0, Ymain =
2908 0, Yxlabel = 0, Xspacing = 15, Yspacing = 15, Ywatermark = 4;
2910 // no legends and no the shall be plotted it's easy
2911 if (im->extra_flags & ONLY_GRAPH) {
2913 im->ximg = im->xsize;
2914 im->yimg = im->ysize;
2915 im->yorigin = im->ysize;
2920 if(im->watermark[0] != '\0') {
2921 Ywatermark = im->text_prop[TEXT_PROP_WATERMARK].size * 2;
2924 // calculate the width of the left vertical legend
2925 if (im->ylegend[0] != '\0') {
2926 Xvertical = im->text_prop[TEXT_PROP_UNIT].size * 2;
2929 // calculate the width of the right vertical legend
2930 if (im->second_axis_legend[0] != '\0') {
2931 Xvertical2 = im->text_prop[TEXT_PROP_UNIT].size * 2;
2934 Xvertical2 = Xspacing;
2937 if (im->title[0] != '\0') {
2938 /* The title is placed "inbetween" two text lines so it
2939 ** automatically has some vertical spacing. The horizontal
2940 ** spacing is added here, on each side.
2942 /* if necessary, reduce the font size of the title until it fits the image width */
2943 Ytitle = im->text_prop[TEXT_PROP_TITLE].size * 2.6 + 10;
2946 // we have no title; get a little clearing from the top
2947 Ytitle = 1.5 * Yspacing;
2951 if (im->draw_x_grid) {
2952 // calculate the height of the horizontal labelling
2953 Yxlabel = im->text_prop[TEXT_PROP_AXIS].size * 2.5;
2955 if (im->draw_y_grid || im->forceleftspace) {
2956 // calculate the width of the vertical labelling
2958 gfx_get_text_width(im, 0,
2959 im->text_prop[TEXT_PROP_AXIS].font_desc,
2960 im->tabwidth, "0") * im->unitslength;
2964 // add some space to the labelling
2965 Xylabel += Xspacing;
2967 /* If the legend is printed besides the graph the width has to be
2968 ** calculated first. Placing the legend north or south of the
2969 ** graph requires the width calculation first, so the legend is
2970 ** skipped for the moment.
2972 im->legendheight = 0;
2973 im->legendwidth = 0;
2974 if (!(im->extra_flags & NOLEGEND)) {
2975 if(im->legendposition == WEST || im->legendposition == EAST){
2976 if (leg_place(im, 1) == -1){
2982 if (im->extra_flags & FULL_SIZE_MODE) {
2984 /* The actual size of the image to draw has been determined by the user.
2985 ** The graph area is the space remaining after accounting for the legend,
2986 ** the watermark, the axis labels, and the title.
2988 im->ximg = im->xsize;
2989 im->yimg = im->ysize;
2993 /* Now calculate the total size. Insert some spacing where
2994 desired. im->xorigin and im->yorigin need to correspond
2995 with the lower left corner of the main graph area or, if
2996 this one is not set, the imaginary box surrounding the
2998 /* Initial size calculation for the main graph area */
3000 Xmain -= Xylabel;// + Xspacing;
3001 if((im->legendposition == WEST || im->legendposition == EAST) && !(im->extra_flags & NOLEGEND) ){
3002 Xmain -= im->legendwidth;// + Xspacing;
3004 if (im->second_axis_scale != 0){
3007 if (!(im->extra_flags & NO_RRDTOOL_TAG)){
3011 Xmain -= Xvertical + Xvertical2;
3013 /* limit the remaining space to 0 */
3019 /* Putting the legend north or south, the height can now be calculated */
3020 if (!(im->extra_flags & NOLEGEND)) {
3021 if(im->legendposition == NORTH || im->legendposition == SOUTH){
3022 im->legendwidth = im->ximg;
3023 if (leg_place(im, 0) == -1){
3029 if( (im->legendposition == NORTH || im->legendposition == SOUTH) && !(im->extra_flags & NOLEGEND) ){
3030 Ymain -= Yxlabel + im->legendheight;
3036 /* reserve space for the title *or* some padding above the graph */
3039 /* reserve space for padding below the graph */
3040 if (im->extra_flags & NOLEGEND) {
3044 if (im->watermark[0] != '\0') {
3045 Ymain -= Ywatermark;
3047 /* limit the remaining height to 0 */
3052 } else { /* dimension options -width and -height refer to the dimensions of the main graph area */
3054 /* The actual size of the image to draw is determined from
3055 ** several sources. The size given on the command line is
3056 ** the graph area but we need more as we have to draw labels
3057 ** and other things outside the graph area.
3061 Xmain = im->xsize; // + Xspacing;
3065 im->ximg = Xmain + Xylabel;
3066 if (!(im->extra_flags & NO_RRDTOOL_TAG)){
3067 im->ximg += Xspacing;
3070 if( (im->legendposition == WEST || im->legendposition == EAST) && !(im->extra_flags & NOLEGEND) ){
3071 im->ximg += im->legendwidth;// + Xspacing;
3073 if (im->second_axis_scale != 0){
3074 im->ximg += Xylabel;
3077 im->ximg += Xvertical + Xvertical2;
3079 if (!(im->extra_flags & NOLEGEND)) {
3080 if(im->legendposition == NORTH || im->legendposition == SOUTH){
3081 im->legendwidth = im->ximg;
3082 if (leg_place(im, 0) == -1){
3088 im->yimg = Ymain + Yxlabel;
3089 if( (im->legendposition == NORTH || im->legendposition == SOUTH) && !(im->extra_flags & NOLEGEND) ){
3090 im->yimg += im->legendheight;
3093 /* reserve space for the title *or* some padding above the graph */
3097 im->yimg += 1.5 * Yspacing;
3099 /* reserve space for padding below the graph */
3100 if (im->extra_flags & NOLEGEND) {
3101 im->yimg += Yspacing;
3104 if (im->watermark[0] != '\0') {
3105 im->yimg += Ywatermark;
3110 /* In case of putting the legend in west or east position the first
3111 ** legend calculation might lead to wrong positions if some items
3112 ** are not aligned on the left hand side (e.g. centered) as the
3113 ** legendwidth wight have been increased after the item was placed.
3114 ** In this case the positions have to be recalculated.
3116 if (!(im->extra_flags & NOLEGEND)) {
3117 if(im->legendposition == WEST || im->legendposition == EAST){
3118 if (leg_place(im, 0) == -1){
3124 /* After calculating all dimensions
3125 ** it is now possible to calculate
3128 switch(im->legendposition){
3130 im->xOriginTitle = Xvertical + Xylabel + (im->xsize / 2);
3131 im->yOriginTitle = 0;
3133 im->xOriginLegend = 0;
3134 im->yOriginLegend = Ytitle;
3136 im->xOriginLegendY = 0;
3137 im->yOriginLegendY = Ytitle + im->legendheight + (Ymain / 2) + Yxlabel;
3139 im->xorigin = Xvertical + Xylabel;
3140 im->yorigin = Ytitle + im->legendheight + Ymain;
3142 im->xOriginLegendY2 = Xvertical + Xylabel + Xmain;
3143 if (im->second_axis_scale != 0){
3144 im->xOriginLegendY2 += Xylabel;
3146 im->yOriginLegendY2 = Ytitle + im->legendheight + (Ymain / 2) + Yxlabel;
3151 im->xOriginTitle = im->legendwidth + Xvertical + Xylabel + im->xsize / 2;
3152 im->yOriginTitle = 0;
3154 im->xOriginLegend = 0;
3155 im->yOriginLegend = Ytitle;
3157 im->xOriginLegendY = im->legendwidth;
3158 im->yOriginLegendY = Ytitle + (Ymain / 2);
3160 im->xorigin = im->legendwidth + Xvertical + Xylabel;
3161 im->yorigin = Ytitle + Ymain;
3163 im->xOriginLegendY2 = im->legendwidth + Xvertical + Xylabel + Xmain;
3164 if (im->second_axis_scale != 0){
3165 im->xOriginLegendY2 += Xylabel;
3167 im->yOriginLegendY2 = Ytitle + (Ymain / 2);
3172 im->xOriginTitle = Xvertical + Xylabel + im->xsize / 2;
3173 im->yOriginTitle = 0;
3175 im->xOriginLegend = 0;
3176 im->yOriginLegend = Ytitle + Ymain + Yxlabel;
3178 im->xOriginLegendY = 0;
3179 im->yOriginLegendY = Ytitle + (Ymain / 2);
3181 im->xorigin = Xvertical + Xylabel;
3182 im->yorigin = Ytitle + Ymain;
3184 im->xOriginLegendY2 = Xvertical + Xylabel + Xmain;
3185 if (im->second_axis_scale != 0){
3186 im->xOriginLegendY2 += Xylabel;
3188 im->yOriginLegendY2 = Ytitle + (Ymain / 2);
3193 im->xOriginTitle = Xvertical + Xylabel + im->xsize / 2;
3194 im->yOriginTitle = 0;
3196 im->xOriginLegend = Xvertical + Xylabel + Xmain + Xvertical2;
3197 if (im->second_axis_scale != 0){
3198 im->xOriginLegend += Xylabel;
3200 im->yOriginLegend = Ytitle;
3202 im->xOriginLegendY = 0;
3203 im->yOriginLegendY = Ytitle + (Ymain / 2);
3205 im->xorigin = Xvertical + Xylabel;
3206 im->yorigin = Ytitle + Ymain;
3208 im->xOriginLegendY2 = Xvertical + Xylabel + Xmain;
3209 if (im->second_axis_scale != 0){
3210 im->xOriginLegendY2 += Xylabel;
3212 im->yOriginLegendY2 = Ytitle + (Ymain / 2);
3214 if (!(im->extra_flags & NO_RRDTOOL_TAG)){
3215 im->xOriginTitle += Xspacing;
3216 im->xOriginLegend += Xspacing;
3217 im->xOriginLegendY += Xspacing;
3218 im->xorigin += Xspacing;
3219 im->xOriginLegendY2 += Xspacing;
3229 static cairo_status_t cairo_output(
3233 unsigned int length)
3235 image_desc_t *im = (image_desc_t*)closure;
3237 im->rendered_image =
3238 (unsigned char*)realloc(im->rendered_image, im->rendered_image_size + length);
3239 if (im->rendered_image == NULL)
3240 return CAIRO_STATUS_WRITE_ERROR;
3241 memcpy(im->rendered_image + im->rendered_image_size, data, length);
3242 im->rendered_image_size += length;
3243 return CAIRO_STATUS_SUCCESS;
3246 /* draw that picture thing ... */
3251 int lazy = lazy_check(im);
3252 double areazero = 0.0;
3253 graph_desc_t *lastgdes = NULL;
3256 // PangoFontMap *font_map = pango_cairo_font_map_get_default();
3258 /* pull the data from the rrd files ... */
3259 if (data_fetch(im) == -1)
3261 /* evaluate VDEF and CDEF operations ... */
3262 if (data_calc(im) == -1)
3264 /* calculate and PRINT and GPRINT definitions. We have to do it at
3265 * this point because it will affect the length of the legends
3266 * if there are no graph elements (i==0) we stop here ...
3267 * if we are lazy, try to quit ...
3273 /* if we want and can be lazy ... quit now */
3277 /**************************************************************
3278 *** Calculating sizes and locations became a bit confusing ***
3279 *** so I moved this into a separate function. ***
3280 **************************************************************/
3281 if (graph_size_location(im, i) == -1)
3284 info.u_cnt = im->xorigin;
3285 grinfo_push(im, sprintf_alloc("graph_left"), RD_I_CNT, info);
3286 info.u_cnt = im->yorigin - im->ysize;
3287 grinfo_push(im, sprintf_alloc("graph_top"), RD_I_CNT, info);
3288 info.u_cnt = im->xsize;
3289 grinfo_push(im, sprintf_alloc("graph_width"), RD_I_CNT, info);
3290 info.u_cnt = im->ysize;
3291 grinfo_push(im, sprintf_alloc("graph_height"), RD_I_CNT, info);
3292 info.u_cnt = im->ximg;
3293 grinfo_push(im, sprintf_alloc("image_width"), RD_I_CNT, info);
3294 info.u_cnt = im->yimg;
3295 grinfo_push(im, sprintf_alloc("image_height"), RD_I_CNT, info);
3296 info.u_cnt = im->start;
3297 grinfo_push(im, sprintf_alloc("graph_start"), RD_I_CNT, info);
3298 info.u_cnt = im->end;
3299 grinfo_push(im, sprintf_alloc("graph_end"), RD_I_CNT, info);
3301 /* if we want and can be lazy ... quit now */
3305 /* get actual drawing data and find min and max values */
3306 if (data_proc(im) == -1)
3308 if (!im->logarithmic) {
3312 /* identify si magnitude Kilo, Mega Giga ? */
3313 if (!im->rigid && !im->logarithmic)
3314 expand_range(im); /* make sure the upper and lower limit are
3317 info.u_val = im->minval;
3318 grinfo_push(im, sprintf_alloc("value_min"), RD_I_VAL, info);
3319 info.u_val = im->maxval;
3320 grinfo_push(im, sprintf_alloc("value_max"), RD_I_VAL, info);
3323 if (!calc_horizontal_grid(im))
3328 apply_gridfit(im); */
3329 /* the actual graph is created by going through the individual
3330 graph elements and then drawing them */
3331 cairo_surface_destroy(im->surface);
3332 switch (im->imgformat) {
3335 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
3336 im->ximg * im->zoom,
3337 im->yimg * im->zoom);
3341 im->surface = strlen(im->graphfile)
3342 ? cairo_pdf_surface_create(im->graphfile, im->ximg * im->zoom,
3343 im->yimg * im->zoom)
3344 : cairo_pdf_surface_create_for_stream
3345 (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3349 im->surface = strlen(im->graphfile)
3351 cairo_ps_surface_create(im->graphfile, im->ximg * im->zoom,
3352 im->yimg * im->zoom)
3353 : cairo_ps_surface_create_for_stream
3354 (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3358 im->surface = strlen(im->graphfile)
3360 cairo_svg_surface_create(im->
3362 im->ximg * im->zoom, im->yimg * im->zoom)
3363 : cairo_svg_surface_create_for_stream
3364 (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3365 cairo_svg_surface_restrict_to_version
3366 (im->surface, CAIRO_SVG_VERSION_1_1);
3369 cairo_destroy(im->cr);
3370 im->cr = cairo_create(im->surface);
3371 cairo_set_antialias(im->cr, im->graph_antialias);
3372 cairo_scale(im->cr, im->zoom, im->zoom);
3373 // pango_cairo_font_map_set_resolution(PANGO_CAIRO_FONT_MAP(font_map), 100);
3374 gfx_new_area(im, 0, 0, 0, im->yimg,
3375 im->ximg, im->yimg, im->graph_col[GRC_BACK]);
3376 gfx_add_point(im, im->ximg, 0);
3378 gfx_new_area(im, im->xorigin,
3381 im->xsize, im->yorigin,
3384 im->yorigin - im->ysize, im->graph_col[GRC_CANVAS]);
3385 gfx_add_point(im, im->xorigin, im->yorigin - im->ysize);
3387 cairo_rectangle(im->cr, im->xorigin, im->yorigin - im->ysize - 1.0,
3388 im->xsize, im->ysize + 2.0);
3390 if (im->minval > 0.0)
3391 areazero = im->minval;
3392 if (im->maxval < 0.0)
3393 areazero = im->maxval;
3394 for (i = 0; i < im->gdes_c; i++) {
3395 switch (im->gdes[i].gf) {
3409 for (ii = 0; ii < im->xsize; ii++) {
3410 if (!isnan(im->gdes[i].p_data[ii])
3411 && im->gdes[i].p_data[ii] != 0.0) {
3412 if (im->gdes[i].yrule > 0) {
3419 im->ysize, 1.0, im->gdes[i].col);
3420 } else if (im->gdes[i].yrule < 0) {
3423 im->yorigin - im->ysize - 1.0,
3425 im->yorigin - im->ysize -
3428 im->ysize, 1.0, im->gdes[i].col);
3436 /* fix data points at oo and -oo */
3437 for (ii = 0; ii < im->xsize; ii++) {
3438 if (isinf(im->gdes[i].p_data[ii])) {
3439 if (im->gdes[i].p_data[ii] > 0) {
3440 im->gdes[i].p_data[ii] = im->maxval;
3442 im->gdes[i].p_data[ii] = im->minval;
3448 /* *******************************************************
3453 -------|--t-1--t--------------------------------
3455 if we know the value at time t was a then
3456 we draw a square from t-1 to t with the value a.
3458 ********************************************************* */
3459 if (im->gdes[i].col.alpha != 0.0) {
3460 /* GF_LINE and friend */
3461 if (im->gdes[i].gf == GF_LINE) {
3462 double last_y = 0.0;
3466 cairo_new_path(im->cr);
3467 cairo_set_line_width(im->cr, im->gdes[i].linewidth);
3468 if (im->gdes[i].dash) {
3469 cairo_set_dash(im->cr,
3470 im->gdes[i].p_dashes,
3471 im->gdes[i].ndash, im->gdes[i].offset);
3474 for (ii = 1; ii < im->xsize; ii++) {
3475 if (isnan(im->gdes[i].p_data[ii])
3476 || (im->slopemode == 1
3477 && isnan(im->gdes[i].p_data[ii - 1]))) {
3482 last_y = ytr(im, im->gdes[i].p_data[ii]);
3483 if (im->slopemode == 0) {
3484 double x = ii - 1 + im->xorigin;
3487 gfx_line_fit(im, &x, &y);
3488 cairo_move_to(im->cr, x, y);
3489 x = ii + im->xorigin;
3491 gfx_line_fit(im, &x, &y);
3492 cairo_line_to(im->cr, x, y);
3494 double x = ii - 1 + im->xorigin;
3496 ytr(im, im->gdes[i].p_data[ii - 1]);
3497 gfx_line_fit(im, &x, &y);
3498 cairo_move_to(im->cr, x, y);
3499 x = ii + im->xorigin;
3501 gfx_line_fit(im, &x, &y);
3502 cairo_line_to(im->cr, x, y);
3506 double x1 = ii + im->xorigin;
3507 double y1 = ytr(im, im->gdes[i].p_data[ii]);
3509 if (im->slopemode == 0
3510 && !AlmostEqual2sComplement(y1, last_y, 4)) {
3511 double x = ii - 1 + im->xorigin;
3514 gfx_line_fit(im, &x, &y);
3515 cairo_line_to(im->cr, x, y);
3518 gfx_line_fit(im, &x1, &y1);
3519 cairo_line_to(im->cr, x1, y1);
3522 cairo_set_source_rgba(im->cr,
3528 col.blue, im->gdes[i].col.alpha);
3529 cairo_set_line_cap(im->cr, CAIRO_LINE_CAP_ROUND);
3530 cairo_set_line_join(im->cr, CAIRO_LINE_JOIN_ROUND);
3531 cairo_stroke(im->cr);
3532 cairo_restore(im->cr);
3538 (double *) malloc(sizeof(double) * im->xsize * 2);
3540 (double *) malloc(sizeof(double) * im->xsize * 2);
3542 (double *) malloc(sizeof(double) * im->xsize * 2);
3544 (double *) malloc(sizeof(double) * im->xsize * 2);
3547 for (ii = 0; ii <= im->xsize; ii++) {
3550 if (idxI > 0 && (drawem != 0 || ii == im->xsize)) {
3556 AlmostEqual2sComplement(foreY
3560 AlmostEqual2sComplement(foreY
3566 if (im->gdes[i].gf != GF_GRAD) {
3571 foreY[cntI], im->gdes[i].col);
3573 lastx = foreX[cntI];
3574 lasty = foreY[cntI];
3576 while (cntI < idxI) {
3581 AlmostEqual2sComplement(foreY
3585 AlmostEqual2sComplement(foreY
3592 if (im->gdes[i].gf != GF_GRAD) {
3593 gfx_add_point(im, foreX[cntI], foreY[cntI]);
3595 gfx_add_rect_fadey(im,
3597 foreX[cntI], foreY[cntI], lasty,
3600 im->gdes[i].gradheight
3602 lastx = foreX[cntI];
3603 lasty = foreY[cntI];
3606 if (im->gdes[i].gf != GF_GRAD) {
3607 gfx_add_point(im, backX[idxI], backY[idxI]);
3609 gfx_add_rect_fadey(im,
3611 backX[idxI], backY[idxI], lasty,
3614 im->gdes[i].gradheight);
3615 lastx = backX[idxI];
3616 lasty = backY[idxI];
3623 AlmostEqual2sComplement(backY
3627 AlmostEqual2sComplement(backY
3634 if (im->gdes[i].gf != GF_GRAD) {
3635 gfx_add_point(im, backX[idxI], backY[idxI]);
3637 gfx_add_rect_fadey(im,
3639 backX[idxI], backY[idxI], lasty,
3642 im->gdes[i].gradheight);
3643 lastx = backX[idxI];
3644 lasty = backY[idxI];
3649 if (im->gdes[i].gf != GF_GRAD)
3656 if (ii == im->xsize)
3658 if (im->slopemode == 0 && ii == 0) {
3661 if (isnan(im->gdes[i].p_data[ii])) {
3665 ytop = ytr(im, im->gdes[i].p_data[ii]);
3666 if (lastgdes && im->gdes[i].stack) {
3667 ybase = ytr(im, lastgdes->p_data[ii]);
3669 ybase = ytr(im, areazero);
3671 if (ybase == ytop) {
3677 double extra = ytop;
3682 if (im->slopemode == 0) {
3683 backY[++idxI] = ybase - 0.2;
3684 backX[idxI] = ii + im->xorigin - 1;
3685 foreY[idxI] = ytop + 0.2;
3686 foreX[idxI] = ii + im->xorigin - 1;
3688 backY[++idxI] = ybase - 0.2;
3689 backX[idxI] = ii + im->xorigin;
3690 foreY[idxI] = ytop + 0.2;
3691 foreX[idxI] = ii + im->xorigin;
3693 /* close up any remaining area */
3698 } /* else GF_LINE */
3700 /* if color != 0x0 */
3701 /* make sure we do not run into trouble when stacking on NaN */
3702 for (ii = 0; ii < im->xsize; ii++) {
3703 if (isnan(im->gdes[i].p_data[ii])) {
3704 if (lastgdes && (im->gdes[i].stack)) {
3705 im->gdes[i].p_data[ii] = lastgdes->p_data[ii];
3707 im->gdes[i].p_data[ii] = areazero;
3711 lastgdes = &(im->gdes[i]);
3715 ("STACK should already be turned into LINE or AREA here");
3720 cairo_reset_clip(im->cr);
3722 /* grid_paint also does the text */
3723 if (!(im->extra_flags & ONLY_GRAPH))
3725 if (!(im->extra_flags & ONLY_GRAPH))
3727 /* the RULES are the last thing to paint ... */
3728 for (i = 0; i < im->gdes_c; i++) {
3730 switch (im->gdes[i].gf) {
3732 if (im->gdes[i].yrule >= im->minval
3733 && im->gdes[i].yrule <= im->maxval) {
3735 if (im->gdes[i].dash) {
3736 cairo_set_dash(im->cr,
3737 im->gdes[i].p_dashes,
3738 im->gdes[i].ndash, im->gdes[i].offset);
3740 gfx_line(im, im->xorigin,
3741 ytr(im, im->gdes[i].yrule),
3742 im->xorigin + im->xsize,
3743 ytr(im, im->gdes[i].yrule), 1.0, im->gdes[i].col);
3744 cairo_stroke(im->cr);
3745 cairo_restore(im->cr);
3749 if (im->gdes[i].xrule >= im->start
3750 && im->gdes[i].xrule <= im->end) {
3752 if (im->gdes[i].dash) {
3753 cairo_set_dash(im->cr,
3754 im->gdes[i].p_dashes,
3755 im->gdes[i].ndash, im->gdes[i].offset);
3758 xtr(im, im->gdes[i].xrule),
3759 im->yorigin, xtr(im,
3763 im->yorigin - im->ysize, 1.0, im->gdes[i].col);
3764 cairo_stroke(im->cr);
3765 cairo_restore(im->cr);
3774 switch (im->imgformat) {
3777 cairo_status_t status;
3779 status = strlen(im->graphfile) ?
3780 cairo_surface_write_to_png(im->surface, im->graphfile)
3781 : cairo_surface_write_to_png_stream(im->surface, &cairo_output,
3784 if (status != CAIRO_STATUS_SUCCESS) {
3785 rrd_set_error("Could not save png to '%s'", im->graphfile);
3791 if (strlen(im->graphfile)) {
3792 cairo_show_page(im->cr);
3794 cairo_surface_finish(im->surface);
3803 /*****************************************************
3805 *****************************************************/
3812 if ((im->gdes = (graph_desc_t *)
3813 rrd_realloc(im->gdes, (im->gdes_c)
3814 * sizeof(graph_desc_t))) == NULL) {
3815 rrd_set_error("realloc graph_descs");
3820 im->gdes[im->gdes_c - 1].step = im->step;
3821 im->gdes[im->gdes_c - 1].step_orig = im->step;
3822 im->gdes[im->gdes_c - 1].stack = 0;
3823 im->gdes[im->gdes_c - 1].linewidth = 0;
3824 im->gdes[im->gdes_c - 1].debug = 0;
3825 im->gdes[im->gdes_c - 1].start = im->start;
3826 im->gdes[im->gdes_c - 1].start_orig = im->start;
3827 im->gdes[im->gdes_c - 1].end = im->end;
3828 im->gdes[im->gdes_c - 1].end_orig = im->end;
3829 im->gdes[im->gdes_c - 1].vname[0] = '\0';
3830 im->gdes[im->gdes_c - 1].data = NULL;
3831 im->gdes[im->gdes_c - 1].ds_namv = NULL;
3832 im->gdes[im->gdes_c - 1].data_first = 0;
3833 im->gdes[im->gdes_c - 1].p_data = NULL;
3834 im->gdes[im->gdes_c - 1].rpnp = NULL;
3835 im->gdes[im->gdes_c - 1].p_dashes = NULL;
3836 im->gdes[im->gdes_c - 1].shift = 0.0;
3837 im->gdes[im->gdes_c - 1].dash = 0;
3838 im->gdes[im->gdes_c - 1].ndash = 0;
3839 im->gdes[im->gdes_c - 1].offset = 0;
3840 im->gdes[im->gdes_c - 1].col.red = 0.0;
3841 im->gdes[im->gdes_c - 1].col.green = 0.0;
3842 im->gdes[im->gdes_c - 1].col.blue = 0.0;
3843 im->gdes[im->gdes_c - 1].col.alpha = 0.0;
3844 im->gdes[im->gdes_c - 1].col2.red = 0.0;
3845 im->gdes[im->gdes_c - 1].col2.green = 0.0;
3846 im->gdes[im->gdes_c - 1].col2.blue = 0.0;
3847 im->gdes[im->gdes_c - 1].col2.alpha = 0.0;
3848 im->gdes[im->gdes_c - 1].gradheight = 50.0;
3849 im->gdes[im->gdes_c - 1].legend[0] = '\0';
3850 im->gdes[im->gdes_c - 1].format[0] = '\0';
3851 im->gdes[im->gdes_c - 1].strftm = 0;
3852 im->gdes[im->gdes_c - 1].rrd[0] = '\0';
3853 im->gdes[im->gdes_c - 1].ds = -1;
3854 im->gdes[im->gdes_c - 1].cf_reduce = CF_AVERAGE;
3855 im->gdes[im->gdes_c - 1].cf = CF_AVERAGE;
3856 im->gdes[im->gdes_c - 1].yrule = DNAN;
3857 im->gdes[im->gdes_c - 1].xrule = 0;
3858 im->gdes[im->gdes_c - 1].daemon[0] = 0;
3862 /* copies input untill the first unescaped colon is found
3863 or until input ends. backslashes have to be escaped as well */
3865 const char *const input,
3871 for (inp = 0; inp < len && input[inp] != ':' && input[inp] != '\0'; inp++) {
3872 if (input[inp] == '\\'
3873 && input[inp + 1] != '\0'
3874 && (input[inp + 1] == '\\' || input[inp + 1] == ':')) {
3875 output[outp++] = input[++inp];
3877 output[outp++] = input[inp];
3880 output[outp] = '\0';
3884 /* Now just a wrapper around rrd_graph_v */
3896 rrd_info_t *grinfo = NULL;
3899 grinfo = rrd_graph_v(argc, argv);
3905 if (strcmp(walker->key, "image_info") == 0) {
3908 (char**)rrd_realloc((*prdata),
3909 (prlines + 1) * sizeof(char *))) == NULL) {
3910 rrd_set_error("realloc prdata");
3913 /* imginfo goes to position 0 in the prdata array */
3914 (*prdata)[prlines - 1] = (char*)malloc((strlen(walker->value.u_str)
3915 + 2) * sizeof(char));
3916 strcpy((*prdata)[prlines - 1], walker->value.u_str);
3917 (*prdata)[prlines] = NULL;
3919 /* skip anything else */
3920 walker = walker->next;
3928 if (strcmp(walker->key, "image_width") == 0) {
3929 *xsize = walker->value.u_cnt;
3930 } else if (strcmp(walker->key, "image_height") == 0) {
3931 *ysize = walker->value.u_cnt;
3932 } else if (strcmp(walker->key, "value_min") == 0) {
3933 *ymin = walker->value.u_val;
3934 } else if (strcmp(walker->key, "value_max") == 0) {
3935 *ymax = walker->value.u_val;
3936 } else if (strncmp(walker->key, "print", 5) == 0) { /* keys are prdate[0..] */
3939 (char**)rrd_realloc((*prdata),
3940 (prlines + 1) * sizeof(char *))) == NULL) {
3941 rrd_set_error("realloc prdata");
3944 (*prdata)[prlines - 1] = (char*)malloc((strlen(walker->value.u_str)
3945 + 2) * sizeof(char));
3946 (*prdata)[prlines] = NULL;
3947 strcpy((*prdata)[prlines - 1], walker->value.u_str);
3948 } else if (strcmp(walker->key, "image") == 0) {
3949 if ( fwrite(walker->value.u_blo.ptr, walker->value.u_blo.size, 1,
3950 (stream ? stream : stdout)) == 0 && ferror(stream ? stream : stdout)){
3951 rrd_set_error("writing image");
3955 /* skip anything else */
3956 walker = walker->next;
3958 rrd_info_free(grinfo);
3963 /* Some surgery done on this function, it became ridiculously big.
3965 ** - initializing now in rrd_graph_init()
3966 ** - options parsing now in rrd_graph_options()
3967 ** - script parsing now in rrd_graph_script()
3969 rrd_info_t *rrd_graph_v(
3976 rrd_graph_init(&im);
3977 /* a dummy surface so that we can measure text sizes for placements */
3978 old_locale = setlocale(LC_NUMERIC, "C");
3979 rrd_graph_options(argc, argv, &im);
3980 if (rrd_test_error()) {
3981 rrd_info_free(im.grinfo);
3986 if (optind >= argc) {
3987 rrd_info_free(im.grinfo);
3989 rrd_set_error("missing filename");
3993 if (strlen(argv[optind]) >= MAXPATH) {
3994 rrd_set_error("filename (including path) too long");
3995 rrd_info_free(im.grinfo);
4000 strncpy(im.graphfile, argv[optind], MAXPATH - 1);
4001 im.graphfile[MAXPATH - 1] = '\0';
4003 if (strcmp(im.graphfile, "-") == 0) {
4004 im.graphfile[0] = '\0';
4007 rrd_graph_script(argc, argv, &im, 1);
4008 setlocale(LC_NUMERIC, old_locale); /* reenable locale for rendering the graph */
4010 if (rrd_test_error()) {
4011 rrd_info_free(im.grinfo);
4016 /* Everything is now read and the actual work can start */
4018 if (graph_paint(&im) == -1) {
4019 rrd_info_free(im.grinfo);
4025 /* The image is generated and needs to be output.
4026 ** Also, if needed, print a line with information about the image.
4034 path = strdup(im.graphfile);
4035 filename = basename(path);
4037 sprintf_alloc(im.imginfo,
4040 im.ximg), (long) (im.zoom * im.yimg));
4041 grinfo_push(&im, sprintf_alloc("image_info"), RD_I_STR, info);
4045 if (im.rendered_image) {
4048 img.u_blo.size = im.rendered_image_size;
4049 img.u_blo.ptr = im.rendered_image;
4050 grinfo_push(&im, sprintf_alloc("image"), RD_I_BLO, img);
4059 image_desc_t *im,int prop,char *font, double size ){
4061 strncpy(im->text_prop[prop].font, font, sizeof(text_prop[prop].font) - 1);
4062 im->text_prop[prop].font[sizeof(text_prop[prop].font) - 1] = '\0';
4063 im->text_prop[prop].font_desc = pango_font_description_from_string( font );
4066 im->text_prop[prop].size = size;
4068 if (im->text_prop[prop].font_desc && im->text_prop[prop].size ){
4069 pango_font_description_set_size(im->text_prop[prop].font_desc, im->text_prop[prop].size * PANGO_SCALE);
4073 void rrd_graph_init(
4078 char *deffont = getenv("RRD_DEFAULT_FONT");
4079 static PangoFontMap *fontmap = NULL;
4080 PangoContext *context;
4087 im->daemon_addr = NULL;
4088 im->draw_x_grid = 1;
4089 im->draw_y_grid = 1;
4090 im->draw_3d_border = 2;
4091 im->dynamic_labels = 0;
4092 im->extra_flags = 0;
4093 im->font_options = cairo_font_options_create();
4094 im->forceleftspace = 0;
4097 im->graph_antialias = CAIRO_ANTIALIAS_GRAY;
4098 im->grid_dash_off = 1;
4099 im->grid_dash_on = 1;
4101 im->grinfo = (rrd_info_t *) NULL;
4102 im->grinfo_current = (rrd_info_t *) NULL;
4103 im->imgformat = IF_PNG;
4106 im->legenddirection = TOP_DOWN;
4107 im->legendheight = 0;
4108 im->legendposition = SOUTH;
4109 im->legendwidth = 0;
4110 im->logarithmic = 0;
4116 im->rendered_image_size = 0;
4117 im->rendered_image = NULL;
4121 im->tabwidth = 40.0;
4122 im->title[0] = '\0';
4123 im->unitsexponent = 9999;
4124 im->unitslength = 6;
4125 im->viewfactor = 1.0;
4126 im->watermark[0] = '\0';
4127 im->with_markup = 0;
4129 im->xlab_user.minsec = -1;
4131 im->xOriginLegend = 0;
4132 im->xOriginLegendY = 0;
4133 im->xOriginLegendY2 = 0;
4134 im->xOriginTitle = 0;
4136 im->ygridstep = DNAN;
4138 im->ylegend[0] = '\0';
4139 im->second_axis_scale = 0; /* 0 disables it */
4140 im->second_axis_shift = 0; /* no shift by default */
4141 im->second_axis_legend[0] = '\0';
4142 im->second_axis_format[0] = '\0';
4144 im->yOriginLegend = 0;
4145 im->yOriginLegendY = 0;
4146 im->yOriginLegendY2 = 0;
4147 im->yOriginTitle = 0;
4151 im->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
4152 im->cr = cairo_create(im->surface);
4154 for (i = 0; i < DIM(text_prop); i++) {
4155 im->text_prop[i].size = -1;
4156 rrd_set_font_desc(im,i, deffont ? deffont : text_prop[i].font,text_prop[i].size);
4159 if (fontmap == NULL){
4160 fontmap = pango_cairo_font_map_get_default();
4163 context = pango_cairo_font_map_create_context((PangoCairoFontMap*)fontmap);
4165 pango_cairo_context_set_resolution(context, 100);
4167 pango_cairo_update_context(im->cr,context);
4169 im->layout = pango_layout_new(context);
4171 // im->layout = pango_cairo_create_layout(im->cr);
4174 cairo_font_options_set_hint_style
4175 (im->font_options, CAIRO_HINT_STYLE_FULL);
4176 cairo_font_options_set_hint_metrics
4177 (im->font_options, CAIRO_HINT_METRICS_ON);
4178 cairo_font_options_set_antialias(im->font_options, CAIRO_ANTIALIAS_GRAY);
4182 for (i = 0; i < DIM(graph_col); i++)
4183 im->graph_col[i] = graph_col[i];
4189 void rrd_graph_options(
4196 char *parsetime_error = NULL;
4197 char scan_gtm[12], scan_mtm[12], scan_ltm[12], col_nam[12];
4198 time_t start_tmp = 0, end_tmp = 0;
4200 rrd_time_value_t start_tv, end_tv;
4201 long unsigned int color;
4203 /* defines for long options without a short equivalent. should be bytes,
4204 and may not collide with (the ASCII value of) short options */
4205 #define LONGOPT_UNITS_SI 255
4208 struct option long_options[] = {
4209 { "alt-autoscale", no_argument, 0, 'A'},
4210 { "imgformat", required_argument, 0, 'a'},
4211 { "font-smoothing-threshold", required_argument, 0, 'B'},
4212 { "base", required_argument, 0, 'b'},
4213 { "color", required_argument, 0, 'c'},
4214 { "full-size-mode", no_argument, 0, 'D'},
4215 { "daemon", required_argument, 0, 'd'},
4216 { "slope-mode", no_argument, 0, 'E'},
4217 { "end", required_argument, 0, 'e'},
4218 { "force-rules-legend", no_argument, 0, 'F'},
4219 { "imginfo", required_argument, 0, 'f'},
4220 { "graph-render-mode", required_argument, 0, 'G'},
4221 { "no-legend", no_argument, 0, 'g'},
4222 { "height", required_argument, 0, 'h'},
4223 { "no-minor", no_argument, 0, 'I'},
4224 { "interlaced", no_argument, 0, 'i'},
4225 { "alt-autoscale-min", no_argument, 0, 'J'},
4226 { "only-graph", no_argument, 0, 'j'},
4227 { "units-length", required_argument, 0, 'L'},
4228 { "lower-limit", required_argument, 0, 'l'},
4229 { "alt-autoscale-max", no_argument, 0, 'M'},
4230 { "zoom", required_argument, 0, 'm'},
4231 { "no-gridfit", no_argument, 0, 'N'},
4232 { "font", required_argument, 0, 'n'},
4233 { "logarithmic", no_argument, 0, 'o'},
4234 { "pango-markup", no_argument, 0, 'P'},
4235 { "font-render-mode", required_argument, 0, 'R'},
4236 { "rigid", no_argument, 0, 'r'},
4237 { "step", required_argument, 0, 'S'},
4238 { "start", required_argument, 0, 's'},
4239 { "tabwidth", required_argument, 0, 'T'},
4240 { "title", required_argument, 0, 't'},
4241 { "upper-limit", required_argument, 0, 'u'},
4242 { "vertical-label", required_argument, 0, 'v'},
4243 { "watermark", required_argument, 0, 'W'},
4244 { "width", required_argument, 0, 'w'},
4245 { "units-exponent", required_argument, 0, 'X'},
4246 { "x-grid", required_argument, 0, 'x'},
4247 { "alt-y-grid", no_argument, 0, 'Y'},
4248 { "y-grid", required_argument, 0, 'y'},
4249 { "lazy", no_argument, 0, 'z'},
4250 { "units", required_argument, 0, LONGOPT_UNITS_SI},
4251 { "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 */
4252 { "disable-rrdtool-tag",no_argument, 0, 1001},
4253 { "right-axis", required_argument, 0, 1002},
4254 { "right-axis-label", required_argument, 0, 1003},
4255 { "right-axis-format", required_argument, 0, 1004},
4256 { "legend-position", required_argument, 0, 1005},
4257 { "legend-direction", required_argument, 0, 1006},
4258 { "border", required_argument, 0, 1007},
4259 { "grid-dash", required_argument, 0, 1008},
4260 { "dynamic-labels", no_argument, 0, 1009},
4266 opterr = 0; /* initialize getopt */
4267 rrd_parsetime("end-24h", &start_tv);
4268 rrd_parsetime("now", &end_tv);
4270 int option_index = 0;
4272 int col_start, col_end;
4274 opt = getopt_long(argc, argv,
4275 "Aa:B:b:c:Dd:Ee:Ff:G:gh:IiJjL:l:Mm:Nn:oPR:rS:s:T:t:u:v:W:w:X:x:Yy:z",
4276 long_options, &option_index);
4281 im->extra_flags |= NOMINOR;
4284 im->extra_flags |= ALTYGRID;
4287 im->extra_flags |= ALTAUTOSCALE;
4290 im->extra_flags |= ALTAUTOSCALE_MIN;
4293 im->extra_flags |= ALTAUTOSCALE_MAX;
4296 im->extra_flags |= ONLY_GRAPH;
4299 im->extra_flags |= NOLEGEND;
4302 if (strcmp(optarg, "north") == 0) {
4303 im->legendposition = NORTH;
4304 } else if (strcmp(optarg, "west") == 0) {
4305 im->legendposition = WEST;
4306 } else if (strcmp(optarg, "south") == 0) {
4307 im->legendposition = SOUTH;
4308 } else if (strcmp(optarg, "east") == 0) {
4309 im->legendposition = EAST;
4311 rrd_set_error("unknown legend-position '%s'", optarg);
4316 if (strcmp(optarg, "topdown") == 0) {
4317 im->legenddirection = TOP_DOWN;
4318 } else if (strcmp(optarg, "bottomup") == 0) {
4319 im->legenddirection = BOTTOM_UP;
4321 rrd_set_error("unknown legend-position '%s'", optarg);
4326 im->extra_flags |= FORCE_RULES_LEGEND;
4329 im->extra_flags |= NO_RRDTOOL_TAG;
4331 case LONGOPT_UNITS_SI:
4332 if (im->extra_flags & FORCE_UNITS) {
4333 rrd_set_error("--units can only be used once!");
4336 if (strcmp(optarg, "si") == 0)
4337 im->extra_flags |= FORCE_UNITS_SI;
4339 rrd_set_error("invalid argument for --units: %s", optarg);
4344 im->unitsexponent = atoi(optarg);
4347 im->unitslength = atoi(optarg);
4348 im->forceleftspace = 1;
4351 im->tabwidth = atof(optarg);
4354 im->step = atoi(optarg);
4360 im->with_markup = 1;
4363 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
4364 rrd_set_error("start time: %s", parsetime_error);
4369 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
4370 rrd_set_error("end time: %s", parsetime_error);
4375 if (strcmp(optarg, "none") == 0) {
4376 im->draw_x_grid = 0;
4380 "%10[A-Z]:%ld:%10[A-Z]:%ld:%10[A-Z]:%ld:%ld:%n",
4382 &im->xlab_user.gridst,
4384 &im->xlab_user.mgridst,
4386 &im->xlab_user.labst,
4387 &im->xlab_user.precis, &stroff) == 7 && stroff != 0) {
4388 strncpy(im->xlab_form, optarg + stroff,
4389 sizeof(im->xlab_form) - 1);
4390 im->xlab_form[sizeof(im->xlab_form) - 1] = '\0';
4392 (im->xlab_user.gridtm = tmt_conv(scan_gtm)) == -1) {
4393 rrd_set_error("unknown keyword %s", scan_gtm);
4396 (im->xlab_user.mgridtm = tmt_conv(scan_mtm))
4398 rrd_set_error("unknown keyword %s", scan_mtm);
4401 (im->xlab_user.labtm = tmt_conv(scan_ltm)) == -1) {
4402 rrd_set_error("unknown keyword %s", scan_ltm);
4405 im->xlab_user.minsec = 1;
4406 im->xlab_user.stst = im->xlab_form;
4408 rrd_set_error("invalid x-grid format");
4414 if (strcmp(optarg, "none") == 0) {
4415 im->draw_y_grid = 0;
4418 if (sscanf(optarg, "%lf:%d", &im->ygridstep, &im->ylabfact) == 2) {
4419 if (im->ygridstep <= 0) {
4420 rrd_set_error("grid step must be > 0");
4422 } else if (im->ylabfact < 1) {
4423 rrd_set_error("label factor must be > 0");
4427 rrd_set_error("invalid y-grid format");
4432 im->draw_3d_border = atoi(optarg);
4434 case 1008: /* grid-dash */
4438 &im->grid_dash_off) != 2) {
4439 rrd_set_error("expected grid-dash format float:float");
4443 case 1009: /* enable dynamic labels */
4444 im->dynamic_labels = 1;
4446 case 1002: /* right y axis */
4450 &im->second_axis_scale,
4451 &im->second_axis_shift) == 2) {
4452 if(im->second_axis_scale==0){
4453 rrd_set_error("the second_axis_scale must not be 0");
4457 rrd_set_error("invalid right-axis format expected scale:shift");
4462 strncpy(im->second_axis_legend,optarg,150);
4463 im->second_axis_legend[150]='\0';
4466 if (bad_format(optarg)){
4467 rrd_set_error("use either %le or %lf formats");
4470 strncpy(im->second_axis_format,optarg,150);
4471 im->second_axis_format[150]='\0';
4474 strncpy(im->ylegend, optarg, 150);
4475 im->ylegend[150] = '\0';
4478 im->maxval = atof(optarg);
4481 im->minval = atof(optarg);
4484 im->base = atol(optarg);
4485 if (im->base != 1024 && im->base != 1000) {
4487 ("the only sensible value for base apart from 1000 is 1024");
4492 long_tmp = atol(optarg);
4493 if (long_tmp < 10) {
4494 rrd_set_error("width below 10 pixels");
4497 im->xsize = long_tmp;
4500 long_tmp = atol(optarg);
4501 if (long_tmp < 10) {
4502 rrd_set_error("height below 10 pixels");
4505 im->ysize = long_tmp;
4508 im->extra_flags |= FULL_SIZE_MODE;
4511 /* interlaced png not supported at the moment */
4517 im->imginfo = optarg;
4521 (im->imgformat = if_conv(optarg)) == -1) {
4522 rrd_set_error("unsupported graphics format '%s'", optarg);
4533 im->logarithmic = 1;
4537 "%10[A-Z]#%n%8lx%n",
4538 col_nam, &col_start, &color, &col_end) == 2) {
4540 int col_len = col_end - col_start;
4545 (((color & 0xF00) * 0x110000) | ((color & 0x0F0) *
4553 (((color & 0xF000) *
4554 0x11000) | ((color & 0x0F00) *
4555 0x01100) | ((color &
4558 ((color & 0x000F) * 0x00011)
4562 color = (color << 8) + 0xff /* shift left by 8 */ ;
4567 rrd_set_error("the color format is #RRGGBB[AA]");
4570 if ((ci = grc_conv(col_nam)) != -1) {
4571 im->graph_col[ci] = gfx_hex_to_col(color);
4573 rrd_set_error("invalid color name '%s'", col_nam);
4577 rrd_set_error("invalid color def format");
4586 if (sscanf(optarg, "%10[A-Z]:%lf%n", prop, &size, &end) >= 2) {
4587 int sindex, propidx;
4589 if ((sindex = text_prop_conv(prop)) != -1) {
4590 for (propidx = sindex;
4591 propidx < TEXT_PROP_LAST; propidx++) {
4593 rrd_set_font_desc(im,propidx,NULL,size);
4595 if ((int) strlen(optarg) > end+2) {
4596 if (optarg[end] == ':') {
4597 rrd_set_font_desc(im,propidx,optarg + end + 1,0);
4600 ("expected : after font size in '%s'",
4605 /* only run the for loop for DEFAULT (0) for
4606 all others, we break here. woodo programming */
4607 if (propidx == sindex && sindex != 0)
4611 rrd_set_error("invalid fonttag '%s'", prop);
4615 rrd_set_error("invalid text property format");
4621 im->zoom = atof(optarg);
4622 if (im->zoom <= 0.0) {
4623 rrd_set_error("zoom factor must be > 0");
4628 strncpy(im->title, optarg, 150);
4629 im->title[150] = '\0';
4632 if (strcmp(optarg, "normal") == 0) {
4633 cairo_font_options_set_antialias
4634 (im->font_options, CAIRO_ANTIALIAS_GRAY);
4635 cairo_font_options_set_hint_style
4636 (im->font_options, CAIRO_HINT_STYLE_FULL);
4637 } else if (strcmp(optarg, "light") == 0) {
4638 cairo_font_options_set_antialias
4639 (im->font_options, CAIRO_ANTIALIAS_GRAY);
4640 cairo_font_options_set_hint_style
4641 (im->font_options, CAIRO_HINT_STYLE_SLIGHT);
4642 } else if (strcmp(optarg, "mono") == 0) {
4643 cairo_font_options_set_antialias
4644 (im->font_options, CAIRO_ANTIALIAS_NONE);
4645 cairo_font_options_set_hint_style
4646 (im->font_options, CAIRO_HINT_STYLE_FULL);
4648 rrd_set_error("unknown font-render-mode '%s'", optarg);
4653 if (strcmp(optarg, "normal") == 0)
4654 im->graph_antialias = CAIRO_ANTIALIAS_GRAY;
4655 else if (strcmp(optarg, "mono") == 0)
4656 im->graph_antialias = CAIRO_ANTIALIAS_NONE;
4658 rrd_set_error("unknown graph-render-mode '%s'", optarg);
4663 /* not supported curently */
4666 strncpy(im->watermark, optarg, 100);
4667 im->watermark[99] = '\0';
4671 if (im->daemon_addr != NULL)
4673 rrd_set_error ("You cannot specify --daemon "
4678 im->daemon_addr = strdup(optarg);
4679 if (im->daemon_addr == NULL)
4681 rrd_set_error("strdup failed");
4689 rrd_set_error("unknown option '%c'", optopt);
4691 rrd_set_error("unknown option '%s'", argv[optind - 1]);
4696 pango_cairo_context_set_font_options(pango_layout_get_context(im->layout), im->font_options);
4697 pango_layout_context_changed(im->layout);
4701 if (im->logarithmic && im->minval <= 0) {
4703 ("for a logarithmic yaxis you must specify a lower-limit > 0");
4707 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
4708 /* error string is set in rrd_parsetime.c */
4712 if (start_tmp < 3600 * 24 * 365 * 10) {
4714 ("the first entry to fetch should be after 1980 (%ld)",
4719 if (end_tmp < start_tmp) {
4721 ("start (%ld) should be less than end (%ld)", start_tmp, end_tmp);
4725 im->start = start_tmp;
4727 im->step = max((long) im->step, (im->end - im->start) / im->xsize);
4730 int rrd_graph_color(
4738 graph_desc_t *gdp = &im->gdes[im->gdes_c - 1];
4740 color = strstr(var, "#");
4741 if (color == NULL) {
4742 if (optional == 0) {
4743 rrd_set_error("Found no color in %s", err);
4750 long unsigned int col;
4752 rest = strstr(color, ":");
4759 sscanf(color, "#%6lx%n", &col, &n);
4760 col = (col << 8) + 0xff /* shift left by 8 */ ;
4762 rrd_set_error("Color problem in %s", err);
4765 sscanf(color, "#%8lx%n", &col, &n);
4769 rrd_set_error("Color problem in %s", err);
4771 if (rrd_test_error())
4773 gdp->col = gfx_hex_to_col(col);
4786 while (*ptr != '\0')
4787 if (*ptr++ == '%') {
4789 /* line cannot end with percent char */
4792 /* '%s', '%S' and '%%' are allowed */
4793 if (*ptr == 's' || *ptr == 'S' || *ptr == '%')
4795 /* %c is allowed (but use only with vdef!) */
4796 else if (*ptr == 'c') {
4801 /* or else '% 6.2lf' and such are allowed */
4803 /* optional padding character */
4804 if (*ptr == ' ' || *ptr == '+' || *ptr == '-')
4806 /* This should take care of 'm.n' with all three optional */
4807 while (*ptr >= '0' && *ptr <= '9')
4811 while (*ptr >= '0' && *ptr <= '9')
4813 /* Either 'le', 'lf' or 'lg' must follow here */
4816 if (*ptr == 'e' || *ptr == 'f' || *ptr == 'g')
4831 const char *const str)
4833 /* A VDEF currently is either "func" or "param,func"
4834 * so the parsing is rather simple. Change if needed.
4841 sscanf(str, "%le,%29[A-Z]%n", ¶m, func, &n);
4842 if (n == (int) strlen(str)) { /* matched */
4846 sscanf(str, "%29[A-Z]%n", func, &n);
4847 if (n == (int) strlen(str)) { /* matched */
4851 ("Unknown function string '%s' in VDEF '%s'",
4856 if (!strcmp("PERCENT", func))
4857 gdes->vf.op = VDEF_PERCENT;
4858 else if (!strcmp("PERCENTNAN", func))
4859 gdes->vf.op = VDEF_PERCENTNAN;
4860 else if (!strcmp("MAXIMUM", func))
4861 gdes->vf.op = VDEF_MAXIMUM;
4862 else if (!strcmp("AVERAGE", func))
4863 gdes->vf.op = VDEF_AVERAGE;
4864 else if (!strcmp("STDEV", func))
4865 gdes->vf.op = VDEF_STDEV;
4866 else if (!strcmp("MINIMUM", func))
4867 gdes->vf.op = VDEF_MINIMUM;
4868 else if (!strcmp("TOTAL", func))
4869 gdes->vf.op = VDEF_TOTAL;
4870 else if (!strcmp("FIRST", func))
4871 gdes->vf.op = VDEF_FIRST;
4872 else if (!strcmp("LAST", func))
4873 gdes->vf.op = VDEF_LAST;
4874 else if (!strcmp("LSLSLOPE", func))
4875 gdes->vf.op = VDEF_LSLSLOPE;
4876 else if (!strcmp("LSLINT", func))
4877 gdes->vf.op = VDEF_LSLINT;
4878 else if (!strcmp("LSLCORREL", func))
4879 gdes->vf.op = VDEF_LSLCORREL;
4882 ("Unknown function '%s' in VDEF '%s'\n", func, gdes->vname);
4885 switch (gdes->vf.op) {
4887 case VDEF_PERCENTNAN:
4888 if (isnan(param)) { /* no parameter given */
4890 ("Function '%s' needs parameter in VDEF '%s'\n",
4894 if (param >= 0.0 && param <= 100.0) {
4895 gdes->vf.param = param;
4896 gdes->vf.val = DNAN; /* undefined */
4897 gdes->vf.when = 0; /* undefined */
4900 ("Parameter '%f' out of range in VDEF '%s'\n",
4901 param, gdes->vname);
4914 case VDEF_LSLCORREL:
4916 gdes->vf.param = DNAN;
4917 gdes->vf.val = DNAN;
4921 ("Function '%s' needs no parameter in VDEF '%s'\n",
4935 graph_desc_t *src, *dst;
4939 dst = &im->gdes[gdi];
4940 src = &im->gdes[dst->vidx];
4941 data = src->data + src->ds;
4943 steps = (src->end - src->start) / src->step;
4946 ("DEBUG: start == %lu, end == %lu, %lu steps\n",
4947 src->start, src->end, steps);
4949 switch (dst->vf.op) {
4953 if ((array = (rrd_value_t*)malloc(steps * sizeof(double))) == NULL) {
4954 rrd_set_error("malloc VDEV_PERCENT");
4957 for (step = 0; step < steps; step++) {
4958 array[step] = data[step * src->ds_cnt];
4960 qsort(array, step, sizeof(double), vdef_percent_compar);
4961 field = round((dst->vf.param * (double)(steps - 1)) / 100.0);
4962 dst->vf.val = array[field];
4963 dst->vf.when = 0; /* no time component */
4966 for (step = 0; step < steps; step++)
4967 printf("DEBUG: %3li:%10.2f %c\n",
4968 step, array[step], step == field ? '*' : ' ');
4972 case VDEF_PERCENTNAN:{
4975 /* count number of "valid" values */
4977 for (step = 0; step < steps; step++) {
4978 if (!isnan(data[step * src->ds_cnt])) { nancount++; }
4980 /* and allocate it */
4981 if ((array = (rrd_value_t*)malloc(nancount * sizeof(double))) == NULL) {
4982 rrd_set_error("malloc VDEV_PERCENT");
4985 /* and fill it in */
4987 for (step = 0; step < steps; step++) {
4988 if (!isnan(data[step * src->ds_cnt])) {
4989 array[field] = data[step * src->ds_cnt];
4993 qsort(array, nancount, sizeof(double), vdef_percent_compar);
4994 field = round( dst->vf.param * (double)(nancount - 1) / 100.0);
4995 dst->vf.val = array[field];
4996 dst->vf.when = 0; /* no time component */
5002 while (step != steps && isnan(data[step * src->ds_cnt]))
5004 if (step == steps) {
5008 dst->vf.val = data[step * src->ds_cnt];
5009 dst->vf.when = src->start + (step + 1) * src->step;
5011 while (step != steps) {
5012 if (finite(data[step * src->ds_cnt])) {
5013 if (data[step * src->ds_cnt] > dst->vf.val) {
5014 dst->vf.val = data[step * src->ds_cnt];
5015 dst->vf.when = src->start + (step + 1) * src->step;
5026 double average = 0.0;
5028 for (step = 0; step < steps; step++) {
5029 if (finite(data[step * src->ds_cnt])) {
5030 sum += data[step * src->ds_cnt];
5035 if (dst->vf.op == VDEF_TOTAL) {
5036 dst->vf.val = sum * src->step;
5037 dst->vf.when = 0; /* no time component */
5038 } else if (dst->vf.op == VDEF_AVERAGE) {
5039 dst->vf.val = sum / cnt;
5040 dst->vf.when = 0; /* no time component */
5042 average = sum / cnt;
5044 for (step = 0; step < steps; step++) {
5045 if (finite(data[step * src->ds_cnt])) {
5046 sum += pow((data[step * src->ds_cnt] - average), 2.0);
5049 dst->vf.val = pow(sum / cnt, 0.5);
5050 dst->vf.when = 0; /* no time component */
5060 while (step != steps && isnan(data[step * src->ds_cnt]))
5062 if (step == steps) {
5066 dst->vf.val = data[step * src->ds_cnt];
5067 dst->vf.when = src->start + (step + 1) * src->step;
5069 while (step != steps) {
5070 if (finite(data[step * src->ds_cnt])) {
5071 if (data[step * src->ds_cnt] < dst->vf.val) {
5072 dst->vf.val = data[step * src->ds_cnt];
5073 dst->vf.when = src->start + (step + 1) * src->step;
5080 /* The time value returned here is one step before the
5081 * actual time value. This is the start of the first
5085 while (step != steps && isnan(data[step * src->ds_cnt]))
5087 if (step == steps) { /* all entries were NaN */
5091 dst->vf.val = data[step * src->ds_cnt];
5092 dst->vf.when = src->start + step * src->step;
5096 /* The time value returned here is the
5097 * actual time value. This is the end of the last
5101 while (step >= 0 && isnan(data[step * src->ds_cnt]))
5103 if (step < 0) { /* all entries were NaN */
5107 dst->vf.val = data[step * src->ds_cnt];
5108 dst->vf.when = src->start + (step + 1) * src->step;
5113 case VDEF_LSLCORREL:{
5114 /* Bestfit line by linear least squares method */
5117 double SUMx, SUMy, SUMxy, SUMxx, SUMyy, slope, y_intercept, correl;
5124 for (step = 0; step < steps; step++) {
5125 if (finite(data[step * src->ds_cnt])) {
5128 SUMxx += step * step;
5129 SUMxy += step * data[step * src->ds_cnt];
5130 SUMy += data[step * src->ds_cnt];
5131 SUMyy += data[step * src->ds_cnt] * data[step * src->ds_cnt];
5135 slope = (SUMx * SUMy - cnt * SUMxy) / (SUMx * SUMx - cnt * SUMxx);
5136 y_intercept = (SUMy - slope * SUMx) / cnt;
5139 (SUMx * SUMy) / cnt) /
5141 (SUMx * SUMx) / cnt) * (SUMyy - (SUMy * SUMy) / cnt));
5143 if (dst->vf.op == VDEF_LSLSLOPE) {
5144 dst->vf.val = slope;
5146 } else if (dst->vf.op == VDEF_LSLINT) {
5147 dst->vf.val = y_intercept;
5149 } else if (dst->vf.op == VDEF_LSLCORREL) {
5150 dst->vf.val = correl;
5163 /* NaN < -INF < finite_values < INF */
5164 int vdef_percent_compar(
5170 /* Equality is not returned; this doesn't hurt except
5171 * (maybe) for a little performance.
5174 /* First catch NaN values. They are smallest */
5175 if (isnan(*(double *) a))
5177 if (isnan(*(double *) b))
5179 /* NaN doesn't reach this part so INF and -INF are extremes.
5180 * The sign from isinf() is compatible with the sign we return
5182 if (isinf(*(double *) a))
5183 return isinf(*(double *) a);
5184 if (isinf(*(double *) b))
5185 return isinf(*(double *) b);
5186 /* If we reach this, both values must be finite */
5187 if (*(double *) a < *(double *) b)
5196 rrd_info_type_t type,
5197 rrd_infoval_t value)
5199 im->grinfo_current = rrd_info_push(im->grinfo_current, key, type, value);
5200 if (im->grinfo == NULL) {
5201 im->grinfo = im->grinfo_current;