c - 如何为 ncurses 直方图程序缩放图形最小值

标签 c graph histogram ncurses

我有 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/

相关文章:

c - 在 Linux 机器上获取用户的默认电子邮件

c - 外部声明中的警告

c++ - 初始化不工作

python - 无法获得合适的直方图

python - matplotlib 框架中的 pandas 直方图,具有日期重采样功能

c - "aborted (core dumped)"在所有程序中

java - OS X Mavericks JNI CallStaticVoidMethod 结果为 SIGSEGV/SIGBUS (Java 8)

algorithm - 访问某些节点的网格图的最短路径算法

python - 如何使用顶点和边的分类属性在图形工具中绘制颜色?

opencv - OpenCV Core.MinMaxLocResult 返回什么?