This question already has an answer here:
I'm working whit charts on c#.
I would like to know a way of get the iten (the spline) clicked on the chart to do something like change color or hide.
This question already has an answer here:
I'm working whit charts on c#.
I would like to know a way of get the iten (the spline) clicked on the chart to do something like change color or hide.
You can check the clicked item using
HitTest
, as suggested..:To help the user you can indicate a possible hit:
Do your users a favor by making the spline lines a little larger:
Of course the chart you show makes a controlled clicking impossible without zooming in first..
Now if all you want is hiding a
Series
or changing itsColor
theHitTestResult
is good enough:or
But to access the clicked values, it may not be..:
Note that especially a
Spline
or aLine
graph, need only very fewDataPoints
to create their lines; so the result fromHitTest
simply is not enough; it will tell you which series was clicked and also the closestDataPoint
but to get at the correct values you still may need to convert the pixels to data values.So instead of the
HitTest
result you may prefer to use the handy functionPixelPositionToValue
:This will get all the values between the actual
DataPoints
. Of course you can do the same for the Y-Values:Note that you can only use these function when the chart is not busy with determining the layout; officially this is only during any of the three
Paint
events. But I found that in practice a mouse event is also fine. You will get a null value when the chart is not ready..