ploot

plot images or real-time plain text from a .csv file
Log | Files | Refs | README

commit 9bd929c04277a0f9d0056e5b50f1233054a4b17f
parent ab839e91e0a3687937992cb48558a15e1547d8ea
Author: Josuah Demangeon <mail@josuah.net>
Date:   Wed,  2 May 2018 12:17:32 +0200

choose whether we scale up or scale down

Diffstat:
Mffplot.c | 22++++++++++------------
Mploot.c | 47+++++++++++++++++++++++++++--------------------
2 files changed, 37 insertions(+), 32 deletions(-)

diff --git a/ffplot.c b/ffplot.c @@ -196,7 +196,7 @@ legend(Canvas *can, Color *label_fg, Vlist *v, int n) int i, x, y; for (i = 0; i < n; i++, v++) { - x = LEGEND_H - n * (FONT_H + MARGIN) - FONT_H / 2; + x = LEGEND_H - i * (FONT_H + MARGIN) - FONT_H / 2; y = MARGIN + FONT_W; ffdraw_str_left(can, &v->col, "\1", font, x, y); @@ -246,22 +246,20 @@ find_scales(Vlist *v, int n, } } - for (i = 1; i != 0; i *= 10) { - for (vs = vscale + LEN(vscale) - 1; vs >= vscale; vs--) { - if (dv > *vs / i * XDENSITY / 2) { - *vstep = *vs / i; - i = 0; - break; + if (dv > 1) for (i = 1; i != 0; i *= 10) { + for (vs = vscale; vs < vscale + LEN(vscale); vs++) { + if (dv < *vs * i * XDENSITY) { + *vstep = *vs * i; + return; } } } for (i = 1; i != 0; i *= 10) { - for (vs = vscale; vs < vscale + LEN(vscale); vs++) { - if (dv < *vs * i * XDENSITY) { - *vstep = *vs * i; - i = 0; - break; + for (vs = vscale + LEN(vscale) - 1; vs >= vscale; vs--) { + if (dv > *vs / i * XDENSITY / 2) { + *vstep = *vs / i; + return; } } } diff --git a/ploot.c b/ploot.c @@ -88,29 +88,31 @@ eatol(char *str) return atol(str); } -static void -add_val(Vlist *v, int *bufsiz, int nval, double field, time_t epoch) +static int +add_val(Vlist *v, int bufsize, int nval, double field, time_t epoch) { - if (nval >= *bufsiz) { - *bufsiz = *bufsiz * 2 + 1; - if ((v->v = realloc(v->v, *bufsiz * sizeof(*v->v))) == NULL) + if (nval >= bufsize) { + bufsize = bufsize * 2 + 1; + if ((v->v = realloc(v->v, bufsize * sizeof(*v->v))) == NULL) perror("reallocating values buffer"), exit(1); - if ((v->t = realloc(v->t, *bufsiz * sizeof(*v->t))) == NULL) + if ((v->t = realloc(v->t, bufsize * sizeof(*v->t))) == NULL) perror("reallocating values buffer"), exit(1); } v->v[nval] = field; v->t[nval] = epoch; v->n = nval + 1; + + return bufsize; } /* * Add to each column the value on the current row. */ -static void -add_row(Vlist *v, int *bufsiz, int ncol, int nval, char *line) +static int +add_row(Vlist *v, int bufsize, int ncol, int nval, char *line) { time_t epoch; - int n; + int bs; char *field, *dot; if ((field = strsep(&line, ",")) == NULL) @@ -119,13 +121,15 @@ add_row(Vlist *v, int *bufsiz, int ncol, int nval, char *line) if ((dot = strchr(field, '.')) != NULL) *dot = '\0'; epoch = eatol(field); - for (n = 0; (field = strsep(&line, ",")) != NULL; n++, v++) { - if (n > ncol) + for (; (field = strsep(&line, ",")) != NULL; ncol--, v++) { + if (ncol <= 0) fprintf(stderr, "%d: too many fields\n", nval), exit(0); - add_val(v, bufsiz, nval, eatof(field), epoch); + bs = add_val(v, bufsize, nval, eatof(field), epoch); } - if (n < ncol) + if (ncol > 0) fprintf(stderr, "%d: too few fields\n", nval), exit(0); + + return bs; } /* @@ -137,13 +141,13 @@ add_row(Vlist *v, int *bufsiz, int ncol, int nval, char *line) static void read_values(Vlist *v, int ncol) { - int nval, bufsiz; + int nval, bufsize; char line[LINE_MAX]; - bufsiz = 0; + bufsize = 0; for (nval = 0; fgets(line, sizeof(line), stdin); nval++) { estriplf(line); - add_row(v, &bufsiz, ncol, nval, line); + bufsize = add_row(v, bufsize, ncol, nval, line); } } @@ -152,10 +156,11 @@ usage(void) { ColorList *c; - fprintf(stderr, "usage: %s [-t title] [-u unit] color...\n" - "available colors as defined by \"config.h\":\n", argv0); - for (c = colorlist; c->name != NULL; c++) - fprintf(stderr, "- %s\n", c->name); + fprintf(stderr, "usage: %s [-t title] [-u unit] {", argv0); + fputs(colorlist->name, stderr); + for (c = colorlist + 1; c->name != NULL; c++) + fprintf(stderr, ",%s", c->name); + fputs("}...\n", stderr); exit(1); } @@ -172,6 +177,8 @@ main(int argc, char **argv) case 'u': uflag = EARGF(usage()); break; + default: + usage(); } ARGEND; if ((v = calloc(argc, sizeof(*v))) == NULL)