Search code examples
c#visual-studio-2010mschart

Getting the x value of a point in a mschart


I have a polar chart like this enter image description here and when I click on a point in the chart, I need to display x value of that point. I have tried ;

  private void chart1_MouseClick(object sender, MouseEventArgs e)
    {
        double x=0; double y=0;
        var pos = e.Location;
        Point? clickPos = pos;

        var results = chart1.HitTest(pos.X, pos.Y, false, ChartElementType.PlottingArea);
            foreach(var result in results) {

                if(result.ChartElementType==ChartElementType.PlottingArea) {
                    x = result.ChartArea.AxisX.PixelPositionToValue(pos.X); ;
                    y=result.ChartArea.AxisY.PixelPositionToValue(pos.Y);

                }

                textBox1.Text=x.ToString();
                textBox2.Text=y.ToString();
            }


    }

But this code gives me weird coordinates.For example, when I clicked the (0,0) point in the chart, Azimuth is 179, Gain is 5,00123. Anyone help?


Solution

  • This code may helps:

    private void chart_MouseDown(object sender, MouseEventArgs e)
    {
        HitTestResult result = chart.HitTest(e.X, e.Y);
        if (result.ChartElementType == ChartElementType.DataPoint)
        {
            var selectedValue = chart.Series[0].Points[result.PointIndex].YValues[0];
            MessageBox.Show(selectedValue.ToString());
        }
    
    }