C# 将图表坐标转换为像素
我应该在极坐标图内画一个圆圈,上面有一些文字 我开始玩后期画,得到了图表图形,所以我能够在上面画和写自定义的东西 我的主要问题是位置 例如,我想在x轴和y轴交叉的位置绘制sg,但我没有找到任何有效的方法将图形坐标(在我的示例中为0,0)转换为像素坐标 我该怎么做?如何将图表坐标转换为像素 .net4/winforms/vs2010/c#找到比率了吗? 若跳线为100x100,单元为伪单元,则转换为200x200像素时,只需使用比率2.0 如果坐标为200x100,实际为100x100,则X的比率为2.0,Y的比率为1.0 当我使用SVG时,我必须通过偏移+1000将0,0(左上角)偏移到笛卡尔坐标(中心为0,0) 所以,我解决了这个问题 我处理mschart的后期事件:C# 将图表坐标转换为像素,c#,.net,visual-studio-2010,mschart,C#,.net,Visual Studio 2010,Mschart,我应该在极坐标图内画一个圆圈,上面有一些文字 我开始玩后期画,得到了图表图形,所以我能够在上面画和写自定义的东西 我的主要问题是位置 例如,我想在x轴和y轴交叉的位置绘制sg,但我没有找到任何有效的方法将图形坐标(在我的示例中为0,0)转换为像素坐标 我该怎么做?如何将图表坐标转换为像素 .net4/winforms/vs2010/c#找到比率了吗? 若跳线为100x100,单元为伪单元,则转换为200x200像素时,只需使用比率2.0 如果坐标为200x100,实际为100x100,则X的比率
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);
}
}
自应答解决方案既不适用于更一般的情况,也不实际地将
数据点
值转换为像素坐标
以下是绘制始终有效的圆的解决方案:
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);
}
.Thx。MSChart中真的没有ChartCoordsToPixels函数吗?