MS Chart Control 中需要垂直光标提示

发布于 2024-12-23 08:41:01 字数 140 浏览 5 评论 0原文

我有一个显示一些时间序列数据的折线图控件。我想在线图上放置几个垂直光标,显示该图的重要点。据我所知,控件中有两个可用的光标,一个是垂直的,一个是水平的。我不需要光标可移动,我只想精确定位特定位置。在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-12-30 08:41:01

使用垂直线注释。

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

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);
一抹微笑 2024-12-30 08:41:01

尝试这样:

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

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();
    }
}
... 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文