From 348440bc8e6063a1f221ca2fac4074bd8cabb3be Mon Sep 17 00:00:00 2001 From: Nikos Ntarmos Date: Mon, 7 Jan 2008 09:28:07 +0100 Subject: [PATCH] autoscale graph widget FS#26 --- config.c | 2 ++ widgets/graph.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/config.c b/config.c index 7bb5fc052..28ac4499e 100644 --- a/config.c +++ b/config.c @@ -584,6 +584,8 @@ config_parse(const char *confpatharg) CFG_STR((char *) "fg", (char *) NULL, CFGF_NONE), CFG_STR((char *) "bg", (char *) NULL, CFGF_NONE), CFG_STR((char *) "bordercolor", (char *) NULL, CFGF_NONE), + CFG_BOOL((char *) "scale", cfg_false, CFGF_NONE), + CFG_FLOAT((char *) "max", 100.0f, CFGF_NONE), CFG_END() }; static cfg_opt_t widget_progressbar_bar_opts[] = diff --git a/widgets/graph.c b/widgets/graph.c index 16ed9b2bd..a7c26dee0 100644 --- a/widgets/graph.c +++ b/widgets/graph.c @@ -41,6 +41,9 @@ typedef struct int *lines; /* keeps the calculated values (line-length); */ int lines_index; /* pointer to current val */ int lines_size; /* size of lines-array */ + float *line_values; /* actual values */ + int line_max_index; /* index of the current maximum value */ + int line_max; /* maximum value */ } Data; @@ -97,7 +100,9 @@ static void graph_tell(Widget *widget, char *command) { Data *d = widget->data; - int percent; + int percent, i, height, omax, nmax; + + height = (int) (widget->statusbar->height * d->height + 0.5); if(!command || d->width < 1) return; @@ -105,9 +110,51 @@ graph_tell(Widget *widget, char *command) if(++d->lines_index >= d->lines_size) /* cycle inside the array */ d->lines_index = 0; + if (d->line_values && d->line_max_index == d->lines_index && + d->line_values[d->line_max_index]) { + omax = d->line_values[d->line_max_index]; + d->line_values[d->line_max_index] = 0; + for (i = 0; i < d->lines_size; i++) + if (d->line_values[i] > d->line_values[d->line_max_index]) { + d->line_max_index = i; + } + nmax = MAX(d->line_values[d->line_max_index], d->line_max); + if (d->line_values[d->line_max_index]) { + for (i = 0; i < d->lines_size; i++) { + d->lines[i] = (int) (100*d->line_values[i] / nmax) * (height - + 2) / 100 + 0.5; + } + } + } + percent = atoi(command); - if(percent <= 100 && percent >= 0) - d->percent = percent; + if (percent < 0) + { + if (d->line_values) + d->line_values[d->lines_index] = 0; + d->percent = 0; + } + else if (d->line_values) + { + d->line_values[d->lines_index] = percent; + nmax = MAX(d->line_max, d->line_values[d->line_max_index]); + if (d->line_values[d->line_max_index] < percent) { + // Increase the current max + d->line_max_index = d->lines_index; + if (d->line_max < d->line_values[d->line_max_index]) { + for (i = 0; i < d->lines_size; i++) { + d->lines[i] = (int) + (100*d->line_values[i]/d->line_values[d->line_max_index]) + * (height - 2) / 100 + 0.5; + } + } + } + d->percent = (int) (100.0 * d->line_values[d->lines_index] / nmax); + } + else { + d->percent = (percent > d->line_max ? 100 : + (int) (100 * ((float)percent) / d->line_max)); + } } Widget * @@ -139,6 +186,9 @@ graph_new(Statusbar *statusbar, cfg_t *config) } d->lines = p_new(int, d->lines_size); + if (cfg_getbool(config, "scale")) + d->line_values = p_new(float, d->lines_size); + d->line_max = cfg_getfloat(config, "max"); if((color = cfg_getstr(config, "fg"))) d->fg = initxcolor(phys_screen, color);