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 */
62 char week_fmt[128] = "Week %V";
65 {0, 0, TMT_SECOND, 30, TMT_MINUTE, 5, TMT_MINUTE, 5, 0, "%H:%M"}
67 {2, 0, TMT_MINUTE, 1, TMT_MINUTE, 5, TMT_MINUTE, 5, 0, "%H:%M"}
69 {5, 0, TMT_MINUTE, 2, TMT_MINUTE, 10, TMT_MINUTE, 10, 0, "%H:%M"}
71 {10, 0, TMT_MINUTE, 5, TMT_MINUTE, 20, TMT_MINUTE, 20, 0, "%H:%M"}
73 {30, 0, TMT_MINUTE, 10, TMT_HOUR, 1, TMT_HOUR, 1, 0, "%H:%M"}
75 {60, 0, TMT_MINUTE, 30, TMT_HOUR, 2, TMT_HOUR, 2, 0, "%H:%M"}
77 {60, 24 * 3600, TMT_MINUTE, 30, TMT_HOUR, 2, TMT_HOUR, 6, 0, "%a %H:%M"}
79 {180, 0, TMT_HOUR, 1, TMT_HOUR, 6, TMT_HOUR, 6, 0, "%H:%M"}
81 {180, 24 * 3600, TMT_HOUR, 1, TMT_HOUR, 6, TMT_HOUR, 12, 0, "%a %H:%M"}
83 /*{300, 0, TMT_HOUR,3, TMT_HOUR,12, TMT_HOUR,12, 12*3600,"%a %p"}, this looks silly */
84 {600, 0, TMT_HOUR, 6, TMT_DAY, 1, TMT_DAY, 1, 24 * 3600, "%a"}
86 {1200, 0, TMT_HOUR, 6, TMT_DAY, 1, TMT_DAY, 1, 24 * 3600, "%d"}
88 {1800, 0, TMT_HOUR, 12, TMT_DAY, 1, TMT_DAY, 2, 24 * 3600, "%a %d"}
90 {2400, 0, TMT_HOUR, 12, TMT_DAY, 1, TMT_DAY, 2, 24 * 3600, "%a"}
92 {3600, 0, TMT_DAY, 1, TMT_WEEK, 1, TMT_WEEK, 1, 7 * 24 * 3600, week_fmt}
94 {3 * 3600, 0, TMT_WEEK, 1, TMT_MONTH, 1, TMT_WEEK, 2, 7 * 24 * 3600,
97 {6 * 3600, 0, TMT_MONTH, 1, TMT_MONTH, 1, TMT_MONTH, 1, 30 * 24 * 3600,
100 {48 * 3600, 0, TMT_MONTH, 1, TMT_MONTH, 3, TMT_MONTH, 3, 30 * 24 * 3600,
103 {315360, 0, TMT_MONTH, 3, TMT_YEAR, 1, TMT_YEAR, 1, 365 * 24 * 3600, "%Y"}
105 {10 * 24 * 3600, 0, TMT_YEAR, 1, TMT_YEAR, 1, TMT_YEAR, 1,
106 365 * 24 * 3600, "%y"}
108 {-1, 0, TMT_MONTH, 0, TMT_MONTH, 0, TMT_MONTH, 0, 0, ""}
111 /* sensible y label intervals ...*/
135 {20.0, {1, 5, 10, 20}
141 {100.0, {1, 2, 5, 10}
144 {200.0, {1, 5, 10, 20}
147 {500.0, {1, 2, 4, 10}
155 gfx_color_t graph_col[] = /* default colors */
157 {1.00, 1.00, 1.00, 1.00}, /* canvas */
158 {0.95, 0.95, 0.95, 1.00}, /* background */
159 {0.81, 0.81, 0.81, 1.00}, /* shade A */
160 {0.62, 0.62, 0.62, 1.00}, /* shade B */
161 {0.56, 0.56, 0.56, 0.75}, /* grid */
162 {0.87, 0.31, 0.31, 0.60}, /* major grid */
163 {0.00, 0.00, 0.00, 1.00}, /* font */
164 {0.50, 0.12, 0.12, 1.00}, /* arrow */
165 {0.12, 0.12, 0.12, 1.00}, /* axis */
166 {0.00, 0.00, 0.00, 1.00} /* frame */
173 # define DPRINT(x) (void)(printf x, printf("\n"))
179 /* initialize with xtr(im,0); */
187 pixie = (double) im->xsize / (double) (im->end - im->start);
190 return (int) ((double) im->xorigin + pixie * (mytime - im->start));
193 /* translate data values into y coordinates */
202 if (!im->logarithmic)
203 pixie = (double) im->ysize / (im->maxval - im->minval);
206 (double) im->ysize / (log10(im->maxval) - log10(im->minval));
208 } else if (!im->logarithmic) {
209 yval = im->yorigin - pixie * (value - im->minval);
211 if (value < im->minval) {
214 yval = im->yorigin - pixie * (log10(value) - log10(im->minval));
222 /* conversion function for symbolic entry names */
225 #define conv_if(VV,VVV) \
226 if (strcmp(#VV, string) == 0) return VVV ;
232 conv_if(PRINT, GF_PRINT);
233 conv_if(GPRINT, GF_GPRINT);
234 conv_if(COMMENT, GF_COMMENT);
235 conv_if(HRULE, GF_HRULE);
236 conv_if(VRULE, GF_VRULE);
237 conv_if(LINE, GF_LINE);
238 conv_if(AREA, GF_AREA);
239 conv_if(GRAD, GF_GRAD);
240 conv_if(STACK, GF_STACK);
241 conv_if(TICK, GF_TICK);
242 conv_if(TEXTALIGN, GF_TEXTALIGN);
243 conv_if(DEF, GF_DEF);
244 conv_if(CDEF, GF_CDEF);
245 conv_if(VDEF, GF_VDEF);
246 conv_if(XPORT, GF_XPORT);
247 conv_if(SHIFT, GF_SHIFT);
249 return (enum gf_en)(-1);
252 enum gfx_if_en if_conv(
256 conv_if(PNG, IF_PNG);
257 conv_if(SVG, IF_SVG);
258 conv_if(EPS, IF_EPS);
259 conv_if(PDF, IF_PDF);
261 return (enum gfx_if_en)(-1);
264 enum tmt_en tmt_conv(
268 conv_if(SECOND, TMT_SECOND);
269 conv_if(MINUTE, TMT_MINUTE);
270 conv_if(HOUR, TMT_HOUR);
271 conv_if(DAY, TMT_DAY);
272 conv_if(WEEK, TMT_WEEK);
273 conv_if(MONTH, TMT_MONTH);
274 conv_if(YEAR, TMT_YEAR);
275 return (enum tmt_en)(-1);
278 enum grc_en grc_conv(
282 conv_if(BACK, GRC_BACK);
283 conv_if(CANVAS, GRC_CANVAS);
284 conv_if(SHADEA, GRC_SHADEA);
285 conv_if(SHADEB, GRC_SHADEB);
286 conv_if(GRID, GRC_GRID);
287 conv_if(MGRID, GRC_MGRID);
288 conv_if(FONT, GRC_FONT);
289 conv_if(ARROW, GRC_ARROW);
290 conv_if(AXIS, GRC_AXIS);
291 conv_if(FRAME, GRC_FRAME);
293 return (enum grc_en)(-1);
296 enum text_prop_en text_prop_conv(
300 conv_if(DEFAULT, TEXT_PROP_DEFAULT);
301 conv_if(TITLE, TEXT_PROP_TITLE);
302 conv_if(AXIS, TEXT_PROP_AXIS);
303 conv_if(UNIT, TEXT_PROP_UNIT);
304 conv_if(LEGEND, TEXT_PROP_LEGEND);
305 conv_if(WATERMARK, TEXT_PROP_WATERMARK);
306 return (enum text_prop_en)(-1);
316 cairo_status_t status = (cairo_status_t) 0;
321 if (im->daemon_addr != NULL)
322 free(im->daemon_addr);
324 for (i = 0; i < (unsigned) im->gdes_c; i++) {
325 if (im->gdes[i].data_first) {
326 /* careful here, because a single pointer can occur several times */
327 free(im->gdes[i].data);
328 if (im->gdes[i].ds_namv) {
329 for (ii = 0; ii < im->gdes[i].ds_cnt; ii++)
330 free(im->gdes[i].ds_namv[ii]);
331 free(im->gdes[i].ds_namv);
334 /* free allocated memory used for dashed lines */
335 if (im->gdes[i].p_dashes != NULL)
336 free(im->gdes[i].p_dashes);
338 free(im->gdes[i].p_data);
339 free(im->gdes[i].rpnp);
343 for (i = 0; i < DIM(text_prop);i++){
344 pango_font_description_free(im->text_prop[i].font_desc);
345 im->text_prop[i].font_desc = NULL;
348 if (im->font_options)
349 cairo_font_options_destroy(im->font_options);
352 status = cairo_status(im->cr);
353 cairo_destroy(im->cr);
357 if (im->rendered_image) {
358 free(im->rendered_image);
362 g_object_unref (im->layout);
366 cairo_surface_destroy(im->surface);
369 fprintf(stderr, "OOPS: Cairo has issues it can't even die: %s\n",
370 cairo_status_to_string(status));
375 /* find SI magnitude symbol for the given number*/
377 image_desc_t *im, /* image description */
383 char *symbol[] = { "a", /* 10e-18 Atto */
384 "f", /* 10e-15 Femto */
385 "p", /* 10e-12 Pico */
386 "n", /* 10e-9 Nano */
387 "u", /* 10e-6 Micro */
388 "m", /* 10e-3 Milli */
393 "T", /* 10e12 Tera */
394 "P", /* 10e15 Peta */
401 if (*value == 0.0 || isnan(*value)) {
405 sindex = floor(log(fabs(*value)) / log((double) im->base));
406 *magfact = pow((double) im->base, (double) sindex);
407 (*value) /= (*magfact);
409 if (sindex <= symbcenter && sindex >= -symbcenter) {
410 (*symb_ptr) = symbol[sindex + symbcenter];
417 static char si_symbol[] = {
418 'a', /* 10e-18 Atto */
419 'f', /* 10e-15 Femto */
420 'p', /* 10e-12 Pico */
421 'n', /* 10e-9 Nano */
422 'u', /* 10e-6 Micro */
423 'm', /* 10e-3 Milli */
428 'T', /* 10e12 Tera */
429 'P', /* 10e15 Peta */
432 static const int si_symbcenter = 6;
434 /* find SI magnitude symbol for the numbers on the y-axis*/
436 image_desc_t *im /* image description */
440 double digits, viewdigits = 0;
443 floor(log(max(fabs(im->minval), fabs(im->maxval))) /
444 log((double) im->base));
446 if (im->unitsexponent != 9999) {
447 /* unitsexponent = 9, 6, 3, 0, -3, -6, -9, etc */
448 viewdigits = floor((double)(im->unitsexponent / 3));
453 im->magfact = pow((double) im->base, digits);
456 printf("digits %6.3f im->magfact %6.3f\n", digits, im->magfact);
459 im->viewfactor = im->magfact / pow((double) im->base, viewdigits);
461 if (((viewdigits + si_symbcenter) < sizeof(si_symbol)) &&
462 ((viewdigits + si_symbcenter) >= 0))
463 im->symbol = si_symbol[(int) viewdigits + si_symbcenter];
468 /* move min and max values around to become sensible */
473 double sensiblevalues[] = { 1000.0, 900.0, 800.0, 750.0, 700.0,
474 600.0, 500.0, 400.0, 300.0, 250.0,
475 200.0, 125.0, 100.0, 90.0, 80.0,
476 75.0, 70.0, 60.0, 50.0, 40.0, 30.0,
477 25.0, 20.0, 10.0, 9.0, 8.0,
478 7.0, 6.0, 5.0, 4.0, 3.5, 3.0,
479 2.5, 2.0, 1.8, 1.5, 1.2, 1.0,
480 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0, -1
483 double scaled_min, scaled_max;
490 printf("Min: %6.2f Max: %6.2f MagFactor: %6.2f\n",
491 im->minval, im->maxval, im->magfact);
494 if (isnan(im->ygridstep)) {
495 if (im->extra_flags & ALTAUTOSCALE) {
496 /* measure the amplitude of the function. Make sure that
497 graph boundaries are slightly higher then max/min vals
498 so we can see amplitude on the graph */
501 delt = im->maxval - im->minval;
503 fact = 2.0 * pow(10.0,
505 (max(fabs(im->minval), fabs(im->maxval)) /
508 adj = (fact - delt) * 0.55;
511 ("Min: %6.2f Max: %6.2f delt: %6.2f fact: %6.2f adj: %6.2f\n",
512 im->minval, im->maxval, delt, fact, adj);
517 } else if (im->extra_flags & ALTAUTOSCALE_MIN) {
518 /* measure the amplitude of the function. Make sure that
519 graph boundaries are slightly lower than min vals
520 so we can see amplitude on the graph */
521 adj = (im->maxval - im->minval) * 0.1;
523 } else if (im->extra_flags & ALTAUTOSCALE_MAX) {
524 /* measure the amplitude of the function. Make sure that
525 graph boundaries are slightly higher than max vals
526 so we can see amplitude on the graph */
527 adj = (im->maxval - im->minval) * 0.1;
530 scaled_min = im->minval / im->magfact;
531 scaled_max = im->maxval / im->magfact;
533 for (i = 1; sensiblevalues[i] > 0; i++) {
534 if (sensiblevalues[i - 1] >= scaled_min &&
535 sensiblevalues[i] <= scaled_min)
536 im->minval = sensiblevalues[i] * (im->magfact);
538 if (-sensiblevalues[i - 1] <= scaled_min &&
539 -sensiblevalues[i] >= scaled_min)
540 im->minval = -sensiblevalues[i - 1] * (im->magfact);
542 if (sensiblevalues[i - 1] >= scaled_max &&
543 sensiblevalues[i] <= scaled_max)
544 im->maxval = sensiblevalues[i - 1] * (im->magfact);
546 if (-sensiblevalues[i - 1] <= scaled_max &&
547 -sensiblevalues[i] >= scaled_max)
548 im->maxval = -sensiblevalues[i] * (im->magfact);
552 /* adjust min and max to the grid definition if there is one */
553 im->minval = (double) im->ylabfact * im->ygridstep *
554 floor(im->minval / ((double) im->ylabfact * im->ygridstep));
555 im->maxval = (double) im->ylabfact * im->ygridstep *
556 ceil(im->maxval / ((double) im->ylabfact * im->ygridstep));
560 fprintf(stderr, "SCALED Min: %6.2f Max: %6.2f Factor: %6.2f\n",
561 im->minval, im->maxval, im->magfact);
569 if (isnan(im->minval) || isnan(im->maxval))
572 if (im->logarithmic) {
573 double ya, yb, ypix, ypixfrac;
574 double log10_range = log10(im->maxval) - log10(im->minval);
576 ya = pow((double) 10, floor(log10(im->minval)));
577 while (ya < im->minval)
580 return; /* don't have y=10^x gridline */
582 if (yb <= im->maxval) {
583 /* we have at least 2 y=10^x gridlines.
584 Make sure distance between them in pixels
585 are an integer by expanding im->maxval */
586 double y_pixel_delta = ytr(im, ya) - ytr(im, yb);
587 double factor = y_pixel_delta / floor(y_pixel_delta);
588 double new_log10_range = factor * log10_range;
589 double new_ymax_log10 = log10(im->minval) + new_log10_range;
591 im->maxval = pow(10, new_ymax_log10);
592 ytr(im, DNAN); /* reset precalc */
593 log10_range = log10(im->maxval) - log10(im->minval);
595 /* make sure first y=10^x gridline is located on
596 integer pixel position by moving scale slightly
597 downwards (sub-pixel movement) */
598 ypix = ytr(im, ya) + im->ysize; /* add im->ysize so it always is positive */
599 ypixfrac = ypix - floor(ypix);
600 if (ypixfrac > 0 && ypixfrac < 1) {
601 double yfrac = ypixfrac / im->ysize;
603 im->minval = pow(10, log10(im->minval) - yfrac * log10_range);
604 im->maxval = pow(10, log10(im->maxval) - yfrac * log10_range);
605 ytr(im, DNAN); /* reset precalc */
608 /* Make sure we have an integer pixel distance between
609 each minor gridline */
610 double ypos1 = ytr(im, im->minval);
611 double ypos2 = ytr(im, im->minval + im->ygrid_scale.gridstep);
612 double y_pixel_delta = ypos1 - ypos2;
613 double factor = y_pixel_delta / floor(y_pixel_delta);
614 double new_range = factor * (im->maxval - im->minval);
615 double gridstep = im->ygrid_scale.gridstep;
616 double minor_y, minor_y_px, minor_y_px_frac;
618 if (im->maxval > 0.0)
619 im->maxval = im->minval + new_range;
621 im->minval = im->maxval - new_range;
622 ytr(im, DNAN); /* reset precalc */
623 /* make sure first minor gridline is on integer pixel y coord */
624 minor_y = gridstep * floor(im->minval / gridstep);
625 while (minor_y < im->minval)
627 minor_y_px = ytr(im, minor_y) + im->ysize; /* ensure > 0 by adding ysize */
628 minor_y_px_frac = minor_y_px - floor(minor_y_px);
629 if (minor_y_px_frac > 0 && minor_y_px_frac < 1) {
630 double yfrac = minor_y_px_frac / im->ysize;
631 double range = im->maxval - im->minval;
633 im->minval = im->minval - yfrac * range;
634 im->maxval = im->maxval - yfrac * range;
635 ytr(im, DNAN); /* reset precalc */
637 calc_horizontal_grid(im); /* recalc with changed im->maxval */
641 /* reduce data reimplementation by Alex */
644 enum cf_en cf, /* which consolidation function ? */
645 unsigned long cur_step, /* step the data currently is in */
646 time_t *start, /* start, end and step as requested ... */
647 time_t *end, /* ... by the application will be ... */
648 unsigned long *step, /* ... adjusted to represent reality */
649 unsigned long *ds_cnt, /* number of data sources in file */
651 { /* two dimensional array containing the data */
652 int i, reduce_factor = ceil((double) (*step) / (double) cur_step);
653 unsigned long col, dst_row, row_cnt, start_offset, end_offset, skiprows =
655 rrd_value_t *srcptr, *dstptr;
657 (*step) = cur_step * reduce_factor; /* set new step size for reduced data */
660 row_cnt = ((*end) - (*start)) / cur_step;
666 printf("Reducing %lu rows with factor %i time %lu to %lu, step %lu\n",
667 row_cnt, reduce_factor, *start, *end, cur_step);
668 for (col = 0; col < row_cnt; col++) {
669 printf("time %10lu: ", *start + (col + 1) * cur_step);
670 for (i = 0; i < *ds_cnt; i++)
671 printf(" %8.2e", srcptr[*ds_cnt * col + i]);
676 /* We have to combine [reduce_factor] rows of the source
677 ** into one row for the destination. Doing this we also
678 ** need to take care to combine the correct rows. First
679 ** alter the start and end time so that they are multiples
680 ** of the new step time. We cannot reduce the amount of
681 ** time so we have to move the end towards the future and
682 ** the start towards the past.
684 end_offset = (*end) % (*step);
685 start_offset = (*start) % (*step);
687 /* If there is a start offset (which cannot be more than
688 ** one destination row), skip the appropriate number of
689 ** source rows and one destination row. The appropriate
690 ** number is what we do know (start_offset/cur_step) of
691 ** the new interval (*step/cur_step aka reduce_factor).
694 printf("start_offset: %lu end_offset: %lu\n", start_offset, end_offset);
695 printf("row_cnt before: %lu\n", row_cnt);
698 (*start) = (*start) - start_offset;
699 skiprows = reduce_factor - start_offset / cur_step;
700 srcptr += skiprows * *ds_cnt;
701 for (col = 0; col < (*ds_cnt); col++)
706 printf("row_cnt between: %lu\n", row_cnt);
709 /* At the end we have some rows that are not going to be
710 ** used, the amount is end_offset/cur_step
713 (*end) = (*end) - end_offset + (*step);
714 skiprows = end_offset / cur_step;
718 printf("row_cnt after: %lu\n", row_cnt);
721 /* Sanity check: row_cnt should be multiple of reduce_factor */
722 /* if this gets triggered, something is REALLY WRONG ... we die immediately */
724 if (row_cnt % reduce_factor) {
725 printf("SANITY CHECK: %lu rows cannot be reduced by %i \n",
726 row_cnt, reduce_factor);
727 printf("BUG in reduce_data()\n");
731 /* Now combine reduce_factor intervals at a time
732 ** into one interval for the destination.
735 for (dst_row = 0; (long int) row_cnt >= reduce_factor; dst_row++) {
736 for (col = 0; col < (*ds_cnt); col++) {
737 rrd_value_t newval = DNAN;
738 unsigned long validval = 0;
740 for (i = 0; i < reduce_factor; i++) {
741 if (isnan(srcptr[i * (*ds_cnt) + col])) {
746 newval = srcptr[i * (*ds_cnt) + col];
755 newval += srcptr[i * (*ds_cnt) + col];
758 newval = min(newval, srcptr[i * (*ds_cnt) + col]);
761 /* an interval contains a failure if any subintervals contained a failure */
763 newval = max(newval, srcptr[i * (*ds_cnt) + col]);
766 newval = srcptr[i * (*ds_cnt) + col];
792 srcptr += (*ds_cnt) * reduce_factor;
793 row_cnt -= reduce_factor;
795 /* If we had to alter the endtime, we didn't have enough
796 ** source rows to fill the last row. Fill it with NaN.
799 for (col = 0; col < (*ds_cnt); col++)
802 row_cnt = ((*end) - (*start)) / *step;
804 printf("Done reducing. Currently %lu rows, time %lu to %lu, step %lu\n",
805 row_cnt, *start, *end, *step);
806 for (col = 0; col < row_cnt; col++) {
807 printf("time %10lu: ", *start + (col + 1) * (*step));
808 for (i = 0; i < *ds_cnt; i++)
809 printf(" %8.2e", srcptr[*ds_cnt * col + i]);
816 /* get the data required for the graphs from the
825 /* pull the data from the rrd files ... */
826 for (i = 0; i < (int) im->gdes_c; i++) {
827 /* only GF_DEF elements fetch data */
828 if (im->gdes[i].gf != GF_DEF)
832 /* do we have it already ? */
833 for (ii = 0; ii < i; ii++) {
834 if (im->gdes[ii].gf != GF_DEF)
836 if ((strcmp(im->gdes[i].rrd, im->gdes[ii].rrd) == 0)
837 && (im->gdes[i].cf == im->gdes[ii].cf)
838 && (im->gdes[i].cf_reduce == im->gdes[ii].cf_reduce)
839 && (im->gdes[i].start_orig == im->gdes[ii].start_orig)
840 && (im->gdes[i].end_orig == im->gdes[ii].end_orig)
841 && (im->gdes[i].step_orig == im->gdes[ii].step_orig)) {
842 /* OK, the data is already there.
843 ** Just copy the header portion
845 im->gdes[i].start = im->gdes[ii].start;
846 im->gdes[i].end = im->gdes[ii].end;
847 im->gdes[i].step = im->gdes[ii].step;
848 im->gdes[i].ds_cnt = im->gdes[ii].ds_cnt;
849 im->gdes[i].ds_namv = im->gdes[ii].ds_namv;
850 im->gdes[i].data = im->gdes[ii].data;
851 im->gdes[i].data_first = 0;
858 unsigned long ft_step = im->gdes[i].step; /* ft_step will record what we got from fetch */
859 const char *rrd_daemon;
862 if (im->gdes[i].daemon[0] != 0)
863 rrd_daemon = im->gdes[i].daemon;
865 rrd_daemon = im->daemon_addr;
867 /* "daemon" may be NULL. ENV_RRDCACHED_ADDRESS is evaluated in that
868 * case. If "daemon" holds the same value as in the previous
869 * iteration, no actual new connection is established - the
870 * existing connection is re-used. */
871 rrdc_connect (rrd_daemon);
873 /* If connecting was successfull, use the daemon to query the data.
874 * If there is no connection, for example because no daemon address
875 * was specified, (try to) use the local file directly. */
876 if (rrdc_is_connected (rrd_daemon))
878 status = rrdc_fetch (im->gdes[i].rrd,
879 cf_to_string (im->gdes[i].cf),
884 &im->gdes[i].ds_namv,
891 if ((rrd_fetch_fn(im->gdes[i].rrd,
897 &im->gdes[i].ds_namv,
898 &im->gdes[i].data)) == -1) {
902 im->gdes[i].data_first = 1;
904 /* must reduce to at least im->step
905 otherwhise we end up with more data than we can handle in the
906 chart and visibility of data will be random */
907 im->gdes[i].step = max(im->gdes[i].step,im->step);
908 if (ft_step < im->gdes[i].step) {
909 reduce_data(im->gdes[i].cf_reduce,
914 &im->gdes[i].ds_cnt, &im->gdes[i].data);
916 im->gdes[i].step = ft_step;
920 /* lets see if the required data source is really there */
921 for (ii = 0; ii < (int) im->gdes[i].ds_cnt; ii++) {
922 if (strcmp(im->gdes[i].ds_namv[ii], im->gdes[i].ds_nam) == 0) {
926 if (im->gdes[i].ds == -1) {
927 rrd_set_error("No DS called '%s' in '%s'",
928 im->gdes[i].ds_nam, im->gdes[i].rrd);
936 /* evaluate the expressions in the CDEF functions */
938 /*************************************************************
940 *************************************************************/
942 long find_var_wrapper(
946 return find_var((image_desc_t *) arg1, key);
949 /* find gdes containing var*/
956 for (ii = 0; ii < im->gdes_c - 1; ii++) {
957 if ((im->gdes[ii].gf == GF_DEF
958 || im->gdes[ii].gf == GF_VDEF || im->gdes[ii].gf == GF_CDEF)
959 && (strcmp(im->gdes[ii].vname, key) == 0)) {
966 /* find the greatest common divisor for all the numbers
967 in the 0 terminated num array */
974 for (i = 0; num[i + 1] != 0; i++) {
976 rest = num[i] % num[i + 1];
982 /* return i==0?num[i]:num[i-1]; */
986 /* run the rpn calculator on all the VDEF and CDEF arguments */
993 long *steparray, rpi;
998 rpnstack_init(&rpnstack);
1000 for (gdi = 0; gdi < im->gdes_c; gdi++) {
1001 /* Look for GF_VDEF and GF_CDEF in the same loop,
1002 * so CDEFs can use VDEFs and vice versa
1004 switch (im->gdes[gdi].gf) {
1008 graph_desc_t *vdp = &im->gdes[im->gdes[gdi].vidx];
1010 /* remove current shift */
1011 vdp->start -= vdp->shift;
1012 vdp->end -= vdp->shift;
1015 if (im->gdes[gdi].shidx >= 0)
1016 vdp->shift = im->gdes[im->gdes[gdi].shidx].vf.val;
1019 vdp->shift = im->gdes[gdi].shval;
1021 /* normalize shift to multiple of consolidated step */
1022 vdp->shift = (vdp->shift / (long) vdp->step) * (long) vdp->step;
1025 vdp->start += vdp->shift;
1026 vdp->end += vdp->shift;
1030 /* A VDEF has no DS. This also signals other parts
1031 * of rrdtool that this is a VDEF value, not a CDEF.
1033 im->gdes[gdi].ds_cnt = 0;
1034 if (vdef_calc(im, gdi)) {
1035 rrd_set_error("Error processing VDEF '%s'",
1036 im->gdes[gdi].vname);
1037 rpnstack_free(&rpnstack);
1042 im->gdes[gdi].ds_cnt = 1;
1043 im->gdes[gdi].ds = 0;
1044 im->gdes[gdi].data_first = 1;
1045 im->gdes[gdi].start = 0;
1046 im->gdes[gdi].end = 0;
1051 /* Find the variables in the expression.
1052 * - VDEF variables are substituted by their values
1053 * and the opcode is changed into OP_NUMBER.
1054 * - CDEF variables are analized for their step size,
1055 * the lowest common denominator of all the step
1056 * sizes of the data sources involved is calculated
1057 * and the resulting number is the step size for the
1058 * resulting data source.
1060 for (rpi = 0; im->gdes[gdi].rpnp[rpi].op != OP_END; rpi++) {
1061 if (im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE ||
1062 im->gdes[gdi].rpnp[rpi].op == OP_PREV_OTHER) {
1063 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
1065 if (im->gdes[ptr].ds_cnt == 0) { /* this is a VDEF data source */
1068 ("DEBUG: inside CDEF '%s' processing VDEF '%s'\n",
1069 im->gdes[gdi].vname, im->gdes[ptr].vname);
1070 printf("DEBUG: value from vdef is %f\n",
1071 im->gdes[ptr].vf.val);
1073 im->gdes[gdi].rpnp[rpi].val = im->gdes[ptr].vf.val;
1074 im->gdes[gdi].rpnp[rpi].op = OP_NUMBER;
1075 } else { /* normal variables and PREF(variables) */
1077 /* add one entry to the array that keeps track of the step sizes of the
1078 * data sources going into the CDEF. */
1080 (long*)rrd_realloc(steparray,
1082 1) * sizeof(*steparray))) == NULL) {
1083 rrd_set_error("realloc steparray");
1084 rpnstack_free(&rpnstack);
1088 steparray[stepcnt - 1] = im->gdes[ptr].step;
1090 /* adjust start and end of cdef (gdi) so
1091 * that it runs from the latest start point
1092 * to the earliest endpoint of any of the
1093 * rras involved (ptr)
1096 if (im->gdes[gdi].start < im->gdes[ptr].start)
1097 im->gdes[gdi].start = im->gdes[ptr].start;
1099 if (im->gdes[gdi].end == 0 ||
1100 im->gdes[gdi].end > im->gdes[ptr].end)
1101 im->gdes[gdi].end = im->gdes[ptr].end;
1103 /* store pointer to the first element of
1104 * the rra providing data for variable,
1105 * further save step size and data source
1108 im->gdes[gdi].rpnp[rpi].data =
1109 im->gdes[ptr].data + im->gdes[ptr].ds;
1110 im->gdes[gdi].rpnp[rpi].step = im->gdes[ptr].step;
1111 im->gdes[gdi].rpnp[rpi].ds_cnt = im->gdes[ptr].ds_cnt;
1113 /* backoff the *.data ptr; this is done so
1114 * rpncalc() function doesn't have to treat
1115 * the first case differently
1117 } /* if ds_cnt != 0 */
1118 } /* if OP_VARIABLE */
1119 } /* loop through all rpi */
1121 /* move the data pointers to the correct period */
1122 for (rpi = 0; im->gdes[gdi].rpnp[rpi].op != OP_END; rpi++) {
1123 if (im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE ||
1124 im->gdes[gdi].rpnp[rpi].op == OP_PREV_OTHER) {
1125 long ptr = im->gdes[gdi].rpnp[rpi].ptr;
1127 im->gdes[gdi].start - im->gdes[ptr].start;
1130 im->gdes[gdi].rpnp[rpi].data +=
1131 (diff / im->gdes[ptr].step) *
1132 im->gdes[ptr].ds_cnt;
1136 if (steparray == NULL) {
1137 rrd_set_error("rpn expressions without DEF"
1138 " or CDEF variables are not supported");
1139 rpnstack_free(&rpnstack);
1142 steparray[stepcnt] = 0;
1143 /* Now find the resulting step. All steps in all
1144 * used RRAs have to be visited
1146 im->gdes[gdi].step = lcd(steparray);
1148 if ((im->gdes[gdi].data = (rrd_value_t*)malloc(((im->gdes[gdi].end -
1149 im->gdes[gdi].start)
1150 / im->gdes[gdi].step)
1151 * sizeof(double))) == NULL) {
1152 rrd_set_error("malloc im->gdes[gdi].data");
1153 rpnstack_free(&rpnstack);
1157 /* Step through the new cdef results array and
1158 * calculate the values
1160 for (now = im->gdes[gdi].start + im->gdes[gdi].step;
1161 now <= im->gdes[gdi].end; now += im->gdes[gdi].step) {
1162 rpnp_t *rpnp = im->gdes[gdi].rpnp;
1164 /* 3rd arg of rpn_calc is for OP_VARIABLE lookups;
1165 * in this case we are advancing by timesteps;
1166 * we use the fact that time_t is a synonym for long
1168 if (rpn_calc(rpnp, &rpnstack, (long) now,
1169 im->gdes[gdi].data, ++dataidx) == -1) {
1170 /* rpn_calc sets the error string */
1171 rpnstack_free(&rpnstack);
1174 } /* enumerate over time steps within a CDEF */
1179 } /* enumerate over CDEFs */
1180 rpnstack_free(&rpnstack);
1184 /* from http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm */
1185 /* yes we are loosing precision by doing tos with floats instead of doubles
1186 but it seems more stable this way. */
1188 static int AlmostEqual2sComplement(
1194 int aInt = *(int *) &A;
1195 int bInt = *(int *) &B;
1198 /* Make sure maxUlps is non-negative and small enough that the
1199 default NAN won't compare as equal to anything. */
1201 /* assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024); */
1203 /* Make aInt lexicographically ordered as a twos-complement int */
1206 aInt = 0x80000000l - aInt;
1208 /* Make bInt lexicographically ordered as a twos-complement int */
1211 bInt = 0x80000000l - bInt;
1213 intDiff = abs(aInt - bInt);
1215 if (intDiff <= maxUlps)
1221 /* massage data so, that we get one value for each x coordinate in the graph */
1226 double pixstep = (double) (im->end - im->start)
1227 / (double) im->xsize; /* how much time
1228 passes in one pixel */
1230 double minval = DNAN, maxval = DNAN;
1232 unsigned long gr_time;
1234 /* memory for the processed data */
1235 for (i = 0; i < im->gdes_c; i++) {
1236 if ((im->gdes[i].gf == GF_LINE)
1237 || (im->gdes[i].gf == GF_AREA)
1238 || (im->gdes[i].gf == GF_TICK)
1239 || (im->gdes[i].gf == GF_GRAD)
1241 if ((im->gdes[i].p_data = (rrd_value_t*)malloc((im->xsize + 1)
1242 * sizeof(rrd_value_t))) == NULL) {
1243 rrd_set_error("malloc data_proc");
1249 for (i = 0; i < im->xsize; i++) { /* for each pixel */
1252 gr_time = im->start + pixstep * i; /* time of the current step */
1255 for (ii = 0; ii < im->gdes_c; ii++) {
1258 switch (im->gdes[ii].gf) {
1263 if (!im->gdes[ii].stack)
1265 value = im->gdes[ii].yrule;
1266 if (isnan(value) || (im->gdes[ii].gf == GF_TICK)) {
1267 /* The time of the data doesn't necessarily match
1268 ** the time of the graph. Beware.
1270 vidx = im->gdes[ii].vidx;
1271 if (im->gdes[vidx].gf == GF_VDEF) {
1272 value = im->gdes[vidx].vf.val;
1274 if (((long int) gr_time >=
1275 (long int) im->gdes[vidx].start)
1276 && ((long int) gr_time <
1277 (long int) im->gdes[vidx].end)) {
1278 value = im->gdes[vidx].data[(unsigned long)
1284 im->gdes[vidx].step)
1285 * im->gdes[vidx].ds_cnt +
1292 if (!isnan(value)) {
1294 im->gdes[ii].p_data[i] = paintval;
1295 /* GF_TICK: the data values are not
1296 ** relevant for min and max
1298 if (finite(paintval) && im->gdes[ii].gf != GF_TICK) {
1299 if ((isnan(minval) || paintval < minval) &&
1300 !(im->logarithmic && paintval <= 0.0))
1302 if (isnan(maxval) || paintval > maxval)
1306 im->gdes[ii].p_data[i] = DNAN;
1311 ("STACK should already be turned into LINE or AREA here");
1320 /* if min or max have not been asigned a value this is because
1321 there was no data in the graph ... this is not good ...
1322 lets set these to dummy values then ... */
1324 if (im->logarithmic) {
1325 if (isnan(minval) || isnan(maxval) || maxval <= 0) {
1326 minval = 0.0; /* catching this right away below */
1329 /* in logarithm mode, where minval is smaller or equal
1330 to 0 make the beast just way smaller than maxval */
1332 minval = maxval / 10e8;
1335 if (isnan(minval) || isnan(maxval)) {
1341 /* adjust min and max values given by the user */
1342 /* for logscale we add something on top */
1343 if (isnan(im->minval)
1344 || ((!im->rigid) && im->minval > minval)
1346 if (im->logarithmic)
1347 im->minval = minval / 2.0;
1349 im->minval = minval;
1351 if (isnan(im->maxval)
1352 || (!im->rigid && im->maxval < maxval)
1354 if (im->logarithmic)
1355 im->maxval = maxval * 2.0;
1357 im->maxval = maxval;
1360 /* make sure min is smaller than max */
1361 if (im->minval > im->maxval) {
1363 im->minval = 0.99 * im->maxval;
1365 im->minval = 1.01 * im->maxval;
1368 /* make sure min and max are not equal */
1369 if (AlmostEqual2sComplement(im->minval, im->maxval, 4)) {
1375 /* make sure min and max are not both zero */
1376 if (AlmostEqual2sComplement(im->maxval, 0, 4)) {
1383 static int find_first_weekday(void){
1384 static int first_weekday = -1;
1385 if (first_weekday == -1){
1386 #ifdef HAVE__NL_TIME_WEEK_1STDAY
1387 /* according to http://sourceware.org/ml/libc-locales/2009-q1/msg00011.html */
1388 long week_1stday_l = (long) nl_langinfo (_NL_TIME_WEEK_1STDAY);
1389 if (week_1stday_l == 19971130) first_weekday = 0; /* Sun */
1390 else if (week_1stday_l == 19971201) first_weekday = 1; /* Mon */
1391 else first_weekday = 1; /* we go for a monday default */
1396 return first_weekday;
1399 /* identify the point where the first gridline, label ... gets placed */
1401 time_t find_first_time(
1402 time_t start, /* what is the initial time */
1403 enum tmt_en baseint, /* what is the basic interval */
1404 long basestep /* how many if these do we jump a time */
1409 localtime_r(&start, &tm);
1413 tm. tm_sec -= tm.tm_sec % basestep;
1418 tm. tm_min -= tm.tm_min % basestep;
1424 tm. tm_hour -= tm.tm_hour % basestep;
1428 /* we do NOT look at the basestep for this ... */
1435 /* we do NOT look at the basestep for this ... */
1439 tm. tm_mday -= tm.tm_wday - find_first_weekday();
1441 if (tm.tm_wday == 0 && find_first_weekday() > 0)
1442 tm. tm_mday -= 7; /* we want the *previous* week */
1450 tm. tm_mon -= tm.tm_mon % basestep;
1461 tm.tm_year + 1900) %basestep;
1467 /* identify the point where the next gridline, label ... gets placed */
1468 time_t find_next_time(
1469 time_t current, /* what is the initial time */
1470 enum tmt_en baseint, /* what is the basic interval */
1471 long basestep /* how many if these do we jump a time */
1477 localtime_r(¤t, &tm);
1481 case TMT_SECOND: limit = 7200; break;
1482 case TMT_MINUTE: limit = 120; break;
1483 case TMT_HOUR: limit = 2; break;
1484 default: limit = 2; break;
1489 tm. tm_sec += basestep;
1493 tm. tm_min += basestep;
1497 tm. tm_hour += basestep;
1501 tm. tm_mday += basestep;
1505 tm. tm_mday += 7 * basestep;
1509 tm. tm_mon += basestep;
1513 tm. tm_year += basestep;
1515 madetime = mktime(&tm);
1516 } while (madetime == -1 && limit-- >= 0); /* this is necessary to skip impossible times
1517 like the daylight saving time skips */
1523 /* calculate values required for PRINT and GPRINT functions */
1528 long i, ii, validsteps;
1531 int graphelement = 0;
1534 double magfact = -1;
1539 /* wow initializing tmvdef is quite a task :-) */
1540 time_t now = time(NULL);
1542 localtime_r(&now, &tmvdef);
1543 for (i = 0; i < im->gdes_c; i++) {
1544 vidx = im->gdes[i].vidx;
1545 switch (im->gdes[i].gf) {
1548 /* PRINT and GPRINT can now print VDEF generated values.
1549 * There's no need to do any calculations on them as these
1550 * calculations were already made.
1552 if (im->gdes[vidx].gf == GF_VDEF) { /* simply use vals */
1553 printval = im->gdes[vidx].vf.val;
1554 localtime_r(&im->gdes[vidx].vf.when, &tmvdef);
1555 } else { /* need to calculate max,min,avg etcetera */
1556 max_ii = ((im->gdes[vidx].end - im->gdes[vidx].start)
1557 / im->gdes[vidx].step * im->gdes[vidx].ds_cnt);
1560 for (ii = im->gdes[vidx].ds;
1561 ii < max_ii; ii += im->gdes[vidx].ds_cnt) {
1562 if (!finite(im->gdes[vidx].data[ii]))
1564 if (isnan(printval)) {
1565 printval = im->gdes[vidx].data[ii];
1570 switch (im->gdes[i].cf) {
1574 case CF_DEVSEASONAL:
1578 printval += im->gdes[vidx].data[ii];
1581 printval = min(printval, im->gdes[vidx].data[ii]);
1585 printval = max(printval, im->gdes[vidx].data[ii]);
1588 printval = im->gdes[vidx].data[ii];
1591 if (im->gdes[i].cf == CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
1592 if (validsteps > 1) {
1593 printval = (printval / validsteps);
1596 } /* prepare printval */
1598 if (!im->gdes[i].strftm && (percent_s = strstr(im->gdes[i].format, "%S")) != NULL) {
1599 /* Magfact is set to -1 upon entry to print_calc. If it
1600 * is still less than 0, then we need to run auto_scale.
1601 * Otherwise, put the value into the correct units. If
1602 * the value is 0, then do not set the symbol or magnification
1603 * so next the calculation will be performed again. */
1604 if (magfact < 0.0) {
1605 auto_scale(im, &printval, &si_symb, &magfact);
1606 if (printval == 0.0)
1609 printval /= magfact;
1611 *(++percent_s) = 's';
1612 } else if (!im->gdes[i].strftm && strstr(im->gdes[i].format, "%s") != NULL) {
1613 auto_scale(im, &printval, &si_symb, &magfact);
1616 if (im->gdes[i].gf == GF_PRINT) {
1617 rrd_infoval_t prline;
1619 if (im->gdes[i].strftm) {
1620 prline.u_str = (char*)malloc((FMT_LEG_LEN + 2) * sizeof(char));
1621 if (im->gdes[vidx].vf.never == 1) {
1622 time_clean(prline.u_str, im->gdes[i].format);
1624 strftime(prline.u_str,
1625 FMT_LEG_LEN, im->gdes[i].format, &tmvdef);
1627 } else if (bad_format(im->gdes[i].format)) {
1629 ("bad format for PRINT in '%s'", im->gdes[i].format);
1633 sprintf_alloc(im->gdes[i].format, printval, si_symb);
1637 ("print[%ld]", prline_cnt++), RD_I_STR, prline);
1642 if (im->gdes[i].strftm) {
1643 if (im->gdes[vidx].vf.never == 1) {
1644 time_clean(im->gdes[i].legend, im->gdes[i].format);
1646 strftime(im->gdes[i].legend,
1647 FMT_LEG_LEN, im->gdes[i].format, &tmvdef);
1650 if (bad_format(im->gdes[i].format)) {
1652 ("bad format for GPRINT in '%s'",
1653 im->gdes[i].format);
1656 #ifdef HAVE_SNPRINTF
1657 snprintf(im->gdes[i].legend,
1659 im->gdes[i].format, printval, si_symb);
1661 sprintf(im->gdes[i].legend,
1662 im->gdes[i].format, printval, si_symb);
1675 if (isnan(im->gdes[i].yrule)) { /* we must set this here or the legend printer can not decide to print the legend */
1676 im->gdes[i].yrule = im->gdes[vidx].vf.val;
1681 if (im->gdes[i].xrule == 0) { /* again ... the legend printer needs it */
1682 im->gdes[i].xrule = im->gdes[vidx].vf.when;
1691 #ifdef WITH_PIECHART
1699 ("STACK should already be turned into LINE or AREA here");
1704 return graphelement;
1709 /* place legends with color spots */
1715 int interleg = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
1716 int border = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
1717 int fill = 0, fill_last;
1718 double legendwidth; // = im->ximg - 2 * border;
1720 double leg_x = border;
1721 int leg_y = 0; //im->yimg;
1722 int leg_y_prev = 0; // im->yimg;
1725 int i, ii, mark = 0;
1726 char default_txtalign = TXA_JUSTIFIED; /*default line orientation */
1729 char saved_legend[FMT_LEG_LEN + 5];
1735 legendwidth = im->legendwidth - 2 * border;
1739 if (!(im->extra_flags & NOLEGEND) && !(im->extra_flags & ONLY_GRAPH)) {
1740 if ((legspace = (int*)malloc(im->gdes_c * sizeof(int))) == NULL) {
1741 rrd_set_error("malloc for legspace");
1745 for (i = 0; i < im->gdes_c; i++) {
1746 char prt_fctn; /*special printfunctions */
1748 strcpy(saved_legend, im->gdes[i].legend);
1752 /* hide legends for rules which are not displayed */
1753 if (im->gdes[i].gf == GF_TEXTALIGN) {
1754 default_txtalign = im->gdes[i].txtalign;
1757 if (!(im->extra_flags & FORCE_RULES_LEGEND)) {
1758 if (im->gdes[i].gf == GF_HRULE
1759 && (im->gdes[i].yrule <
1760 im->minval || im->gdes[i].yrule > im->maxval))
1761 im->gdes[i].legend[0] = '\0';
1762 if (im->gdes[i].gf == GF_VRULE
1763 && (im->gdes[i].xrule <
1764 im->start || im->gdes[i].xrule > im->end))
1765 im->gdes[i].legend[0] = '\0';
1768 /* turn \\t into tab */
1769 while ((tab = strstr(im->gdes[i].legend, "\\t"))) {
1770 memmove(tab, tab + 1, strlen(tab));
1774 leg_cc = strlen(im->gdes[i].legend);
1775 /* is there a controle code at the end of the legend string ? */
1776 if (leg_cc >= 2 && im->gdes[i].legend[leg_cc - 2] == '\\') {
1777 prt_fctn = im->gdes[i].legend[leg_cc - 1];
1779 im->gdes[i].legend[leg_cc] = '\0';
1783 /* only valid control codes */
1784 if (prt_fctn != 'l' && prt_fctn != 'n' && /* a synonym for l */
1789 prt_fctn != 's' && prt_fctn != '\0' && prt_fctn != 'g') {
1792 ("Unknown control code at the end of '%s\\%c'",
1793 im->gdes[i].legend, prt_fctn);
1797 if (prt_fctn == 'n') {
1801 /* remove exess space from the end of the legend for \g */
1802 while (prt_fctn == 'g' &&
1803 leg_cc > 0 && im->gdes[i].legend[leg_cc - 1] == ' ') {
1805 im->gdes[i].legend[leg_cc] = '\0';
1810 /* no interleg space if string ends in \g */
1811 legspace[i] = (prt_fctn == 'g' ? 0 : interleg);
1813 fill += legspace[i];
1816 gfx_get_text_width(im,
1822 im->tabwidth, im->gdes[i].legend);
1827 /* who said there was a special tag ... ? */
1828 if (prt_fctn == 'g') {
1832 if (prt_fctn == '\0') {
1833 if(calc_width && (fill > legendwidth)){
1836 if (i == im->gdes_c - 1 || fill > legendwidth) {
1837 /* just one legend item is left right or center */
1838 switch (default_txtalign) {
1853 /* is it time to place the legends ? */
1854 if (fill > legendwidth) {
1862 if (leg_c == 1 && prt_fctn == 'j') {
1867 if (prt_fctn != '\0') {
1869 if (leg_c >= 2 && prt_fctn == 'j') {
1870 glue = (double)(legendwidth - fill) / (double)(leg_c - 1);
1874 if (prt_fctn == 'c')
1875 leg_x = border + (double)(legendwidth - fill) / 2.0;
1876 if (prt_fctn == 'r')
1877 leg_x = legendwidth - fill + border;
1878 for (ii = mark; ii <= i; ii++) {
1879 if (im->gdes[ii].legend[0] == '\0')
1880 continue; /* skip empty legends */
1881 im->gdes[ii].leg_x = leg_x;
1882 im->gdes[ii].leg_y = leg_y + border;
1884 (double)gfx_get_text_width(im, leg_x,
1889 im->tabwidth, im->gdes[ii].legend)
1890 +(double)legspace[ii]
1894 if (leg_x > border || prt_fctn == 's')
1895 leg_y += im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
1896 if (prt_fctn == 's')
1897 leg_y -= im->text_prop[TEXT_PROP_LEGEND].size;
1898 if (prt_fctn == 'u')
1899 leg_y -= im->text_prop[TEXT_PROP_LEGEND].size *1.8;
1901 if(calc_width && (fill > legendwidth)){
1910 strcpy(im->gdes[i].legend, saved_legend);
1915 im->legendwidth = legendwidth + 2 * border;
1918 im->legendheight = leg_y + border * 0.6;
1925 /* create a grid on the graph. it determines what to do
1926 from the values of xsize, start and end */
1928 /* the xaxis labels are determined from the number of seconds per pixel
1929 in the requested graph */
1931 int calc_horizontal_grid(
1939 int decimals, fractionals;
1941 im->ygrid_scale.labfact = 2;
1942 range = im->maxval - im->minval;
1943 scaledrange = range / im->magfact;
1944 /* does the scale of this graph make it impossible to put lines
1945 on it? If so, give up. */
1946 if (isnan(scaledrange)) {
1950 /* find grid spaceing */
1952 if (isnan(im->ygridstep)) {
1953 if (im->extra_flags & ALTYGRID) {
1954 /* find the value with max number of digits. Get number of digits */
1957 (max(fabs(im->maxval), fabs(im->minval)) *
1958 im->viewfactor / im->magfact));
1959 if (decimals <= 0) /* everything is small. make place for zero */
1961 im->ygrid_scale.gridstep =
1963 floor(log10(range * im->viewfactor / im->magfact))) /
1964 im->viewfactor * im->magfact;
1965 if (im->ygrid_scale.gridstep == 0) /* range is one -> 0.1 is reasonable scale */
1966 im->ygrid_scale.gridstep = 0.1;
1967 /* should have at least 5 lines but no more then 15 */
1968 if (range / im->ygrid_scale.gridstep < 5
1969 && im->ygrid_scale.gridstep >= 30)
1970 im->ygrid_scale.gridstep /= 10;
1971 if (range / im->ygrid_scale.gridstep > 15)
1972 im->ygrid_scale.gridstep *= 10;
1973 if (range / im->ygrid_scale.gridstep > 5) {
1974 im->ygrid_scale.labfact = 1;
1975 if (range / im->ygrid_scale.gridstep > 8
1976 || im->ygrid_scale.gridstep <
1977 1.8 * im->text_prop[TEXT_PROP_AXIS].size)
1978 im->ygrid_scale.labfact = 2;
1980 im->ygrid_scale.gridstep /= 5;
1981 im->ygrid_scale.labfact = 5;
1985 (im->ygrid_scale.gridstep *
1986 (double) im->ygrid_scale.labfact * im->viewfactor /
1988 if (fractionals < 0) { /* small amplitude. */
1989 int len = decimals - fractionals + 1;
1991 if (im->unitslength < len + 2)
1992 im->unitslength = len + 2;
1993 sprintf(im->ygrid_scale.labfmt,
1995 -fractionals, (im->symbol != ' ' ? " %c" : ""));
1997 int len = decimals + 1;
1999 if (im->unitslength < len + 2)
2000 im->unitslength = len + 2;
2001 sprintf(im->ygrid_scale.labfmt,
2002 "%%%d.0f%s", len, (im->symbol != ' ' ? " %c" : ""));
2004 } else { /* classic rrd grid */
2005 for (i = 0; ylab[i].grid > 0; i++) {
2006 pixel = im->ysize / (scaledrange / ylab[i].grid);
2012 for (i = 0; i < 4; i++) {
2013 if (pixel * ylab[gridind].lfac[i] >=
2014 1.8 * im->text_prop[TEXT_PROP_AXIS].size) {
2015 im->ygrid_scale.labfact = ylab[gridind].lfac[i];
2020 im->ygrid_scale.gridstep = ylab[gridind].grid * im->magfact;
2023 im->ygrid_scale.gridstep = im->ygridstep;
2024 im->ygrid_scale.labfact = im->ylabfact;
2029 int draw_horizontal_grid(
2035 char graph_label[100];
2037 double X0 = im->xorigin;
2038 double X1 = im->xorigin + im->xsize;
2039 int sgrid = (int) (im->minval / im->ygrid_scale.gridstep - 1);
2040 int egrid = (int) (im->maxval / im->ygrid_scale.gridstep + 1);
2042 double second_axis_magfact = 0;
2043 char *second_axis_symb = "";
2046 im->ygrid_scale.gridstep /
2047 (double) im->magfact * (double) im->viewfactor;
2048 MaxY = scaledstep * (double) egrid;
2049 for (i = sgrid; i <= egrid; i++) {
2051 im->ygrid_scale.gridstep * i);
2053 im->ygrid_scale.gridstep * (i + 1));
2055 if (floor(Y0 + 0.5) >=
2056 im->yorigin - im->ysize && floor(Y0 + 0.5) <= im->yorigin) {
2057 /* Make sure at least 2 grid labels are shown, even if it doesn't agree
2058 with the chosen settings. Add a label if required by settings, or if
2059 there is only one label so far and the next grid line is out of bounds. */
2060 if (i % im->ygrid_scale.labfact == 0
2062 && (YN < im->yorigin - im->ysize || YN > im->yorigin))) {
2063 if (im->symbol == ' ') {
2064 if (im->extra_flags & ALTYGRID) {
2065 sprintf(graph_label,
2066 im->ygrid_scale.labfmt,
2067 scaledstep * (double) i);
2070 sprintf(graph_label, "%4.1f",
2071 scaledstep * (double) i);
2073 sprintf(graph_label, "%4.0f",
2074 scaledstep * (double) i);
2078 char sisym = (i == 0 ? ' ' : im->symbol);
2080 if (im->extra_flags & ALTYGRID) {
2081 sprintf(graph_label,
2082 im->ygrid_scale.labfmt,
2083 scaledstep * (double) i, sisym);
2086 sprintf(graph_label, "%4.1f %c",
2087 scaledstep * (double) i, sisym);
2089 sprintf(graph_label, "%4.0f %c",
2090 scaledstep * (double) i, sisym);
2095 if (im->second_axis_scale != 0){
2096 char graph_label_right[100];
2097 double sval = im->ygrid_scale.gridstep*(double)i*im->second_axis_scale+im->second_axis_shift;
2098 if (im->second_axis_format[0] == '\0'){
2099 if (!second_axis_magfact){
2100 double dummy = im->ygrid_scale.gridstep*(double)(sgrid+egrid)/2.0*im->second_axis_scale+im->second_axis_shift;
2101 auto_scale(im,&dummy,&second_axis_symb,&second_axis_magfact);
2103 sval /= second_axis_magfact;
2106 sprintf(graph_label_right,"%5.1f %s",sval,second_axis_symb);
2108 sprintf(graph_label_right,"%5.0f %s",sval,second_axis_symb);
2112 sprintf(graph_label_right,im->second_axis_format,sval);
2116 im->graph_col[GRC_FONT],
2117 im->text_prop[TEXT_PROP_AXIS].font_desc,
2118 im->tabwidth,0.0, GFX_H_LEFT, GFX_V_CENTER,
2119 graph_label_right );
2125 text_prop[TEXT_PROP_AXIS].
2127 im->graph_col[GRC_FONT],
2129 text_prop[TEXT_PROP_AXIS].
2132 GFX_H_RIGHT, GFX_V_CENTER, graph_label);
2133 gfx_line(im, X0 - 2, Y0, X0, Y0,
2134 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2135 gfx_line(im, X1, Y0, X1 + 2, Y0,
2136 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2137 gfx_dashed_line(im, X0 - 2, Y0,
2143 im->grid_dash_on, im->grid_dash_off);
2144 } else if (!(im->extra_flags & NOMINOR)) {
2147 X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2148 gfx_line(im, X1, Y0, X1 + 2, Y0,
2149 GRIDWIDTH, im->graph_col[GRC_GRID]);
2150 gfx_dashed_line(im, X0 - 1, Y0,
2154 graph_col[GRC_GRID],
2155 im->grid_dash_on, im->grid_dash_off);
2162 /* this is frexp for base 10 */
2173 iexp = floor(log((double)fabs(x)) / log((double)10));
2174 mnt = x / pow(10.0, iexp);
2177 mnt = x / pow(10.0, iexp);
2184 /* logaritmic horizontal grid */
2185 int horizontal_log_grid(
2189 double yloglab[][10] = {
2191 1.0, 10., 0.0, 0.0, 0.0, 0.0, 0.0,
2193 1.0, 5.0, 10., 0.0, 0.0, 0.0, 0.0,
2195 1.0, 2.0, 5.0, 7.0, 10., 0.0, 0.0,
2212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* last line */
2214 int i, j, val_exp, min_exp;
2215 double nex; /* number of decades in data */
2216 double logscale; /* scale in logarithmic space */
2217 int exfrac = 1; /* decade spacing */
2218 int mid = -1; /* row in yloglab for major grid */
2219 double mspac; /* smallest major grid spacing (pixels) */
2220 int flab; /* first value in yloglab to use */
2221 double value, tmp, pre_value;
2223 char graph_label[100];
2225 nex = log10(im->maxval / im->minval);
2226 logscale = im->ysize / nex;
2227 /* major spacing for data with high dynamic range */
2228 while (logscale * exfrac < 3 * im->text_prop[TEXT_PROP_LEGEND].size) {
2235 /* major spacing for less dynamic data */
2237 /* search best row in yloglab */
2239 for (i = 0; yloglab[mid][i + 1] < 10.0; i++);
2240 mspac = logscale * log10(10.0 / yloglab[mid][i]);
2243 2 * im->text_prop[TEXT_PROP_LEGEND].size && yloglab[mid][0] > 0);
2246 /* find first value in yloglab */
2248 yloglab[mid][flab] < 10
2249 && frexp10(im->minval, &tmp) > yloglab[mid][flab]; flab++);
2250 if (yloglab[mid][flab] == 10.0) {
2255 if (val_exp % exfrac)
2256 val_exp += abs(-val_exp % exfrac);
2258 X1 = im->xorigin + im->xsize;
2263 value = yloglab[mid][flab] * pow(10.0, val_exp);
2264 if (AlmostEqual2sComplement(value, pre_value, 4))
2265 break; /* it seems we are not converging */
2267 Y0 = ytr(im, value);
2268 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2270 /* major grid line */
2272 X0 - 2, Y0, X0, Y0, MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2273 gfx_line(im, X1, Y0, X1 + 2, Y0,
2274 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2275 gfx_dashed_line(im, X0 - 2, Y0,
2280 [GRC_MGRID], im->grid_dash_on, im->grid_dash_off);
2282 if (im->extra_flags & FORCE_UNITS_SI) {
2287 scale = floor(val_exp / 3.0);
2289 pvalue = pow(10.0, val_exp % 3);
2291 pvalue = pow(10.0, ((val_exp + 1) % 3) + 2);
2292 pvalue *= yloglab[mid][flab];
2293 if (((scale + si_symbcenter) < (int) sizeof(si_symbol))
2294 && ((scale + si_symbcenter) >= 0))
2295 symbol = si_symbol[scale + si_symbcenter];
2298 sprintf(graph_label, "%3.0f %c", pvalue, symbol);
2300 sprintf(graph_label, "%3.0e", value);
2302 if (im->second_axis_scale != 0){
2303 char graph_label_right[100];
2304 double sval = value*im->second_axis_scale+im->second_axis_shift;
2305 if (im->second_axis_format[0] == '\0'){
2306 if (im->extra_flags & FORCE_UNITS_SI) {
2309 auto_scale(im,&sval,&symb,&mfac);
2310 sprintf(graph_label_right,"%4.0f %s", sval,symb);
2313 sprintf(graph_label_right,"%3.0e", sval);
2317 sprintf(graph_label_right,im->second_axis_format,sval,"");
2322 im->graph_col[GRC_FONT],
2323 im->text_prop[TEXT_PROP_AXIS].font_desc,
2324 im->tabwidth,0.0, GFX_H_LEFT, GFX_V_CENTER,
2325 graph_label_right );
2331 text_prop[TEXT_PROP_AXIS].
2333 im->graph_col[GRC_FONT],
2335 text_prop[TEXT_PROP_AXIS].
2338 GFX_H_RIGHT, GFX_V_CENTER, graph_label);
2340 if (mid < 4 && exfrac == 1) {
2341 /* find first and last minor line behind current major line
2342 * i is the first line and j tha last */
2344 min_exp = val_exp - 1;
2345 for (i = 1; yloglab[mid][i] < 10.0; i++);
2346 i = yloglab[mid][i - 1] + 1;
2350 i = yloglab[mid][flab - 1] + 1;
2351 j = yloglab[mid][flab];
2354 /* draw minor lines below current major line */
2355 for (; i < j; i++) {
2357 value = i * pow(10.0, min_exp);
2358 if (value < im->minval)
2360 Y0 = ytr(im, value);
2361 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2366 X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2367 gfx_line(im, X1, Y0, X1 + 2, Y0,
2368 GRIDWIDTH, im->graph_col[GRC_GRID]);
2369 gfx_dashed_line(im, X0 - 1, Y0,
2373 graph_col[GRC_GRID],
2374 im->grid_dash_on, im->grid_dash_off);
2376 } else if (exfrac > 1) {
2377 for (i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
2378 value = pow(10.0, i);
2379 if (value < im->minval)
2381 Y0 = ytr(im, value);
2382 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2387 X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2388 gfx_line(im, X1, Y0, X1 + 2, Y0,
2389 GRIDWIDTH, im->graph_col[GRC_GRID]);
2390 gfx_dashed_line(im, X0 - 1, Y0,
2394 graph_col[GRC_GRID],
2395 im->grid_dash_on, im->grid_dash_off);
2400 if (yloglab[mid][++flab] == 10.0) {
2406 /* draw minor lines after highest major line */
2407 if (mid < 4 && exfrac == 1) {
2408 /* find first and last minor line below current major line
2409 * i is the first line and j tha last */
2411 min_exp = val_exp - 1;
2412 for (i = 1; yloglab[mid][i] < 10.0; i++);
2413 i = yloglab[mid][i - 1] + 1;
2417 i = yloglab[mid][flab - 1] + 1;
2418 j = yloglab[mid][flab];
2421 /* draw minor lines below current major line */
2422 for (; i < j; i++) {
2424 value = i * pow(10.0, min_exp);
2425 if (value < im->minval)
2427 Y0 = ytr(im, value);
2428 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2432 X0 - 2, Y0, X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2433 gfx_line(im, X1, Y0, X1 + 2, Y0,
2434 GRIDWIDTH, im->graph_col[GRC_GRID]);
2435 gfx_dashed_line(im, X0 - 1, Y0,
2439 graph_col[GRC_GRID],
2440 im->grid_dash_on, im->grid_dash_off);
2443 /* fancy minor gridlines */
2444 else if (exfrac > 1) {
2445 for (i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
2446 value = pow(10.0, i);
2447 if (value < im->minval)
2449 Y0 = ytr(im, value);
2450 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2454 X0 - 2, Y0, X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2455 gfx_line(im, X1, Y0, X1 + 2, Y0,
2456 GRIDWIDTH, im->graph_col[GRC_GRID]);
2457 gfx_dashed_line(im, X0 - 1, Y0,
2461 graph_col[GRC_GRID],
2462 im->grid_dash_on, im->grid_dash_off);
2473 int xlab_sel; /* which sort of label and grid ? */
2474 time_t ti, tilab, timajor;
2476 char graph_label[100];
2477 double X0, Y0, Y1; /* points for filled graph and more */
2480 /* the type of time grid is determined by finding
2481 the number of seconds per pixel in the graph */
2482 if (im->xlab_user.minsec == -1) {
2483 factor = (im->end - im->start) / im->xsize;
2485 while (xlab[xlab_sel + 1].minsec !=
2486 -1 && xlab[xlab_sel + 1].minsec <= factor) {
2488 } /* pick the last one */
2489 while (xlab[xlab_sel - 1].minsec ==
2490 xlab[xlab_sel].minsec
2491 && xlab[xlab_sel].length > (im->end - im->start)) {
2493 } /* go back to the smallest size */
2494 im->xlab_user.gridtm = xlab[xlab_sel].gridtm;
2495 im->xlab_user.gridst = xlab[xlab_sel].gridst;
2496 im->xlab_user.mgridtm = xlab[xlab_sel].mgridtm;
2497 im->xlab_user.mgridst = xlab[xlab_sel].mgridst;
2498 im->xlab_user.labtm = xlab[xlab_sel].labtm;
2499 im->xlab_user.labst = xlab[xlab_sel].labst;
2500 im->xlab_user.precis = xlab[xlab_sel].precis;
2501 im->xlab_user.stst = xlab[xlab_sel].stst;
2504 /* y coords are the same for every line ... */
2506 Y1 = im->yorigin - im->ysize;
2507 /* paint the minor grid */
2508 if (!(im->extra_flags & NOMINOR)) {
2509 for (ti = find_first_time(im->start,
2517 find_first_time(im->start,
2522 ti < im->end && ti != -1;
2524 find_next_time(ti, im->xlab_user.gridtm, im->xlab_user.gridst)
2526 /* are we inside the graph ? */
2527 if (ti < im->start || ti > im->end)
2529 while (timajor < ti && timajor != -1) {
2530 timajor = find_next_time(timajor,
2533 mgridtm, im->xlab_user.mgridst);
2535 if (timajor == -1) break; /* fail in case of problems with time increments */
2537 continue; /* skip as falls on major grid line */
2539 gfx_line(im, X0, Y1 - 2, X0, Y1,
2540 GRIDWIDTH, im->graph_col[GRC_GRID]);
2541 gfx_line(im, X0, Y0, X0, Y0 + 2,
2542 GRIDWIDTH, im->graph_col[GRC_GRID]);
2543 gfx_dashed_line(im, X0, Y0 + 1, X0,
2546 graph_col[GRC_GRID],
2547 im->grid_dash_on, im->grid_dash_off);
2551 /* paint the major grid */
2552 for (ti = find_first_time(im->start,
2559 ti < im->end && ti != -1;
2560 ti = find_next_time(ti, im->xlab_user.mgridtm, im->xlab_user.mgridst)
2562 /* are we inside the graph ? */
2563 if (ti < im->start || ti > im->end)
2566 gfx_line(im, X0, Y1 - 2, X0, Y1,
2567 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2568 gfx_line(im, X0, Y0, X0, Y0 + 3,
2569 MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2570 gfx_dashed_line(im, X0, Y0 + 3, X0,
2574 [GRC_MGRID], im->grid_dash_on, im->grid_dash_off);
2576 /* paint the labels below the graph */
2578 find_first_time(im->start -
2587 im->xlab_user.precis / 2) && ti != -1;
2588 ti = find_next_time(ti, im->xlab_user.labtm, im->xlab_user.labst)
2590 tilab = ti + im->xlab_user.precis / 2; /* correct time for the label */
2591 /* are we inside the graph ? */
2592 if (tilab < im->start || tilab > im->end)
2595 localtime_r(&tilab, &tm);
2596 strftime(graph_label, 99, im->xlab_user.stst, &tm);
2598 # error "your libc has no strftime I guess we'll abort the exercise here."
2603 im->graph_col[GRC_FONT],
2605 text_prop[TEXT_PROP_AXIS].
2608 GFX_H_CENTER, GFX_V_TOP, graph_label);
2617 /* draw x and y axis */
2618 /* gfx_line ( im->canvas, im->xorigin+im->xsize,im->yorigin,
2619 im->xorigin+im->xsize,im->yorigin-im->ysize,
2620 GRIDWIDTH, im->graph_col[GRC_AXIS]);
2622 gfx_line ( im->canvas, im->xorigin,im->yorigin-im->ysize,
2623 im->xorigin+im->xsize,im->yorigin-im->ysize,
2624 GRIDWIDTH, im->graph_col[GRC_AXIS]); */
2626 gfx_line(im, im->xorigin - 4,
2628 im->xorigin + im->xsize +
2629 4, im->yorigin, MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2630 gfx_line(im, im->xorigin,
2633 im->yorigin - im->ysize -
2634 4, MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2635 /* arrow for X and Y axis direction */
2636 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 */
2637 im->graph_col[GRC_ARROW]);
2639 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 */
2640 im->graph_col[GRC_ARROW]);
2642 if (im->second_axis_scale != 0){
2643 gfx_line ( im, im->xorigin+im->xsize,im->yorigin+4,
2644 im->xorigin+im->xsize,im->yorigin-im->ysize-4,
2645 MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2647 im->xorigin+im->xsize-2, im->yorigin-im->ysize-2,
2648 im->xorigin+im->xsize+3, im->yorigin-im->ysize-2,
2649 im->xorigin+im->xsize, im->yorigin-im->ysize-7, /* LINEOFFSET */
2650 im->graph_col[GRC_ARROW]);
2661 double X0, Y0; /* points for filled graph and more */
2662 struct gfx_color_t water_color;
2664 if (im->draw_3d_border > 0) {
2665 /* draw 3d border */
2666 i = im->draw_3d_border;
2667 gfx_new_area(im, 0, im->yimg,
2668 i, im->yimg - i, i, i, im->graph_col[GRC_SHADEA]);
2669 gfx_add_point(im, im->ximg - i, i);
2670 gfx_add_point(im, im->ximg, 0);
2671 gfx_add_point(im, 0, 0);
2673 gfx_new_area(im, i, im->yimg - i,
2675 im->yimg - i, im->ximg - i, i, im->graph_col[GRC_SHADEB]);
2676 gfx_add_point(im, im->ximg, 0);
2677 gfx_add_point(im, im->ximg, im->yimg);
2678 gfx_add_point(im, 0, im->yimg);
2681 if (im->draw_x_grid == 1)
2683 if (im->draw_y_grid == 1) {
2684 if (im->logarithmic) {
2685 res = horizontal_log_grid(im);
2687 res = draw_horizontal_grid(im);
2690 /* dont draw horizontal grid if there is no min and max val */
2692 char *nodata = "No Data found";
2694 gfx_text(im, im->ximg / 2,
2697 im->graph_col[GRC_FONT],
2699 text_prop[TEXT_PROP_AXIS].
2702 GFX_H_CENTER, GFX_V_CENTER, nodata);
2706 /* yaxis unit description */
2707 if (im->ylegend[0] != '\0'){
2709 im->xOriginLegendY+10,
2711 im->graph_col[GRC_FONT],
2713 text_prop[TEXT_PROP_UNIT].
2716 RRDGRAPH_YLEGEND_ANGLE, GFX_H_CENTER, GFX_V_CENTER, im->ylegend);
2719 if (im->second_axis_legend[0] != '\0'){
2721 im->xOriginLegendY2+10,
2722 im->yOriginLegendY2,
2723 im->graph_col[GRC_FONT],
2724 im->text_prop[TEXT_PROP_UNIT].font_desc,
2726 RRDGRAPH_YLEGEND_ANGLE,
2727 GFX_H_CENTER, GFX_V_CENTER,
2728 im->second_axis_legend);
2733 im->xOriginTitle, im->yOriginTitle+6,
2734 im->graph_col[GRC_FONT],
2736 text_prop[TEXT_PROP_TITLE].
2738 im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_TOP, im->title);
2739 /* rrdtool 'logo' */
2740 if (!(im->extra_flags & NO_RRDTOOL_TAG)){
2741 water_color = im->graph_col[GRC_FONT];
2742 water_color.alpha = 0.3;
2743 double xpos = im->legendposition == EAST ? im->xOriginLegendY : im->ximg - 4;
2744 gfx_text(im, xpos, 5,
2747 text_prop[TEXT_PROP_WATERMARK].
2748 font_desc, im->tabwidth,
2749 -90, GFX_H_LEFT, GFX_V_TOP, "RRDTOOL / TOBI OETIKER");
2751 /* graph watermark */
2752 if (im->watermark[0] != '\0') {
2753 water_color = im->graph_col[GRC_FONT];
2754 water_color.alpha = 0.3;
2756 im->ximg / 2, im->yimg - 6,
2759 text_prop[TEXT_PROP_WATERMARK].
2760 font_desc, im->tabwidth, 0,
2761 GFX_H_CENTER, GFX_V_BOTTOM, im->watermark);
2765 if (!(im->extra_flags & NOLEGEND) && !(im->extra_flags & ONLY_GRAPH)) {
2766 for (i = 0; i < im->gdes_c; i++) {
2767 if (im->gdes[i].legend[0] == '\0')
2769 /* im->gdes[i].leg_y is the bottom of the legend */
2770 X0 = im->xOriginLegend + im->gdes[i].leg_x;
2771 Y0 = im->legenddirection == TOP_DOWN ? im->yOriginLegend + im->gdes[i].leg_y : im->yOriginLegend + im->legendheight - im->gdes[i].leg_y;
2772 gfx_text(im, X0, Y0,
2773 im->graph_col[GRC_FONT],
2776 [TEXT_PROP_LEGEND].font_desc,
2778 GFX_H_LEFT, GFX_V_BOTTOM, im->gdes[i].legend);
2779 /* The legend for GRAPH items starts with "M " to have
2780 enough space for the box */
2781 if (im->gdes[i].gf != GF_PRINT &&
2782 im->gdes[i].gf != GF_GPRINT && im->gdes[i].gf != GF_COMMENT) {
2786 boxH = gfx_get_text_width(im, 0,
2791 im->tabwidth, "o") * 1.2;
2793 /* shift the box up a bit */
2796 if (im->dynamic_labels && im->gdes[i].gf == GF_HRULE) { /* [-] */
2798 cairo_new_path(im->cr);
2799 cairo_set_line_width(im->cr, 1.0);
2802 X0 + boxH, Y0 - boxV / 2,
2803 1.0, im->gdes[i].col);
2805 } else if (im->dynamic_labels && im->gdes[i].gf == GF_VRULE) { /* [|] */
2807 cairo_new_path(im->cr);
2808 cairo_set_line_width(im->cr, 1.0);
2811 X0 + boxH / 2, Y0 - boxV,
2812 1.0, im->gdes[i].col);
2814 } else if (im->dynamic_labels && im->gdes[i].gf == GF_LINE) { /* [/] */
2816 cairo_new_path(im->cr);
2817 cairo_set_line_width(im->cr, im->gdes[i].linewidth);
2820 X0 + boxH, Y0 - boxV,
2821 im->gdes[i].linewidth, im->gdes[i].col);
2824 /* make sure transparent colors show up the same way as in the graph */
2827 X0, Y0, X0 + boxH, Y0, im->graph_col[GRC_BACK]);
2828 gfx_add_point(im, X0 + boxH, Y0 - boxV);
2830 gfx_new_area(im, X0, Y0 - boxV, X0,
2831 Y0, X0 + boxH, Y0, im->gdes[i].col);
2832 gfx_add_point(im, X0 + boxH, Y0 - boxV);
2835 cairo_new_path(im->cr);
2836 cairo_set_line_width(im->cr, 1.0);
2839 gfx_line_fit(im, &X0, &Y0);
2840 gfx_line_fit(im, &X1, &Y1);
2841 cairo_move_to(im->cr, X0, Y0);
2842 cairo_line_to(im->cr, X1, Y0);
2843 cairo_line_to(im->cr, X1, Y1);
2844 cairo_line_to(im->cr, X0, Y1);
2845 cairo_close_path(im->cr);
2846 cairo_set_source_rgba(im->cr,
2847 im->graph_col[GRC_FRAME].red,
2848 im->graph_col[GRC_FRAME].green,
2849 im->graph_col[GRC_FRAME].blue,
2850 im->graph_col[GRC_FRAME].alpha);
2852 if (im->gdes[i].dash) {
2853 /* make box borders in legend dashed if the graph is dashed */
2857 cairo_set_dash(im->cr, dashes, 1, 0.0);
2859 cairo_stroke(im->cr);
2860 cairo_restore(im->cr);
2867 /*****************************************************
2868 * lazy check make sure we rely need to create this graph
2869 *****************************************************/
2876 struct stat imgstat;
2879 return 0; /* no lazy option */
2880 if (strlen(im->graphfile) == 0)
2881 return 0; /* inmemory option */
2882 if (stat(im->graphfile, &imgstat) != 0)
2883 return 0; /* can't stat */
2884 /* one pixel in the existing graph is more then what we would
2886 if (time(NULL) - imgstat.st_mtime > (im->end - im->start) / im->xsize)
2888 if ((fd = fopen(im->graphfile, "rb")) == NULL)
2889 return 0; /* the file does not exist */
2890 switch (im->imgformat) {
2892 size = PngSize(fd, &(im->ximg), &(im->yimg));
2902 int graph_size_location(
2907 /* The actual size of the image to draw is determined from
2908 ** several sources. The size given on the command line is
2909 ** the graph area but we need more as we have to draw labels
2910 ** and other things outside the graph area. If the option
2911 ** --full-size-mode is selected the size defines the total
2912 ** image size and the size available for the graph is
2916 /** +---+-----------------------------------+
2917 ** | y |...............graph title.........|
2918 ** | +---+-------------------------------+
2922 ** | s | x | main graph area |
2927 ** | l | b +-------------------------------+
2928 ** | e | l | x axis labels |
2929 ** +---+---+-------------------------------+
2930 ** |....................legends............|
2931 ** +---------------------------------------+
2933 ** +---------------------------------------+
2936 int Xvertical = 0, Xvertical2 = 0, Ytitle =
2937 0, Xylabel = 0, Xmain = 0, Ymain =
2938 0, Yxlabel = 0, Xspacing = 15, Yspacing = 15, Ywatermark = 4;
2940 // no legends and no the shall be plotted it's easy
2941 if (im->extra_flags & ONLY_GRAPH) {
2943 im->ximg = im->xsize;
2944 im->yimg = im->ysize;
2945 im->yorigin = im->ysize;
2951 if(im->watermark[0] != '\0') {
2952 Ywatermark = im->text_prop[TEXT_PROP_WATERMARK].size * 2;
2955 // calculate the width of the left vertical legend
2956 if (im->ylegend[0] != '\0') {
2957 Xvertical = im->text_prop[TEXT_PROP_UNIT].size * 2;
2960 // calculate the width of the right vertical legend
2961 if (im->second_axis_legend[0] != '\0') {
2962 Xvertical2 = im->text_prop[TEXT_PROP_UNIT].size * 2;
2965 Xvertical2 = Xspacing;
2968 if (im->title[0] != '\0') {
2969 /* The title is placed "inbetween" two text lines so it
2970 ** automatically has some vertical spacing. The horizontal
2971 ** spacing is added here, on each side.
2973 /* if necessary, reduce the font size of the title until it fits the image width */
2974 Ytitle = im->text_prop[TEXT_PROP_TITLE].size * 2.6 + 10;
2977 // we have no title; get a little clearing from the top
2982 if (im->draw_x_grid) {
2983 // calculate the height of the horizontal labelling
2984 Yxlabel = im->text_prop[TEXT_PROP_AXIS].size * 2.5;
2986 if (im->draw_y_grid || im->forceleftspace) {
2987 // calculate the width of the vertical labelling
2989 gfx_get_text_width(im, 0,
2990 im->text_prop[TEXT_PROP_AXIS].font_desc,
2991 im->tabwidth, "0") * im->unitslength;
2995 // add some space to the labelling
2996 Xylabel += Xspacing;
2998 /* If the legend is printed besides the graph the width has to be
2999 ** calculated first. Placing the legend north or south of the
3000 ** graph requires the width calculation first, so the legend is
3001 ** skipped for the moment.
3003 im->legendheight = 0;
3004 im->legendwidth = 0;
3005 if (!(im->extra_flags & NOLEGEND)) {
3006 if(im->legendposition == WEST || im->legendposition == EAST){
3007 if (leg_place(im, 1) == -1){
3013 if (im->extra_flags & FULL_SIZE_MODE) {
3015 /* The actual size of the image to draw has been determined by the user.
3016 ** The graph area is the space remaining after accounting for the legend,
3017 ** the watermark, the axis labels, and the title.
3019 im->ximg = im->xsize;
3020 im->yimg = im->ysize;
3024 /* Now calculate the total size. Insert some spacing where
3025 desired. im->xorigin and im->yorigin need to correspond
3026 with the lower left corner of the main graph area or, if
3027 this one is not set, the imaginary box surrounding the
3029 /* Initial size calculation for the main graph area */
3031 Xmain -= Xylabel;// + Xspacing;
3032 if((im->legendposition == WEST || im->legendposition == EAST) && !(im->extra_flags & NOLEGEND) ){
3033 Xmain -= im->legendwidth;// + Xspacing;
3035 if (im->second_axis_scale != 0){
3038 if (!(im->extra_flags & NO_RRDTOOL_TAG)){
3042 Xmain -= Xvertical + Xvertical2;
3044 /* limit the remaining space to 0 */
3050 /* Putting the legend north or south, the height can now be calculated */
3051 if (!(im->extra_flags & NOLEGEND)) {
3052 if(im->legendposition == NORTH || im->legendposition == SOUTH){
3053 im->legendwidth = im->ximg;
3054 if (leg_place(im, 0) == -1){
3060 if( (im->legendposition == NORTH || im->legendposition == SOUTH) && !(im->extra_flags & NOLEGEND) ){
3061 Ymain -= Yxlabel + im->legendheight;
3067 /* reserve space for the title *or* some padding above the graph */
3070 /* reserve space for padding below the graph */
3071 if (im->extra_flags & NOLEGEND) {
3072 Ymain -= 0.5*Yspacing;
3075 if (im->watermark[0] != '\0') {
3076 Ymain -= Ywatermark;
3078 /* limit the remaining height to 0 */
3083 } else { /* dimension options -width and -height refer to the dimensions of the main graph area */
3085 /* The actual size of the image to draw is determined from
3086 ** several sources. The size given on the command line is
3087 ** the graph area but we need more as we have to draw labels
3088 ** and other things outside the graph area.
3092 Xmain = im->xsize; // + Xspacing;
3096 im->ximg = Xmain + Xylabel;
3097 if (!(im->extra_flags & NO_RRDTOOL_TAG)){
3098 im->ximg += Xspacing;
3101 if( (im->legendposition == WEST || im->legendposition == EAST) && !(im->extra_flags & NOLEGEND) ){
3102 im->ximg += im->legendwidth;// + Xspacing;
3104 if (im->second_axis_scale != 0){
3105 im->ximg += Xylabel;
3108 im->ximg += Xvertical + Xvertical2;
3110 if (!(im->extra_flags & NOLEGEND)) {
3111 if(im->legendposition == NORTH || im->legendposition == SOUTH){
3112 im->legendwidth = im->ximg;
3113 if (leg_place(im, 0) == -1){
3119 im->yimg = Ymain + Yxlabel;
3120 if( (im->legendposition == NORTH || im->legendposition == SOUTH) && !(im->extra_flags & NOLEGEND) ){
3121 im->yimg += im->legendheight;
3124 /* reserve space for the title *or* some padding above the graph */
3128 im->yimg += 1.5 * Yspacing;
3130 /* reserve space for padding below the graph */
3131 if (im->extra_flags & NOLEGEND) {
3132 im->yimg += 0.5*Yspacing;
3135 if (im->watermark[0] != '\0') {
3136 im->yimg += Ywatermark;
3141 /* In case of putting the legend in west or east position the first
3142 ** legend calculation might lead to wrong positions if some items
3143 ** are not aligned on the left hand side (e.g. centered) as the
3144 ** legendwidth wight have been increased after the item was placed.
3145 ** In this case the positions have to be recalculated.
3147 if (!(im->extra_flags & NOLEGEND)) {
3148 if(im->legendposition == WEST || im->legendposition == EAST){
3149 if (leg_place(im, 0) == -1){
3155 /* After calculating all dimensions
3156 ** it is now possible to calculate
3159 switch(im->legendposition){
3161 im->xOriginTitle = (im->ximg / 2);
3162 im->yOriginTitle = 0;
3164 im->xOriginLegend = 0;
3165 im->yOriginLegend = Ytitle;
3167 im->xOriginLegendY = 0;
3168 im->yOriginLegendY = Ytitle + im->legendheight + (Ymain / 2) + Yxlabel;
3170 im->xorigin = Xvertical + Xylabel;
3171 im->yorigin = Ytitle + im->legendheight + Ymain;
3173 im->xOriginLegendY2 = Xvertical + Xylabel + Xmain;
3174 if (im->second_axis_scale != 0){
3175 im->xOriginLegendY2 += Xylabel;
3177 im->yOriginLegendY2 = Ytitle + im->legendheight + (Ymain / 2) + Yxlabel;
3182 im->xOriginTitle = im->legendwidth + im->xsize / 2;
3183 im->yOriginTitle = 0;
3185 im->xOriginLegend = 0;
3186 im->yOriginLegend = Ytitle;
3188 im->xOriginLegendY = im->legendwidth;
3189 im->yOriginLegendY = Ytitle + (Ymain / 2);
3191 im->xorigin = im->legendwidth + Xvertical + Xylabel;
3192 im->yorigin = Ytitle + Ymain;
3194 im->xOriginLegendY2 = im->legendwidth + Xvertical + Xylabel + Xmain;
3195 if (im->second_axis_scale != 0){
3196 im->xOriginLegendY2 += Xylabel;
3198 im->yOriginLegendY2 = Ytitle + (Ymain / 2);
3203 im->xOriginTitle = im->ximg / 2;
3204 im->yOriginTitle = 0;
3206 im->xOriginLegend = 0;
3207 im->yOriginLegend = Ytitle + Ymain + Yxlabel;
3209 im->xOriginLegendY = 0;
3210 im->yOriginLegendY = Ytitle + (Ymain / 2);
3212 im->xorigin = Xvertical + Xylabel;
3213 im->yorigin = Ytitle + Ymain;
3215 im->xOriginLegendY2 = Xvertical + Xylabel + Xmain;
3216 if (im->second_axis_scale != 0){
3217 im->xOriginLegendY2 += Xylabel;
3219 im->yOriginLegendY2 = Ytitle + (Ymain / 2);
3224 im->xOriginTitle = im->xsize / 2;
3225 im->yOriginTitle = 0;
3227 im->xOriginLegend = Xvertical + Xylabel + Xmain + Xvertical2;
3228 if (im->second_axis_scale != 0){
3229 im->xOriginLegend += Xylabel;
3231 im->yOriginLegend = Ytitle;
3233 im->xOriginLegendY = 0;
3234 im->yOriginLegendY = Ytitle + (Ymain / 2);
3236 im->xorigin = Xvertical + Xylabel;
3237 im->yorigin = Ytitle + Ymain;
3239 im->xOriginLegendY2 = Xvertical + Xylabel + Xmain;
3240 if (im->second_axis_scale != 0){
3241 im->xOriginLegendY2 += Xylabel;
3243 im->yOriginLegendY2 = Ytitle + (Ymain / 2);
3245 if (!(im->extra_flags & NO_RRDTOOL_TAG)){
3246 im->xOriginTitle += Xspacing;
3247 im->xOriginLegend += Xspacing;
3248 im->xOriginLegendY += Xspacing;
3249 im->xorigin += Xspacing;
3250 im->xOriginLegendY2 += Xspacing;
3260 static cairo_status_t cairo_output(
3264 unsigned int length)
3266 image_desc_t *im = (image_desc_t*)closure;
3268 im->rendered_image =
3269 (unsigned char*)realloc(im->rendered_image, im->rendered_image_size + length);
3270 if (im->rendered_image == NULL)
3271 return CAIRO_STATUS_WRITE_ERROR;
3272 memcpy(im->rendered_image + im->rendered_image_size, data, length);
3273 im->rendered_image_size += length;
3274 return CAIRO_STATUS_SUCCESS;
3277 /* draw that picture thing ... */
3282 int lazy = lazy_check(im);
3283 double areazero = 0.0;
3284 graph_desc_t *lastgdes = NULL;
3287 // PangoFontMap *font_map = pango_cairo_font_map_get_default();
3289 /* pull the data from the rrd files ... */
3290 if (data_fetch(im) == -1)
3292 /* evaluate VDEF and CDEF operations ... */
3293 if (data_calc(im) == -1)
3295 /* calculate and PRINT and GPRINT definitions. We have to do it at
3296 * this point because it will affect the length of the legends
3297 * if there are no graph elements (i==0) we stop here ...
3298 * if we are lazy, try to quit ...
3304 /* if we want and can be lazy ... quit now */
3308 /**************************************************************
3309 *** Calculating sizes and locations became a bit confusing ***
3310 *** so I moved this into a separate function. ***
3311 **************************************************************/
3312 if (graph_size_location(im, i) == -1)
3315 info.u_cnt = im->xorigin;
3316 grinfo_push(im, sprintf_alloc("graph_left"), RD_I_CNT, info);
3317 info.u_cnt = im->yorigin - im->ysize;
3318 grinfo_push(im, sprintf_alloc("graph_top"), RD_I_CNT, info);
3319 info.u_cnt = im->xsize;
3320 grinfo_push(im, sprintf_alloc("graph_width"), RD_I_CNT, info);
3321 info.u_cnt = im->ysize;
3322 grinfo_push(im, sprintf_alloc("graph_height"), RD_I_CNT, info);
3323 info.u_cnt = im->ximg;
3324 grinfo_push(im, sprintf_alloc("image_width"), RD_I_CNT, info);
3325 info.u_cnt = im->yimg;
3326 grinfo_push(im, sprintf_alloc("image_height"), RD_I_CNT, info);
3327 info.u_cnt = im->start;
3328 grinfo_push(im, sprintf_alloc("graph_start"), RD_I_CNT, info);
3329 info.u_cnt = im->end;
3330 grinfo_push(im, sprintf_alloc("graph_end"), RD_I_CNT, info);
3332 /* if we want and can be lazy ... quit now */
3336 /* get actual drawing data and find min and max values */
3337 if (data_proc(im) == -1)
3339 if (!im->logarithmic) {
3343 /* identify si magnitude Kilo, Mega Giga ? */
3344 if (!im->rigid && !im->logarithmic)
3345 expand_range(im); /* make sure the upper and lower limit are
3348 info.u_val = im->minval;
3349 grinfo_push(im, sprintf_alloc("value_min"), RD_I_VAL, info);
3350 info.u_val = im->maxval;
3351 grinfo_push(im, sprintf_alloc("value_max"), RD_I_VAL, info);
3354 if (!calc_horizontal_grid(im))
3359 apply_gridfit(im); */
3360 /* the actual graph is created by going through the individual
3361 graph elements and then drawing them */
3362 cairo_surface_destroy(im->surface);
3363 switch (im->imgformat) {
3366 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
3367 im->ximg * im->zoom,
3368 im->yimg * im->zoom);
3372 im->surface = strlen(im->graphfile)
3373 ? cairo_pdf_surface_create(im->graphfile, im->ximg * im->zoom,
3374 im->yimg * im->zoom)
3375 : cairo_pdf_surface_create_for_stream
3376 (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3380 im->surface = strlen(im->graphfile)
3382 cairo_ps_surface_create(im->graphfile, im->ximg * im->zoom,
3383 im->yimg * im->zoom)
3384 : cairo_ps_surface_create_for_stream
3385 (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3389 im->surface = strlen(im->graphfile)
3391 cairo_svg_surface_create(im->
3393 im->ximg * im->zoom, im->yimg * im->zoom)
3394 : cairo_svg_surface_create_for_stream
3395 (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3396 cairo_svg_surface_restrict_to_version
3397 (im->surface, CAIRO_SVG_VERSION_1_1);
3400 cairo_destroy(im->cr);
3401 im->cr = cairo_create(im->surface);
3402 cairo_set_antialias(im->cr, im->graph_antialias);
3403 cairo_scale(im->cr, im->zoom, im->zoom);
3404 // pango_cairo_font_map_set_resolution(PANGO_CAIRO_FONT_MAP(font_map), 100);
3405 gfx_new_area(im, 0, 0, 0, im->yimg,
3406 im->ximg, im->yimg, im->graph_col[GRC_BACK]);
3407 gfx_add_point(im, im->ximg, 0);
3409 gfx_new_area(im, im->xorigin,
3412 im->xsize, im->yorigin,
3415 im->yorigin - im->ysize, im->graph_col[GRC_CANVAS]);
3416 gfx_add_point(im, im->xorigin, im->yorigin - im->ysize);
3418 cairo_rectangle(im->cr, im->xorigin, im->yorigin - im->ysize - 1.0,
3419 im->xsize, im->ysize + 2.0);
3421 if (im->minval > 0.0)
3422 areazero = im->minval;
3423 if (im->maxval < 0.0)
3424 areazero = im->maxval;
3425 for (i = 0; i < im->gdes_c; i++) {
3426 switch (im->gdes[i].gf) {
3440 for (ii = 0; ii < im->xsize; ii++) {
3441 if (!isnan(im->gdes[i].p_data[ii])
3442 && im->gdes[i].p_data[ii] != 0.0) {
3443 if (im->gdes[i].yrule > 0) {
3450 im->ysize, 1.0, im->gdes[i].col);
3451 } else if (im->gdes[i].yrule < 0) {
3454 im->yorigin - im->ysize - 1.0,
3456 im->yorigin - im->ysize -
3459 im->ysize, 1.0, im->gdes[i].col);
3467 rrd_value_t diffval = im->maxval - im->minval;
3468 rrd_value_t maxlimit = im->maxval + 9 * diffval;
3469 rrd_value_t minlimit = im->minval - 9 * diffval;
3470 for (ii = 0; ii < im->xsize; ii++) {
3471 /* fix data points at oo and -oo */
3472 if (isinf(im->gdes[i].p_data[ii])) {
3473 if (im->gdes[i].p_data[ii] > 0) {
3474 im->gdes[i].p_data[ii] = im->maxval;
3476 im->gdes[i].p_data[ii] = im->minval;
3479 /* some versions of cairo go unstable when trying
3480 to draw way out of the canvas ... lets not even try */
3481 if (im->gdes[i].p_data[ii] > maxlimit) {
3482 im->gdes[i].p_data[ii] = maxlimit;
3484 if (im->gdes[i].p_data[ii] < minlimit) {
3485 im->gdes[i].p_data[ii] = minlimit;
3489 /* *******************************************************
3494 -------|--t-1--t--------------------------------
3496 if we know the value at time t was a then
3497 we draw a square from t-1 to t with the value a.
3499 ********************************************************* */
3500 if (im->gdes[i].col.alpha != 0.0) {
3501 /* GF_LINE and friend */
3502 if (im->gdes[i].gf == GF_LINE) {
3503 double last_y = 0.0;
3507 cairo_new_path(im->cr);
3508 cairo_set_line_width(im->cr, im->gdes[i].linewidth);
3509 if (im->gdes[i].dash) {
3510 cairo_set_dash(im->cr,
3511 im->gdes[i].p_dashes,
3512 im->gdes[i].ndash, im->gdes[i].offset);
3515 for (ii = 1; ii < im->xsize; ii++) {
3516 if (isnan(im->gdes[i].p_data[ii])
3517 || (im->slopemode == 1
3518 && isnan(im->gdes[i].p_data[ii - 1]))) {
3523 last_y = ytr(im, im->gdes[i].p_data[ii]);
3524 if (im->slopemode == 0) {
3525 double x = ii - 1 + im->xorigin;
3528 gfx_line_fit(im, &x, &y);
3529 cairo_move_to(im->cr, x, y);
3530 x = ii + im->xorigin;
3532 gfx_line_fit(im, &x, &y);
3533 cairo_line_to(im->cr, x, y);
3535 double x = ii - 1 + im->xorigin;
3537 ytr(im, im->gdes[i].p_data[ii - 1]);
3538 gfx_line_fit(im, &x, &y);
3539 cairo_move_to(im->cr, x, y);
3540 x = ii + im->xorigin;
3542 gfx_line_fit(im, &x, &y);
3543 cairo_line_to(im->cr, x, y);
3547 double x1 = ii + im->xorigin;
3548 double y1 = ytr(im, im->gdes[i].p_data[ii]);
3550 if (im->slopemode == 0
3551 && !AlmostEqual2sComplement(y1, last_y, 4)) {
3552 double x = ii - 1 + im->xorigin;
3555 gfx_line_fit(im, &x, &y);
3556 cairo_line_to(im->cr, x, y);
3559 gfx_line_fit(im, &x1, &y1);
3560 cairo_line_to(im->cr, x1, y1);
3563 cairo_set_source_rgba(im->cr,
3569 col.blue, im->gdes[i].col.alpha);
3570 cairo_set_line_cap(im->cr, CAIRO_LINE_CAP_ROUND);
3571 cairo_set_line_join(im->cr, CAIRO_LINE_JOIN_ROUND);
3572 cairo_stroke(im->cr);
3573 cairo_restore(im->cr);
3579 (double *) malloc(sizeof(double) * im->xsize * 2);
3581 (double *) malloc(sizeof(double) * im->xsize * 2);
3583 (double *) malloc(sizeof(double) * im->xsize * 2);
3585 (double *) malloc(sizeof(double) * im->xsize * 2);
3588 for (ii = 0; ii <= im->xsize; ii++) {
3591 if (idxI > 0 && (drawem != 0 || ii == im->xsize)) {
3597 AlmostEqual2sComplement(foreY
3601 AlmostEqual2sComplement(foreY
3607 if (im->gdes[i].gf != GF_GRAD) {
3612 foreY[cntI], im->gdes[i].col);
3614 lastx = foreX[cntI];
3615 lasty = foreY[cntI];
3617 while (cntI < idxI) {
3622 AlmostEqual2sComplement(foreY
3626 AlmostEqual2sComplement(foreY
3633 if (im->gdes[i].gf != GF_GRAD) {
3634 gfx_add_point(im, foreX[cntI], foreY[cntI]);
3636 gfx_add_rect_fadey(im,
3638 foreX[cntI], foreY[cntI], lasty,
3641 im->gdes[i].gradheight
3643 lastx = foreX[cntI];
3644 lasty = foreY[cntI];
3647 if (im->gdes[i].gf != GF_GRAD) {
3648 gfx_add_point(im, backX[idxI], backY[idxI]);
3650 gfx_add_rect_fadey(im,
3652 backX[idxI], backY[idxI], lasty,
3655 im->gdes[i].gradheight);
3656 lastx = backX[idxI];
3657 lasty = backY[idxI];
3664 AlmostEqual2sComplement(backY
3668 AlmostEqual2sComplement(backY
3675 if (im->gdes[i].gf != GF_GRAD) {
3676 gfx_add_point(im, backX[idxI], backY[idxI]);
3678 gfx_add_rect_fadey(im,
3680 backX[idxI], backY[idxI], lasty,
3683 im->gdes[i].gradheight);
3684 lastx = backX[idxI];
3685 lasty = backY[idxI];
3690 if (im->gdes[i].gf != GF_GRAD)
3697 if (ii == im->xsize)
3699 if (im->slopemode == 0 && ii == 0) {
3702 if (isnan(im->gdes[i].p_data[ii])) {
3706 ytop = ytr(im, im->gdes[i].p_data[ii]);
3707 if (lastgdes && im->gdes[i].stack) {
3708 ybase = ytr(im, lastgdes->p_data[ii]);
3710 ybase = ytr(im, areazero);
3712 if (ybase == ytop) {
3718 double extra = ytop;
3723 if (im->slopemode == 0) {
3724 backY[++idxI] = ybase - 0.2;
3725 backX[idxI] = ii + im->xorigin - 1;
3726 foreY[idxI] = ytop + 0.2;
3727 foreX[idxI] = ii + im->xorigin - 1;
3729 backY[++idxI] = ybase - 0.2;
3730 backX[idxI] = ii + im->xorigin;
3731 foreY[idxI] = ytop + 0.2;
3732 foreX[idxI] = ii + im->xorigin;
3734 /* close up any remaining area */
3739 } /* else GF_LINE */
3741 /* if color != 0x0 */
3742 /* make sure we do not run into trouble when stacking on NaN */
3743 for (ii = 0; ii < im->xsize; ii++) {
3744 if (isnan(im->gdes[i].p_data[ii])) {
3745 if (lastgdes && (im->gdes[i].stack)) {
3746 im->gdes[i].p_data[ii] = lastgdes->p_data[ii];
3748 im->gdes[i].p_data[ii] = areazero;
3752 lastgdes = &(im->gdes[i]);
3754 } /* GF_AREA, GF_LINE, GF_GRAD */
3757 ("STACK should already be turned into LINE or AREA here");
3762 cairo_reset_clip(im->cr);
3764 /* grid_paint also does the text */
3765 if (!(im->extra_flags & ONLY_GRAPH))
3767 if (!(im->extra_flags & ONLY_GRAPH))
3769 /* the RULES are the last thing to paint ... */
3770 for (i = 0; i < im->gdes_c; i++) {
3772 switch (im->gdes[i].gf) {
3774 if (im->gdes[i].yrule >= im->minval
3775 && im->gdes[i].yrule <= im->maxval) {
3777 if (im->gdes[i].dash) {
3778 cairo_set_dash(im->cr,
3779 im->gdes[i].p_dashes,
3780 im->gdes[i].ndash, im->gdes[i].offset);
3782 gfx_line(im, im->xorigin,
3783 ytr(im, im->gdes[i].yrule),
3784 im->xorigin + im->xsize,
3785 ytr(im, im->gdes[i].yrule), 1.0, im->gdes[i].col);
3786 cairo_stroke(im->cr);
3787 cairo_restore(im->cr);
3791 if (im->gdes[i].xrule >= im->start
3792 && im->gdes[i].xrule <= im->end) {
3794 if (im->gdes[i].dash) {
3795 cairo_set_dash(im->cr,
3796 im->gdes[i].p_dashes,
3797 im->gdes[i].ndash, im->gdes[i].offset);
3800 xtr(im, im->gdes[i].xrule),
3801 im->yorigin, xtr(im,
3805 im->yorigin - im->ysize, 1.0, im->gdes[i].col);
3806 cairo_stroke(im->cr);
3807 cairo_restore(im->cr);
3816 switch (im->imgformat) {
3819 cairo_status_t status;
3821 status = strlen(im->graphfile) ?
3822 cairo_surface_write_to_png(im->surface, im->graphfile)
3823 : cairo_surface_write_to_png_stream(im->surface, &cairo_output,
3826 if (status != CAIRO_STATUS_SUCCESS) {
3827 rrd_set_error("Could not save png to '%s'", im->graphfile);
3833 if (strlen(im->graphfile)) {
3834 cairo_show_page(im->cr);
3836 cairo_surface_finish(im->surface);
3845 /*****************************************************
3847 *****************************************************/
3854 if ((im->gdes = (graph_desc_t *)
3855 rrd_realloc(im->gdes, (im->gdes_c)
3856 * sizeof(graph_desc_t))) == NULL) {
3857 rrd_set_error("realloc graph_descs");
3862 im->gdes[im->gdes_c - 1].step = im->step;
3863 im->gdes[im->gdes_c - 1].step_orig = im->step;
3864 im->gdes[im->gdes_c - 1].stack = 0;
3865 im->gdes[im->gdes_c - 1].linewidth = 0;
3866 im->gdes[im->gdes_c - 1].debug = 0;
3867 im->gdes[im->gdes_c - 1].start = im->start;
3868 im->gdes[im->gdes_c - 1].start_orig = im->start;
3869 im->gdes[im->gdes_c - 1].end = im->end;
3870 im->gdes[im->gdes_c - 1].end_orig = im->end;
3871 im->gdes[im->gdes_c - 1].vname[0] = '\0';
3872 im->gdes[im->gdes_c - 1].data = NULL;
3873 im->gdes[im->gdes_c - 1].ds_namv = NULL;
3874 im->gdes[im->gdes_c - 1].data_first = 0;
3875 im->gdes[im->gdes_c - 1].p_data = NULL;
3876 im->gdes[im->gdes_c - 1].rpnp = NULL;
3877 im->gdes[im->gdes_c - 1].p_dashes = NULL;
3878 im->gdes[im->gdes_c - 1].shift = 0.0;
3879 im->gdes[im->gdes_c - 1].dash = 0;
3880 im->gdes[im->gdes_c - 1].ndash = 0;
3881 im->gdes[im->gdes_c - 1].offset = 0;
3882 im->gdes[im->gdes_c - 1].col.red = 0.0;
3883 im->gdes[im->gdes_c - 1].col.green = 0.0;
3884 im->gdes[im->gdes_c - 1].col.blue = 0.0;
3885 im->gdes[im->gdes_c - 1].col.alpha = 0.0;
3886 im->gdes[im->gdes_c - 1].col2.red = 0.0;
3887 im->gdes[im->gdes_c - 1].col2.green = 0.0;
3888 im->gdes[im->gdes_c - 1].col2.blue = 0.0;
3889 im->gdes[im->gdes_c - 1].col2.alpha = 0.0;
3890 im->gdes[im->gdes_c - 1].gradheight = 50.0;
3891 im->gdes[im->gdes_c - 1].legend[0] = '\0';
3892 im->gdes[im->gdes_c - 1].format[0] = '\0';
3893 im->gdes[im->gdes_c - 1].strftm = 0;
3894 im->gdes[im->gdes_c - 1].rrd[0] = '\0';
3895 im->gdes[im->gdes_c - 1].ds = -1;
3896 im->gdes[im->gdes_c - 1].cf_reduce = CF_AVERAGE;
3897 im->gdes[im->gdes_c - 1].cf = CF_AVERAGE;
3898 im->gdes[im->gdes_c - 1].yrule = DNAN;
3899 im->gdes[im->gdes_c - 1].xrule = 0;
3900 im->gdes[im->gdes_c - 1].daemon[0] = 0;
3904 /* copies input untill the first unescaped colon is found
3905 or until input ends. backslashes have to be escaped as well */
3907 const char *const input,
3913 for (inp = 0; inp < len && input[inp] != ':' && input[inp] != '\0'; inp++) {
3914 if (input[inp] == '\\'
3915 && input[inp + 1] != '\0'
3916 && (input[inp + 1] == '\\' || input[inp + 1] == ':')) {
3917 output[outp++] = input[++inp];
3919 output[outp++] = input[inp];
3922 output[outp] = '\0';
3926 /* Now just a wrapper around rrd_graph_v */
3938 rrd_info_t *grinfo = NULL;
3941 grinfo = rrd_graph_v(argc, argv);
3947 if (strcmp(walker->key, "image_info") == 0) {
3950 (char**)rrd_realloc((*prdata),
3951 (prlines + 1) * sizeof(char *))) == NULL) {
3952 rrd_set_error("realloc prdata");
3955 /* imginfo goes to position 0 in the prdata array */
3956 (*prdata)[prlines - 1] = (char*)malloc((strlen(walker->value.u_str)
3957 + 2) * sizeof(char));
3958 strcpy((*prdata)[prlines - 1], walker->value.u_str);
3959 (*prdata)[prlines] = NULL;
3961 /* skip anything else */
3962 walker = walker->next;
3970 if (strcmp(walker->key, "image_width") == 0) {
3971 *xsize = walker->value.u_cnt;
3972 } else if (strcmp(walker->key, "image_height") == 0) {
3973 *ysize = walker->value.u_cnt;
3974 } else if (strcmp(walker->key, "value_min") == 0) {
3975 *ymin = walker->value.u_val;
3976 } else if (strcmp(walker->key, "value_max") == 0) {
3977 *ymax = walker->value.u_val;
3978 } else if (strncmp(walker->key, "print", 5) == 0) { /* keys are prdate[0..] */
3981 (char**)rrd_realloc((*prdata),
3982 (prlines + 1) * sizeof(char *))) == NULL) {
3983 rrd_set_error("realloc prdata");
3986 (*prdata)[prlines - 1] = (char*)malloc((strlen(walker->value.u_str)
3987 + 2) * sizeof(char));
3988 (*prdata)[prlines] = NULL;
3989 strcpy((*prdata)[prlines - 1], walker->value.u_str);
3990 } else if (strcmp(walker->key, "image") == 0) {
3991 if ( fwrite(walker->value.u_blo.ptr, walker->value.u_blo.size, 1,
3992 (stream ? stream : stdout)) == 0 && ferror(stream ? stream : stdout)){
3993 rrd_set_error("writing image");
3997 /* skip anything else */
3998 walker = walker->next;
4000 rrd_info_free(grinfo);
4005 /* Some surgery done on this function, it became ridiculously big.
4007 ** - initializing now in rrd_graph_init()
4008 ** - options parsing now in rrd_graph_options()
4009 ** - script parsing now in rrd_graph_script()
4011 rrd_info_t *rrd_graph_v(
4018 rrd_graph_init(&im);
4019 /* a dummy surface so that we can measure text sizes for placements */
4020 old_locale = setlocale(LC_NUMERIC, NULL);
4021 setlocale(LC_NUMERIC, "C");
4022 rrd_graph_options(argc, argv, &im);
4023 if (rrd_test_error()) {
4024 setlocale(LC_NUMERIC, old_locale); /* reenable locale */
4025 rrd_info_free(im.grinfo);
4030 if (optind >= argc) {
4031 setlocale(LC_NUMERIC, old_locale); /* reenable locale */
4032 rrd_info_free(im.grinfo);
4034 rrd_set_error("missing filename");
4038 if (strlen(argv[optind]) >= MAXPATH) {
4039 setlocale(LC_NUMERIC, old_locale); /* reenable locale */
4040 rrd_set_error("filename (including path) too long");
4041 rrd_info_free(im.grinfo);
4046 strncpy(im.graphfile, argv[optind], MAXPATH - 1);
4047 im.graphfile[MAXPATH - 1] = '\0';
4049 if (strcmp(im.graphfile, "-") == 0) {
4050 im.graphfile[0] = '\0';
4053 rrd_graph_script(argc, argv, &im, 1);
4054 setlocale(LC_NUMERIC, old_locale); /* reenable locale for rendering the graph */
4056 if (rrd_test_error()) {
4057 rrd_info_free(im.grinfo);
4062 /* Everything is now read and the actual work can start */
4064 if (graph_paint(&im) == -1) {
4065 rrd_info_free(im.grinfo);
4071 /* The image is generated and needs to be output.
4072 ** Also, if needed, print a line with information about the image.
4080 path = strdup(im.graphfile);
4081 filename = basename(path);
4083 sprintf_alloc(im.imginfo,
4086 im.ximg), (long) (im.zoom * im.yimg));
4087 grinfo_push(&im, sprintf_alloc("image_info"), RD_I_STR, info);
4091 if (im.rendered_image) {
4094 img.u_blo.size = im.rendered_image_size;
4095 img.u_blo.ptr = im.rendered_image;
4096 grinfo_push(&im, sprintf_alloc("image"), RD_I_BLO, img);
4105 image_desc_t *im,int prop,char *font, double size ){
4107 strncpy(im->text_prop[prop].font, font, sizeof(text_prop[prop].font) - 1);
4108 im->text_prop[prop].font[sizeof(text_prop[prop].font) - 1] = '\0';
4109 /* if we already got one, drop it first */
4110 pango_font_description_free(im->text_prop[prop].font_desc);
4111 im->text_prop[prop].font_desc = pango_font_description_from_string( font );
4114 im->text_prop[prop].size = size;
4116 if (im->text_prop[prop].font_desc && im->text_prop[prop].size ){
4117 pango_font_description_set_size(im->text_prop[prop].font_desc, im->text_prop[prop].size * PANGO_SCALE);
4121 void rrd_graph_init(
4126 char *deffont = getenv("RRD_DEFAULT_FONT");
4127 static PangoFontMap *fontmap = NULL;
4128 PangoContext *context;
4135 im->daemon_addr = NULL;
4136 im->draw_x_grid = 1;
4137 im->draw_y_grid = 1;
4138 im->draw_3d_border = 2;
4139 im->dynamic_labels = 0;
4140 im->extra_flags = 0;
4141 im->font_options = cairo_font_options_create();
4142 im->forceleftspace = 0;
4145 im->graph_antialias = CAIRO_ANTIALIAS_GRAY;
4146 im->grid_dash_off = 1;
4147 im->grid_dash_on = 1;
4149 im->grinfo = (rrd_info_t *) NULL;
4150 im->grinfo_current = (rrd_info_t *) NULL;
4151 im->imgformat = IF_PNG;
4154 im->legenddirection = TOP_DOWN;
4155 im->legendheight = 0;
4156 im->legendposition = SOUTH;
4157 im->legendwidth = 0;
4158 im->logarithmic = 0;
4165 im->rendered_image_size = 0;
4166 im->rendered_image = NULL;
4170 im->tabwidth = 40.0;
4171 im->title[0] = '\0';
4172 im->unitsexponent = 9999;
4173 im->unitslength = 6;
4174 im->viewfactor = 1.0;
4175 im->watermark[0] = '\0';
4176 im->with_markup = 0;
4178 im->xlab_user.minsec = -1;
4180 im->xOriginLegend = 0;
4181 im->xOriginLegendY = 0;
4182 im->xOriginLegendY2 = 0;
4183 im->xOriginTitle = 0;
4185 im->ygridstep = DNAN;
4187 im->ylegend[0] = '\0';
4188 im->second_axis_scale = 0; /* 0 disables it */
4189 im->second_axis_shift = 0; /* no shift by default */
4190 im->second_axis_legend[0] = '\0';
4191 im->second_axis_format[0] = '\0';
4193 im->yOriginLegend = 0;
4194 im->yOriginLegendY = 0;
4195 im->yOriginLegendY2 = 0;
4196 im->yOriginTitle = 0;
4200 im->surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
4201 im->cr = cairo_create(im->surface);
4203 for (i = 0; i < DIM(text_prop); i++) {
4204 im->text_prop[i].size = -1;
4205 im->text_prop[i].font_desc = NULL;
4206 rrd_set_font_desc(im,i, deffont ? deffont : text_prop[i].font,text_prop[i].size);
4209 if (fontmap == NULL){
4210 fontmap = pango_cairo_font_map_get_default();
4213 context = pango_cairo_font_map_create_context((PangoCairoFontMap*)fontmap);
4215 pango_cairo_context_set_resolution(context, 100);
4217 pango_cairo_update_context(im->cr,context);
4219 im->layout = pango_layout_new(context);
4220 g_object_unref (context);
4222 // im->layout = pango_cairo_create_layout(im->cr);
4225 cairo_font_options_set_hint_style
4226 (im->font_options, CAIRO_HINT_STYLE_FULL);
4227 cairo_font_options_set_hint_metrics
4228 (im->font_options, CAIRO_HINT_METRICS_ON);
4229 cairo_font_options_set_antialias(im->font_options, CAIRO_ANTIALIAS_GRAY);
4233 for (i = 0; i < DIM(graph_col); i++)
4234 im->graph_col[i] = graph_col[i];
4240 void rrd_graph_options(
4247 char *parsetime_error = NULL;
4248 char scan_gtm[12], scan_mtm[12], scan_ltm[12], col_nam[12];
4249 time_t start_tmp = 0, end_tmp = 0;
4251 rrd_time_value_t start_tv, end_tv;
4252 long unsigned int color;
4254 /* defines for long options without a short equivalent. should be bytes,
4255 and may not collide with (the ASCII value of) short options */
4256 #define LONGOPT_UNITS_SI 255
4259 struct option long_options[] = {
4260 { "alt-autoscale", no_argument, 0, 'A'},
4261 { "imgformat", required_argument, 0, 'a'},
4262 { "font-smoothing-threshold", required_argument, 0, 'B'},
4263 { "base", required_argument, 0, 'b'},
4264 { "color", required_argument, 0, 'c'},
4265 { "full-size-mode", no_argument, 0, 'D'},
4266 { "daemon", required_argument, 0, 'd'},
4267 { "slope-mode", no_argument, 0, 'E'},
4268 { "end", required_argument, 0, 'e'},
4269 { "force-rules-legend", no_argument, 0, 'F'},
4270 { "imginfo", required_argument, 0, 'f'},
4271 { "graph-render-mode", required_argument, 0, 'G'},
4272 { "no-legend", no_argument, 0, 'g'},
4273 { "height", required_argument, 0, 'h'},
4274 { "no-minor", no_argument, 0, 'I'},
4275 { "interlaced", no_argument, 0, 'i'},
4276 { "alt-autoscale-min", no_argument, 0, 'J'},
4277 { "only-graph", no_argument, 0, 'j'},
4278 { "units-length", required_argument, 0, 'L'},
4279 { "lower-limit", required_argument, 0, 'l'},
4280 { "alt-autoscale-max", no_argument, 0, 'M'},
4281 { "zoom", required_argument, 0, 'm'},
4282 { "no-gridfit", no_argument, 0, 'N'},
4283 { "font", required_argument, 0, 'n'},
4284 { "logarithmic", no_argument, 0, 'o'},
4285 { "pango-markup", no_argument, 0, 'P'},
4286 { "font-render-mode", required_argument, 0, 'R'},
4287 { "rigid", no_argument, 0, 'r'},
4288 { "step", required_argument, 0, 'S'},
4289 { "start", required_argument, 0, 's'},
4290 { "tabwidth", required_argument, 0, 'T'},
4291 { "title", required_argument, 0, 't'},
4292 { "upper-limit", required_argument, 0, 'u'},
4293 { "vertical-label", required_argument, 0, 'v'},
4294 { "watermark", required_argument, 0, 'W'},
4295 { "width", required_argument, 0, 'w'},
4296 { "units-exponent", required_argument, 0, 'X'},
4297 { "x-grid", required_argument, 0, 'x'},
4298 { "alt-y-grid", no_argument, 0, 'Y'},
4299 { "y-grid", required_argument, 0, 'y'},
4300 { "lazy", no_argument, 0, 'z'},
4301 { "units", required_argument, 0, LONGOPT_UNITS_SI},
4302 { "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 */
4303 { "disable-rrdtool-tag",no_argument, 0, 1001},
4304 { "right-axis", required_argument, 0, 1002},
4305 { "right-axis-label", required_argument, 0, 1003},
4306 { "right-axis-format", required_argument, 0, 1004},
4307 { "legend-position", required_argument, 0, 1005},
4308 { "legend-direction", required_argument, 0, 1006},
4309 { "border", required_argument, 0, 1007},
4310 { "grid-dash", required_argument, 0, 1008},
4311 { "dynamic-labels", no_argument, 0, 1009},
4312 { "week-fmt", required_argument, 0, 1010},
4318 opterr = 0; /* initialize getopt */
4319 rrd_parsetime("end-24h", &start_tv);
4320 rrd_parsetime("now", &end_tv);
4322 int option_index = 0;
4324 int col_start, col_end;
4326 opt = getopt_long(argc, argv,
4327 "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",
4328 long_options, &option_index);
4333 im->extra_flags |= NOMINOR;
4336 im->extra_flags |= ALTYGRID;
4339 im->extra_flags |= ALTAUTOSCALE;
4342 im->extra_flags |= ALTAUTOSCALE_MIN;
4345 im->extra_flags |= ALTAUTOSCALE_MAX;
4348 im->extra_flags |= ONLY_GRAPH;
4351 im->extra_flags |= NOLEGEND;
4354 if (strcmp(optarg, "north") == 0) {
4355 im->legendposition = NORTH;
4356 } else if (strcmp(optarg, "west") == 0) {
4357 im->legendposition = WEST;
4358 } else if (strcmp(optarg, "south") == 0) {
4359 im->legendposition = SOUTH;
4360 } else if (strcmp(optarg, "east") == 0) {
4361 im->legendposition = EAST;
4363 rrd_set_error("unknown legend-position '%s'", optarg);
4368 if (strcmp(optarg, "topdown") == 0) {
4369 im->legenddirection = TOP_DOWN;
4370 } else if (strcmp(optarg, "bottomup") == 0) {
4371 im->legenddirection = BOTTOM_UP;
4373 rrd_set_error("unknown legend-position '%s'", optarg);
4378 im->extra_flags |= FORCE_RULES_LEGEND;
4381 im->extra_flags |= NO_RRDTOOL_TAG;
4383 case LONGOPT_UNITS_SI:
4384 if (im->extra_flags & FORCE_UNITS) {
4385 rrd_set_error("--units can only be used once!");
4388 if (strcmp(optarg, "si") == 0)
4389 im->extra_flags |= FORCE_UNITS_SI;
4391 rrd_set_error("invalid argument for --units: %s", optarg);
4396 im->unitsexponent = atoi(optarg);
4399 im->unitslength = atoi(optarg);
4400 im->forceleftspace = 1;
4403 im->tabwidth = atof(optarg);
4406 im->step = atoi(optarg);
4412 im->with_markup = 1;
4415 if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) {
4416 rrd_set_error("start time: %s", parsetime_error);
4421 if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) {
4422 rrd_set_error("end time: %s", parsetime_error);
4427 if (strcmp(optarg, "none") == 0) {
4428 im->draw_x_grid = 0;
4432 "%10[A-Z]:%ld:%10[A-Z]:%ld:%10[A-Z]:%ld:%ld:%n",
4434 &im->xlab_user.gridst,
4436 &im->xlab_user.mgridst,
4438 &im->xlab_user.labst,
4439 &im->xlab_user.precis, &stroff) == 7 && stroff != 0) {
4440 strncpy(im->xlab_form, optarg + stroff,
4441 sizeof(im->xlab_form) - 1);
4442 im->xlab_form[sizeof(im->xlab_form) - 1] = '\0';
4444 (im->xlab_user.gridtm = tmt_conv(scan_gtm)) == -1) {
4445 rrd_set_error("unknown keyword %s", scan_gtm);
4448 (im->xlab_user.mgridtm = tmt_conv(scan_mtm))
4450 rrd_set_error("unknown keyword %s", scan_mtm);
4453 (im->xlab_user.labtm = tmt_conv(scan_ltm)) == -1) {
4454 rrd_set_error("unknown keyword %s", scan_ltm);
4457 im->xlab_user.minsec = 1;
4458 im->xlab_user.stst = im->xlab_form;
4460 rrd_set_error("invalid x-grid format");
4466 if (strcmp(optarg, "none") == 0) {
4467 im->draw_y_grid = 0;
4470 if (sscanf(optarg, "%lf:%d", &im->ygridstep, &im->ylabfact) == 2) {
4471 if (im->ygridstep <= 0) {
4472 rrd_set_error("grid step must be > 0");
4474 } else if (im->ylabfact < 1) {
4475 rrd_set_error("label factor must be > 0");
4479 rrd_set_error("invalid y-grid format");
4484 im->draw_3d_border = atoi(optarg);
4486 case 1008: /* grid-dash */
4490 &im->grid_dash_off) != 2) {
4491 rrd_set_error("expected grid-dash format float:float");
4495 case 1009: /* enable dynamic labels */
4496 im->dynamic_labels = 1;
4499 strncpy(week_fmt,optarg,sizeof week_fmt);
4500 week_fmt[(sizeof week_fmt)-1]='\0';
4502 case 1002: /* right y axis */
4506 &im->second_axis_scale,
4507 &im->second_axis_shift) == 2) {
4508 if(im->second_axis_scale==0){
4509 rrd_set_error("the second_axis_scale must not be 0");
4513 rrd_set_error("invalid right-axis format expected scale:shift");
4518 strncpy(im->second_axis_legend,optarg,150);
4519 im->second_axis_legend[150]='\0';
4522 if (bad_format(optarg)){
4523 rrd_set_error("use either %le or %lf formats");
4526 strncpy(im->second_axis_format,optarg,150);
4527 im->second_axis_format[150]='\0';
4530 strncpy(im->ylegend, optarg, 150);
4531 im->ylegend[150] = '\0';
4534 im->maxval = atof(optarg);
4537 im->minval = atof(optarg);
4540 im->base = atol(optarg);
4541 if (im->base != 1024 && im->base != 1000) {
4543 ("the only sensible value for base apart from 1000 is 1024");
4548 long_tmp = atol(optarg);
4549 if (long_tmp < 10) {
4550 rrd_set_error("width below 10 pixels");
4553 im->xsize = long_tmp;
4556 long_tmp = atol(optarg);
4557 if (long_tmp < 10) {
4558 rrd_set_error("height below 10 pixels");
4561 im->ysize = long_tmp;
4564 im->extra_flags |= FULL_SIZE_MODE;
4567 /* interlaced png not supported at the moment */
4573 im->imginfo = optarg;
4577 (im->imgformat = if_conv(optarg)) == -1) {
4578 rrd_set_error("unsupported graphics format '%s'", optarg);
4589 im->logarithmic = 1;
4593 "%10[A-Z]#%n%8lx%n",
4594 col_nam, &col_start, &color, &col_end) == 2) {
4596 int col_len = col_end - col_start;
4601 (((color & 0xF00) * 0x110000) | ((color & 0x0F0) *
4609 (((color & 0xF000) *
4610 0x11000) | ((color & 0x0F00) *
4611 0x01100) | ((color &
4614 ((color & 0x000F) * 0x00011)
4618 color = (color << 8) + 0xff /* shift left by 8 */ ;
4623 rrd_set_error("the color format is #RRGGBB[AA]");
4626 if ((ci = grc_conv(col_nam)) != -1) {
4627 im->graph_col[ci] = gfx_hex_to_col(color);
4629 rrd_set_error("invalid color name '%s'", col_nam);
4633 rrd_set_error("invalid color def format");
4642 if (sscanf(optarg, "%10[A-Z]:%lf%n", prop, &size, &end) >= 2) {
4643 int sindex, propidx;
4645 if ((sindex = text_prop_conv(prop)) != -1) {
4646 for (propidx = sindex;
4647 propidx < TEXT_PROP_LAST; propidx++) {
4649 rrd_set_font_desc(im,propidx,NULL,size);
4651 if ((int) strlen(optarg) > end+2) {
4652 if (optarg[end] == ':') {
4653 rrd_set_font_desc(im,propidx,optarg + end + 1,0);
4656 ("expected : after font size in '%s'",
4661 /* only run the for loop for DEFAULT (0) for
4662 all others, we break here. woodo programming */
4663 if (propidx == sindex && sindex != 0)
4667 rrd_set_error("invalid fonttag '%s'", prop);
4671 rrd_set_error("invalid text property format");
4677 im->zoom = atof(optarg);
4678 if (im->zoom <= 0.0) {
4679 rrd_set_error("zoom factor must be > 0");
4684 strncpy(im->title, optarg, 150);
4685 im->title[150] = '\0';
4688 if (strcmp(optarg, "normal") == 0) {
4689 cairo_font_options_set_antialias
4690 (im->font_options, CAIRO_ANTIALIAS_GRAY);
4691 cairo_font_options_set_hint_style
4692 (im->font_options, CAIRO_HINT_STYLE_FULL);
4693 } else if (strcmp(optarg, "light") == 0) {
4694 cairo_font_options_set_antialias
4695 (im->font_options, CAIRO_ANTIALIAS_GRAY);
4696 cairo_font_options_set_hint_style
4697 (im->font_options, CAIRO_HINT_STYLE_SLIGHT);
4698 } else if (strcmp(optarg, "mono") == 0) {
4699 cairo_font_options_set_antialias
4700 (im->font_options, CAIRO_ANTIALIAS_NONE);
4701 cairo_font_options_set_hint_style
4702 (im->font_options, CAIRO_HINT_STYLE_FULL);
4704 rrd_set_error("unknown font-render-mode '%s'", optarg);
4709 if (strcmp(optarg, "normal") == 0)
4710 im->graph_antialias = CAIRO_ANTIALIAS_GRAY;
4711 else if (strcmp(optarg, "mono") == 0)
4712 im->graph_antialias = CAIRO_ANTIALIAS_NONE;
4714 rrd_set_error("unknown graph-render-mode '%s'", optarg);
4719 /* not supported curently */
4722 strncpy(im->watermark, optarg, 100);
4723 im->watermark[99] = '\0';
4727 if (im->daemon_addr != NULL)
4729 rrd_set_error ("You cannot specify --daemon "
4734 im->daemon_addr = strdup(optarg);
4735 if (im->daemon_addr == NULL)
4737 rrd_set_error("strdup failed");
4745 rrd_set_error("unknown option '%c'", optopt);
4747 rrd_set_error("unknown option '%s'", argv[optind - 1]);
4752 pango_cairo_context_set_font_options(pango_layout_get_context(im->layout), im->font_options);
4753 pango_layout_context_changed(im->layout);
4757 if (im->logarithmic && im->minval <= 0) {
4759 ("for a logarithmic yaxis you must specify a lower-limit > 0");
4763 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
4764 /* error string is set in rrd_parsetime.c */
4768 if (start_tmp < 3600 * 24 * 365 * 10) {
4770 ("the first entry to fetch should be after 1980 (%ld)",
4775 if (end_tmp < start_tmp) {
4777 ("start (%ld) should be less than end (%ld)", start_tmp, end_tmp);
4781 im->start = start_tmp;
4783 im->step = max((long) im->step, (im->end - im->start) / im->xsize);
4786 int rrd_graph_color(
4794 graph_desc_t *gdp = &im->gdes[im->gdes_c - 1];
4796 color = strstr(var, "#");
4797 if (color == NULL) {
4798 if (optional == 0) {
4799 rrd_set_error("Found no color in %s", err);
4806 long unsigned int col;
4808 rest = strstr(color, ":");
4815 sscanf(color, "#%6lx%n", &col, &n);
4816 col = (col << 8) + 0xff /* shift left by 8 */ ;
4818 rrd_set_error("Color problem in %s", err);
4821 sscanf(color, "#%8lx%n", &col, &n);
4825 rrd_set_error("Color problem in %s", err);
4827 if (rrd_test_error())
4829 gdp->col = gfx_hex_to_col(col);
4842 while (*ptr != '\0')
4843 if (*ptr++ == '%') {
4845 /* line cannot end with percent char */
4848 /* '%s', '%S' and '%%' are allowed */
4849 if (*ptr == 's' || *ptr == 'S' || *ptr == '%')
4851 /* %c is allowed (but use only with vdef!) */
4852 else if (*ptr == 'c') {
4857 /* or else '% 6.2lf' and such are allowed */
4859 /* optional padding character */
4860 if (*ptr == ' ' || *ptr == '+' || *ptr == '-')
4862 /* This should take care of 'm.n' with all three optional */
4863 while (*ptr >= '0' && *ptr <= '9')
4867 while (*ptr >= '0' && *ptr <= '9')
4869 /* Either 'le', 'lf' or 'lg' must follow here */
4872 if (*ptr == 'e' || *ptr == 'f' || *ptr == 'g')
4887 const char *const str)
4889 /* A VDEF currently is either "func" or "param,func"
4890 * so the parsing is rather simple. Change if needed.
4897 sscanf(str, "%le,%29[A-Z]%n", ¶m, func, &n);
4898 if (n == (int) strlen(str)) { /* matched */
4902 sscanf(str, "%29[A-Z]%n", func, &n);
4903 if (n == (int) strlen(str)) { /* matched */
4907 ("Unknown function string '%s' in VDEF '%s'",
4912 if (!strcmp("PERCENT", func))
4913 gdes->vf.op = VDEF_PERCENT;
4914 else if (!strcmp("PERCENTNAN", func))
4915 gdes->vf.op = VDEF_PERCENTNAN;
4916 else if (!strcmp("MAXIMUM", func))
4917 gdes->vf.op = VDEF_MAXIMUM;
4918 else if (!strcmp("AVERAGE", func))
4919 gdes->vf.op = VDEF_AVERAGE;
4920 else if (!strcmp("STDEV", func))
4921 gdes->vf.op = VDEF_STDEV;
4922 else if (!strcmp("MINIMUM", func))
4923 gdes->vf.op = VDEF_MINIMUM;
4924 else if (!strcmp("TOTAL", func))
4925 gdes->vf.op = VDEF_TOTAL;
4926 else if (!strcmp("FIRST", func))
4927 gdes->vf.op = VDEF_FIRST;
4928 else if (!strcmp("LAST", func))
4929 gdes->vf.op = VDEF_LAST;
4930 else if (!strcmp("LSLSLOPE", func))
4931 gdes->vf.op = VDEF_LSLSLOPE;
4932 else if (!strcmp("LSLINT", func))
4933 gdes->vf.op = VDEF_LSLINT;
4934 else if (!strcmp("LSLCORREL", func))
4935 gdes->vf.op = VDEF_LSLCORREL;
4938 ("Unknown function '%s' in VDEF '%s'\n", func, gdes->vname);
4941 switch (gdes->vf.op) {
4943 case VDEF_PERCENTNAN:
4944 if (isnan(param)) { /* no parameter given */
4946 ("Function '%s' needs parameter in VDEF '%s'\n",
4950 if (param >= 0.0 && param <= 100.0) {
4951 gdes->vf.param = param;
4952 gdes->vf.val = DNAN; /* undefined */
4953 gdes->vf.when = 0; /* undefined */
4957 ("Parameter '%f' out of range in VDEF '%s'\n",
4958 param, gdes->vname);
4971 case VDEF_LSLCORREL:
4973 gdes->vf.param = DNAN;
4974 gdes->vf.val = DNAN;
4979 ("Function '%s' needs no parameter in VDEF '%s'\n",
4993 graph_desc_t *src, *dst;
4997 dst = &im->gdes[gdi];
4998 src = &im->gdes[dst->vidx];
4999 data = src->data + src->ds;
5001 steps = (src->end - src->start) / src->step;
5004 ("DEBUG: start == %lu, end == %lu, %lu steps\n",
5005 src->start, src->end, steps);
5007 switch (dst->vf.op) {
5011 if ((array = (rrd_value_t*)malloc(steps * sizeof(double))) == NULL) {
5012 rrd_set_error("malloc VDEV_PERCENT");
5015 for (step = 0; step < steps; step++) {
5016 array[step] = data[step * src->ds_cnt];
5018 qsort(array, step, sizeof(double), vdef_percent_compar);
5019 field = round((dst->vf.param * (double)(steps - 1)) / 100.0);
5020 dst->vf.val = array[field];
5021 dst->vf.when = 0; /* no time component */
5025 for (step = 0; step < steps; step++)
5026 printf("DEBUG: %3li:%10.2f %c\n",
5027 step, array[step], step == field ? '*' : ' ');
5031 case VDEF_PERCENTNAN:{
5034 /* count number of "valid" values */
5036 for (step = 0; step < steps; step++) {
5037 if (!isnan(data[step * src->ds_cnt])) { nancount++; }
5039 /* and allocate it */
5040 if ((array = (rrd_value_t*)malloc(nancount * sizeof(double))) == NULL) {
5041 rrd_set_error("malloc VDEV_PERCENT");
5044 /* and fill it in */
5046 for (step = 0; step < steps; step++) {
5047 if (!isnan(data[step * src->ds_cnt])) {
5048 array[field] = data[step * src->ds_cnt];
5052 qsort(array, nancount, sizeof(double), vdef_percent_compar);
5053 field = round( dst->vf.param * (double)(nancount - 1) / 100.0);
5054 dst->vf.val = array[field];
5055 dst->vf.when = 0; /* no time component */
5062 while (step != steps && isnan(data[step * src->ds_cnt]))
5064 if (step == steps) {
5069 dst->vf.val = data[step * src->ds_cnt];
5070 dst->vf.when = src->start + (step + 1) * src->step;
5073 while (step != steps) {
5074 if (finite(data[step * src->ds_cnt])) {
5075 if (data[step * src->ds_cnt] > dst->vf.val) {
5076 dst->vf.val = data[step * src->ds_cnt];
5077 dst->vf.when = src->start + (step + 1) * src->step;
5089 double average = 0.0;
5091 for (step = 0; step < steps; step++) {
5092 if (finite(data[step * src->ds_cnt])) {
5093 sum += data[step * src->ds_cnt];
5098 if (dst->vf.op == VDEF_TOTAL) {
5099 dst->vf.val = sum * src->step;
5100 dst->vf.when = 0; /* no time component */
5102 } else if (dst->vf.op == VDEF_AVERAGE) {
5103 dst->vf.val = sum / cnt;
5104 dst->vf.when = 0; /* no time component */
5107 average = sum / cnt;
5109 for (step = 0; step < steps; step++) {
5110 if (finite(data[step * src->ds_cnt])) {
5111 sum += pow((data[step * src->ds_cnt] - average), 2.0);
5114 dst->vf.val = pow(sum / cnt, 0.5);
5115 dst->vf.when = 0; /* no time component */
5127 while (step != steps && isnan(data[step * src->ds_cnt]))
5129 if (step == steps) {
5134 dst->vf.val = data[step * src->ds_cnt];
5135 dst->vf.when = src->start + (step + 1) * src->step;
5138 while (step != steps) {
5139 if (finite(data[step * src->ds_cnt])) {
5140 if (data[step * src->ds_cnt] < dst->vf.val) {
5141 dst->vf.val = data[step * src->ds_cnt];
5142 dst->vf.when = src->start + (step + 1) * src->step;
5150 /* The time value returned here is one step before the
5151 * actual time value. This is the start of the first
5155 while (step != steps && isnan(data[step * src->ds_cnt]))
5157 if (step == steps) { /* all entries were NaN */
5162 dst->vf.val = data[step * src->ds_cnt];
5163 dst->vf.when = src->start + step * src->step;
5168 /* The time value returned here is the
5169 * actual time value. This is the end of the last
5173 while (step >= 0 && isnan(data[step * src->ds_cnt]))
5175 if (step < 0) { /* all entries were NaN */
5180 dst->vf.val = data[step * src->ds_cnt];
5181 dst->vf.when = src->start + (step + 1) * src->step;
5187 case VDEF_LSLCORREL:{
5188 /* Bestfit line by linear least squares method */
5191 double SUMx, SUMy, SUMxy, SUMxx, SUMyy, slope, y_intercept, correl;
5198 for (step = 0; step < steps; step++) {
5199 if (finite(data[step * src->ds_cnt])) {
5202 SUMxx += step * step;
5203 SUMxy += step * data[step * src->ds_cnt];
5204 SUMy += data[step * src->ds_cnt];
5205 SUMyy += data[step * src->ds_cnt] * data[step * src->ds_cnt];
5209 slope = (SUMx * SUMy - cnt * SUMxy) / (SUMx * SUMx - cnt * SUMxx);
5210 y_intercept = (SUMy - slope * SUMx) / cnt;
5213 (SUMx * SUMy) / cnt) /
5215 (SUMx * SUMx) / cnt) * (SUMyy - (SUMy * SUMy) / cnt));
5217 if (dst->vf.op == VDEF_LSLSLOPE) {
5218 dst->vf.val = slope;
5221 } else if (dst->vf.op == VDEF_LSLINT) {
5222 dst->vf.val = y_intercept;
5225 } else if (dst->vf.op == VDEF_LSLCORREL) {
5226 dst->vf.val = correl;
5241 /* NaN < -INF < finite_values < INF */
5242 int vdef_percent_compar(
5248 /* Equality is not returned; this doesn't hurt except
5249 * (maybe) for a little performance.
5252 /* First catch NaN values. They are smallest */
5253 if (isnan(*(double *) a))
5255 if (isnan(*(double *) b))
5257 /* NaN doesn't reach this part so INF and -INF are extremes.
5258 * The sign from isinf() is compatible with the sign we return
5260 if (isinf(*(double *) a))
5261 return isinf(*(double *) a);
5262 if (isinf(*(double *) b))
5263 return isinf(*(double *) b);
5264 /* If we reach this, both values must be finite */
5265 if (*(double *) a < *(double *) b)
5274 rrd_info_type_t type,
5275 rrd_infoval_t value)
5277 im->grinfo_current = rrd_info_push(im->grinfo_current, key, type, value);
5278 if (im->grinfo == NULL) {
5279 im->grinfo = im->grinfo_current;
5290 /* Handling based on
5291 - ANSI C99 Specifications http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
5292 - Single UNIX Specification version 2 http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html
5293 - POSIX:2001/Single UNIX Specification version 3 http://www.opengroup.org/onlinepubs/009695399/functions/strftime.html
5294 - POSIX:2008 Specifications http://www.opengroup.org/onlinepubs/9699919799/functions/strftime.html
5295 Specifications tells
5296 "If a conversion specifier is not one of the above, the behavior is undefined."
5299 "A conversion specifier consists of a % character, possibly followed by an E or O modifier character (described below), followed by a character that determines the behavior of the conversion specifier.
5302 "A conversion specification consists of a '%' character, possibly followed by an E or O modifier, and a terminating conversion specifier character that determines the conversion specification's behavior."
5304 POSIX:2008 introduce more complexe behavior that are not handled here.
5306 According to this, this code will replace:
5307 - % followed by @ by a %@
5308 - % followed by by a %SPACE
5309 - % followed by . by a %.
5310 - % followed by % by a %
5311 - % followed by t by a TAB
5312 - % followed by E then anything by '-'
5313 - % followed by O then anything by '-'
5314 - % followed by anything else by at least one '-'. More characters may be added to better fit expected output length
5318 for(j = 0; (j < FMT_LEG_LEN - 1) && (jj < FMT_LEG_LEN); j++) { /* we don't need to parse the last char */
5319 if (format[j] == '%') {
5320 if ((format[j+1] == 'E') || (format[j+1] == 'O')) {
5322 j+=2; /* We skip next 2 following char */
5323 } else if ((format[j+1] == 'C') || (format[j+1] == 'd') ||
5324 (format[j+1] == 'g') || (format[j+1] == 'H') ||
5325 (format[j+1] == 'I') || (format[j+1] == 'm') ||
5326 (format[j+1] == 'M') || (format[j+1] == 'S') ||
5327 (format[j+1] == 'U') || (format[j+1] == 'V') ||
5328 (format[j+1] == 'W') || (format[j+1] == 'y')) {
5330 if (jj < FMT_LEG_LEN) {
5333 j++; /* We skip the following char */
5334 } else if (format[j+1] == 'j') {
5336 if (jj < FMT_LEG_LEN - 1) {
5340 j++; /* We skip the following char */
5341 } else if ((format[j+1] == 'G') || (format[j+1] == 'Y')) {
5342 /* Assuming Year on 4 digit */
5344 if (jj < FMT_LEG_LEN - 2) {
5349 j++; /* We skip the following char */
5350 } else if (format[j+1] == 'R') {
5352 if (jj < FMT_LEG_LEN - 3) {
5358 j++; /* We skip the following char */
5359 } else if (format[j+1] == 'T') {
5361 if (jj < FMT_LEG_LEN - 6) {
5370 j++; /* We skip the following char */
5371 } else if (format[j+1] == 'F') {
5373 if (jj < FMT_LEG_LEN - 8) {
5384 j++; /* We skip the following char */
5385 } else if (format[j+1] == 'D') {
5387 if (jj < FMT_LEG_LEN - 6) {
5396 j++; /* We skip the following char */
5397 } else if (format[j+1] == 'n') {
5398 result[jj++] = '\r';
5399 result[jj++] = '\n';
5400 j++; /* We skip the following char */
5401 } else if (format[j+1] == 't') {
5402 result[jj++] = '\t';
5403 j++; /* We skip the following char */
5404 } else if (format[j+1] == '%') {
5406 j++; /* We skip the following char */
5407 } else if (format[j+1] == ' ') {
5408 if (jj < FMT_LEG_LEN - 1) {
5412 j++; /* We skip the following char */
5413 } else if (format[j+1] == '.') {
5414 if (jj < FMT_LEG_LEN - 1) {
5418 j++; /* We skip the following char */
5419 } else if (format[j+1] == '@') {
5420 if (jj < FMT_LEG_LEN - 1) {
5424 j++; /* We skip the following char */
5427 j++; /* We skip the following char */
5430 result[jj++] = format[j];
5433 result[jj] = '\0'; /* We must force the end of the string */