[add] time at the bottom axis - ploot - simple plotting tools HTML git clone git://bitreich.org/ploot git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ploot DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 8f05e8625d7d5a684fba3ab97c911d3b98e1ecdd DIR parent ae31e2f1bccbf48ba05d29941119f73b34e1a66b HTML Author: Josuah Demangeon <mail@josuah.net> Date: Wed, 14 Feb 2018 20:13:55 +0100 [add] time at the bottom axis Diffstat: M ploot.c | 109 ++++++++++++++++++------------- A ploot.core | 0 A txt | 0 3 files changed, 62 insertions(+), 47 deletions(-) --- DIR diff --git a/ploot.c b/ploot.c @@ -7,18 +7,15 @@ #include "config.h" -#define MAX_VAL 80 -#define MARGIN 7 - #define ABS(x) ((x) < 0 ? -(x) : (x)) #define MIN(x, y) ((x) < (y) ? (x) : (y)) #define MAX(x, y) ((x) > (y) ? (x) : (y)) #define LEN(buf) (sizeof(buf) / sizeof(*(buf))) /* - * Add `val' at the current position `pos' of the `ring' buffer and set pos to - * the next postion. - */ +** Add `val' at the current position `pos' of the `ring' buffer and set pos to +** the next postion. +*/ #define RING_ADD(rbuf, len, pos, val) \ do { \ rbuf[pos] = val; \ @@ -26,24 +23,27 @@ do { \ } while (0) /* - * Copy the ring buffer `rbuf' content with current position `pos' into the - * buffer `buf'. Both buffer of length `len'. - */ +** Copy the ring buffer `rbuf' content with current position `pos' into the +** buffer `buf'. Both buffer of length `len'. +*/ #define RING_COPY(buf, rbuf, len, pos) \ do { \ memcpy(buf, rbuf + pos, (len - pos) * sizeof(*rbuf)); \ memcpy(buf + (len - pos), rbuf, pos * sizeof(*rbuf)); \ } while (0) +#define MAX_VAL 80 +#define MARGIN 7 + int flag_h = 20; char *flag_t = NULL; time_t flag_o = 0; /* - * Set `str' to a human-readable form of `num' with always a width of 7 (+ 1 - * the '\0' terminator). Buffer overflow is ensured not to happen due to the - * max size of a double. - */ +** Set `str' to a human-readable form of `num' with always a width of 7 (+ 1 +** the '\0' terminator). Buffer overflow is ensured not to happen due to the +** max size of a double. +*/ void humanize(char *str, double val) { @@ -63,8 +63,8 @@ humanize(char *str, double val) } /* - * Returns the maximal double of values between `beg' and `end'. - */ +** Returns the maximal double of values between `beg' and `end'. +*/ double maxdv(double *beg, double *end) { @@ -79,8 +79,8 @@ maxdv(double *beg, double *end) } /* - * If not null, print the title `str' centered on width. - */ +** If not null, print the title `str' centered on width. +*/ void title(char *str, int width) { @@ -90,9 +90,9 @@ title(char *str, int width) } /* - * Print vertical axis with humanized number from time to time, with occurences - * determined after the position on the vertical axis from the bottom `pos'. - */ +** Print vertical axis with humanized number from time to time, with occurences +** determined after the position on the vertical axis from the bottom `pos'. +*/ void vaxis(double val, int pos) { @@ -107,22 +107,35 @@ vaxis(double val, int pos) } /* - * Print horizontal axis for up to `col' values. - */ +** Print horizontal axis for up to `col' values along with dates if reading time +** series. +*/ void -haxis(double *beg, double *end) +haxis(double *beg, double *end, time_t time) { - double *tp; + double *tp; + char buf[9], dbeg[11], dend[11]; printf("%*d -+", MARGIN, 0); for (tp = beg; tp < end; tp++) putchar((*tp < 0) ? ('x') : ('-')); putchar('\n'); + if (flag_o > 0) { + printf("%*c", MARGIN - 1, ' '); + strftime(dbeg, sizeof(dbeg), "%Y/%m/%d", localtime(&time)); + for (tp = beg; tp < end; tp += 7) { + strftime(buf, sizeof(buf), " %H:%M", localtime(&time)); + fputs(buf, stdout); + time += flag_o * 7; + } + strftime(dend, sizeof(dend), "%Y/%m/%d", localtime(&time)); + printf("\n %-*s %s\n", (int)(beg - end) + 4, dbeg, dend); + } } /* - * Print two rows of a plot into a single line using ' ', '.' and ':'. - */ +** Print two rows of a plot into a single line using ' ', '.' and ':'. +*/ void line(double *beg, double *end, double top, double bot) { @@ -135,11 +148,11 @@ line(double *beg, double *end, double top, double bot) } /* - * Plot values between `beg' and `end' in a plot of height `height'. - * If `str' is not NULL, it is set as a title above the graph. - */ +** Plot values between `beg' and `end' in a plot of height `height'. +** If `str' is not NULL, it is set as a title above the graph. +*/ void -plot(double *beg, double *end, int height, char *str) +plot(double *beg, double *end, int height, char *str, time_t start) { double top, bot, max; int h; @@ -156,16 +169,16 @@ plot(double *beg, double *end, int height, char *str) vaxis(top, h); line(beg, end, top, bot); } - haxis(beg, end); + haxis(beg, end, start); putchar('\n'); } /* - * Read a simple format with one double per line and save the last `MAX_WIDTH' - * values into `buf' which must be at least MAX_VAL wide and return a pointer - * to the last element or NULL if the input contains error. - */ +** Read a simple format with one double per line and save the last `MAX_WIDTH' +** values into `buf' which must be at least MAX_VAL wide and return a pointer +** to the last element or NULL if the input contains error. +*/ double * read_simple(double buf[MAX_VAL]) { @@ -183,11 +196,11 @@ read_simple(double buf[MAX_VAL]) } /* - * Read a format with blank separated time_t-double pairs, one per line and save - * the last `MAX_WIDTH' values into `tbuf' and `vbuf' which must both be at - * least MAX_VAL wide and return a pointer to the last element of `vbuf' or - * NULL if the input contains error. - */ +** Read a format with blank separated time_t-double pairs, one per line and save +** the last `MAX_WIDTH' values into `tbuf' and `vbuf' which must both be at +** least MAX_VAL wide and return a pointer to the last element of `vbuf' or +** NULL if the input contains error. +*/ time_t * read_time_series(double *vbuf, time_t *tbuf) { @@ -210,9 +223,9 @@ read_time_series(double *vbuf, time_t *tbuf) } /* - * Walk from `tbeg' and `tend' and add offset in `tbuf' every time there is no - * value in `step' amount of time, by setting a value to -1. - */ +** Walk from `tbeg' and `tend' and add offset in `tbuf' every time there is no +** value in `step' amount of time, by setting a value to -1. +*/ double * skip_gaps(time_t *tbeg, time_t *tend, double *vbuf, time_t step) { @@ -221,14 +234,14 @@ skip_gaps(time_t *tbeg, time_t *tend, double *vbuf, time_t step) double *vp, vrbuf[MAX_VAL]; /* Compute the average alignment of the timestamps values according to - * the step size. */ + ** the step size. */ toff = 0; for (tp = tbeg; tp < tend; tp++) toff += *tp % step; toff = *tbeg + toff / (tend - tbeg) + step / 2; /* Fill `vbuf' with gap added at each time gap using vrbuf as - * intermediate ring buffer. */ + ** intermediate ring buffer. */ len = LEN(vrbuf); for (p = pos = 0, tp = tbeg, vp = vbuf; tp < tend; p++, vp++, tp++) { for (; toff < *tp; toff += step) @@ -253,7 +266,7 @@ usage(void) int main(int argc, char **argv) { - time_t tbuf[MAX_VAL], *tend; + time_t tbuf[MAX_VAL], *tend, start; double vbuf[MAX_VAL], *vend; int c; @@ -278,11 +291,13 @@ main(int argc, char **argv) if (flag_o == 0) { vend = read_simple(vbuf); + start = 0; } else { tend = read_time_series(vbuf, tbuf); vend = skip_gaps(tbuf, tend, vbuf, flag_o); + start = *tbuf; } - plot(vbuf, vend, flag_h, flag_t); + plot(vbuf, vend, flag_h, flag_t, start); return 0; } DIR diff --git a/ploot.core b/ploot.core Binary files differ. DIR diff --git a/txt b/txt