autoscale graph widget

FS#26
This commit is contained in:
Nikos Ntarmos 2008-01-07 09:28:07 +01:00 committed by Julien Danjou
parent 127a871367
commit 348440bc8e
2 changed files with 55 additions and 3 deletions

View File

@ -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[] =

View File

@ -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);