[英]How to scale graph minimum for ncurses histogram program
我有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);
}
您需要min
之外的所有值的max
。 您的條件將是:
if ((value - min) / (max - min) * lines < currentline)
addch('*');
else
addch(' ');
(商(value - min) / (max - min)
在0和1之間,並且需要浮點運算。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.