BarDataSet.java 7.1 KB

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