|
|
tscale.c - ploot - simple plotting tools |
|
|
 |
git clone git://bitreich.org/ploot git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ploot (git://bitreich.org) |
|
|
 |
Log |
|
|
 |
Files |
|
|
 |
Refs |
|
|
 |
Tags |
|
|
|
--- |
|
|
|
tscale.c (1963B) |
|
|
|
--- |
|
|
|
1 #include "scale.h" |
|
|
|
2 |
|
|
|
3 #include <stddef.h> |
|
|
|
4 #include <time.h> |
|
|
|
5 |
|
|
|
6 #include "util.h" |
|
|
|
7 |
|
|
|
8 /* |
|
|
|
9 * - <max ^ |
|
|
|
10 * - | Translate the coordinates between double values |
|
|
|
11 * - <val szy and height in the plot of <row> rows. |
|
|
|
12 * - | |
|
|
|
13 * - <min v |
|
|
|
14 */ |
|
|
|
15 int |
|
|
|
16 scale_ypos(double val, double min, double max, int szy) |
|
|
|
17 { |
|
|
|
18 return szy * (val - min) / (max - min); |
|
|
|
19 } |
|
|
|
20 |
|
|
|
21 /* |
|
|
|
22 * <---- szx ----> Translate the coordinates between the time |
|
|
|
23 * range and position in the plot of <col> cols. |
|
|
|
24 * t1 t t2 |
|
|
|
25 * | . . | . . | |
|
|
|
26 */ |
|
|
|
27 int |
|
|
|
28 scale_xpos(time_t t, time_t t1, time_t t2, int szx) |
|
|
|
29 { |
|
|
|
30 return szx * (t - t1) / (t2 - t1); |
|
|
|
31 } |
|
|
|
32 |
|
|
|
33 void |
|
|
|
34 scale_minmax(struct csv *vl, int ncol, |
|
|
|
35 time_t *tmin, time_t *tmax, |
|
|
|
36 double *vmin, double *vmax) |
|
|
|
37 { |
|
|
|
38 double *v; |
|
|
|
39 time_t *t; |
|
|
|
40 size_t n; |
|
|
|
41 |
|
|
|
42 *vmin = *vmax = 0; |
|
|
|
43 *tmin = *tmax = *vl->t; |
|
|
|
44 |
|
|
|
45 for (; ncol > 0; ncol--, vl++) { |
|
|
|
46 for (t = vl->t, v = vl->v, n = vl->n; n > 0; t++, v++, n--) { |
|
|
|
47 if (*v < *vmin) *vmin = *v; |
|
|
|
48 if (*v > *vmax) *vmax = *v; |
|
|
|
49 if (*t < *tmin) *tmin = *t; |
|
|
|
50 if (*t > *tmax) *tmax = *t; |
|
|
|
51 } |
|
|
|
52 } |
|
|
|
53 |
|
|
|
54 if (*tmin == *tmax) |
|
|
|
55 err(1, "invalid time scale: min=%lld max=%lld", *tmin, *tmax); |
|
|
|
56 } |
|
|
|
57 |
|
|
|
58 time_t |
|
|
|
59 scale_csvep(time_t min, time_t max, int nval) |
|
|
|
60 { |
|
|
|
61 time_t dt, *sc, scale[] = { |
|
|
|
62 1, 5, 2, 10, 20, 30, 60, 60*2, 60*5, 60*10, 60*20, 60*30, 3600, |
|
|
|
63 3600*2, 3600*5, 3600*10, 3600*18, 3600*24, 3600*24*2, |
|
|
|
64 3600*24*5, 3600*24*10, 3600*24*20, 3600*24*30, 3600*24*50, |
|
|
|
65 3600*24*100, 3600*24*365, 0 |
|
|
|
66 }; |
|
|
|
67 |
|
|
|
68 dt = max - min; |
|
|
|
69 |
|
|
|
70 for (sc = scale; *sc > 0; sc++) |
|
|
|
71 if (dt < *sc * nval) |
|
|
|
72 return *sc; |
|
|
|
73 return dt / nval; |
|
|
|
74 } |
|
|
|
75 |
|
|
|
76 double |
|
|
|
77 scale_vstep(double min, double max, int nval) |
|
|
|
78 { |
|
|
|
79 double dv, d, *sc, scale[] = { 1, 2, 3, 5 }; |
|
|
|
80 |
|
|
|
81 dv = max - min; |
|
|
|
82 |
|
|
|
83 if (dv > 1) |
|
|
|
84 for (d = 1; d != 0; d *= 10) |
|
|
|
85 for (sc = scale; sc < scale + LEN(scale); sc++) |
|
|
|
86 if (dv < *sc * d * nval) |
|
|
|
87 return *sc * d; |
|
|
|
88 if (dv < 1) |
|
|
|
89 for (d = 1; d != 0; d *= 10) |
|
|
|
90 for (sc = scale + LEN(scale) - 1; sc >= scale; sc--) |
|
|
|
91 if (dv > *sc / d * nval / 2) |
|
|
|
92 return *sc / d; |
|
|
|
93 return 0; |
|
|
|
94 } |
|