ascii_chart.h
Go to the documentation of this file.00001 #ifndef MATH_ASCII_CHART_H
00002 #define MATH_ASCII_CHART_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00034
00035 #include <utility>
00036 #include <vector>
00037 #include <iostream>
00038 #include <string>
00039
00040 namespace cxxtls
00041 {
00042
00043 struct ascii_chart
00066 {
00067 typedef double X_t;
00068 typedef double Y_t;
00069
00070 typedef std::pair<X_t,Y_t> point_t;
00071 typedef std::vector<point_t> data_t;
00072
00073
00074 int width_;
00075 int height_;
00076
00077 bool dirty_;
00078
00079 char **data_;
00080
00081
00082
00083
00084
00085 char blank_;
00086 char star_;
00087
00088 std::pair<X_t,X_t> xrange_;
00089 std::pair<Y_t,Y_t> yrange_;
00090
00091 std::string title_;
00092
00093 ascii_chart(int width, int height, char blank=' ', char star='*');
00094 ~ascii_chart();
00095
00096 void clear();
00097
00098
00099 void plot(data_t const &data,
00100 int xola=0
00101 );
00115
00116 template<class X, class Y>
00117 void plot(std::vector< std::pair<X,Y> > const &data)
00122 {
00123
00124
00125
00126
00127
00128 data_t tmp(data.size());
00129
00130 typedef std::vector< std::pair<X,Y> > input_t;
00131
00132 data_t::iterator cur = tmp.begin();
00133
00134 for(typename input_t::const_iterator i = data.begin(); i != data.end(); ++i, ++cur)
00135 {
00136 cur->first = i->first;
00137 cur->second = i->second;
00138 }
00139
00140 plot(tmp,1);
00141
00142 }
00143
00144 template<class Container>
00145 void plot(Container const &c)
00146
00147
00148
00149 {
00150 data_t tmp(c.size());
00151
00152 typename Container::const_iterator first = c.begin(),
00153 last = c.end();
00154
00155 data_t::iterator output = tmp.begin();
00156
00157 while(first != last)
00158 {
00159 output->first = first->first;
00160 output->second= first->second;
00161
00162 ++first;
00163 ++output;
00164 }
00165
00166 plot(tmp);
00167
00168 }
00169
00170
00171 void print_helper(std::ostream& stream) const;
00172
00173 friend std::ostream& operator<< (std::ostream&s, ascii_chart const &a)
00174 {
00175 a.print_helper(s);
00176 return s;
00177 }
00178
00179
00180 private:
00181 ascii_chart& operator=(ascii_chart const &);
00182 ascii_chart(ascii_chart const &);
00183
00184 void range(data_t const &data);
00185 };
00186
00187
00188
00189 }
00190 #endif