[英]How to scale graph minimum for ncurses histogram program
I have ncurses program that prints histogram for bandwidth usage. 我有ncurses程序,可打印直方图以显示带宽使用情况。 I would like it to scale to graph minimum instead of being always 0 (So the graph would start from minimum speed instead of zero).
我希望将其缩放到最小值而不是始终为0(因此,图表将从最小速度而不是零开始)。
The graph is basically printed like this: 图形基本上是这样打印的:
if (value / max * lines < currentline)
addch('*');
else
addch(' ');
How can i change the calculation so it would scale the graph minimum? 我如何更改计算方式,以便将图形最小化?
Here is the full graph printing function: 这是完整的图形打印功能:
void printgraphw(WINDOW *win, char *name,
unsigned long *array, unsigned long max, bool siunits,
int lines, int cols, int color) {
int y, x;
werase(win);
box(win, 0, 0);
mvwvline(win, 0, 1, '-', lines-1);
if (name)
mvwprintw(win, 0, cols - 5 - strlen(name), "[ %s ]",name);
mvwprintw(win, 0, 1, "[ %s/s ]", bytestostr(max, siunits));
mvwprintw(win, lines-1, 1, "[ %s/s ]", bytestostr(0.0, siunits));
wattron(win, color);
for (y = 0; y < (lines - 2); y++) {
for (x = 0; x < (cols - 3); x++) {
if (array[x] && max) {
if (lines - 3 - ((double) array[x] / max * lines) < y)
mvwaddch(win, y + 1, x + 2, '*');
}
}
}
wattroff(win, color);
wnoutrefresh(win);
}
You need the min
of all values in addition to max
. 您需要
min
之外的所有值的max
。 Your condition will then be: 您的条件将是:
if ((value - min) / (max - min) * lines < currentline)
addch('*');
else
addch(' ');
(The quotient (value - min) / (max - min)
is between 0 and 1 and requires floating-point arithmetic.) (商
(value - min) / (max - min)
在0和1之间,并且需要浮点运算。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.