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