|
|
@@ -18,6 +18,7 @@ import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
import static android.R.attr.ellipsize;
|
|
|
+import static android.R.attr.value;
|
|
|
|
|
|
/**
|
|
|
* Created by Bero on 7.7.2017..
|
|
|
@@ -36,6 +37,8 @@ public class PieChartView extends View {
|
|
|
int descriptionWidth;
|
|
|
|
|
|
String valueDescription;
|
|
|
+ int valueDescriptionWidth;
|
|
|
+ int valueDescriptionHeight;
|
|
|
|
|
|
float getAngle() {
|
|
|
return value * 3.6f;
|
|
|
@@ -245,6 +248,9 @@ public class PieChartView extends View {
|
|
|
|
|
|
startAngle = 90.f;
|
|
|
for (PieChartItem item : items) {
|
|
|
+ if (item.valueDescription == null)
|
|
|
+ continue;
|
|
|
+
|
|
|
float angleMiddle = startAngle - item.getAngle() / 2;
|
|
|
float r = pieChartRect.width() / 2;
|
|
|
float x = (float) Math.cos(Math.toRadians(angleMiddle)) * r;
|
|
|
@@ -253,7 +259,17 @@ public class PieChartView extends View {
|
|
|
float left = x + pieChartRect.centerX();
|
|
|
float top = pieChartRect.centerY() - y;
|
|
|
|
|
|
- // RectF textRect = new RectF(left, top, left + textWidth, top + valuePaint.getTextSize());
|
|
|
+ // Adjust text position based on quadrant.
|
|
|
+ // negative x have left shift by valueDescriptionWidth
|
|
|
+ float modAngle = (angleMiddle + 360) % 360;
|
|
|
+ if (modAngle > 90 && modAngle < 270) {
|
|
|
+ left -= item.valueDescriptionWidth;
|
|
|
+ }
|
|
|
+ // negative y have top shift by text height
|
|
|
+ if ((angleMiddle + 360) % 360 > 180) {
|
|
|
+ top += item.valueDescriptionHeight;
|
|
|
+ }
|
|
|
+
|
|
|
canvas.drawText(item.valueDescription, left, top, valuePaint);
|
|
|
|
|
|
startAngle -= item.getAngle();
|
|
|
@@ -352,7 +368,12 @@ public class PieChartView extends View {
|
|
|
pieChartItem.description = adapter.getDescription(i);
|
|
|
pieChartItem.valueDescription = adapter.getValueDescription(i);
|
|
|
|
|
|
- pieChartItem.descriptionWidth = Utils.textWidth(pieChartItem.description, legendDescriptionPaint, true);
|
|
|
+ if (pieChartItem.description != null)
|
|
|
+ pieChartItem.descriptionWidth = Utils.textWidth(pieChartItem.description, legendDescriptionPaint, true);
|
|
|
+ if (pieChartItem.valueDescription != null) {
|
|
|
+ pieChartItem.valueDescriptionWidth = Utils.textWidth(pieChartItem.valueDescription, valuePaint, true);
|
|
|
+ pieChartItem.valueDescriptionHeight = Utils.textHeight(pieChartItem.valueDescription, valuePaint);
|
|
|
+ }
|
|
|
|
|
|
items.add(pieChartItem);
|
|
|
}
|