MarcoMauricio
MarcoMauricio

Reputation: 3

MPAndroidChart PieChart Angles

I needed a pie chart with three entries in the data set so that when i touch the chart it rotates to the correct data( This means it starts at data[0] -> click -> spin to data[1] -> click -> spin to data[2] -> click -> spin to data[0]).

The solution I've implemented goes something like this :

START_OFFSET = 95;
_index0= absoluteAngles[0] - (drawAngles[0] / 2) - START_OFFSET ;
_index1= absoluteAngles[1] - (drawAngles[1] / 2) - START_OFFSET ;
_pendingPieOffset = absoluteAngles[2] - (drawAngles[2] / 2) - START_OFFSET;

And here are where the problems start. This supposedly gives me the correct angle as in absoluteAngle represents the end of the slice and drawAngles represents the width of the slice. So even though my math is correct I can't center the center of each slice with the bottom.

      _overlay.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
    ...
case AVAILABLE_BALANCE:
    _pieChart.spin(SPIN_DURATION, _index2, _index0, Easing.EasingOption.EaseInCubic);
    break;
case USED_BALANCE:
   _pieChart.spin(SPIN_DURATION, _index0, _index1, Easing.EasingOption.EaseInCubic);
   break;
case PENDING_BALANCE:
   _pieChart.spin(SPIN_DURATION, _index1, _index0, Easing.EasingOption.EaseInCubic);
    break;
    ....
    }

Upvotes: 0

Views: 1816

Answers (1)

V.Moroz
V.Moroz

Reputation: 56

I did some similar functionality in my project, maybe it will help you:

@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
    //get current angle
    float start = chart.getRotationAngle();
    //get index of current slice
    int i = e.getXIndex();
    //calculate center of slice 
    float offset = mDrawAngles[i] / 2;
    // calculate the next angle
    float end = 270f-(mAbsoluteAngles[i]-offset);

    //rotate to slice center
    chart.spin(SPIN_DURATION,start,end,Easing.EasingOption.EaseInOutQuad);
}

Just implement OnChartValueSelectedListener interface in activity or fragment - MPAndroidChart Wiki.

P.S. In my case, selected slice is centered at the top.(Change 270 to 90 for bottom)

Upvotes: 4

Related Questions