我应该在极坐标图中画一个圆圈,上面有一些文字。
我开始使用 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/