How to highlight (not select) a data point in a pie chart?


#1

I think I understand the concepts of selection and the difference between Style and SelectedStyle. Is it possible to “highlight” a data point (pie) - it should be in this state if the point is tapped but the finger is not yet up, to allow for more precision when selecting in a pie chart with many data points. I tried using IShinobiChartOnGestureListener to change the flavor color inside *TouchDown, “resetting” it in *TouchUp, but can’t find a way to convert the PointF passed into anything useful (that would allow me to get to the data point under finger, its series and then style). I am using Xamarin.Android version of the charts library.


#2

Hi cynic,

It is possible to achieve this functionality using, as you suggested, the IShinobiChartOnGestureListener. However, there isn’t anything in our library that will directly get you the pie/donut slice for the given touch position - you have to work this out manually.

It’s not too difficult to do this but is a bit fiddly. Essentially you need to:

  1. Check the touch is inside the pie by comparing the distance of the touch from the centre of the plot area with the radius of the pie
  2. Calculate the angle of the touch and work out which slice it falls in by calculating the angle of each slice

For 1. you can use shinobiChart.getPlotAreaRect(); to get the Rect for the plot area (relative to the ChartView/Fragment). With this you can find the centre point of the Pie (which is the centre of the Rect) and the radius of the pie (using also pie.getOuterRadius()).

For 2. you can get the angle of the touch by using Math.atan2(dy, dx) where dy is position.y - pieCentre.y and dx is position.x - pieCentre.x but will need to transform it accordingly in order to compare against the angles for the slices. The slice angles can be calculated based on your data but will need to take into account draw direction, rotation on the pie etc.

In the iOS version of ShinobiCharts there is a indexOfSliceAtAngle method available on the SChartDonutSeries which would obviously be much more straight forward! As such we are thinking of adding something similar (it may take a position rather than an angle but the principle would be the same). Would this be something you’d find useful? While I can’t make any promises, it may be possible to fit this into the next release (which is currently scheduled for early/mid June).

Just as a side note I had a quick play with the Java library to try this out rather than the Xamarin one but it should have exactly the same functionality.

Kind regards,

Patrick


#3

Thanks, I’ll try this approach. I’d certainly find builtin support useful though.