1 2 package com.github.mikephil.charting.data; 3 4 import com.github.mikephil.charting.interfaces.datasets.IPieDataSet; 5 import com.github.mikephil.charting.utils.Utils; 6 import androidx.annotation.Nullable; 7 8 import java.util.ArrayList; 9 import java.util.List; 10 11 public class PieDataSet extends DataSet<PieEntry> implements IPieDataSet { 12 13 /** 14 * the space in pixels between the chart-slices, default 0f 15 */ 16 private float mSliceSpace = 0f; 17 private boolean mAutomaticallyDisableSliceSpacing; 18 19 /** 20 * indicates the selection distance of a pie slice 21 */ 22 private float mShift = 18f; 23 24 private ValuePosition mXValuePosition = ValuePosition.INSIDE_SLICE; 25 private ValuePosition mYValuePosition = ValuePosition.INSIDE_SLICE; 26 private int mValueLineColor = 0xff000000; 27 private boolean mUseValueColorForLine = false; 28 private float mValueLineWidth = 1.0f; 29 private float mValueLinePart1OffsetPercentage = 75.f; 30 private float mValueLinePart1Length = 0.3f; 31 private float mValueLinePart2Length = 0.4f; 32 private boolean mValueLineVariableLength = true; 33 private Integer mHighlightColor = null; 34 PieDataSet(List<PieEntry> yVals, String label)35 public PieDataSet(List<PieEntry> yVals, String label) { 36 super(yVals, label); 37 // mShift = Utils.convertDpToPixel(12f); 38 } 39 40 @Override copy()41 public DataSet<PieEntry> copy() { 42 List<PieEntry> entries = new ArrayList<>(); 43 for (int i = 0; i < mEntries.size(); i++) { 44 entries.add(mEntries.get(i).copy()); 45 } 46 PieDataSet copied = new PieDataSet(entries, getLabel()); 47 copy(copied); 48 return copied; 49 } 50 copy(PieDataSet pieDataSet)51 protected void copy(PieDataSet pieDataSet) { 52 super.copy(pieDataSet); 53 } 54 55 @Override calcMinMax(PieEntry e)56 protected void calcMinMax(PieEntry e) { 57 58 if (e == null) 59 return; 60 61 calcMinMaxY(e); 62 } 63 64 /** 65 * Sets the space that is left out between the piechart-slices in dp. 66 * Default: 0 --> no space, maximum 20f 67 * 68 * @param spaceDp 69 */ setSliceSpace(float spaceDp)70 public void setSliceSpace(float spaceDp) { 71 72 if (spaceDp > 20) 73 spaceDp = 20f; 74 if (spaceDp < 0) 75 spaceDp = 0f; 76 77 mSliceSpace = Utils.convertDpToPixel(spaceDp); 78 } 79 80 @Override getSliceSpace()81 public float getSliceSpace() { 82 return mSliceSpace; 83 } 84 85 /** 86 * When enabled, slice spacing will be 0.0 when the smallest value is going to be 87 * smaller than the slice spacing itself. 88 * 89 * @param autoDisable 90 */ setAutomaticallyDisableSliceSpacing(boolean autoDisable)91 public void setAutomaticallyDisableSliceSpacing(boolean autoDisable) { 92 mAutomaticallyDisableSliceSpacing = autoDisable; 93 } 94 95 /** 96 * When enabled, slice spacing will be 0.0 when the smallest value is going to be 97 * smaller than the slice spacing itself. 98 * 99 * @return 100 */ 101 @Override isAutomaticallyDisableSliceSpacingEnabled()102 public boolean isAutomaticallyDisableSliceSpacingEnabled() { 103 return mAutomaticallyDisableSliceSpacing; 104 } 105 106 /** 107 * sets the distance the highlighted piechart-slice of this DataSet is 108 * "shifted" away from the center of the chart, default 12f 109 * 110 * @param shift 111 */ setSelectionShift(float shift)112 public void setSelectionShift(float shift) { 113 mShift = Utils.convertDpToPixel(shift); 114 } 115 116 @Override getSelectionShift()117 public float getSelectionShift() { 118 return mShift; 119 } 120 121 @Override getXValuePosition()122 public ValuePosition getXValuePosition() { 123 return mXValuePosition; 124 } 125 setXValuePosition(ValuePosition xValuePosition)126 public void setXValuePosition(ValuePosition xValuePosition) { 127 this.mXValuePosition = xValuePosition; 128 } 129 130 @Override getYValuePosition()131 public ValuePosition getYValuePosition() { 132 return mYValuePosition; 133 } 134 setYValuePosition(ValuePosition yValuePosition)135 public void setYValuePosition(ValuePosition yValuePosition) { 136 this.mYValuePosition = yValuePosition; 137 } 138 139 /** 140 * This method is deprecated. 141 * Use isUseValueColorForLineEnabled() instead. 142 */ 143 @Deprecated isUsingSliceColorAsValueLineColor()144 public boolean isUsingSliceColorAsValueLineColor() { 145 return isUseValueColorForLineEnabled(); 146 } 147 148 /** 149 * This method is deprecated. 150 * Use setUseValueColorForLine(...) instead. 151 * 152 * @param enabled 153 */ 154 @Deprecated setUsingSliceColorAsValueLineColor(boolean enabled)155 public void setUsingSliceColorAsValueLineColor(boolean enabled) { 156 setUseValueColorForLine(enabled); 157 } 158 159 /** 160 * When valuePosition is OutsideSlice, indicates line color 161 */ 162 @Override getValueLineColor()163 public int getValueLineColor() { 164 return mValueLineColor; 165 } 166 setValueLineColor(int valueLineColor)167 public void setValueLineColor(int valueLineColor) { 168 this.mValueLineColor = valueLineColor; 169 } 170 171 @Override isUseValueColorForLineEnabled()172 public boolean isUseValueColorForLineEnabled() 173 { 174 return mUseValueColorForLine; 175 } 176 setUseValueColorForLine(boolean enabled)177 public void setUseValueColorForLine(boolean enabled) 178 { 179 mUseValueColorForLine = enabled; 180 } 181 182 /** 183 * When valuePosition is OutsideSlice, indicates line width 184 */ 185 @Override getValueLineWidth()186 public float getValueLineWidth() { 187 return mValueLineWidth; 188 } 189 setValueLineWidth(float valueLineWidth)190 public void setValueLineWidth(float valueLineWidth) { 191 this.mValueLineWidth = valueLineWidth; 192 } 193 194 /** 195 * When valuePosition is OutsideSlice, indicates offset as percentage out of the slice size 196 */ 197 @Override getValueLinePart1OffsetPercentage()198 public float getValueLinePart1OffsetPercentage() { 199 return mValueLinePart1OffsetPercentage; 200 } 201 setValueLinePart1OffsetPercentage(float valueLinePart1OffsetPercentage)202 public void setValueLinePart1OffsetPercentage(float valueLinePart1OffsetPercentage) { 203 this.mValueLinePart1OffsetPercentage = valueLinePart1OffsetPercentage; 204 } 205 206 /** 207 * When valuePosition is OutsideSlice, indicates length of first half of the line 208 */ 209 @Override getValueLinePart1Length()210 public float getValueLinePart1Length() { 211 return mValueLinePart1Length; 212 } 213 setValueLinePart1Length(float valueLinePart1Length)214 public void setValueLinePart1Length(float valueLinePart1Length) { 215 this.mValueLinePart1Length = valueLinePart1Length; 216 } 217 218 /** 219 * When valuePosition is OutsideSlice, indicates length of second half of the line 220 */ 221 @Override getValueLinePart2Length()222 public float getValueLinePart2Length() { 223 return mValueLinePart2Length; 224 } 225 setValueLinePart2Length(float valueLinePart2Length)226 public void setValueLinePart2Length(float valueLinePart2Length) { 227 this.mValueLinePart2Length = valueLinePart2Length; 228 } 229 230 /** 231 * When valuePosition is OutsideSlice, this allows variable line length 232 */ 233 @Override isValueLineVariableLength()234 public boolean isValueLineVariableLength() { 235 return mValueLineVariableLength; 236 } 237 setValueLineVariableLength(boolean valueLineVariableLength)238 public void setValueLineVariableLength(boolean valueLineVariableLength) { 239 this.mValueLineVariableLength = valueLineVariableLength; 240 } 241 242 /** Gets the color for the highlighted sector */ 243 @Override 244 @Nullable getHighlightColor()245 public Integer getHighlightColor() 246 { 247 return mHighlightColor; 248 } 249 250 /** Sets the color for the highlighted sector (null for using entry color) */ setHighlightColor(@ullable Integer color)251 public void setHighlightColor(@Nullable Integer color) 252 { 253 this.mHighlightColor = color; 254 } 255 256 257 public enum ValuePosition { 258 INSIDE_SLICE, 259 OUTSIDE_SLICE 260 } 261 } 262