将图表坐标转换为像素

时间:2011-08-12 13:11:05

标签: c# .net visual-studio-2010 mschart

我应该在极坐标图表中绘制一个圆圈,上面有一些文字。

我开始使用PostPaint,获得图表图形,因此我可以在其上绘制和编写自定义内容。

我的主要问题是展示位置。

E.g。我想绘制我的x和y轴交叉的sg,但我没有找到任何有效的方法将图形坐标(在我的示例0,0中)转换为像素坐标。

我该怎么做?如何将图表坐标转换为像素?

.NET4 /的WinForms / VS2010 / C#

3 个答案:

答案 0 :(得分:3)

所以,我解决了。

我处理mschart的postpaint事件:

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);
        }
    }

答案 1 :(得分:2)

自我回答的解决方案既不适用于更一般的情况,也不会将DataPoint 转换为像素坐标

以下是绘制始终有效的圆的解决方案:

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));
}

它使用了两个辅助函数:

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

答案 2 :(得分:0)

找出比率? 如果cord是100x100,其中单位是伪的,那么当转换为200x200像素时,你只需要使用比率2.0 如果coord是200x100并且物理上100x100 X具有比率2.0并且Y具有比率1.0 当我做SVG时,我必须将0,0(左上角)偏移到笛卡尔坐标(中心位置为0,0) http://en.wikipedia.org/wiki/Cartesian_coordinate_system

相关问题