0
我有一個顯示某些時間序列數據的折線圖控件。我想在線圖上放置幾個垂直光標,顯示該圖的重要點。 Afaik在控件中有兩個可用的遊標,一個是垂直的和一個是水平的。我不需要遊標是可移動的,我只是想要指出特定的地方。在MS圖表控件中實現這一點的首選方法是什麼?MS Chart Control中需要垂直光標提示
我有一個顯示某些時間序列數據的折線圖控件。我想在線圖上放置幾個垂直光標,顯示該圖的重要點。 Afaik在控件中有兩個可用的遊標,一個是垂直的和一個是水平的。我不需要遊標是可移動的,我只是想要指出特定的地方。在MS圖表控件中實現這一點的首選方法是什麼?MS Chart Control中需要垂直光標提示
嘗試這樣的:
using System.Windows.Forms.DataVisualization.Charting;
...
this.chart1.CursorPositionChanging += new System.Windows.Forms.DataVisualization.Charting.Chart.CursorEventHandler(this.chart1_CursorPositionChanging);
...
// Edit Controls
private System.Windows.Forms.TextBox CursorX;
private System.Windows.Forms.TextBox CursorY;
// Cursor Position Changing Event
private void chart1_CursorPositionChanging(object sender, System.Windows.Forms.DataVisualization.Charting.CursorEventArgs e)
{
SetPosition(e.Axis, e.NewPosition);
}
// Set Cursor Position to Edit control.
private void SetPosition(Axis axis, double position)
{
if(double.IsNaN(position))
return;
if(axis.AxisName == AxisName.X)
{
// Convert Double to DateTime.
DateTime dateTimeX = DateTime.FromOADate(position);
// Set X cursor position to edit Control
CursorX.Text = dateTimeX.ToLongDateString();
}
else
{
// Set Y cursor position to edit Control
CursorY.Text = position.ToString();
}
}
...
使用垂直線註解。
double xPosition1 = 5;
chart.VerticalLineAnnotation annotationVerticalLine1 = new chart.VerticalLineAnnotation();
annotationVerticalLine1.AxisX = chart1.ChartAreas["ChartArea1"].AxisX;
annotationVerticalLine1.AxisY = chart1.ChartAreas["ChartArea1"].AxisY;
annotationVerticalLine1.IsSizeAlwaysRelative = false;
annotationVerticalLine1.AnchorX = xPosition1;
annotationVerticalLine1.IsInfinitive = true;
annotationVerticalLine1.ClipToChartArea = chart1.ChartAreas["ChartArea1"].Name;
annotationVerticalLine1.LineColor = Color.Red;
annotationVerticalLine1.LineWidth = 1;
chart1.Annotations.Add(annotationVerticalLine1);
double xPosition2 = 10;
chart.VerticalLineAnnotation annotationVerticalLine2 = new chart.VerticalLineAnnotation();
annotationVerticalLine2.AxisX = chart1.ChartAreas["ChartArea1"].AxisX;
annotationVerticalLine2.AxisY = chart1.ChartAreas["ChartArea1"].AxisY;
annotationVerticalLine2.IsSizeAlwaysRelative = false;
annotationVerticalLine2.AnchorX = xPosition2;
annotationVerticalLine2.IsInfinitive = true;
annotationVerticalLine2.ClipToChartArea = chart1.ChartAreas["ChartArea1"].Name;
annotationVerticalLine2.LineColor = Color.Red;
annotationVerticalLine2.LineWidth = 1;
chart1.Annotations.Add(annotationVerticalLine2);