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