13 #ifndef _ASCII_PLOTTER
14 #define _ASCII_PLOTTER
33 const std::vector<V> & _xval;
34 const std::vector<V> & _yval;
39 assert(_xval.size() == _yval.size());
40 assert(_xval.size() > 1);
45 V xs = _xval[0], xe = _xval[1], ys = _yval[0], ye = _yval[1];
47 if( (x < _xval[0]) || (x > _xval[_xval.size()-1]) )
50 size_t size = _xval.size();
52 while(x > xe && i < size-1) {
53 xs = _xval[i], xe = _xval[i+1];
54 ys = _yval[i], ye = _yval[i+1];
58 V fctr = (x - xs) / (xe - xs);
59 y = ys + (ye - ys)*fctr;
72 std::vector< std::vector<char> > _canvas;
75 double _x_min, _x_max, _x_div;
76 double _y_min, _y_max, _y_div;
81 for(
unsigned int i=0; i<_rows; i++) {
82 _canvas[i].assign(_cols + 1,
' ');
83 _canvas[i][_cols] =
'\0';
95 void compute_xrange(
const std::vector<V> & x)
97 _x_min = x[0], _x_max = x[x.size()-1];
98 _x_div = (_x_max - _x_min) / (_cols - 1);
102 void compute_yrange(
const std::vector<V> & func)
104 _y_min = func[0], _y_max = func[0];
105 for(
size_t i=0; i<func.size(); i++) {
106 if(_y_min > func[i]) _y_min = func[i];
107 if(_y_max < func[i]) _y_max = func[i];
109 _y_div = (_y_max - _y_min) / (_rows - 1);
114 void draw_graph(
const std::vector<V> & x,
const std::vector<V> & y,
char c)
119 for(
size_t i=0; i<_cols*dpd; i++) {
121 V xval = _x_min + float(i)/float(dpd)*_x_div;
125 unsigned int yidx = (yval - _y_min) / _y_div;
127 _canvas[_rows-1 - yidx][xidx] = c;
136 for(
size_t i=0; i<_cols+2; i++) printf(
"-");
143 for(
size_t i=0; i<_cols+2; i++) printf(
"-");
146 unsigned int bsize = 8;
147 unsigned int nblocks = _cols / bsize;
149 for(
size_t i=0; i<=nblocks; i++) {
150 if(i%2 == 0) printf(
"%8.1lf", _x_min + i*bsize*_x_div);
181 void setSize(
const unsigned int rows,
const unsigned int cols)
186 _canvas.resize(_rows);
200 assert(_cols > 0 && _rows > 0);
202 std::vector<V> x(func.size());
203 for(
size_t i=0; i<func.size(); i++) x[i] = i;
205 if(_x_div < 0) compute_xrange(x);
206 if(_y_div < 0) compute_yrange(func);
208 draw_graph(x, func, c);
217 void add_graph(
const std::vector<V> & x,
const std::vector<V> & y,
char c)
219 assert(_cols > 0 && _rows > 0);
220 assert(x.size() == y.size());
222 if(_x_div < 0) compute_xrange(x);
223 if(_y_div < 0) compute_yrange(y);
232 for(
size_t i=0; i<_rows; i++) {
233 if( (i == 0) || (_rows-1-i)%4 == 0 )
234 printf(
"%+8.2lf |%s|\n", _y_max - i*_y_div, _canvas[i].data());
236 printf(
" |%s|\n", _canvas[i].data());
245 this->compute_xrange(x);
253 _x_div = (_x_max - _x_min) / (_cols - 1);
260 this->compute_yrange(func);
268 _y_div = (_y_max - _y_min) / (_rows - 1);
284 std::vector<float> _xval, _yval;
290 void stats(
const std::vector<V> & data)
292 _min = data[0], _max = data[0];
295 for(
size_t i=0; i<data.size(); i++)
298 if(_min > c) _min = c;
299 if(_max < c) _max = c;
302 _avrg /= double(data.size());
305 void generate_xy(
const std::vector<V> & data)
309 float delta = float(_max - _min) / float(_num_int);
311 for(
size_t i=0; i<_num_int+1; i++) {
312 _xval[i] = float(_min) + i*delta;
316 for(
size_t i=0; i < data.size(); i++) {
317 size_t idx = size_t((data[i] - _min) / delta);
321 for(
size_t i=0; i<_num_int+1; i++) _yval[i] /= ((
float)data.size());
326 _plotter(rows, cols), _xval(num_int+1), _yval(num_int+1), _num_int(num_int)
329 void plot(
const std::vector<V> & data, std::string msg)
331 this->generate_xy(data);
337 std::cout << msg << std::endl << std::endl;
338 std::cout <<
"Average: " << _avrg <<
", (min: " << _min <<
", max: " << _max <<
")" << std::endl;
339 std::cout << std::endl <<
" == Histogram == " << std::endl << std::endl;
353 std::vector< std::vector<char> > _canvas;
365 void draw_matrix(
const VEC & cnt,
369 long int nrow = cnt.size();
370 long int ncol = *std::max_element(col.begin(), col.end()) + 1;
371 int xdiv = (ncol + _cols - 1) / _cols;
372 int ydiv = (nrow + _rows - 1) / _rows;
374 for(
size_t i=0, k=0; i<cnt.size(); i++)
375 for(
long int j=0; j < cnt[i]; j++, k++) {
377 int xpos = col[k] / xdiv;
378 _canvas[ypos][xpos] = s;
404 for(
unsigned int i=0; i<_rows; i++) {
405 _canvas[i].assign(_cols + 1,
' ');
406 _canvas[i][_cols] =
'\0';
416 void setSize(
const unsigned int rows,
const unsigned int cols)
421 _canvas.resize(_rows);
436 draw_matrix(cnt, col, s);
438 for(
size_t i=0; i<_rows; i++) {
439 printf(
" |%s|\n", _canvas[i].data());
void add_graph(const std::vector< V > &func, char c)
asciiPlotter(const unsigned int rows, const unsigned int cols)
void set_xrange(V min, V max)
set x range
void set_yrange(const std::vector< V > &func)
set y range according to min/max of the given function
void set_yrange(V min, V max)
set y range
void setSize(const unsigned int rows, const unsigned int cols)
set canvas size
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 print()
print the plot on stdout
asciiPlotter()
empty constructor
Class that generates a histogram for a data vector and prints it using asciiPlotter.
void plot(const std::vector< V > &data, std::string msg)
histogramm_plotter(size_t num_int, size_t rows, size_t cols)
Ascii matrix graph plotter.
void reset()
Clear the canvas.
matrixgraph_plotter()
empty constructor
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 print(const VEC &cnt, const VEC &col, char s)
Print a matrix graph to stdout.
piecewiseLinear_function(const std::vector< V > &xval, const std::vector< V > &yval)
bool operator()(const V x, V &y)
constexpr T min(T a, T b)
constexpr T max(T a, T b)