CandleDataSet.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package com.github.mikephil.charting.data;
  2. import android.graphics.Paint;
  3. import com.github.mikephil.charting.interfaces.datasets.ICandleDataSet;
  4. import com.github.mikephil.charting.utils.ColorTemplate;
  5. import com.github.mikephil.charting.utils.Utils;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. /**
  9. * DataSet for the CandleStickChart.
  10. *
  11. * @author Philipp Jahoda
  12. */
  13. public class CandleDataSet extends LineScatterCandleRadarDataSet<CandleEntry> implements ICandleDataSet {
  14. /**
  15. * the width of the shadow of the candle
  16. */
  17. private float mShadowWidth = 3f;
  18. /**
  19. * should the candle bars show?
  20. * when false, only "ticks" will show
  21. * <p/>
  22. * - default: true
  23. */
  24. private boolean mShowCandleBar = true;
  25. /**
  26. * the space between the candle entries, default 0.1f (10%)
  27. */
  28. private float mBarSpace = 0.1f;
  29. /**
  30. * use candle color for the shadow
  31. */
  32. private boolean mShadowColorSameAsCandle = false;
  33. /**
  34. * paint style when open < close
  35. * increasing candlesticks are traditionally hollow
  36. */
  37. protected Paint.Style mIncreasingPaintStyle = Paint.Style.STROKE;
  38. /**
  39. * paint style when open > close
  40. * descreasing candlesticks are traditionally filled
  41. */
  42. protected Paint.Style mDecreasingPaintStyle = Paint.Style.FILL;
  43. /**
  44. * color for open == close
  45. */
  46. protected int mNeutralColor = ColorTemplate.COLOR_SKIP;
  47. /**
  48. * color for open < close
  49. */
  50. protected int mIncreasingColor = ColorTemplate.COLOR_SKIP;
  51. /**
  52. * color for open > close
  53. */
  54. protected int mDecreasingColor = ColorTemplate.COLOR_SKIP;
  55. /**
  56. * shadow line color, set -1 for backward compatibility and uses default
  57. * color
  58. */
  59. protected int mShadowColor = ColorTemplate.COLOR_SKIP;
  60. public CandleDataSet(List<CandleEntry> yVals, String label) {
  61. super(yVals, label);
  62. }
  63. @Override
  64. public DataSet<CandleEntry> copy() {
  65. List<CandleEntry> entries = new ArrayList<CandleEntry>();
  66. for (int i = 0; i < mEntries.size(); i++) {
  67. entries.add(mEntries.get(i).copy());
  68. }
  69. CandleDataSet copied = new CandleDataSet(entries, getLabel());
  70. copy(copied);
  71. return copied;
  72. }
  73. protected void copy(CandleDataSet candleDataSet) {
  74. super.copy(candleDataSet);
  75. candleDataSet.mShadowWidth = mShadowWidth;
  76. candleDataSet.mShowCandleBar = mShowCandleBar;
  77. candleDataSet.mBarSpace = mBarSpace;
  78. candleDataSet.mShadowColorSameAsCandle = mShadowColorSameAsCandle;
  79. candleDataSet.mHighLightColor = mHighLightColor;
  80. candleDataSet.mIncreasingPaintStyle = mIncreasingPaintStyle;
  81. candleDataSet.mDecreasingPaintStyle = mDecreasingPaintStyle;
  82. candleDataSet.mNeutralColor = mNeutralColor;
  83. candleDataSet.mIncreasingColor = mIncreasingColor;
  84. candleDataSet.mDecreasingColor = mDecreasingColor;
  85. candleDataSet.mShadowColor = mShadowColor;
  86. }
  87. @Override
  88. protected void calcMinMax(CandleEntry e) {
  89. if (e.getLow() < mYMin)
  90. mYMin = e.getLow();
  91. if (e.getHigh() > mYMax)
  92. mYMax = e.getHigh();
  93. calcMinMaxX(e);
  94. }
  95. @Override
  96. protected void calcMinMaxY(CandleEntry e) {
  97. if (e.getHigh() < mYMin)
  98. mYMin = e.getHigh();
  99. if (e.getHigh() > mYMax)
  100. mYMax = e.getHigh();
  101. if (e.getLow() < mYMin)
  102. mYMin = e.getLow();
  103. if (e.getLow() > mYMax)
  104. mYMax = e.getLow();
  105. }
  106. /**
  107. * Sets the space that is left out on the left and right side of each
  108. * candle, default 0.1f (10%), max 0.45f, min 0f
  109. *
  110. * @param space
  111. */
  112. public void setBarSpace(float space) {
  113. if (space < 0f)
  114. space = 0f;
  115. if (space > 0.45f)
  116. space = 0.45f;
  117. mBarSpace = space;
  118. }
  119. @Override
  120. public float getBarSpace() {
  121. return mBarSpace;
  122. }
  123. /**
  124. * Sets the width of the candle-shadow-line in pixels. Default 3f.
  125. *
  126. * @param width
  127. */
  128. public void setShadowWidth(float width) {
  129. mShadowWidth = Utils.convertDpToPixel(width);
  130. }
  131. @Override
  132. public float getShadowWidth() {
  133. return mShadowWidth;
  134. }
  135. /**
  136. * Sets whether the candle bars should show?
  137. *
  138. * @param showCandleBar
  139. */
  140. public void setShowCandleBar(boolean showCandleBar) {
  141. mShowCandleBar = showCandleBar;
  142. }
  143. @Override
  144. public boolean getShowCandleBar() {
  145. return mShowCandleBar;
  146. }
  147. // TODO
  148. /**
  149. * It is necessary to implement ColorsList class that will encapsulate
  150. * colors list functionality, because It's wrong to copy paste setColor,
  151. * addColor, ... resetColors for each time when we want to add a coloring
  152. * options for one of objects
  153. *
  154. * @author Mesrop
  155. */
  156. /** BELOW THIS COLOR HANDLING */
  157. /**
  158. * Sets the one and ONLY color that should be used for this DataSet when
  159. * open == close.
  160. *
  161. * @param color
  162. */
  163. public void setNeutralColor(int color) {
  164. mNeutralColor = color;
  165. }
  166. @Override
  167. public int getNeutralColor() {
  168. return mNeutralColor;
  169. }
  170. /**
  171. * Sets the one and ONLY color that should be used for this DataSet when
  172. * open <= close.
  173. *
  174. * @param color
  175. */
  176. public void setIncreasingColor(int color) {
  177. mIncreasingColor = color;
  178. }
  179. @Override
  180. public int getIncreasingColor() {
  181. return mIncreasingColor;
  182. }
  183. /**
  184. * Sets the one and ONLY color that should be used for this DataSet when
  185. * open > close.
  186. *
  187. * @param color
  188. */
  189. public void setDecreasingColor(int color) {
  190. mDecreasingColor = color;
  191. }
  192. @Override
  193. public int getDecreasingColor() {
  194. return mDecreasingColor;
  195. }
  196. @Override
  197. public Paint.Style getIncreasingPaintStyle() {
  198. return mIncreasingPaintStyle;
  199. }
  200. /**
  201. * Sets paint style when open < close
  202. *
  203. * @param paintStyle
  204. */
  205. public void setIncreasingPaintStyle(Paint.Style paintStyle) {
  206. this.mIncreasingPaintStyle = paintStyle;
  207. }
  208. @Override
  209. public Paint.Style getDecreasingPaintStyle() {
  210. return mDecreasingPaintStyle;
  211. }
  212. /**
  213. * Sets paint style when open > close
  214. *
  215. * @param decreasingPaintStyle
  216. */
  217. public void setDecreasingPaintStyle(Paint.Style decreasingPaintStyle) {
  218. this.mDecreasingPaintStyle = decreasingPaintStyle;
  219. }
  220. @Override
  221. public int getShadowColor() {
  222. return mShadowColor;
  223. }
  224. /**
  225. * Sets shadow color for all entries
  226. *
  227. * @param shadowColor
  228. */
  229. public void setShadowColor(int shadowColor) {
  230. this.mShadowColor = shadowColor;
  231. }
  232. @Override
  233. public boolean getShadowColorSameAsCandle() {
  234. return mShadowColorSameAsCandle;
  235. }
  236. /**
  237. * Sets shadow color to be the same color as the candle color
  238. *
  239. * @param shadowColorSameAsCandle
  240. */
  241. public void setShadowColorSameAsCandle(boolean shadowColorSameAsCandle) {
  242. this.mShadowColorSameAsCandle = shadowColorSameAsCandle;
  243. }
  244. }