+ /* no interleg space if string ends in \g */
+ legspace[i] = (prt_fctn == 'g' ? 0 : interleg);
+ if (fill > 0) {
+ fill += legspace[i];
+ }
+ fill +=
+ gfx_get_text_width(im,
+ fill + border,
+ im->
+ text_prop
+ [TEXT_PROP_LEGEND].
+ font_desc,
+ im->tabwidth, im->gdes[i].legend);
+ leg_c++;
+ } else {
+ legspace[i] = 0;
+ }
+ /* who said there was a special tag ... ? */
+ if (prt_fctn == 'g') {
+ prt_fctn = '\0';
+ }
+
+ if (prt_fctn == '\0') {
+ if(calc_width && (fill > legendwidth)){
+ legendwidth = fill;
+ }
+ if (i == im->gdes_c - 1 || fill > legendwidth) {
+ /* just one legend item is left right or center */
+ switch (default_txtalign) {
+ case TXA_RIGHT:
+ prt_fctn = 'r';
+ break;
+ case TXA_CENTER:
+ prt_fctn = 'c';
+ break;
+ case TXA_JUSTIFIED:
+ prt_fctn = 'j';
+ break;
+ default:
+ prt_fctn = 'l';
+ break;
+ }
+ }
+ /* is it time to place the legends ? */
+ if (fill > legendwidth) {
+ if (leg_c > 1) {
+ /* go back one */
+ i--;
+ fill = fill_last;
+ leg_c--;
+ }
+ }
+ if (leg_c == 1 && prt_fctn == 'j') {
+ prt_fctn = 'l';
+ }
+ }
+
+ if (prt_fctn != '\0') {
+ leg_x = border;
+ if (leg_c >= 2 && prt_fctn == 'j') {
+ glue = (double)(legendwidth - fill) / (double)(leg_c - 1);
+ } else {
+ glue = 0;
+ }
+ if (prt_fctn == 'c')
+ leg_x = (double)(legendwidth - fill) / 2.0;
+ if (prt_fctn == 'r')
+ leg_x = legendwidth - fill - border;
+ for (ii = mark; ii <= i; ii++) {
+ if (im->gdes[ii].legend[0] == '\0')
+ continue; /* skip empty legends */
+ im->gdes[ii].leg_x = leg_x;
+ im->gdes[ii].leg_y = leg_y + border;
+ leg_x +=
+ (double)gfx_get_text_width(im, leg_x,
+ im->
+ text_prop
+ [TEXT_PROP_LEGEND].
+ font_desc,
+ im->tabwidth, im->gdes[ii].legend)
+ +(double)legspace[ii]
+ + glue;
+ }
+ leg_y_prev = leg_y;
+ if (leg_x > border || prt_fctn == 's')
+ leg_y += im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
+ if (prt_fctn == 's')
+ leg_y -= im->text_prop[TEXT_PROP_LEGEND].size;
+
+ if(calc_width && (fill > legendwidth)){
+ legendwidth = fill;
+ }
+ fill = 0;
+ leg_c = 0;
+ mark = ii;
+ }
+
+ if(calc_width){
+ strcpy(im->gdes[i].legend, saved_legend);
+ }
+ }
+
+ if(calc_width){
+ im->legendwidth = legendwidth + 2 * border;
+ }
+ else{
+ im->legendheight = leg_y + border * 0.6;
+ }
+ free(legspace);
+ }
+ return 0;
+}
+
+/* create a grid on the graph. it determines what to do
+ from the values of xsize, start and end */
+
+/* the xaxis labels are determined from the number of seconds per pixel
+ in the requested graph */
+
+int calc_horizontal_grid(
+ image_desc_t
+ *im)
+{
+ double range;
+ double scaledrange;
+ int pixel, i;
+ int gridind = 0;
+ int decimals, fractionals;
+
+ im->ygrid_scale.labfact = 2;
+ range = im->maxval - im->minval;
+ scaledrange = range / im->magfact;
+ /* does the scale of this graph make it impossible to put lines
+ on it? If so, give up. */
+ if (isnan(scaledrange)) {
+ return 0;
+ }
+
+ /* find grid spaceing */
+ pixel = 1;
+ if (isnan(im->ygridstep)) {
+ if (im->extra_flags & ALTYGRID) {
+ /* find the value with max number of digits. Get number of digits */
+ decimals =
+ ceil(log10
+ (max(fabs(im->maxval), fabs(im->minval)) *
+ im->viewfactor / im->magfact));
+ if (decimals <= 0) /* everything is small. make place for zero */
+ decimals = 1;
+ im->ygrid_scale.gridstep =
+ pow((double) 10,
+ floor(log10(range * im->viewfactor / im->magfact))) /
+ im->viewfactor * im->magfact;
+ if (im->ygrid_scale.gridstep == 0) /* range is one -> 0.1 is reasonable scale */
+ im->ygrid_scale.gridstep = 0.1;
+ /* should have at least 5 lines but no more then 15 */
+ if (range / im->ygrid_scale.gridstep < 5
+ && im->ygrid_scale.gridstep >= 30)
+ im->ygrid_scale.gridstep /= 10;
+ if (range / im->ygrid_scale.gridstep > 15)
+ im->ygrid_scale.gridstep *= 10;
+ if (range / im->ygrid_scale.gridstep > 5) {
+ im->ygrid_scale.labfact = 1;
+ if (range / im->ygrid_scale.gridstep > 8
+ || im->ygrid_scale.gridstep <
+ 1.8 * im->text_prop[TEXT_PROP_AXIS].size)
+ im->ygrid_scale.labfact = 2;
+ } else {
+ im->ygrid_scale.gridstep /= 5;
+ im->ygrid_scale.labfact = 5;
+ }
+ fractionals =
+ floor(log10
+ (im->ygrid_scale.gridstep *
+ (double) im->ygrid_scale.labfact * im->viewfactor /
+ im->magfact));
+ if (fractionals < 0) { /* small amplitude. */
+ int len = decimals - fractionals + 1;
+
+ if (im->unitslength < len + 2)
+ im->unitslength = len + 2;
+ sprintf(im->ygrid_scale.labfmt,
+ "%%%d.%df%s", len,
+ -fractionals, (im->symbol != ' ' ? " %c" : ""));
+ } else {
+ int len = decimals + 1;
+
+ if (im->unitslength < len + 2)
+ im->unitslength = len + 2;
+ sprintf(im->ygrid_scale.labfmt,
+ "%%%d.0f%s", len, (im->symbol != ' ' ? " %c" : ""));
+ }
+ } else { /* classic rrd grid */
+ for (i = 0; ylab[i].grid > 0; i++) {
+ pixel = im->ysize / (scaledrange / ylab[i].grid);
+ gridind = i;
+ if (pixel >= 5)
+ break;
+ }
+
+ for (i = 0; i < 4; i++) {
+ if (pixel * ylab[gridind].lfac[i] >=
+ 1.8 * im->text_prop[TEXT_PROP_AXIS].size) {
+ im->ygrid_scale.labfact = ylab[gridind].lfac[i];
+ break;
+ }
+ }
+
+ im->ygrid_scale.gridstep = ylab[gridind].grid * im->magfact;
+ }
+ } else {
+ im->ygrid_scale.gridstep = im->ygridstep;
+ im->ygrid_scale.labfact = im->ylabfact;
+ }
+ return 1;
+}
+
+int draw_horizontal_grid(
+ image_desc_t
+ *im)
+{
+ int i;
+ double scaledstep;
+ char graph_label[100];
+ int nlabels = 0;
+ double X0 = im->xorigin;
+ double X1 = im->xorigin + im->xsize;
+ int sgrid = (int) (im->minval / im->ygrid_scale.gridstep - 1);
+ int egrid = (int) (im->maxval / im->ygrid_scale.gridstep + 1);
+ double MaxY;
+ double second_axis_magfact = 0;
+ char *second_axis_symb = "";
+
+ scaledstep =
+ im->ygrid_scale.gridstep /
+ (double) im->magfact * (double) im->viewfactor;
+ MaxY = scaledstep * (double) egrid;
+ for (i = sgrid; i <= egrid; i++) {
+ double Y0 = ytr(im,
+ im->ygrid_scale.gridstep * i);
+ double YN = ytr(im,
+ im->ygrid_scale.gridstep * (i + 1));
+
+ if (floor(Y0 + 0.5) >=
+ im->yorigin - im->ysize && floor(Y0 + 0.5) <= im->yorigin) {
+ /* Make sure at least 2 grid labels are shown, even if it doesn't agree
+ with the chosen settings. Add a label if required by settings, or if
+ there is only one label so far and the next grid line is out of bounds. */
+ if (i % im->ygrid_scale.labfact == 0
+ || (nlabels == 1
+ && (YN < im->yorigin - im->ysize || YN > im->yorigin))) {
+ if (im->symbol == ' ') {
+ if (im->extra_flags & ALTYGRID) {
+ sprintf(graph_label,
+ im->ygrid_scale.labfmt,
+ scaledstep * (double) i);
+ } else {
+ if (MaxY < 10) {
+ sprintf(graph_label, "%4.1f",
+ scaledstep * (double) i);
+ } else {
+ sprintf(graph_label, "%4.0f",
+ scaledstep * (double) i);
+ }
+ }
+ } else {
+ char sisym = (i == 0 ? ' ' : im->symbol);
+
+ if (im->extra_flags & ALTYGRID) {
+ sprintf(graph_label,
+ im->ygrid_scale.labfmt,
+ scaledstep * (double) i, sisym);
+ } else {
+ if (MaxY < 10) {
+ sprintf(graph_label, "%4.1f %c",
+ scaledstep * (double) i, sisym);
+ } else {
+ sprintf(graph_label, "%4.0f %c",
+ scaledstep * (double) i, sisym);
+ }
+ }
+ }
+ nlabels++;
+ if (im->second_axis_scale != 0){
+ char graph_label_right[100];
+ double sval = im->ygrid_scale.gridstep*(double)i*im->second_axis_scale+im->second_axis_shift;
+ if (im->second_axis_format[0] == '\0'){
+ if (!second_axis_magfact){
+ double dummy = im->ygrid_scale.gridstep*(double)(sgrid+egrid)/2.0*im->second_axis_scale+im->second_axis_shift;
+ auto_scale(im,&dummy,&second_axis_symb,&second_axis_magfact);
+ }
+ sval /= second_axis_magfact;
+
+ if(MaxY < 10) {
+ sprintf(graph_label_right,"%5.1f %s",sval,second_axis_symb);
+ } else {
+ sprintf(graph_label_right,"%5.0f %s",sval,second_axis_symb);
+ }
+ }
+ else {
+ sprintf(graph_label_right,im->second_axis_format,sval);
+ }
+ gfx_text ( im,
+ X1+7, Y0,
+ im->graph_col[GRC_FONT],
+ im->text_prop[TEXT_PROP_AXIS].font_desc,
+ im->tabwidth,0.0, GFX_H_LEFT, GFX_V_CENTER,
+ graph_label_right );
+ }
+
+ gfx_text(im,
+ X0 -
+ im->
+ text_prop[TEXT_PROP_AXIS].
+ size, Y0,
+ im->graph_col[GRC_FONT],
+ im->
+ text_prop[TEXT_PROP_AXIS].
+ font_desc,
+ im->tabwidth, 0.0,
+ GFX_H_RIGHT, GFX_V_CENTER, graph_label);
+ gfx_line(im, X0 - 2, Y0, X0, Y0,
+ MGRIDWIDTH, im->graph_col[GRC_MGRID]);
+ gfx_line(im, X1, Y0, X1 + 2, Y0,
+ MGRIDWIDTH, im->graph_col[GRC_MGRID]);
+ gfx_dashed_line(im, X0 - 2, Y0,
+ X1 + 2, Y0,
+ MGRIDWIDTH,
+ im->
+ graph_col
+ [GRC_MGRID],
+ im->grid_dash_on, im->grid_dash_off);
+ } else if (!(im->extra_flags & NOMINOR)) {
+ gfx_line(im,
+ X0 - 2, Y0,
+ X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
+ gfx_line(im, X1, Y0, X1 + 2, Y0,
+ GRIDWIDTH, im->graph_col[GRC_GRID]);
+ gfx_dashed_line(im, X0 - 1, Y0,
+ X1 + 1, Y0,
+ GRIDWIDTH,
+ im->
+ graph_col[GRC_GRID],
+ im->grid_dash_on, im->grid_dash_off);
+ }
+ }
+ }
+ return 1;
+}
+
+/* this is frexp for base 10 */
+double frexp10(
+ double,
+ double *);
+double frexp10(
+ double x,
+ double *e)
+{
+ double mnt;
+ int iexp;
+
+ iexp = floor(log((double)fabs(x)) / log((double)10));
+ mnt = x / pow(10.0, iexp);
+ if (mnt >= 10.0) {
+ iexp++;
+ mnt = x / pow(10.0, iexp);
+ }
+ *e = iexp;
+ return mnt;
+}
+
+
+/* logaritmic horizontal grid */
+int horizontal_log_grid(
+ image_desc_t
+ *im)
+{
+ double yloglab[][10] = {
+ {
+ 1.0, 10., 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0}, {
+ 1.0, 5.0, 10., 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0}, {
+ 1.0, 2.0, 5.0, 7.0, 10., 0.0, 0.0,
+ 0.0, 0.0, 0.0}, {
+ 1.0, 2.0, 4.0,
+ 6.0, 8.0, 10.,
+ 0.0,
+ 0.0, 0.0, 0.0}, {
+ 1.0,
+ 2.0,
+ 3.0,
+ 4.0,
+ 5.0,
+ 6.0,
+ 7.0,
+ 8.0,
+ 9.0,
+ 10.},
+ {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* last line */
+ };
+ int i, j, val_exp, min_exp;
+ double nex; /* number of decades in data */
+ double logscale; /* scale in logarithmic space */
+ int exfrac = 1; /* decade spacing */
+ int mid = -1; /* row in yloglab for major grid */
+ double mspac; /* smallest major grid spacing (pixels) */
+ int flab; /* first value in yloglab to use */
+ double value, tmp, pre_value;
+ double X0, X1, Y0;
+ char graph_label[100];
+
+ nex = log10(im->maxval / im->minval);
+ logscale = im->ysize / nex;
+ /* major spacing for data with high dynamic range */
+ while (logscale * exfrac < 3 * im->text_prop[TEXT_PROP_LEGEND].size) {
+ if (exfrac == 1)
+ exfrac = 3;
+ else
+ exfrac += 3;
+ }
+
+ /* major spacing for less dynamic data */
+ do {
+ /* search best row in yloglab */
+ mid++;
+ for (i = 0; yloglab[mid][i + 1] < 10.0; i++);
+ mspac = logscale * log10(10.0 / yloglab[mid][i]);
+ }
+ while (mspac >
+ 2 * im->text_prop[TEXT_PROP_LEGEND].size && yloglab[mid][0] > 0);
+ if (mid)
+ mid--;
+ /* find first value in yloglab */
+ for (flab = 0;
+ yloglab[mid][flab] < 10
+ && frexp10(im->minval, &tmp) > yloglab[mid][flab]; flab++);
+ if (yloglab[mid][flab] == 10.0) {
+ tmp += 1.0;
+ flab = 0;
+ }
+ val_exp = tmp;
+ if (val_exp % exfrac)
+ val_exp += abs(-val_exp % exfrac);
+ X0 = im->xorigin;
+ X1 = im->xorigin + im->xsize;
+ /* draw grid */
+ pre_value = DNAN;
+ while (1) {
+
+ value = yloglab[mid][flab] * pow(10.0, val_exp);
+ if (AlmostEqual2sComplement(value, pre_value, 4))
+ break; /* it seems we are not converging */
+ pre_value = value;
+ Y0 = ytr(im, value);
+ if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
+ break;
+ /* major grid line */
+ gfx_line(im,
+ X0 - 2, Y0, X0, Y0, MGRIDWIDTH, im->graph_col[GRC_MGRID]);
+ gfx_line(im, X1, Y0, X1 + 2, Y0,
+ MGRIDWIDTH, im->graph_col[GRC_MGRID]);
+ gfx_dashed_line(im, X0 - 2, Y0,
+ X1 + 2, Y0,
+ MGRIDWIDTH,
+ im->
+ graph_col
+ [GRC_MGRID], im->grid_dash_on, im->grid_dash_off);
+ /* label */
+ if (im->extra_flags & FORCE_UNITS_SI) {
+ int scale;
+ double pvalue;
+ char symbol;
+
+ scale = floor(val_exp / 3.0);
+ if (value >= 1.0)
+ pvalue = pow(10.0, val_exp % 3);
+ else
+ pvalue = pow(10.0, ((val_exp + 1) % 3) + 2);
+ pvalue *= yloglab[mid][flab];
+ if (((scale + si_symbcenter) < (int) sizeof(si_symbol))
+ && ((scale + si_symbcenter) >= 0))
+ symbol = si_symbol[scale + si_symbcenter];
+ else
+ symbol = '?';
+ sprintf(graph_label, "%3.0f %c", pvalue, symbol);
+ } else {
+ sprintf(graph_label, "%3.0e", value);
+ }
+ if (im->second_axis_scale != 0){
+ char graph_label_right[100];
+ double sval = value*im->second_axis_scale+im->second_axis_shift;
+ if (im->second_axis_format[0] == '\0'){
+ if (im->extra_flags & FORCE_UNITS_SI) {
+ double mfac = 1;
+ char *symb = "";
+ auto_scale(im,&sval,&symb,&mfac);
+ sprintf(graph_label_right,"%4.0f %s", sval,symb);
+ }
+ else {
+ sprintf(graph_label_right,"%3.0e", sval);
+ }
+ }
+ else {
+ sprintf(graph_label_right,im->second_axis_format,sval);
+ }
+
+ gfx_text ( im,
+ X1+7, Y0,
+ im->graph_col[GRC_FONT],
+ im->text_prop[TEXT_PROP_AXIS].font_desc,
+ im->tabwidth,0.0, GFX_H_LEFT, GFX_V_CENTER,
+ graph_label_right );
+ }
+
+ gfx_text(im,
+ X0 -
+ im->
+ text_prop[TEXT_PROP_AXIS].
+ size, Y0,
+ im->graph_col[GRC_FONT],
+ im->
+ text_prop[TEXT_PROP_AXIS].
+ font_desc,
+ im->tabwidth, 0.0,
+ GFX_H_RIGHT, GFX_V_CENTER, graph_label);
+ /* minor grid */
+ if (mid < 4 && exfrac == 1) {
+ /* find first and last minor line behind current major line
+ * i is the first line and j tha last */
+ if (flab == 0) {
+ min_exp = val_exp - 1;
+ for (i = 1; yloglab[mid][i] < 10.0; i++);
+ i = yloglab[mid][i - 1] + 1;
+ j = 10;
+ } else {
+ min_exp = val_exp;
+ i = yloglab[mid][flab - 1] + 1;
+ j = yloglab[mid][flab];
+ }
+
+ /* draw minor lines below current major line */
+ for (; i < j; i++) {
+
+ value = i * pow(10.0, min_exp);
+ if (value < im->minval)
+ continue;
+ Y0 = ytr(im, value);
+ if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
+ break;
+ /* draw lines */
+ gfx_line(im,
+ X0 - 2, Y0,
+ X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
+ gfx_line(im, X1, Y0, X1 + 2, Y0,
+ GRIDWIDTH, im->graph_col[GRC_GRID]);
+ gfx_dashed_line(im, X0 - 1, Y0,
+ X1 + 1, Y0,
+ GRIDWIDTH,
+ im->
+ graph_col[GRC_GRID],
+ im->grid_dash_on, im->grid_dash_off);
+ }
+ } else if (exfrac > 1) {
+ for (i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
+ value = pow(10.0, i);
+ if (value < im->minval)
+ continue;
+ Y0 = ytr(im, value);
+ if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
+ break;
+ /* draw lines */
+ gfx_line(im,
+ X0 - 2, Y0,
+ X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
+ gfx_line(im, X1, Y0, X1 + 2, Y0,
+ GRIDWIDTH, im->graph_col[GRC_GRID]);
+ gfx_dashed_line(im, X0 - 1, Y0,
+ X1 + 1, Y0,
+ GRIDWIDTH,
+ im->
+ graph_col[GRC_GRID],
+ im->grid_dash_on, im->grid_dash_off);
+ }
+ }
+
+ /* next decade */
+ if (yloglab[mid][++flab] == 10.0) {
+ flab = 0;
+ val_exp += exfrac;
+ }
+ }
+
+ /* draw minor lines after highest major line */
+ if (mid < 4 && exfrac == 1) {
+ /* find first and last minor line below current major line
+ * i is the first line and j tha last */
+ if (flab == 0) {
+ min_exp = val_exp - 1;
+ for (i = 1; yloglab[mid][i] < 10.0; i++);
+ i = yloglab[mid][i - 1] + 1;
+ j = 10;
+ } else {
+ min_exp = val_exp;
+ i = yloglab[mid][flab - 1] + 1;
+ j = yloglab[mid][flab];
+ }
+
+ /* draw minor lines below current major line */
+ for (; i < j; i++) {
+
+ value = i * pow(10.0, min_exp);
+ if (value < im->minval)
+ continue;
+ Y0 = ytr(im, value);
+ if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
+ break;
+ /* draw lines */
+ gfx_line(im,
+ X0 - 2, Y0, X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
+ gfx_line(im, X1, Y0, X1 + 2, Y0,
+ GRIDWIDTH, im->graph_col[GRC_GRID]);
+ gfx_dashed_line(im, X0 - 1, Y0,
+ X1 + 1, Y0,
+ GRIDWIDTH,
+ im->
+ graph_col[GRC_GRID],
+ im->grid_dash_on, im->grid_dash_off);
+ }
+ }
+ /* fancy minor gridlines */
+ else if (exfrac > 1) {
+ for (i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
+ value = pow(10.0, i);
+ if (value < im->minval)
+ continue;
+ Y0 = ytr(im, value);
+ if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
+ break;
+ /* draw lines */
+ gfx_line(im,
+ X0 - 2, Y0, X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
+ gfx_line(im, X1, Y0, X1 + 2, Y0,
+ GRIDWIDTH, im->graph_col[GRC_GRID]);
+ gfx_dashed_line(im, X0 - 1, Y0,
+ X1 + 1, Y0,
+ GRIDWIDTH,
+ im->
+ graph_col[GRC_GRID],
+ im->grid_dash_on, im->grid_dash_off);
+ }
+ }
+
+ return 1;
+}
+
+
+void vertical_grid(
+ image_desc_t *im)
+{
+ int xlab_sel; /* which sort of label and grid ? */
+ time_t ti, tilab, timajor;
+ long factor;
+ char graph_label[100];
+ double X0, Y0, Y1; /* points for filled graph and more */
+ struct tm tm;