- Timestamp:
- 08/15/2009 08:56:54 PM (3 years ago)
- Location:
- trunk/src
- Files:
-
- 2 edited
-
simple_pie_plot.cpp (modified) (6 diffs)
-
simple_pie_plot.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/simple_pie_plot.cpp
r206 r207 22 22 #include <boost/foreach.hpp> 23 23 #include <memory> 24 #include <iostream>25 24 #include <cmath> 26 25 using namespace std; 27 26 using namespace simple_pie_plot; 28 27 29 #define PI 3.14159265 28 const double PI = 4.0 * atan(1.0); 30 29 31 30 /** … … 59 58 : wxPanel(parent, id, pos, size, style, name) 60 59 { 60 m_highlight = -1; 61 61 Connect(this->GetId(), wxEVT_PAINT, wxPaintEventHandler(SimplePiePlot::OnPaint)); 62 62 Connect(this->GetId(), wxEVT_SIZE, wxSizeEventHandler(SimplePiePlot::OnResize)); 63 63 64 Connect(this->GetId(), wxEVT_MOTION, wxMouseEventHandler(SimplePiePlot::OnMouseMove)); 65 Connect(this->GetId(), wxEVT_LEAVE_WINDOW, wxMouseEventHandler(SimplePiePlot::OnMouseLeaveWindow)); 64 66 } 65 67 … … 68 70 wxPaintDC pdc(this); 69 71 auto_ptr<wxGraphicsContext> dc(wxGraphicsContext::Create(pdc)); 70 72 wxBrush color_brush; 71 73 int width, height; 74 72 75 GetClientSize(&width, &height); 73 74 wxBrush color_brush;75 int i = 0;76 double item_x, item_y, item_ratio, item_height;77 76 78 77 // radius should be set so it fits exactly 79 78 double radius = (width>height? height : width)/2.0; 80 double start_angle = 0;81 double end_angle;82 79 83 BOOST_FOREACH(double d, m_data) {80 for (int i = 1; i<m_angles.size(); i++) { 84 81 // create brush 85 color_brush.SetColour(GetSegmentColor(i , false));82 color_brush.SetColour(GetSegmentColor(i-1)); 86 83 dc->SetBrush(color_brush); 87 84 88 item_ratio = m_data_total ? d/m_data_total : 0; 89 end_angle = start_angle + 2*PI* (item_ratio); 90 91 DrawPieSlice(width/2.0, height/2.0, radius, start_angle, 92 end_angle, dc.get()); 93 94 start_angle = end_angle; 95 i++; 85 DrawPieSlice(width/2.0, height/2.0, radius, m_angles[i-1], 86 m_angles[i], dc.get()); 96 87 } 97 88 } … … 105 96 void SimplePiePlot::SetData(vector<double> d) 106 97 { 107 //m_data.assign(d.begin(), d.end());98 m_data.clear(); 108 99 m_data_total = 0; 109 100 BOOST_FOREACH(double tmp, d) { … … 112 103 m_data.push_back(tmp); 113 104 } 105 106 m_angles.clear(); 107 double new_angle = 0; 108 m_angles.push_back(new_angle); 109 BOOST_FOREACH(double tmp, m_data) { 110 new_angle = new_angle + 2 * PI * (tmp/m_data_total); 111 m_angles.push_back(new_angle); 112 } 114 113 } 115 114 116 wxColour SimplePiePlot::GetSegmentColor(int i , bool highlight)115 wxColour SimplePiePlot::GetSegmentColor(int i) 117 116 { 118 117 double hue_step = 1.0/(m_data.size()); 119 118 double hue = i * hue_step; 120 double value = highlight? 1.0 : 0.8;119 double value = m_highlight == i ? 1.0 : 0.8; 121 120 wxColour color; 122 121 wxImage::RGBValue rgb; … … 127 126 return color; 128 127 } 128 129 void SimplePiePlot::OnMouseMove(wxMouseEvent& event) 130 { 131 //check if the move is even in the plot 132 int width, height; 133 GetClientSize(&width, &height); 134 // radius should be set so it fits exactly 135 const double radius = (width>height? height : width)/2.0; 136 137 const double dist_mouse = (width/2.0-event.m_x)*(width/2.0-event.m_x) + 138 (height/2.0-event.m_y)*(height/2.0-event.m_y); 139 if (dist_mouse > (radius*radius)) { 140 ClearHighlight(); 141 return; 142 } 143 144 //angles are clockwise from the x-axis 145 double angle = acos((event.m_x-width/2.0)/sqrt(dist_mouse)); 146 if (event.m_y < height/2.0) 147 angle = 2*PI - angle; 148 int i; 149 for (i = 1; i<m_angles.size(); i++) { 150 if (angle<=m_angles[i]) 151 break; 152 } 153 Highlight(i-1); 154 } 155 156 void SimplePiePlot::OnMouseLeaveWindow(wxMouseEvent& event) 157 { 158 ClearHighlight(); 159 } 160 161 void SimplePiePlot::Highlight(int i) 162 { 163 m_highlight = i; 164 Refresh(); 165 } 166 167 void SimplePiePlot::ClearHighlight() 168 { 169 if (m_highlight != -1) { 170 m_highlight = -1; 171 Refresh(); 172 } 173 } -
trunk/src/simple_pie_plot.h
r206 r207 35 35 void OnPaint(wxPaintEvent& event); 36 36 void OnResize(wxSizeEvent& event); 37 void OnMouseLeaveWindow(wxMouseEvent& event); 38 void OnMouseMove(wxMouseEvent& event); 37 39 private: 38 40 /** 39 41 * Gets the color suiting the \a i th segment. 40 42 * \param i The segment number. 41 * \param hightlight Return color suiting for highlighting.42 43 */ 43 wxColour GetSegmentColor(int i, bool highlight); 44 wxColour GetSegmentColor(int i); 45 46 void Highlight(int i); 47 void ClearHighlight(); 44 48 45 49 std::vector<double> m_data; 50 std::vector<double> m_angles; 46 51 double m_data_total; 52 int m_highlight; 47 53 }; 48 54
Note: See TracChangeset
for help on using the changeset viewer.
