fix off by 1 error
[rrdtool.git] / src / rrd_graph.h
1 #ifndef _RRD_GRAPH_H
2 #define _RRD_GRAPH_H
3
4 #define y0 cairo_y0
5 #define y1 cairo_y1
6 #define index cairo_index
7
8 /* this may configure __EXTENSIONS__ without which pango will fail to compile
9    so load this early */
10 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
11 #include "../win32/config.h"
12 #else
13 #ifdef HAVE_CONFIG_H
14 #include "../rrd_config.h"
15 #endif
16 #endif
17
18 #include <cairo.h>
19 #include <cairo-pdf.h>
20 #include <cairo-svg.h>
21 #include <cairo-ps.h>
22
23 #include <pango/pangocairo.h>
24
25
26 #include "rrd_tool.h"
27 #include "rrd_rpncalc.h"
28
29 #ifdef WIN32
30 #  include <windows.h>
31 #  define MAXPATH MAX_PATH
32 #endif
33
34 #define ALTYGRID         0x01   /* use alternative y grid algorithm */
35 #define ALTAUTOSCALE     0x02   /* use alternative algorithm to find lower and upper bounds */
36 #define ALTAUTOSCALE_MIN 0x04   /* use alternative algorithm to find lower bounds */
37 #define ALTAUTOSCALE_MAX 0x08   /* use alternative algorithm to find upper bounds */
38 #define NOLEGEND         0x10   /* use no legend */
39 #define NOMINOR          0x20   /* Turn off minor gridlines */
40 #define ONLY_GRAPH       0x40   /* use only graph */
41 #define FORCE_RULES_LEGEND 0x80 /* force printing of HRULE and VRULE legend */
42
43 #define FORCE_UNITS 0x100   /* mask for all FORCE_UNITS_* flags */
44 #define FORCE_UNITS_SI 0x100    /* force use of SI units in Y axis (no effect in linear graph, SI instead of E in log graph) */
45
46 #define FULL_SIZE_MODE     0x200    /* -width and -height indicate the total size of the image */
47 #define NO_RRDTOOL_TAG 0x400  /* disable the rrdtool tag */
48
49 enum tmt_en { TMT_SECOND = 0, TMT_MINUTE, TMT_HOUR, TMT_DAY,
50     TMT_WEEK, TMT_MONTH, TMT_YEAR
51 };
52
53 enum grc_en { GRC_CANVAS = 0, GRC_BACK, GRC_SHADEA, GRC_SHADEB,
54     GRC_GRID, GRC_MGRID, GRC_FONT, GRC_ARROW, GRC_AXIS, GRC_FRAME, __GRC_END__
55 };
56
57 #define MGRIDWIDTH 0.6
58 #define GRIDWIDTH  0.4
59
60 enum gf_en { GF_PRINT = 0, GF_GPRINT, GF_COMMENT, GF_HRULE, GF_VRULE, GF_LINE,
61     GF_AREA,GF_GRAD, GF_STACK, GF_TICK, GF_TEXTALIGN,
62     GF_DEF, GF_CDEF, GF_VDEF, GF_SHIFT,
63     GF_XPORT, GF_XAXIS, GF_YAXIS
64 };
65
66 enum txa_en { TXA_LEFT = 0, TXA_RIGHT, TXA_CENTER, TXA_JUSTIFIED };
67
68 enum vdef_op_en {
69     VDEF_MAXIMUM = 0    /* like the MAX in (G)PRINT */
70         , VDEF_MINIMUM  /* like the MIN in (G)PRINT */
71         , VDEF_AVERAGE  /* like the AVERAGE in (G)PRINT */
72         , VDEF_STDEV    /* the standard deviation */
73         , VDEF_PERCENT  /* Nth percentile */
74         , VDEF_TOTAL    /* average multiplied by time */
75         , VDEF_FIRST    /* first non-unknown value and time */
76         , VDEF_LAST     /* last  non-unknown value and time */
77         , VDEF_LSLSLOPE /* least squares line slope */
78         , VDEF_LSLINT   /* least squares line y_intercept */
79         , VDEF_LSLCORREL    /* least squares line correlation coefficient */
80         , VDEF_PERCENTNAN  /* Nth percentile ignoring NAN*/
81 };
82 enum text_prop_en { 
83     TEXT_PROP_DEFAULT = 0,  /* default settings */
84     TEXT_PROP_TITLE,    /* properties for the title */
85     TEXT_PROP_AXIS,     /* for the numbers next to the axis */
86     TEXT_PROP_UNIT,     /* for the vertical unit description */
87     TEXT_PROP_LEGEND,   /* for the legend below the graph */
88     TEXT_PROP_WATERMARK, /* for the little text to the side of the graph */
89     TEXT_PROP_LAST
90 };
91
92 enum legend_pos{ NORTH = 0, WEST, SOUTH, EAST };
93 enum legend_direction { TOP_DOWN = 0, BOTTOM_UP };
94
95 enum gfx_if_en { IF_PNG = 0, IF_SVG, IF_EPS, IF_PDF, 
96                  IF_XML=128, IF_CSV=129, IF_TSV=130, IF_SSV=131, IF_JSON=132,
97                  IF_XMLENUM=133, IF_JSONTIME=134
98 };
99 enum gfx_en { GFX_LINE = 0, GFX_AREA, GFX_TEXT };
100 enum gfx_h_align_en { GFX_H_NULL = 0, GFX_H_LEFT, GFX_H_RIGHT, GFX_H_CENTER };
101 enum gfx_v_align_en { GFX_V_NULL = 0, GFX_V_TOP, GFX_V_BOTTOM, GFX_V_CENTER };
102
103 enum gfx_type_en {GTYPE_TIME=0,GTYPE_XY=1};
104
105 /* cairo color components */
106 typedef struct gfx_color_t {
107     double    red;
108     double    green;
109     double    blue;
110     double    alpha;
111 } gfx_color_t;
112
113 typedef struct keyvalue_t {
114   char* key;
115   char* value;
116   int pos;
117   int flag;
118 } keyvalue_t;
119
120 typedef struct keyint_t {
121   char*key;
122   int value;
123 } keyint_t;
124
125 typedef struct parsedargs_t {
126   char *arg; /* copy of the parsed string - rewritten*/
127   const char *arg_orig; /* original argument */
128   int kv_cnt; /* number of key/value arguments */
129   keyvalue_t *kv_args; /* key value arguments */
130 } parsedargs_t;
131 void initParsedArguments(parsedargs_t*);
132 void freeParsedArguments(parsedargs_t*);
133 int addToArguments(parsedargs_t*, char*, char*, int);
134 int parseArguments(const char*, parsedargs_t*);
135 void dumpKeyValue(char* ,keyvalue_t*);
136 void dumpArguments(parsedargs_t*);
137 char* getKeyValueArgument(const char*, int, parsedargs_t*);
138 int getMappedKeyValueArgument(const char*,int, parsedargs_t*,
139                               int*,keyint_t**);
140 int getLong(const char*,long *,char**,int);
141 int getDouble(const char*,double *,char**);
142 keyvalue_t* getFirstUnusedArgument(int, parsedargs_t*);
143 char* checkUnusedValues(parsedargs_t*);
144
145 typedef struct text_prop_t {
146     double    size;
147     char      font[1024];
148     PangoFontDescription *font_desc;
149 } text_prop_t;
150
151
152 typedef struct vdef_t {
153     enum vdef_op_en op;
154     double    param;    /* parameter for function, if applicable */
155     double    val;      /* resulting value */
156     time_t    when;     /* timestamp, if applicable */
157     int       never;    /* boolean, indicate that when value mean never */
158 } vdef_t;
159
160 typedef struct xlab_t {
161     long      minsec;   /* minimum sec per pix */
162     long      length;   /* number of secs on the image */
163     enum tmt_en gridtm; /* grid interval in what ? */
164     long      gridst;   /* how many whats per grid */
165     enum tmt_en mgridtm;    /* label interval in what ? */
166     long      mgridst;  /* how many whats per label */
167     enum tmt_en labtm;  /* label interval in what ? */
168     long      labst;    /* how many whats per label */
169     long      precis;   /* label precision -> label placement */
170     char     *stst;     /* strftime string */
171 } xlab_t;
172
173 typedef struct ygrid_scale_t {  /* y axis grid scaling info */
174     double    gridstep;
175     int       labfact;
176     char      labfmt[64];
177 } ygrid_scale_t;
178
179 /* sensible y label intervals ...*/
180
181 typedef struct ylab_t {
182     double    grid;     /* grid spacing */
183     int       lfac[4];  /* associated label spacing */
184 } ylab_t;
185
186 /* this structure describes the elements which can make up a graph.
187    because they are quite diverse, not all elements will use all the
188    possible parts of the structure. */
189 #ifdef HAVE_SNPRINTF
190 #define FMT_LEG_LEN 200
191 #else
192 #define FMT_LEG_LEN 2000
193 #endif
194
195 # define MAX_AXIS 4
196
197 typedef struct graph_desc_t {
198     enum gf_en gf;      /* graphing function */
199     int       stack;    /* boolean */
200     int       debug;    /* boolean */
201     char      vname[MAX_VNAME_LEN + 1]; /* name of the variable */
202     long      vidx;     /* gdes reference */
203     char      rrd[1024];    /* name of the rrd_file containing data */
204     char      ds_nam[DS_NAM_SIZE];  /* data source name */
205     long      ds;       /* data source number */
206     char      daemon[256];
207     enum cf_en cf;      /* consolidation function */
208     enum cf_en cf_reduce;   /* consolidation function for reduce_data() */
209     struct gfx_color_t col, col2; /* graph color */
210         double    gradheight;
211     char      format[FMT_LEG_LEN + 5];  /* format for PRINT AND GPRINT */
212     char      legend[FMT_LEG_LEN + 5];  /* legend */
213     int       strftm;   /* should the VDEF legend be formated with strftime */
214     double    leg_x, leg_y; /* location of legend */
215     double    yrule;    /* value for y rule line and for VDEF */
216     time_t    xrule;    /* time for x rule line and for VDEF */
217     vdef_t    vf;       /* instruction for VDEF function */
218     rpnp_t   *rpnp;     /* instructions for CDEF function */
219     char     *rpn;      /* string representation of rpn */
220
221     /* SHIFT implementation */
222     int       shidx;    /* gdes reference for offset (-1 --> constant) */
223     time_t    shval;    /* offset if shidx is -1 */
224     time_t    shift;    /* current shift applied */
225
226     /* description of data fetched for the graph element */
227     time_t    start, end;   /* timestaps for first and last data element */
228     time_t    start_orig, end_orig; /* timestaps for first and last data element */
229     unsigned long step; /* time between samples */
230     unsigned long step_orig;    /* time between samples */
231     unsigned long ds_cnt;   /* how many data sources are there in the fetch */
232     long      data_first;   /* first pointer to this data */
233     char    **ds_namv;  /* name of datasources  in the fetch. */
234     rrd_value_t *data;  /* the raw data drawn from the rrd */
235     rrd_value_t *p_data;    /* processed data, xsize elments */
236     double    linewidth;    /* linewideth */
237
238     /* dashed line stuff */
239     int       dash;     /* boolean, draw dashed line? */
240     double   *p_dashes; /* pointer do dash array which keeps the lengths of dashes */
241     int       ndash;    /* number of dash segments */
242     double    offset;   /* dash offset along the line */
243
244     enum txa_en txtalign;   /* change default alignment strategy for text */
245
246     /* the axis to use for this graph in x and y*/
247     int xaxisidx;
248     int yaxisidx;
249 } graph_desc_t;
250
251 typedef struct image_desc_t {
252
253     /* configuration of graph */
254     char      graphfile[MAXPATH];   /* filename for graphic */
255     enum      gfx_type_en graph_type; /* type of the graph */
256     long      xsize, ysize; /* graph area size in pixels */
257     struct gfx_color_t graph_col[__GRC_END__];  /* real colors for the graph */
258     text_prop_t text_prop[TEXT_PROP_LAST];  /* text properties */
259     char      ylegend[210]; /* legend along the yaxis */
260     char      title[210];   /* title for graph */
261     char      watermark[110];   /* watermark for graph */
262     int       draw_x_grid;  /* no x-grid at all */
263     int       draw_y_grid;  /* no y-grid at all */
264     unsigned int draw_3d_border; /* size of border in pixels, 0 for off */
265     unsigned int dynamic_labels; /* pick the label shape according to the line drawn */
266     double    grid_dash_on, grid_dash_off;
267     xlab_t    xlab_user;    /* user defined labeling for xaxis */
268     char      xlab_form[210];   /* format for the label on the xaxis */
269     double    second_axis_scale; /* relative to the first axis (0 to disable) */
270     double    second_axis_shift; /* how much is it shifted vs the first axis */
271     char      second_axis_legend[210]; /* label to put on the seond axis */
272     char      second_axis_format[210]; /* format for the numbers on the scond axis */    
273
274     double    ygridstep;    /* user defined step for y grid */
275     int       ylabfact; /* every how many y grid shall a label be written ? */
276     double    tabwidth; /* tabwdith */
277     time_t    start, end;   /* what time does the graph cover */
278     unsigned long step; /* any preference for the default step ? */
279     rrd_value_t minval, maxval; /* extreme values in the data */
280     int       rigid;    /* do not expand range even with 
281                            values outside */
282     ygrid_scale_t ygrid_scale;  /* calculated y axis grid info */
283     int       gridfit;  /* adjust y-axis range etc so all
284                            grindlines falls in integer pixel values */
285     char     *imginfo;  /* construct an <IMG ... tag and return 
286                            as first retval */
287     enum gfx_if_en imgformat;   /* image format */
288     char     *daemon_addr;  /* rrdcached connection string */
289     int       lazy;     /* only update the image if there is
290                            reasonable probablility that the
291                            existing one is out of date */
292     int       slopemode;    /* connect the dots of the curve directly, not using a stair */
293     enum legend_pos legendposition; /* the position of the legend: north, west, south or east */
294     enum legend_direction legenddirection; /* The direction of the legend topdown or bottomup */
295     int       logarithmic;  /* scale the yaxis logarithmic */
296     double    force_scale_min;  /* Force a scale--min */
297     double    force_scale_max;  /* Force a scale--max */
298
299     /* status information */
300     int       with_markup;
301     long      xorigin, yorigin; /* where is (0,0) of the graph */
302     long      xOriginTitle, yOriginTitle; /* where is the origin of the title */
303     long      xOriginLegendY, yOriginLegendY; /* where is the origin of the y legend */
304     long      xOriginLegendY2, yOriginLegendY2; /* where is the origin of the second y legend */
305     long      xOriginLegend, yOriginLegend; /* where is the origin of the legend */
306     long      ximg, yimg;   /* total size of the image */
307     long      legendwidth, legendheight; /* the calculated height and width of the legend */
308     size_t    rendered_image_size;
309     double    zoom;
310     double    magfact;  /* numerical magnitude */
311     long      base;     /* 1000 or 1024 depending on what we graph */
312     char      symbol;   /* magnitude symbol for y-axis */
313     float     viewfactor;   /* how should the numbers on the y-axis be scaled for viewing ? */
314     int       unitsexponent;    /* 10*exponent for units on y-asis */
315     int       unitslength;  /* width of the yaxis labels */
316     int       forceleftspace;   /* do not kill the space to the left of the y-axis if there is no grid */
317
318     int       extra_flags;  /* flags for boolean options */
319     /* data elements */
320
321     unsigned char *rendered_image;
322     long      prt_c;    /* number of print elements */
323     long      gdes_c;   /* number of graphics elements */
324     graph_desc_t *gdes; /* points to an array of graph elements */
325     cairo_surface_t *surface;   /* graphics library */
326     cairo_t  *cr;       /* drawin context */
327     cairo_font_options_t *font_options; /* cairo font options */
328     cairo_antialias_t graph_antialias;  /* antialiasing for the graph */
329     PangoLayout *layout; /* the pango layout we use for writing fonts */
330     rrd_info_t *grinfo; /* root pointer to extra graph info */
331     rrd_info_t *grinfo_current; /* pointing to current entry */
332 } image_desc_t;
333
334 /* Prototypes */
335 int       xtr(
336     image_desc_t *,
337     time_t);
338 double    ytr(
339     image_desc_t *,
340     double);
341 enum gf_en gf_conv(
342     char *);
343 enum gfx_if_en if_conv(
344     char *);
345 enum gfx_type_en type_conv(
346     char *);
347 enum tmt_en tmt_conv(
348     char *);
349 enum grc_en grc_conv(
350     char *);
351 enum text_prop_en text_prop_conv(
352     char *);
353 int       im_free(
354     image_desc_t *);
355 void      auto_scale(
356     image_desc_t *,
357     double *,
358     char **,
359     double *);
360 void      si_unit(
361     image_desc_t *);
362 void      expand_range(
363     image_desc_t *);
364 void      apply_gridfit(
365     image_desc_t *);
366 void      reduce_data(
367     enum cf_en,
368     unsigned long,
369     time_t *,
370     time_t *,
371     unsigned long *,
372     unsigned long *,
373     rrd_value_t **);
374 int       data_fetch(
375     image_desc_t *);
376 long      find_var(
377     image_desc_t *,
378     char *);
379 long      find_var_wrapper(
380     void *arg1,
381     char *key);
382 long      lcd(
383     long *);
384 int       data_calc(
385     image_desc_t *);
386 int       data_proc(
387     image_desc_t *);
388 time_t    find_first_time(
389     time_t,
390     enum tmt_en,
391     long);
392 time_t    find_next_time(
393     time_t,
394     enum tmt_en,
395     long);
396 int       print_calc(
397     image_desc_t *);
398 int       leg_place(
399     image_desc_t *,
400     int);
401 int       calc_horizontal_grid(
402     image_desc_t *);
403 int       draw_horizontal_grid(
404     image_desc_t *);
405 int       horizontal_log_grid(
406     image_desc_t *);
407 void      vertical_grid(
408     image_desc_t *);
409 void      axis_paint(
410     image_desc_t *);
411 void      grid_paint(
412     image_desc_t *);
413 int       lazy_check(
414     image_desc_t *);
415 int       graph_paint(
416     image_desc_t *);
417 int       graph_paint_timestring(
418                                 image_desc_t *,int,int);
419 int       graph_paint_xy(
420                         image_desc_t *,int,int);
421 int       rrd_graph_xport(
422     image_desc_t *);
423
424 int       graph_cairo_setup(
425     image_desc_t *);
426 int       graph_cairo_finish(
427     image_desc_t *);
428
429 int       gdes_alloc(
430     image_desc_t *);
431 int       scan_for_col(
432     const char *const,
433     int,
434     char *const);
435 void      rrd_graph_init(
436     image_desc_t *);
437
438 void      time_clean(
439     char *result,
440     char *format);
441
442 void      rrd_graph_options(
443     int,
444     char **,
445     image_desc_t *);
446 void      rrd_graph_script(
447     int,
448     char **,
449     image_desc_t *,
450     int);
451 int       rrd_graph_color(
452     image_desc_t *,
453     char *,
454     char *,
455     int);
456 int       bad_format(
457     char *);
458 int       vdef_parse(
459     struct graph_desc_t *,
460     const char *const);
461 int       vdef_calc(
462     image_desc_t *,
463     int);
464 int       vdef_percent_compar(
465     const void *,
466     const void *);
467 int       graph_size_location(
468     image_desc_t *,
469     int);
470
471
472 /* create a new line */
473 void      gfx_line(
474     image_desc_t *im,
475     double X0,
476     double Y0,
477     double X1,
478     double Y1,
479     double width,
480     gfx_color_t color);
481
482 void      gfx_dashed_line(
483     image_desc_t *im,
484     double X0,
485     double Y0,
486     double X1,
487     double Y1,
488     double width,
489     gfx_color_t color,
490     double dash_on,
491     double dash_off);
492
493 /* create a new area */
494 void      gfx_new_area(
495     image_desc_t *im,
496     double X0,
497     double Y0,
498     double X1,
499     double Y1,
500     double X2,
501     double Y2,
502     gfx_color_t color);
503
504 /* add a point to a line or to an area */
505 void      gfx_add_point(
506     image_desc_t *im,
507     double x,
508     double y);
509
510 /* create a rect that has a gradient from color1 to color2 in height pixels 
511  * height > 0:
512  *              gradient starts at top and goes down a fixed number of pixels (fire style)
513  * height < 0:
514  *              gradient starts at bottom and goes up a fixed number of pixels (constant style)
515  * height == 0:
516  *              gradient is stretched between two points
517  */
518 void gfx_add_rect_fadey(
519     image_desc_t *im,
520     double x1,double y1,
521     double x2,double y2,
522         double py,
523     gfx_color_t color1,
524         gfx_color_t color2,
525         double height);
526                                 
527
528
529 /* close current path so it ends at the same point as it started */
530 void      gfx_close_path(
531     image_desc_t *im);
532
533
534 /* create a text node */
535 void      gfx_text(
536     image_desc_t *im,
537     double x,
538     double y,
539     gfx_color_t color,
540     PangoFontDescription *font_desc,
541     double tabwidth,
542     double angle,
543     enum gfx_h_align_en h_align,
544     enum gfx_v_align_en v_align,
545     const char *text);
546
547 /* measure width of a text string */
548 double    gfx_get_text_width(
549     image_desc_t *im,
550     double start,
551     PangoFontDescription *font_desc,
552     double tabwidth,
553     char *text);
554
555
556 /* convert color */
557 gfx_color_t gfx_hex_to_col(
558     long unsigned int);
559
560 void      gfx_line_fit(
561     image_desc_t *im,
562     double *x,
563     double *y);
564
565 void      gfx_area_fit(
566     image_desc_t *im,
567     double *x,
568     double *y);
569
570 #endif
571
572 void      grinfo_push(
573     image_desc_t *im,
574     char *key,
575     rrd_info_type_t type,    rrd_infoval_t value);
576
577