简体   繁体   中英

How to scale graph minimum for ncurses histogram program

I have ncurses program that prints histogram for bandwidth usage. 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).

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 . 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM