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