1 2 package com.github.mikephil.charting.data; 3 4 import android.graphics.Color; 5 6 import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; 7 import com.github.mikephil.charting.utils.Fill; 8 9 import java.util.ArrayList; 10 import java.util.List; 11 12 public class BarDataSet extends BarLineScatterCandleBubbleDataSet<BarEntry> implements IBarDataSet { 13 14 /** 15 * the maximum number of bars that are stacked upon each other, this value 16 * is calculated from the Entries that are added to the DataSet 17 */ 18 private int mStackSize = 1; 19 20 /** 21 * the color used for drawing the bar shadows 22 */ 23 private int mBarShadowColor = Color.rgb(215, 215, 215); 24 25 private float mBarBorderWidth = 0.0f; 26 27 private int mBarBorderColor = Color.BLACK; 28 29 /** 30 * the alpha value used to draw the highlight indicator bar 31 */ 32 private int mHighLightAlpha = 120; 33 34 /** 35 * the overall entry count, including counting each stack-value individually 36 */ 37 private int mEntryCountStacks = 0; 38 39 /** 40 * array of labels used to describe the different values of the stacked bars 41 */ 42 private String[] mStackLabels = new String[]{}; 43 44 protected List<Fill> mFills = null; 45 BarDataSet(List<BarEntry> yVals, String label)46 public BarDataSet(List<BarEntry> yVals, String label) { 47 super(yVals, label); 48 49 mHighLightColor = Color.rgb(0, 0, 0); 50 51 calcStackSize(yVals); 52 calcEntryCountIncludingStacks(yVals); 53 } 54 55 @Override copy()56 public DataSet<BarEntry> copy() { 57 List<BarEntry> entries = new ArrayList<BarEntry>(); 58 for (int i = 0; i < mEntries.size(); i++) { 59 entries.add(mEntries.get(i).copy()); 60 } 61 BarDataSet copied = new BarDataSet(entries, getLabel()); 62 copy(copied); 63 return copied; 64 } 65 copy(BarDataSet barDataSet)66 protected void copy(BarDataSet barDataSet) { 67 super.copy(barDataSet); 68 barDataSet.mStackSize = mStackSize; 69 barDataSet.mBarShadowColor = mBarShadowColor; 70 barDataSet.mBarBorderWidth = mBarBorderWidth; 71 barDataSet.mStackLabels = mStackLabels; 72 barDataSet.mHighLightAlpha = mHighLightAlpha; 73 } 74 75 @Override getFills()76 public List<Fill> getFills() { 77 return mFills; 78 } 79 80 @Override getFill(int index)81 public Fill getFill(int index) { 82 return mFills.get(index % mFills.size()); 83 } 84 85 /** 86 * This method is deprecated. 87 * Use getFills() instead. 88 */ 89 @Deprecated getGradients()90 public List<Fill> getGradients() { 91 return mFills; 92 } 93 94 /** 95 * This method is deprecated. 96 * Use getFill(...) instead. 97 * 98 * @param index 99 */ 100 @Deprecated getGradient(int index)101 public Fill getGradient(int index) { 102 return getFill(index); 103 } 104 105 /** 106 * Sets the start and end color for gradient color, ONLY color that should be used for this DataSet. 107 * 108 * @param startColor 109 * @param endColor 110 */ setGradientColor(int startColor, int endColor)111 public void setGradientColor(int startColor, int endColor) { 112 mFills.clear(); 113 mFills.add(new Fill(startColor, endColor)); 114 } 115 116 /** 117 * This method is deprecated. 118 * Use setFills(...) instead. 119 * 120 * @param gradientColors 121 */ 122 @Deprecated setGradientColors(List<Fill> gradientColors)123 public void setGradientColors(List<Fill> gradientColors) { 124 this.mFills = gradientColors; 125 } 126 127 /** 128 * Sets the fills for the bars in this dataset. 129 * 130 * @param fills 131 */ setFills(List<Fill> fills)132 public void setFills(List<Fill> fills) { 133 this.mFills = fills; 134 } 135 136 /** 137 * Calculates the total number of entries this DataSet represents, including 138 * stacks. All values belonging to a stack are calculated separately. 139 */ calcEntryCountIncludingStacks(List<BarEntry> yVals)140 private void calcEntryCountIncludingStacks(List<BarEntry> yVals) { 141 142 mEntryCountStacks = 0; 143 144 for (int i = 0; i < yVals.size(); i++) { 145 146 float[] vals = yVals.get(i).getYVals(); 147 148 if (vals == null) 149 mEntryCountStacks++; 150 else 151 mEntryCountStacks += vals.length; 152 } 153 } 154 155 /** 156 * calculates the maximum stacksize that occurs in the Entries array of this 157 * DataSet 158 */ calcStackSize(List<BarEntry> yVals)159 private void calcStackSize(List<BarEntry> yVals) { 160 161 for (int i = 0; i < yVals.size(); i++) { 162 163 float[] vals = yVals.get(i).getYVals(); 164 165 if (vals != null && vals.length > mStackSize) 166 mStackSize = vals.length; 167 } 168 } 169 170 @Override calcMinMax(BarEntry e)171 protected void calcMinMax(BarEntry e) { 172 173 if (e != null && !Float.isNaN(e.getY())) { 174 175 if (e.getYVals() == null) { 176 177 if (e.getY() < mYMin) 178 mYMin = e.getY(); 179 180 if (e.getY() > mYMax) 181 mYMax = e.getY(); 182 } else { 183 184 if (-e.getNegativeSum() < mYMin) 185 mYMin = -e.getNegativeSum(); 186 187 if (e.getPositiveSum() > mYMax) 188 mYMax = e.getPositiveSum(); 189 } 190 191 calcMinMaxX(e); 192 } 193 } 194 195 @Override getStackSize()196 public int getStackSize() { 197 return mStackSize; 198 } 199 200 @Override isStacked()201 public boolean isStacked() { 202 return mStackSize > 1 ? true : false; 203 } 204 205 /** 206 * returns the overall entry count, including counting each stack-value 207 * individually 208 * 209 * @return 210 */ getEntryCountStacks()211 public int getEntryCountStacks() { 212 return mEntryCountStacks; 213 } 214 215 /** 216 * Sets the color used for drawing the bar-shadows. The bar shadows is a 217 * surface behind the bar that indicates the maximum value. Don't for get to 218 * use getResources().getColor(...) to set this. Or Color.rgb(...). 219 * 220 * @param color 221 */ setBarShadowColor(int color)222 public void setBarShadowColor(int color) { 223 mBarShadowColor = color; 224 } 225 226 @Override getBarShadowColor()227 public int getBarShadowColor() { 228 return mBarShadowColor; 229 } 230 231 /** 232 * Sets the width used for drawing borders around the bars. 233 * If borderWidth == 0, no border will be drawn. 234 * 235 * @return 236 */ setBarBorderWidth(float width)237 public void setBarBorderWidth(float width) { 238 mBarBorderWidth = width; 239 } 240 241 /** 242 * Returns the width used for drawing borders around the bars. 243 * If borderWidth == 0, no border will be drawn. 244 * 245 * @return 246 */ 247 @Override getBarBorderWidth()248 public float getBarBorderWidth() { 249 return mBarBorderWidth; 250 } 251 252 /** 253 * Sets the color drawing borders around the bars. 254 * 255 * @return 256 */ setBarBorderColor(int color)257 public void setBarBorderColor(int color) { 258 mBarBorderColor = color; 259 } 260 261 /** 262 * Returns the color drawing borders around the bars. 263 * 264 * @return 265 */ 266 @Override getBarBorderColor()267 public int getBarBorderColor() { 268 return mBarBorderColor; 269 } 270 271 /** 272 * Set the alpha value (transparency) that is used for drawing the highlight 273 * indicator bar. min = 0 (fully transparent), max = 255 (fully opaque) 274 * 275 * @param alpha 276 */ setHighLightAlpha(int alpha)277 public void setHighLightAlpha(int alpha) { 278 mHighLightAlpha = alpha; 279 } 280 281 @Override getHighLightAlpha()282 public int getHighLightAlpha() { 283 return mHighLightAlpha; 284 } 285 286 /** 287 * Sets labels for different values of bar-stacks, in case there are one. 288 * 289 * @param labels 290 */ setStackLabels(String[] labels)291 public void setStackLabels(String[] labels) { 292 mStackLabels = labels; 293 } 294 295 @Override getStackLabels()296 public String[] getStackLabels() { 297 return mStackLabels; 298 } 299 } 300