2014-10-06 40 views
0

我有ncurses程序打印带宽使用的直方图。我希望它缩放到最小值而不是始终为0(因此,图将从最小速度开始,而不是从零开始)。如何缩放ncurses直方图程序的最小图形

该图基本上打印:

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); 
} 

回答

1

你需要的所有值的min除了max。那么你的条件是:

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

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

+0

谢谢!这正是我想要实现的。 – 2014-10-06 12:55:36