aboutsummaryrefslogtreecommitdiff
path: root/MPChartLib/src/main/java/com
diff options
context:
space:
mode:
authoralmic <mick.ashton@flare-esports.net>2018-11-15 09:51:46 -0700
committeralmic <mick.ashton@flare-esports.net>2018-11-15 09:51:46 -0700
commitaea2ff3417e30d6d4b1ce7e777cbd8bc83e1c95d (patch)
tree43a4bb5705f06c514f4ddc144bd4163b4622986e /MPChartLib/src/main/java/com
parent42cdba535f5af8076f3a376afba543c7e41eb9d0 (diff)
downloadMPAndroidChart-aea2ff3417e30d6d4b1ce7e777cbd8bc83e1c95d.tar.gz
Add option to set minimum angles
Allows to force minimum slice angles when drawing charts, which means that any slices with angles lower than the minimum will be drawn with the minimum angle. When changing this setting on the fly, you have to call `notifyDataSetChanged()` and `invalidate()` for the minimum angle to take effect. This only functions if all slices can be drawn with the minimum angle. For example if a chart has 5 slices, the largest functioning minimum angle is `72f` degrees on a 360 degree chart, or 20% of the chart's `maxAngle`.
Diffstat (limited to 'MPChartLib/src/main/java/com')
-rw-r--r--MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java66
1 files changed, 65 insertions, 1 deletions
diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java
index 0ed45027..de11b3a8 100644
--- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java
+++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java
@@ -94,6 +94,12 @@ public class PieChart extends PieRadarChartBase<PieData> {
protected float mMaxAngle = 360f;
+ /**
+ * Minimum angle to draw slices, this only works if there is enough room for all slices to have
+ * the minimum angle, default 0f.
+ */
+ private float mMinAngleForSlices = 0f;
+
public PieChart(Context context) {
super(context);
}
@@ -228,7 +234,12 @@ public class PieChart extends PieRadarChartBase<PieData> {
List<IPieDataSet> dataSets = mData.getDataSets();
+ boolean hasMinAngle = mMinAngleForSlices != 0f && entryCount * mMinAngleForSlices <= mMaxAngle;
+ float[] minAngles = new float[entryCount];
+
int cnt = 0;
+ float offset = 0f;
+ float diff = 0f;
for (int i = 0; i < mData.getDataSetCount(); i++) {
@@ -236,7 +247,20 @@ public class PieChart extends PieRadarChartBase<PieData> {
for (int j = 0; j < set.getEntryCount(); j++) {
- mDrawAngles[cnt] = calcAngle(Math.abs(set.getEntryForIndex(j).getY()), yValueSum);
+ float drawAngle = calcAngle(Math.abs(set.getEntryForIndex(j).getY()), yValueSum);
+
+ if (hasMinAngle) {
+ float temp = drawAngle - mMinAngleForSlices;
+ if (temp <= 0) {
+ minAngles[cnt] = mMinAngleForSlices;
+ offset += -temp;
+ } else {
+ minAngles[cnt] = drawAngle;
+ diff += temp;
+ }
+ }
+
+ mDrawAngles[cnt] = drawAngle;
if (cnt == 0) {
mAbsoluteAngles[cnt] = mDrawAngles[cnt];
@@ -248,6 +272,20 @@ public class PieChart extends PieRadarChartBase<PieData> {
}
}
+ if (hasMinAngle) {
+ // Correct bigger slices by relatively reducing their angles based on the total angle needed to subtract
+ // This requires that `entryCount * mMinAngleForSlices <= mMaxAngle` be true to properly work!
+ for (int i = 0; i < entryCount; i++) {
+ minAngles[i] -= (minAngles[i] - mMinAngleForSlices) / diff * offset;
+ if (i == 0) {
+ mAbsoluteAngles[0] = minAngles[0];
+ } else {
+ mAbsoluteAngles[i] = mAbsoluteAngles[i - 1] + minAngles[i];
+ }
+ }
+
+ mDrawAngles = minAngles;
+ }
}
/**
@@ -729,6 +767,32 @@ public class PieChart extends PieRadarChartBase<PieData> {
this.mMaxAngle = maxangle;
}
+ /**
+ * The minimum angle slices on the chart are rendered with, default is 0f.
+ *
+ * @return minimum angle for slices
+ */
+ public float getMinAngleForSlices() {
+ return mMinAngleForSlices;
+ }
+
+ /**
+ * Set the angle to set minimum size for slices, you must call {@link #notifyDataSetChanged()}
+ * and {@link #invalidate()} when changing this, only works if there is enough room for all
+ * slices to have the minimum angle.
+ *
+ * @param minAngle minimum 0, maximum is half of {@link #setMaxAngle(float)}
+ */
+ public void setMinAngleForSlices(float minAngle) {
+
+ if (minAngle > (mMaxAngle / 2f))
+ minAngle = mMaxAngle / 2f;
+ else if (minAngle < 0)
+ minAngle = 0f;
+
+ this.mMinAngleForSlices = minAngle;
+ }
+
@Override
protected void onDetachedFromWindow() {
// releases the bitmap in the renderer to avoid oom error