Get Series value from a mouse click

1.2k Views Asked by At

I use Microsoft.DataVisualization.Charting and want to get the value of the point when i click on it.

My problem: i want exactly that value i clicked, even if its only a value calculated by the Chart and between 2 points.

Example: 3 points: P(0;3), P(1;6), P(3;12)

When i click at x-Value 2 i want to get 9 as result if the line is linear.

Currently i do that:

            HitTestResult[] hits = chart.HitTest(e.X, e.Y, false, ChartElementType.PlottingArea);

            //DataInformation save the DateTime and Value for later use
            DataInformation[] dinfo = new DataInformation[hits.Length];

            foreach (ChartArea area in chart.ChartAreas)
            {
                area.CursorX.LineWidth = 0; //clear old lines
            }

            for (int i = 0; i < hits.Length; i++) //for all hits
            {
                if (hits[i].ChartElementType == ChartElementType.PlottingArea)
                {
                    //val saves the x-value clicked in the ChartArea
                    double val = hits[i].ChartArea.AxisX.PixelPositionToValue(e.X);
                    DataPoint pt = chart.Series[hits[i].ChartArea.Name].Points.Last(elem => elem.XValue < val);

                    dinfo[i].caption = hits[i].ChartArea.Name;
                    dinfo[i].value = pt.YValues[0].ToString();

                    //hits[i].ChartArea.CursorX.Position = pt.XValue;
                }
            }

This show the right values for every existing data point but not that clicked point.

How can i get the exact value?

1

There are 1 best solutions below

0
On BEST ANSWER

It seems, there is no way to get the exact value. I changed to OxyPlot. OxyPlot can show the data much faster and you can get the exact value for any point.