new GPRINT option :strftime to print time associated with a VDEF value
[rrdtool.git] / src / rrd_graph.h
1 #ifndef _RRD_GRAPH_H
2 #define _RRD_GRAPH_H
3
4 #include "rrd_tool.h"
5 #include "rrd_rpncalc.h"
6 #include "rrd_gfx.h"
7
8 #define MAX_VNAME_LEN 255
9 #define DEF_NAM_FMT "%255[-_A-Za-z0-9]"
10
11 #define ALTYGRID         0x01   /* use alternative y grid algorithm */
12 #define ALTAUTOSCALE     0x02   /* use alternative algorithm to find lower and upper bounds */
13 #define ALTAUTOSCALE_MAX 0x04   /* use alternative algorithm to find upper bounds */
14 #define NOLEGEND         0x08   /* use no legend */
15 #define NOMINOR          0x10    /* Turn off minor gridlines */
16 #define ONLY_GRAPH       0x20   /* use only graph */
17 #define FORCE_RULES_LEGEND 0x40 /* force printing of HRULE and VRULE legend */
18
19 #define FORCE_UNITS 0x80        /* mask for all FORCE_UNITS_* flags */
20 #define FORCE_UNITS_SI 0x80     /* force use of SI units in Y axis (no effect in linear graph, SI instead of E in log graph) */
21
22 enum tmt_en {TMT_SECOND=0,TMT_MINUTE,TMT_HOUR,TMT_DAY,
23              TMT_WEEK,TMT_MONTH,TMT_YEAR};
24
25 enum grc_en {GRC_CANVAS=0,GRC_BACK,GRC_SHADEA,GRC_SHADEB,
26              GRC_GRID,GRC_MGRID,GRC_FONT,GRC_ARROW,GRC_AXIS,GRC_FRAME,__GRC_END__};
27
28 #define MGRIDWIDTH 0.6
29 #define GRIDWIDTH  0.4
30
31 enum gf_en {GF_PRINT=0,GF_GPRINT,GF_COMMENT,GF_HRULE,GF_VRULE,GF_LINE,
32             GF_AREA,GF_STACK,GF_TICK,
33             GF_DEF, GF_CDEF, GF_VDEF, GF_SHIFT,
34 #ifdef WITH_PIECHART
35             GF_PART,
36 #endif
37             GF_XPORT};
38
39 enum vdef_op_en {
40                  VDEF_MAXIMUM=0 /* like the MAX in (G)PRINT */
41                 ,VDEF_MINIMUM   /* like the MIN in (G)PRINT */
42                 ,VDEF_AVERAGE   /* like the AVERAGE in (G)PRINT */
43                 ,VDEF_PERCENT   /* Nth percentile */
44                 ,VDEF_TOTAL     /* average multiplied by time */
45                 ,VDEF_FIRST     /* first non-unknown value and time */
46                 ,VDEF_LAST      /* last  non-unknown value and time */
47                 ,VDEF_LSLSLOPE  /* least squares line slope */
48                 ,VDEF_LSLINT    /* least squares line y_intercept */
49                 ,VDEF_LSLCORREL /* least squares line correlation coefficient */
50                 };
51 enum text_prop_en { TEXT_PROP_DEFAULT=0,   /* default settings */
52                     TEXT_PROP_TITLE,       /* properties for the title */
53                     TEXT_PROP_AXIS,        /* for the numbers next to the axis */
54                     TEXT_PROP_UNIT,        /* for the vertical unit description */
55                     TEXT_PROP_LEGEND,      /* fot the legend below the graph */
56                     TEXT_PROP_LAST };
57
58 typedef struct text_prop_t {
59   double       size;
60   char         font[1024];
61 } text_prop_t;
62
63
64 typedef struct vdef_t {
65     enum vdef_op_en     op;
66     double              param;  /* parameter for function, if applicable */
67     double              val;    /* resulting value */
68     time_t              when;   /* timestamp, if applicable */
69 } vdef_t;
70
71 typedef struct xlab_t {
72     long         minsec;       /* minimum sec per pix */
73     long         length;       /* number of secs on the image */
74     enum tmt_en  gridtm;       /* grid interval in what ?*/
75     long         gridst;       /* how many whats per grid*/
76     enum tmt_en  mgridtm;      /* label interval in what ?*/
77     long         mgridst;      /* how many whats per label*/
78     enum tmt_en  labtm;        /* label interval in what ?*/
79     long         labst;        /* how many whats per label*/
80     long         precis;       /* label precision -> label placement*/
81     char         *stst;        /* strftime string*/
82 } xlab_t;
83
84 typedef struct ygrid_scale_t {  /* y axis grid scaling info */
85     double       gridstep;
86     int          labfact;
87     char         labfmt[64];
88 } ygrid_scale_t;
89
90 /* sensible y label intervals ...*/
91
92 typedef struct ylab_t {
93     double   grid;    /* grid spacing */
94     int      lfac[4]; /* associated label spacing*/
95 } ylab_t;
96
97
98 /* this structure describes the elements which can make up a graph.
99    because they are quite diverse, not all elements will use all the
100    possible parts of the structure. */
101 #ifdef HAVE_SNPRINTF
102 #define FMT_LEG_LEN 200
103 #else
104 #define FMT_LEG_LEN 2000
105 #endif
106
107 typedef  struct graph_desc_t {
108     enum gf_en     gf;         /* graphing function */
109     int            stack;      /* boolean */
110     int            debug;      /* boolean */
111     char           vname[MAX_VNAME_LEN+1];  /* name of the variable */
112     long           vidx;       /* gdes reference */
113     char           rrd[1024];   /* name of the rrd_file containing data */
114     char           ds_nam[DS_NAM_SIZE]; /* data source name */
115     long           ds;         /* data source number */
116     enum cf_en     cf;         /* consolidation function */
117     enum cf_en     cf_reduce;  /* consolidation function for reduce_data() */
118     gfx_color_t    col;        /* graph color */
119     char  format[FMT_LEG_LEN+5]; /* format for PRINT AND GPRINT */
120     char  legend[FMT_LEG_LEN+5]; /* legend*/
121     int            strftm;     /* should the VDEF legend be formated with strftime */
122     double         leg_x,leg_y;  /* location of legend */   
123     double         yrule;      /* value for y rule line and for VDEF */
124     time_t         xrule;      /* time for x rule line and for VDEF */
125     vdef_t         vf;         /* instruction for VDEF function */
126     rpnp_t         *rpnp;     /* instructions for CDEF function */
127
128     /* SHIFT implementation */
129     int            shidx; /* gdes reference for offset (-1 --> constant) */
130     time_t         shval; /* offset if shidx is -1 */
131     time_t         shift; /* current shift applied */
132
133     /* description of data fetched for the graph element */
134     time_t         start,end; /* timestaps for first and last data element */
135     time_t         start_orig,end_orig; /* timestaps for first and last data element */
136     unsigned long  step;      /* time between samples */
137     unsigned long  step_orig;      /* time between samples */
138     unsigned long  ds_cnt; /* how many data sources are there in the fetch */
139     long           data_first; /* first pointer to this data */
140     char           **ds_namv; /* name of datasources  in the fetch. */
141     rrd_value_t    *data; /* the raw data drawn from the rrd */
142     rrd_value_t    *p_data; /* processed data, xsize elments */
143     double         linewidth;  /* linewideth */
144 } graph_desc_t;
145
146 typedef struct image_desc_t {
147
148     /* configuration of graph */
149
150     char           graphfile[MAXPATH]; /* filename for graphic */
151     FILE          *graphhandle;        /* FILE to use if filename is "-" */
152     long           xsize,ysize;        /* graph area size in pixels */
153 #ifdef WITH_PIECHART
154     long           piesize;            /* size of the piechart */
155 #endif
156     gfx_color_t    graph_col[__GRC_END__]; /* real colors for the graph */   
157     text_prop_t    text_prop[TEXT_PROP_LAST]; /* text properties */
158     char           ylegend[200];   /* legend along the yaxis */
159     char           title[200];     /* title for graph */
160     char           watermark[100];   /* watermark for graph */
161     int            draw_x_grid;      /* no x-grid at all */
162     int            draw_y_grid;      /* no x-grid at all */
163     double         grid_dash_on, grid_dash_off;
164     xlab_t         xlab_user;      /* user defined labeling for xaxis */
165     char           xlab_form[200]; /* format for the label on the xaxis */
166
167     double         ygridstep;      /* user defined step for y grid */
168     int            ylabfact;       /* every how many y grid shall a label be written ? */
169     double         tabwidth;       /* tabwdith */
170     time_t         start,end;      /* what time does the graph cover */
171     unsigned long  step;           /* any preference for the default step ? */
172     rrd_value_t    minval,maxval;  /* extreme values in the data */
173     int            rigid;          /* do not expand range even with 
174                                       values outside */
175     ygrid_scale_t  ygrid_scale;    /* calculated y axis grid info */
176     int            gridfit;        /* adjust y-axis range etc so all
177                                       grindlines falls in integer pixel values */
178     char*          imginfo;        /* construct an <IMG ... tag and return 
179                                       as first retval */
180     int            lazy;           /* only update the image if there is
181                                       reasonable probablility that the
182                                       existing one is out of date */
183     int            slopemode;      /* connect the dots of the curve directly, not using a stair */
184     int            logarithmic;    /* scale the yaxis logarithmic */
185     
186     /* status information */
187             
188     long           xorigin,yorigin;/* where is (0,0) of the graph */
189 #ifdef WITH_PIECHART
190     long           pie_x,pie_y;    /* where is the centerpoint */
191 #endif
192     long           ximg,yimg;      /* total size of the image */
193     double         magfact;        /* numerical magnitude*/
194     long         base;            /* 1000 or 1024 depending on what we graph */
195     char           symbol;         /* magnitude symbol for y-axis */
196     float          viewfactor;     /* how should the numbers on the y-axis be scaled for viewing ? */
197     int            unitsexponent;    /* 10*exponent for units on y-asis */
198     int            unitslength;    /* width of the yaxis labels */
199     int            extra_flags;    /* flags for boolean options */
200     /* data elements */
201
202     long  prt_c;                  /* number of print elements */
203     long  gdes_c;                  /* number of graphics elements */
204     graph_desc_t   *gdes;          /* points to an array of graph elements */
205     gfx_canvas_t   *canvas;        /* graphics library */
206 } image_desc_t;
207
208 /* Prototypes */
209 int xtr(image_desc_t *,time_t);
210 double ytr(image_desc_t *, double);
211 enum gf_en gf_conv(char *);
212 enum gfx_if_en if_conv(char *);
213 enum tmt_en tmt_conv(char *);
214 enum grc_en grc_conv(char *);
215 enum text_prop_en text_prop_conv(char *);
216 int im_free(image_desc_t *);
217 void auto_scale( image_desc_t *,  double *, char **, double *);
218 void si_unit( image_desc_t *);
219 void expand_range(image_desc_t *);
220 void apply_gridfit(image_desc_t *);
221 void reduce_data( enum cf_en,  unsigned long,  time_t *, time_t *,  unsigned long *,  unsigned long *,  rrd_value_t **);
222 int data_fetch( image_desc_t *);
223 long find_var(image_desc_t *, char *);
224 long find_var_wrapper(void *arg1, char *key);
225 long lcd(long *);
226 int data_calc( image_desc_t *);
227 int data_proc( image_desc_t *);
228 time_t find_first_time( time_t,  enum tmt_en,  long);
229 time_t find_next_time( time_t,  enum tmt_en,  long);
230 int print_calc(image_desc_t *, char ***);
231 int leg_place(image_desc_t *);
232 int calc_horizontal_grid(image_desc_t *);
233 int draw_horizontal_grid(image_desc_t *);
234 int horizontal_log_grid(image_desc_t *);
235 void vertical_grid(image_desc_t *);
236 void axis_paint(image_desc_t *);
237 void grid_paint(image_desc_t *);
238 int lazy_check(image_desc_t *);
239 int graph_paint(image_desc_t *, char ***);
240 #ifdef WITH_PIECHART
241 void pie_part(image_desc_t *, gfx_color_t, double, double, double, double, double);
242 #endif
243 int gdes_alloc(image_desc_t *);
244 int scan_for_col(const char *const , int, char *const);
245 int rrd_graph(int, char **, char ***, int *, int *, FILE *, double *, double *);
246 void rrd_graph_init(image_desc_t *);
247 void rrd_graph_options(int, char **, image_desc_t *);
248 void rrd_graph_script(int, char **, image_desc_t *, int);
249 int rrd_graph_check_vname(image_desc_t *, char *, char *);
250 int rrd_graph_color(image_desc_t *, char *, char *, int);
251 int bad_format(char *);
252 int vdef_parse(struct graph_desc_t *,const char *const);
253 int vdef_calc(image_desc_t *, int);
254 int vdef_percent_compar(const void *,const void *);
255 int graph_size_location(image_desc_t *, int
256 #ifdef WITH_PIECHART
257  ,int
258 #endif
259 );
260
261 #endif