+/* 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);
+ 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 */