我有一个日期和股票价格的数据库表。我根据文章将它绑定到WinForm Chart控件。 https://msdn.microsoft.com/en-us/library/dd489231(v=vs.110).aspx
我想在本文中标记最高数据点。 https://msdn.microsoft.com/en-us/library/dd456612(v=vs.110).aspx
// Get relative coordinates of the data point
System.Drawing.PointF pos = System.Drawing.PointF.Empty;
pos.X = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.X, i);
pos.Y = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.Y, max);
// Convert relative coordinates to absolute coordinates.
os = cg.GetAbsolutePoint(pos);
由于X轴是日期时间值,我得到了pos.X = -99845。我想我应该使用OADate作为文章。 https://msdn.microsoft.com/en-us/library/dd456614(v=vs.110).aspx但我还没有找到绑定源的方法。
答案 0 :(得分:1)
我发现我应该在该教程示例中使用点的X值而不是其索引。
private void chart_PostPaint(object sender, ChartPaintEventArgs e)
{
if (e.ChartElement is Series && ((Series)e.ChartElement).Name == "Series3")
{
Series s = e.Chart.Series[0];
ChartGraphics cg = e.ChartGraphics;
double max = s.Points.FindMaxByValue().YValues[0];
for (int i = 0; i < s.Points.Count; i++)
{
if (s.Points[i].YValues[0] == max)
{
PointF pos = PointF.Empty;
pos.X = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.X, s.Points[i].XValue);
pos.Y = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.Y, max);
pos = cg.GetAbsolutePoint(pos);
for (int r = 10; r < 40; r+=10)
{
cg.Graphics.DrawEllipse(
Pens.Red,
pos.X - r / 2,
pos.Y - r / 2,
r, r);
}
}
}
}
}