Getting x-coordinate on Chart from mouse click

bheklilr picture bheklilr · Mar 22, 2012 · Viewed 11.8k times · Source

I have a chart on a Windows Form with several line graphs. I would like to be able to get the x-coordinate from a mouse click on the chart in such a way that the user would be able to place vertical annotations on the chart at the location of the click. I looked at the Chart.HitTest function, but that will only do what I need if the user clicks on a data point, not anywhere in the chart area. Is there a way to do this easily?

Answers in C# or VB welcome.

Thanks!

Answer

bheklilr picture bheklilr · Mar 23, 2012

As @Quantbuff pointed out, this has been answered in this post.

Essentially, you must handle the click event (e) on the chart and call Chart.ChartAreas[0].AxisX.PixelPositionToValue(e.X) and Chart.ChartAreas[0].AxisY.PixelPositionToValue(e.Y) to get the values.