1 #include <string> 2 #include <list> 3 #include <vector> 4 5 class Color { 6 private: 7 int M_color; // GNUplot color index. 8 int M_column; // The corresponding column. 9 std::string M_label; // The corresponding label. 10 11 protected: 12 friend class Graph; Color(int color,int column,std::string const & label)13 Color(int color, int column, std::string const& label) : M_color(color), M_column(column), M_label(label) { } 14 15 public: get_color(void)16 int get_color(void) const { return M_color; } get_column(void)17 int get_column(void) const { return M_column; } get_label(void)18 std::string const& get_label(void) const { return M_label; } 19 swap_column_with(Color & color)20 void swap_column_with(Color& color) { std::swap(M_column, color.M_column); } 21 22 friend bool operator==(Color const& color1, Color const& color2) { return color1.M_column == color2.M_column; } 23 }; 24 25 typedef std::list<Color> color_list_type; 26 27 class DataPoint { 28 private: 29 int M_x; // x coordinate of data point. 30 int M_y; // y coordinate of data point. 31 color_list_type::const_iterator M_color; // The color & label to use for this data point. 32 33 public: DataPoint(int x,int y,color_list_type::const_iterator const & color)34 DataPoint(int x, int y, color_list_type::const_iterator const& color) : M_x(x), M_y(y), M_color(color) { } 35 get_x(void)36 int get_x(void) const { return M_x; } get_y(void)37 int get_y(void) const { return M_y; } get_color(void)38 Color const& get_color(void) const { return *M_color; } 39 40 friend bool operator<(DataPoint const& dp1, DataPoint const& dp2) 41 { 42 return dp1.M_x < dp2.M_x || (dp1.M_x == dp2.M_x && dp1.M_color->get_column() < dp2.M_color->get_column()); 43 } 44 }; 45 46 class Graph { 47 private: 48 std::string M_basename; 49 std::string M_title; 50 int M_color_hint; 51 int M_next_column; 52 std::vector<DataPoint> M_data; 53 color_list_type M_colors; 54 55 public: Graph(std::string const & basename,std::string const & title)56 Graph(std::string const& basename, std::string const& title) : 57 M_basename(basename), M_title(title), M_color_hint(0), M_next_column(0) { } 58 get_title(void)59 std::string const& get_title(void) const { return M_title; } get_data(void)60 std::vector<DataPoint> const& get_data(void) const { return M_data; } get_data(void)61 std::vector<DataPoint>& get_data(void) { return M_data; } get_colors(void)62 color_list_type const& get_colors(void) const { return M_colors; } 63 64 bool add_point(int x, int y, int column); 65 int add_point(int x, int y, std::string const& label, int color = -2); 66 void swap_columns(int col1, int col2); 67 68 void plot(void); 69 70 private: 71 int M_new_color(void); 72 }; 73