Search code examples
c#.netvisual-studio-2010mschart

Convert chart coords to pixels


I should draw a circle inside a polar chart with some text on it.

I started to play with PostPaint, got chart graphics, so I am able to draw and write custom things on it.

My main problem is the placement.

E.g. I'd like to draw sg where my x and y axes are crossing, but I did not find any effective way to convert graph coordinates (in my example 0,0) to pixel coordinates.

How can I do this? How can I convert chart coords to pixels?

.net4/winforms/vs2010/c#


Solution

  • So, I solved it.

    I handle mschart's postpaint event:

    private void chartMain_PostPaint(object sender, ChartPaintEventArgs e)
        {
            try
            {
                if (chartMain.ChartAreas.FirstOrDefault(a=>a.Name == "Default") == null)
                    return;
    
                Graphics graph = e.ChartGraphics.Graphics;
    
                var day = Date.GetBoundaries(daySelectorMain.DateTimePickerDay.Value);
                var toDate = day.Item2; //it is maxdate value (max value of x axis)
    
                var centerY = (float)chartMain.ChartAreas["Default"].AxisY.ValueToPixelPosition(-80); //-80 is the min value of y (y axis)
                var centerX = (float)chartMain.ChartAreas["Default"].AxisX.ValueToPixelPosition(toDate.ToOADate());
    
                var origoY = (float)chartMain.ChartAreas["Default"].AxisY.ValueToPixelPosition(0);
                var origoX = (float)chartMain.ChartAreas["Default"].AxisX.ValueToPixelPosition(toDate.ToOADate());
    
                var radius = (float)(centerY - origoY) - 1;
    
                graph.DrawLine(System.Drawing.Pens.Blue,
                               new PointF(origoX, origoY),
                               new PointF(centerX, centerY));
    
                graph.FillEllipse(new SolidBrush(Color.White), centerX - radius, centerY - radius, radius * 2, radius * 2);
            }
            catch (System.Exception exc)
            {
                Debug.Assert(false, exc.Message);
            }
        }