28 #ifndef _ASCII_PLOTTER 29 #define _ASCII_PLOTTER 48 const std::vector<V> & _xval;
49 const std::vector<V> & _yval;
54 assert(_xval.size() == _yval.size());
55 assert(_xval.size() > 1);
60 V xs = _xval[0], xe = _xval[1], ys = _yval[0], ye = _yval[1];
62 if( (x < _xval[0]) || (x > _xval[_xval.size()-1]) )
65 size_t size = _xval.size();
67 while(x > xe && i < size-1) {
68 xs = _xval[i], xe = _xval[i+1];
69 ys = _yval[i], ye = _yval[i+1];
73 V fctr = (x - xs) / (xe - xs);
74 y = ys + (ye - ys)*fctr;
87 std::vector< std::vector<char> > _canvas;
90 double _x_min, _x_max, _x_div;
91 double _y_min, _y_max, _y_div;
96 for(
unsigned int i=0; i<_rows; i++) {
97 _canvas[i].assign(_cols + 1,
' ');
98 _canvas[i][_cols] =
'\0';
110 void compute_xrange(
const std::vector<V> & x)
112 _x_min = x[0], _x_max = x[x.size()-1];
113 _x_div = (_x_max - _x_min) / (_cols - 1);
117 void compute_yrange(
const std::vector<V> & func)
119 _y_min = func[0], _y_max = func[0];
120 for(
size_t i=0; i<func.size(); i++) {
121 if(_y_min > func[i]) _y_min = func[i];
122 if(_y_max < func[i]) _y_max = func[i];
124 _y_div = (_y_max - _y_min) / (_rows - 1);
129 void draw_graph(
const std::vector<V> & x,
const std::vector<V> & y,
char c)
134 for(
size_t i=0; i<_cols*dpd; i++) {
136 V xval = _x_min + float(i)/float(dpd)*_x_div;
140 unsigned int yidx = (yval - _y_min) / _y_div;
142 _canvas[_rows-1 - yidx][xidx] = c;
151 for(
size_t i=0; i<_cols+2; i++) printf(
"-");
158 for(
size_t i=0; i<_cols+2; i++) printf(
"-");
161 unsigned int bsize = 8;
162 unsigned int nblocks = _cols / bsize;
164 for(
size_t i=0; i<=nblocks; i++) {
165 if(i%2 == 0) printf(
"%8.1lf", _x_min + i*bsize*_x_div);
192 this->setSize(rows, cols);
196 void setSize(
const unsigned int rows,
const unsigned int cols)
201 _canvas.resize(_rows);
215 assert(_cols > 0 && _rows > 0);
217 std::vector<V> x(func.size());
218 for(
size_t i=0; i<func.size(); i++) x[i] = i;
220 if(_x_div < 0) compute_xrange(x);
221 if(_y_div < 0) compute_yrange(func);
223 draw_graph(x, func, c);
232 void add_graph(
const std::vector<V> & x,
const std::vector<V> & y,
char c)
234 assert(_cols > 0 && _rows > 0);
235 assert(x.size() == y.size());
237 if(_x_div < 0) compute_xrange(x);
238 if(_y_div < 0) compute_yrange(y);
247 for(
size_t i=0; i<_rows; i++) {
248 if( (i == 0) || (_rows-1-i)%4 == 0 )
249 printf(
"%+8.2lf |%s|\n", _y_max - i*_y_div, _canvas[i].data());
251 printf(
" |%s|\n", _canvas[i].data());
260 this->compute_xrange(x);
268 _x_div = (_x_max - _x_min) / (_cols - 1);
275 this->compute_yrange(func);
283 _y_div = (_y_max - _y_min) / (_rows - 1);
299 std::vector<float> _xval, _yval;
305 void stats(
const std::vector<V> & data)
307 _min = data[0], _max = data[0];
310 for(
size_t i=0; i<data.size(); i++)
313 if(_min > c) _min = c;
314 if(_max < c) _max = c;
317 _avrg /= double(data.size());
320 void generate_xy(
const std::vector<V> & data)
324 float delta = float(_max - _min) / float(_num_int);
326 for(
size_t i=0; i<_num_int+1; i++) {
327 _xval[i] = float(_min) + i*delta;
331 for(
size_t i=0; i < data.size(); i++) {
332 size_t idx = size_t((data[i] - _min) / delta);
336 for(
size_t i=0; i<_num_int+1; i++) _yval[i] /= ((
float)data.size());
341 _plotter(rows, cols), _xval(num_int+1), _yval(num_int+1), _num_int(num_int)
344 void plot(
const std::vector<V> & data, std::string msg)
346 this->generate_xy(data);
352 std::cout << msg << std::endl << std::endl;
353 std::cout <<
"Average: " << _avrg <<
", (min: " << _min <<
", max: " << _max <<
")" << std::endl;
354 std::cout << std::endl <<
" == Histogram == " << std::endl << std::endl;
368 std::vector< std::vector<char> > _canvas;
380 void draw_matrix(
const VEC & cnt,
384 long int nrow = cnt.size();
385 long int ncol = *std::max_element(col.begin(), col.end()) + 1;
386 int xdiv = (ncol + _cols - 1) / _cols;
387 int ydiv = (nrow + _rows - 1) / _rows;
389 for(
size_t i=0, k=0; i<cnt.size(); i++)
390 for(
long int j=0; j < cnt[i]; j++, k++) {
392 int xpos = col[k] / xdiv;
393 _canvas[ypos][xpos] = s;
411 this->setSize(rows, cols);
419 for(
unsigned int i=0; i<_rows; i++) {
420 _canvas[i].assign(_cols + 1,
' ');
421 _canvas[i][_cols] =
'\0';
431 void setSize(
const unsigned int rows,
const unsigned int cols)
436 _canvas.resize(_rows);
451 draw_matrix(cnt, col, s);
453 for(
size_t i=0; i<_rows; i++) {
454 printf(
" |%s|\n", _canvas[i].data());
constexpr T min(T a, T b)
piecewiseLinear_function(const std::vector< V > &xval, const std::vector< V > &yval)
constexpr T max(T a, T b)
histogramm_plotter(size_t num_int, size_t rows, size_t cols)
Class that generates a histogram for a data vector and prints it using asciiPlotter.
asciiPlotter()
empty constructor
void set_yrange(const std::vector< V > &func)
set y range according to min/max of the given function
bool operator()(const V x, V &y)
void set_xrange(const std::vector< V > &x)
set x range according to min/max of the given vector
void add_graph(const std::vector< V > &x, const std::vector< V > &y, char c)
void set_xrange(V min, V max)
set x range
void setSize(const unsigned int rows, const unsigned int cols)
Set new size and clear the canvas.
matrixgraph_plotter(const unsigned int rows, const unsigned int cols)
Constructor specifying the canvas size.
void reset()
Clear the canvas.
void setSize(const unsigned int rows, const unsigned int cols)
set canvas size
void print(const VEC &cnt, const VEC &col, char s)
Print a matrix graph to stdout.
Ascii matrix graph plotter.
void add_graph(const std::vector< V > &func, char c)
void set_yrange(V min, V max)
set y range
matrixgraph_plotter()
empty constructor
asciiPlotter(const unsigned int rows, const unsigned int cols)
void print()
print the plot on stdout
void plot(const std::vector< V > &data, std::string msg)