MS Chart 折线图点击数据点时显示其值

对于MS Chart当鼠标移动到数据点时或鼠标点击数据点时显示其点的值(X,Y)值

 public partial class Form1 : Form
    {

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

        public Form1()
        {
            InitializeComponent();
        }


        //鼠标移动到数据点时显示点的数值
        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("次数:{0},数值:{1}", dp.XValue.ToString(), dp.YValues[0]);
            }

        }

        //鼠标点击数据点时得到数据点的值
        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();
            }

        }
    }

 

猜你喜欢

转载自www.cnblogs.com/YuanDong1314/p/8967672.html