c# - 将图表坐标转换为像素

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

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

我开始使用 PostPaint,获得了图表图形,因此我能够在上面绘制和写入自定义内容。

我的主要问题是位置。

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

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

.net4/winforms/vs2010/c#

最佳答案

所以,我解决了。

我处理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);
        }
    }

关于c# - 将图表坐标转换为像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7040569/

相关文章:

c# - 将大文本拆分为较小块的最快方法

SQL Server唯一约束问题

c# - 模拟跨上下文连接--LINQ/C#

c# - Unity和Hololens : Reading non-text file exception

c# - WP7 中带有图像的消息框

.Net 将 NULL 值从变量值插入到 SQL Server 数据库中

visual-studio - 用于在 Visual C++ 中配置构建的自定义宏

c# - 模棱两可的引用

c# - Azure IoT 中心对其他设备的未经授权访问

c# - 确定 PayPal IPN 使用哪种编码?