Reputation: 224
I'm using the HoloGraphLibrary for Android, but I need the percentage of each segment inside the pie chart (in its segment).
Because there isn't any coordinate system, I can't just find the segment and add some text, so I need a clever trick to do it. The only values I have at my disposal are: - The midX and midY coordinate of the pie chart - The inner and outer radius - each segments sweep (or angle our of 360 it covers)
I've managed to get the Y coordinate of the label, but using the same method for the X coordinate isn't yielding the correct result. This is the code I have so far:
canvas.drawText(slice.getTitle(), (float) (midX + (innerRadius + (radius - innerRadius)/2)*-1*Math.cos((currentSweep-padding)/2*(Math.PI/180) )), (float) (midY - (innerRadius + (radius - innerRadius)/2)*Math.cos((currentSweep)/2*(Math.PI/180))), paint);
Can anyone help with centring the label in the segment?
Thanks
Upvotes: 1
Views: 420
Reputation: 1904
Try this. It's working for me.
canvas.drawText(slice.getTitle(),
(float) (midX + (innerRadius + (radius - innerRadius)/2)*Math.sin((currentSweep)*(Math.PI/360) )),
(float) (midY - (innerRadius + (radius - innerRadius)/2)*Math.cos((currentSweep)*(Math.PI/360))),
mPaint);
Upvotes: 0
Reputation: 21
Ok my bad, This is an answer :
// add a text title centered in each slice :
textAngle += currentSweep;
mBlackColorPaint.setTextAlign(Paint.Align.CENTER);
System.out.println("title : " + slice.getTitle() + "Sweep : "+currentSweep + " Angle = "+currentAngle);
canvas.drawText(slice.getTitle(),
(float) (midX + (innerRadius + (radius - innerRadius)/2)*Math.sin((textAngle)*(Math.PI/360) )),
(float) (midY - (innerRadius + (radius - innerRadius)/2)*Math.cos((textAngle)*(Math.PI/360))),
mBlackColorPaint);
textAngle += currentSweep;
put this in the onDraw() method after setting the Region object
Upvotes: 2