【问题标题】:How to scale graph minimum for ncurses histogram program如何为 ncurses 直方图程序缩放图形最小值
【发布时间】:2014-11-30 15:59:22
【问题描述】:

我有一个 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);
}

【问题讨论】:

    标签: c graph histogram ncurses


    【解决方案1】:

    除了max 之外,您还需要所有值的min。您的情况将是:

    if ((value - min) / (max - min) * lines < currentline)
        addch('*');
    else
        addch(' ');
    

    (商(value - min) / (max - min)介于0和1之间,需要浮点运算。)

    【讨论】:

    • 谢谢!这正是我想要实现的目标。
    猜你喜欢
    • 1970-01-01
    • 2014-01-26
    • 2013-01-10
    • 2014-11-20
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    相关资源
    最近更新 更多