1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2015 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "chart-geometry.h"
26 #include "gl/xalloc.h"
27 #include "gl/minmax.h"
28 #include "gl/xvasprintf.h"
30 static const double standard_tick[] = {1, 2, 5, 10};
32 /* Adjust tick to be a sensible value
33 ie: ... 0.1,0.2,0.5, 1,2,5, 10,20,50 ... */
35 chart_rounded_tick (double tick, struct decimal *result)
39 struct decimal ddif = {1, 1000};
41 /* Avoid arithmetic problems with very small values */
42 if (fabs (tick) < DBL_EPSILON)
50 decimal_from_double (&dt, tick);
52 double expd = dec_log10 (&dt) - 1;
54 for (i = 0 ; i < 4 ; ++i)
56 struct decimal candidate;
59 decimal_init (&candidate, standard_tick[i], expd);
62 decimal_subtract (&delta, &candidate);
63 delta.ordinate = llabs (delta.ordinate);
65 if (decimal_cmp (&delta, &ddif) < 0)
74 Find a set {LOWER, INTERVAL, N_TICKS} such that:
77 LOWER + INTERVAL > LOWDBL,
79 LOWER + N_TICKS * INTERVAL < HIGHDBL
80 LOWER + (N_TICKS + 1) * INTERVAL >= HIGHDBL
84 and X is an element of {1, 2, 5}
90 |....+....+....+. .+....|
95 chart_get_scale (double highdbl, double lowdbl,
96 struct decimal *lower,
97 struct decimal *interval,
101 double fitness = DBL_MAX;
106 assert (highdbl >= lowdbl);
108 decimal_from_double (&high, highdbl);
109 decimal_from_double (&low, lowdbl);
111 struct decimal diff = high;
112 decimal_subtract (&diff, &low);
114 double expd = dec_log10 (&diff) - 2;
116 /* Find the most pleasing interval */
117 for (i = 1; i < 4; ++i)
119 struct decimal clbound = low;
120 struct decimal cubound = high;
121 struct decimal candidate;
122 decimal_init (&candidate, standard_tick[i], expd);
124 decimal_divide (&clbound, &candidate);
125 int fl = decimal_floor (&clbound);
126 decimal_int_multiply (&candidate, fl);
130 decimal_init (&candidate, standard_tick[i], expd);
131 decimal_divide (&cubound, &candidate);
132 int fu = decimal_ceil (&cubound);
133 decimal_int_multiply (&candidate, fu);
137 decimal_init (&candidate, standard_tick[i], expd);
138 decimal_subtract (&cubound, &clbound);
139 decimal_divide (&cubound, &candidate);
142 ord_t n_int = decimal_floor (&cubound);
144 /* We prefer to have between 5 and 10 tick marks on a scale */
145 double f = 1 - exp (-0.2 * fabs (n_int - 7.5) / 7.5);
151 *interval = candidate;
158 * Compute the optimum format string and the scaling
159 * for the tick drawing on a chart axis
160 * Input: max: the maximum value of the range
161 * min: the minimum value of the range
162 * nticks: the number of tick intervals (bins) on the axis
163 * Return: fs: format string for printf to print the tick value
164 * scale: scaling factor for the tick value
165 * The format string has to be freed after usage.
166 * An example format string and scalefactor:
167 * Non Scientific: "%.3lf", scale=1.00
168 * Scientific: "%.2lfe3", scale = 0.001
170 * fs = chart_get_ticks_format(95359943.3,34434.9,8,&scale,&long);
171 * printf(fs,value*scale);
175 chart_get_ticks_format (const double max, const double min,
176 const unsigned int nticks, double *scale)
179 double interval = (max - min)/nticks;
180 double logmax = log10(fmax(fabs(max),fabs(min)));
181 double logintv = log10(interval);
183 char *format_string = NULL;
186 if (logmax > 0.0 && logintv < 0.0)
188 nrdecs = MIN(6,(int)(fabs(logintv))+1);
190 format_string = xasprintf("%%.%dlf",nrdecs);
192 else if (logmax > 0.0) /*logintv is > 0*/
196 logshift = 0; /* No scientific format */
198 format_string = xstrdup("%.0lf");
202 logshift = (int)logmax;
203 nrdecs = MIN(6,(int)(logmax-logintv)+1);
204 format_string = xasprintf("%%.%dlfe%d",nrdecs,logshift);
207 else /* logmax and logintv are < 0 */
211 logshift = 0; /* No scientific format */
212 nrdecs = (int)(-logintv) + 1;
213 format_string = xasprintf("%%.%dlf",nrdecs);
217 logshift = (int)logmax-1;
218 nrdecs = MIN(6,(int)(logmax-logintv)+1);
219 format_string = xasprintf("%%.%dlfe%d",nrdecs,logshift);
222 *scale = pow(10.0,-(double)logshift);
223 return format_string;