Changeset 207 for trunk/src


Ignore:
Timestamp:
08/15/2009 08:56:54 PM (3 years ago)
Author:
guyru
Message:

highlight pie plot segments when moving over them

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/simple_pie_plot.cpp

    r206 r207  
    2222#include <boost/foreach.hpp> 
    2323#include <memory> 
    24 #include <iostream> 
    2524#include <cmath> 
    2625using namespace std; 
    2726using namespace simple_pie_plot; 
    2827 
    29 #define PI 3.14159265 
     28const double PI = 4.0 * atan(1.0); 
    3029 
    3130/** 
     
    5958                                : wxPanel(parent, id, pos, size, style, name) 
    6059{ 
     60        m_highlight = -1; 
    6161        Connect(this->GetId(), wxEVT_PAINT, wxPaintEventHandler(SimplePiePlot::OnPaint)); 
    6262        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)); 
    6466} 
    6567 
     
    6870        wxPaintDC pdc(this); 
    6971        auto_ptr<wxGraphicsContext> dc(wxGraphicsContext::Create(pdc)); 
    70          
     72        wxBrush color_brush; 
    7173        int width, height; 
     74 
    7275        GetClientSize(&width, &height); 
    73  
    74         wxBrush color_brush; 
    75         int i = 0; 
    76         double item_x, item_y, item_ratio, item_height; 
    7776 
    7877        // radius should be set so it fits exactly 
    7978        double radius = (width>height? height : width)/2.0; 
    80         double start_angle = 0; 
    81         double end_angle; 
    8279 
    83         BOOST_FOREACH(double d, m_data) { 
     80        for (int i = 1; i<m_angles.size(); i++) { 
    8481                // create brush 
    85                 color_brush.SetColour(GetSegmentColor(i, false)); 
     82                color_brush.SetColour(GetSegmentColor(i-1)); 
    8683                dc->SetBrush(color_brush); 
    8784 
    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()); 
    9687        } 
    9788} 
     
    10596void SimplePiePlot::SetData(vector<double> d) 
    10697{ 
    107         //m_data.assign(d.begin(), d.end()); 
     98        m_data.clear(); 
    10899        m_data_total = 0; 
    109100        BOOST_FOREACH(double tmp, d) { 
     
    112103                        m_data.push_back(tmp); 
    113104        } 
     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        } 
    114113} 
    115114 
    116 wxColour SimplePiePlot::GetSegmentColor(int i, bool highlight) 
     115wxColour SimplePiePlot::GetSegmentColor(int i) 
    117116{ 
    118117        double hue_step = 1.0/(m_data.size()); 
    119118        double hue = i * hue_step; 
    120         double value = highlight ? 1.0 : 0.8; 
     119        double value = m_highlight == i ? 1.0 : 0.8; 
    121120        wxColour color; 
    122121        wxImage::RGBValue rgb; 
     
    127126        return color; 
    128127} 
     128 
     129void 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 
     156void SimplePiePlot::OnMouseLeaveWindow(wxMouseEvent& event) 
     157{ 
     158        ClearHighlight(); 
     159} 
     160 
     161void SimplePiePlot::Highlight(int i) 
     162{ 
     163        m_highlight = i; 
     164        Refresh(); 
     165} 
     166 
     167void SimplePiePlot::ClearHighlight() 
     168{ 
     169        if (m_highlight != -1) { 
     170                m_highlight = -1; 
     171                Refresh(); 
     172        } 
     173} 
  • trunk/src/simple_pie_plot.h

    r206 r207  
    3535        void OnPaint(wxPaintEvent& event); 
    3636        void OnResize(wxSizeEvent& event); 
     37        void OnMouseLeaveWindow(wxMouseEvent& event); 
     38        void OnMouseMove(wxMouseEvent& event); 
    3739private: 
    3840        /** 
    3941         * Gets the color suiting the \a i th segment. 
    4042         * \param i The segment number. 
    41          * \param hightlight Return color suiting for highlighting. 
    4243         */ 
    43         wxColour GetSegmentColor(int i, bool highlight); 
     44        wxColour GetSegmentColor(int i); 
     45 
     46        void Highlight(int i); 
     47        void ClearHighlight(); 
    4448 
    4549        std::vector<double> m_data; 
     50        std::vector<double> m_angles; 
    4651        double m_data_total; 
     52        int m_highlight; 
    4753}; 
    4854 
Note: See TracChangeset for help on using the changeset viewer.