Convert chart coords to pixels

前端 未结 3 1036
花落未央
花落未央 2021-01-22 17:52

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 i

3条回答
  •  醉话见心
    2021-01-22 18:29

    The self-answered solution neither works in a more general case nor does it actually transform DataPoint values to pixel-coordinates.

    Here is a solution to draw the circle that will always work:

    private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
    {
        RectangleF ipp = InnerPlotPositionClientRectangle(chart1, chart1.ChartAreas[0]);
        using (Graphics g = chart1.CreateGraphics())
            g.DrawEllipse(Pens.Red, new RectangleF(ipp.Location, ipp.Size));
    }
    

    It makes use of two helper functions:

    RectangleF ChartAreaClientRectangle(Chart chart, ChartArea CA)
    {
        RectangleF CAR = CA.Position.ToRectangleF();
        float pw = chart.ClientSize.Width / 100f;
        float ph = chart.ClientSize.Height / 100f;
        return new RectangleF(pw * CAR.X, ph * CAR.Y, pw * CAR.Width, ph * CAR.Height);
    }
    
    RectangleF InnerPlotPositionClientRectangle(Chart chart, ChartArea CA)
    {
        RectangleF IPP = CA.InnerPlotPosition.ToRectangleF();
        RectangleF CArp = ChartAreaClientRectangle(chart, CA);
    
        float pw = CArp.Width / 100f;
        float ph = CArp.Height / 100f;
    
        return new RectangleF(CArp.X + pw * IPP.X, CArp.Y + ph * IPP.Y,
                                pw * IPP.Width, ph * IPP.Height);
    }
    

    And here is a link to a general coordinate transformation function for polar charts.

提交回复
热议问题