JustGreg
JustGreg

Reputation: 241

Vertical cursor tips needed in MS Chart Control

I've got a line chart control showing some time series data. I'd like to place several vertical cursors on the line plot showing important points of the plot. Afaik there are two cursors available in the control, a vertical and a horizontal. I don't need the cursors to be moveable, I just want the to pinpoint particular places. What is the preferred way to achieve this in MS Chart Control?

Upvotes: 0

Views: 1550

Answers (2)

Haris Iltifat
Haris Iltifat

Reputation: 534

Use vertical line annotations.

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);

Upvotes: 1

adi sba
adi sba

Reputation: 621

Try like this:

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

Upvotes: 0

Related Questions