Bar chart with wxMathPlot

Martin Ma
  • Martin Ma

    Martin Ma - 2009-07-20

    I like this library. Is there any plan to add bar charts in wxMathPlot?

    • cdron77

      cdron77 - 2009-07-21

      Well, not at the moment, mainly because I have little time to work on the library, but it is a good idea, and I'll add it to the wish list.

      In any case, it should not be difficult to implement a new layer to plot bar charts: it is a matter to derive a new class from mpLayer and to override the Plot method to draw rectangles instead of lines.


      • Nigel Paton

        Nigel Paton - 2009-07-23

        Some example code for the Plot function of a bar based layer. Note that this code uses some other data particular to my derived classes so it will not work as is with mathplot you will need to modify it to do this but it gives an idea of what is needed.

            // Set the pen
            dc.SetPen( m_pen);
            // Plot all points
            int numPoints = (int)(points.size() - m_firstValidPoint);
            // Calculate the X coord of the left and right
            // border of the plot window. This will allow
            // us to only plot points that fall in this region
            wxCoord startPx = m_drawOutsideMargins ? 0 : w.GetMarginLeft();
            wxCoord endPx   = m_drawOutsideMargins ? w.GetScrX() : w.GetScrX() - w.GetMarginRight();
            double leftX    = w.p2x(startPx);
            double rightX   = w.p2x(endPx);

            wxCoord ptX, ptY;

            // Create pen for points with colour
            //wxPen diffPen = m_pen;
            wxBrush plotBrush;
            // Caclulate width of bar to draw
            int barWidth = 0.8 * w.GetScaleX();
            if (barWidth == 0)
                barWidth = 1;
            // Get the zero y position
            wxCoord zeroY = w.y2p(0);
            for (int counter = m_firstValidPoint; counter < numPoints; counter++)
                // Check if in range
                if (points[counter].xCoord <= rightX)
                    if (points[counter].xCoord >= leftX)
                        ptX = w.x2p(points[counter].xCoord);
                        ptY = w.y2p(points[counter].yCoord);
                        // Set brush (fill) colour
                        if (points[counter].colour != wxNullColour)
                        // Set brush
                        // Draw line
                        dc.DrawRectangle(ptX - (barWidth/2), ptY, barWidth, zeroY - ptY);
            // Reset brush to normal

  • Jeeva

    Jeeva - 2012-07-25


                          When i am going to click mouse left on a mpwindow it needs to find  a particular layer.Can u given me a Suggestion.

    Thank You,

  • JuPe

    JuPe - 2012-07-25

    Hi, There's already bar -charts implemented to wxMathPlot:
    gpPanel contains several other new feature also which might be useful for you.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks