Search code examples
c#graphicsmschart

MS Chart axes are drawn trough data points


I'm working with MS Charts, and somehow I can't figure out a way to draw the points on top of the y-axis. As you can see in the image below, the points at x = 0 (label 1998) are below the Y-axis. Does anyone recognize this problem? Has it something to do with order of prepaint and postpaint events?

axis trough points

EDIT: Test with custom drawn dots, only until the y-axis.. enter image description here


Solution

  • In a Chart all DataPoints go over the GridLines but under the Axes and the TickMarks.

    To paint them over the Axes as well you need to owner draw them in one of the Paint events.

    This is simple if your ChartType is of type Points, Spline or Line and your MarkerType of Circle or Rectangle.

    For most other types like Column or Bar etc this is a lot harder.

    Here is a simple example for Points; pick a size you like better..!

    private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
    {
        if (chart1.Series.Count == 0) return;
    
        Axis AX = chart1.ChartAreas[0].AxisX;
        Axis AY = chart1.ChartAreas[0].AxisY;
        chart1.ApplyPaletteColors();
        foreach (Series s in chart1.Series)
            foreach (DataPoint dp in s.Points)
            {
               float x = (float )AX.ValueToPixelPosition(dp.XValue);
               float y = (float )AY.ValueToPixelPosition(dp.YValues[0]);
               float w2 = 3.6f;
               using (SolidBrush brush = new SolidBrush(Color.YellowGreen))
                      e.ChartGraphics.Graphics.FillEllipse(brush, 
                                                           x - w2, y - w2, w2 * 2f, w2 * 2f);
            }
    }
    

    enter image description hereenter image description here

    I have suppressed the custom drawing for some points.