Советы по вертикальному курсору, необходимые в MS Chart Control - PullRequest
0 голосов
/ 31 декабря 2011

У меня есть элемент управления линейной диаграммы, показывающий некоторые данные временных рядов.Я хотел бы разместить несколько вертикальных курсоров на линейном графике, показывая важные точки графика.На данный момент в элементе управления есть два курсора: вертикальный и горизонтальный.Мне не нужно, чтобы курсоры были подвижными, я просто хочу указывать конкретные места.Каков предпочтительный способ достижения этого в MS Chart Control?

Ответы [ 2 ]

1 голос
/ 09 июля 2014

Использовать вертикальные линейные аннотации.

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);
0 голосов
/ 26 января 2012

Попробуй так:

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();
    }
}
... 
...