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