我有 ncurses 程序,可以打印带宽使用情况的直方图。我希望它能够缩放到图形最小值而不是始终为 0(因此图形将从最小速度而不是零开始)。
图表基本上是这样打印的:
if (value / max * lines < currentline)
addch('*');
else
addch(' ');
如何更改计算,以便将图表缩放至最小值?
这是完整的图形打印功能:
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);
}
最佳答案
除了 max
之外,您还需要所有值的 min
。那么你的情况将是:
if ((value - min) / (max - min) * lines < currentline)
addch('*');
else
addch(' ');
(商(值 - 最小值)/(最大值 - 最小值)
介于 0 和 1 之间,需要浮点运算。)
关于c - 如何为 ncurses 直方图程序缩放图形最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26215442/