MS Chart line chart shows the value of a data point when clicked

For MS Chart, when the mouse moves to the data point or when the mouse clicks on the data point, it displays the value of its point (X, Y) value

 public partial class Form1 : Form
    {

        System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs toolTipEventArgs;

        public Form1()
        {
            InitializeComponent();
        }


        // The value of the display point when the mouse moves to the data point 
        private  void chart1_GetToolTipText( object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
        {
            toolTipEventArgs = e;

            if (e.HitTestResult.ChartElementType == System.Windows.Forms.DataVisualization.Charting.ChartElementType.DataPoint)
            {
                int i = e.HitTestResult.PointIndex;
                System.Windows.Forms.DataVisualization.Charting.DataPoint dp = e.HitTestResult.Series.Points[i];
                e.Text = string .Format( " Number:{0},Value:{1} " , dp.XValue.ToString(), dp.YValues[ 0 ]);
            }

        }

        // Get the value of the data point when the mouse clicks on the data point 
        private  void chart1_Click( object sender, EventArgs e)
        {
            if (toolTipEventArgs.HitTestResult.PointIndex < 0)
            {
                return;
            }
            if (toolTipEventArgs.HitTestResult.ChartElementType == System.Windows.Forms.DataVisualization.Charting.ChartElementType.DataPoint)
            {
                int i = toolTipEventArgs.HitTestResult.PointIndex;
                System.Windows.Forms.DataVisualization.Charting.DataPoint dp = toolTipEventArgs.HitTestResult.Series.Points[i];
                textBox1.Text = dp.XValue.ToString();
                textBox2.Text = dp.YValues[0].ToString();
            }

        }
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325077391&siteId=291194637