1 2 package com.github.mikephil.charting.data; 3 4 import android.content.Context; 5 import android.graphics.Color; 6 import android.graphics.DashPathEffect; 7 import android.util.Log; 8 9 import com.github.mikephil.charting.formatter.DefaultFillFormatter; 10 import com.github.mikephil.charting.formatter.IFillFormatter; 11 import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; 12 import com.github.mikephil.charting.utils.ColorTemplate; 13 import com.github.mikephil.charting.utils.Utils; 14 15 import java.util.ArrayList; 16 import java.util.List; 17 18 public class LineDataSet extends LineRadarDataSet<Entry> implements ILineDataSet { 19 20 /** 21 * Drawing mode for this line dataset 22 **/ 23 private LineDataSet.Mode mMode = Mode.LINEAR; 24 25 /** 26 * List representing all colors that are used for the circles 27 */ 28 private List<Integer> mCircleColors = null; 29 30 /** 31 * the color of the inner circles 32 */ 33 private int mCircleHoleColor = Color.WHITE; 34 35 /** 36 * the radius of the circle-shaped value indicators 37 */ 38 private float mCircleRadius = 8f; 39 40 /** 41 * the hole radius of the circle-shaped value indicators 42 */ 43 private float mCircleHoleRadius = 4f; 44 45 /** 46 * sets the intensity of the cubic lines 47 */ 48 private float mCubicIntensity = 0.2f; 49 50 /** 51 * the path effect of this DataSet that makes dashed lines possible 52 */ 53 private DashPathEffect mDashPathEffect = null; 54 55 /** 56 * formatter for customizing the position of the fill-line 57 */ 58 private IFillFormatter mFillFormatter = new DefaultFillFormatter(); 59 60 /** 61 * if true, drawing circles is enabled 62 */ 63 private boolean mDrawCircles = true; 64 65 private boolean mDrawCircleHole = true; 66 67 LineDataSet(List<Entry> yVals, String label)68 public LineDataSet(List<Entry> yVals, String label) { 69 super(yVals, label); 70 71 // mCircleRadius = Utils.convertDpToPixel(4f); 72 // mLineWidth = Utils.convertDpToPixel(1f); 73 74 if (mCircleColors == null) { 75 mCircleColors = new ArrayList<Integer>(); 76 } 77 mCircleColors.clear(); 78 79 // default colors 80 // mColors.add(Color.rgb(192, 255, 140)); 81 // mColors.add(Color.rgb(255, 247, 140)); 82 mCircleColors.add(Color.rgb(140, 234, 255)); 83 } 84 85 @Override copy()86 public DataSet<Entry> copy() { 87 List<Entry> entries = new ArrayList<Entry>(); 88 for (int i = 0; i < mEntries.size(); i++) { 89 entries.add(mEntries.get(i).copy()); 90 } 91 LineDataSet copied = new LineDataSet(entries, getLabel()); 92 copy(copied); 93 return copied; 94 } 95 copy(LineDataSet lineDataSet)96 protected void copy(LineDataSet lineDataSet) { 97 super.copy(lineDataSet); 98 lineDataSet.mCircleColors = mCircleColors; 99 lineDataSet.mCircleHoleColor = mCircleHoleColor; 100 lineDataSet.mCircleHoleRadius = mCircleHoleRadius; 101 lineDataSet.mCircleRadius = mCircleRadius; 102 lineDataSet.mCubicIntensity = mCubicIntensity; 103 lineDataSet.mDashPathEffect = mDashPathEffect; 104 lineDataSet.mDrawCircleHole = mDrawCircleHole; 105 lineDataSet.mDrawCircles = mDrawCircleHole; 106 lineDataSet.mFillFormatter = mFillFormatter; 107 lineDataSet.mMode = mMode; 108 } 109 110 /** 111 * Returns the drawing mode for this line dataset 112 * 113 * @return 114 */ 115 @Override getMode()116 public LineDataSet.Mode getMode() { 117 return mMode; 118 } 119 120 /** 121 * Returns the drawing mode for this LineDataSet 122 * 123 * @return 124 */ setMode(LineDataSet.Mode mode)125 public void setMode(LineDataSet.Mode mode) { 126 mMode = mode; 127 } 128 129 /** 130 * Sets the intensity for cubic lines (if enabled). Max = 1f = very cubic, 131 * Min = 0.05f = low cubic effect, Default: 0.2f 132 * 133 * @param intensity 134 */ setCubicIntensity(float intensity)135 public void setCubicIntensity(float intensity) { 136 137 if (intensity > 1f) 138 intensity = 1f; 139 if (intensity < 0.05f) 140 intensity = 0.05f; 141 142 mCubicIntensity = intensity; 143 } 144 145 @Override getCubicIntensity()146 public float getCubicIntensity() { 147 return mCubicIntensity; 148 } 149 150 151 /** 152 * Sets the radius of the drawn circles. 153 * Default radius = 4f, Min = 1f 154 * 155 * @param radius 156 */ setCircleRadius(float radius)157 public void setCircleRadius(float radius) { 158 159 if (radius >= 1f) { 160 mCircleRadius = Utils.convertDpToPixel(radius); 161 } else { 162 Log.e("LineDataSet", "Circle radius cannot be < 1"); 163 } 164 } 165 166 @Override getCircleRadius()167 public float getCircleRadius() { 168 return mCircleRadius; 169 } 170 171 /** 172 * Sets the hole radius of the drawn circles. 173 * Default radius = 2f, Min = 0.5f 174 * 175 * @param holeRadius 176 */ setCircleHoleRadius(float holeRadius)177 public void setCircleHoleRadius(float holeRadius) { 178 179 if (holeRadius >= 0.5f) { 180 mCircleHoleRadius = Utils.convertDpToPixel(holeRadius); 181 } else { 182 Log.e("LineDataSet", "Circle radius cannot be < 0.5"); 183 } 184 } 185 186 @Override getCircleHoleRadius()187 public float getCircleHoleRadius() { 188 return mCircleHoleRadius; 189 } 190 191 /** 192 * sets the size (radius) of the circle shpaed value indicators, 193 * default size = 4f 194 * <p/> 195 * This method is deprecated because of unclarity. Use setCircleRadius instead. 196 * 197 * @param size 198 */ 199 @Deprecated setCircleSize(float size)200 public void setCircleSize(float size) { 201 setCircleRadius(size); 202 } 203 204 /** 205 * This function is deprecated because of unclarity. Use getCircleRadius instead. 206 */ 207 @Deprecated getCircleSize()208 public float getCircleSize() { 209 return getCircleRadius(); 210 } 211 212 /** 213 * Enables the line to be drawn in dashed mode, e.g. like this 214 * "- - - - - -". THIS ONLY WORKS IF HARDWARE-ACCELERATION IS TURNED OFF. 215 * Keep in mind that hardware acceleration boosts performance. 216 * 217 * @param lineLength the length of the line pieces 218 * @param spaceLength the length of space in between the pieces 219 * @param phase offset, in degrees (normally, use 0) 220 */ enableDashedLine(float lineLength, float spaceLength, float phase)221 public void enableDashedLine(float lineLength, float spaceLength, float phase) { 222 mDashPathEffect = new DashPathEffect(new float[]{ 223 lineLength, spaceLength 224 }, phase); 225 } 226 227 /** 228 * Disables the line to be drawn in dashed mode. 229 */ disableDashedLine()230 public void disableDashedLine() { 231 mDashPathEffect = null; 232 } 233 234 @Override isDashedLineEnabled()235 public boolean isDashedLineEnabled() { 236 return mDashPathEffect == null ? false : true; 237 } 238 239 @Override getDashPathEffect()240 public DashPathEffect getDashPathEffect() { 241 return mDashPathEffect; 242 } 243 244 /** 245 * set this to true to enable the drawing of circle indicators for this 246 * DataSet, default true 247 * 248 * @param enabled 249 */ setDrawCircles(boolean enabled)250 public void setDrawCircles(boolean enabled) { 251 this.mDrawCircles = enabled; 252 } 253 254 @Override isDrawCirclesEnabled()255 public boolean isDrawCirclesEnabled() { 256 return mDrawCircles; 257 } 258 259 @Deprecated 260 @Override isDrawCubicEnabled()261 public boolean isDrawCubicEnabled() { 262 return mMode == Mode.CUBIC_BEZIER; 263 } 264 265 @Deprecated 266 @Override isDrawSteppedEnabled()267 public boolean isDrawSteppedEnabled() { 268 return mMode == Mode.STEPPED; 269 } 270 271 /** ALL CODE BELOW RELATED TO CIRCLE-COLORS */ 272 273 /** 274 * returns all colors specified for the circles 275 * 276 * @return 277 */ getCircleColors()278 public List<Integer> getCircleColors() { 279 return mCircleColors; 280 } 281 282 @Override getCircleColor(int index)283 public int getCircleColor(int index) { 284 return mCircleColors.get(index); 285 } 286 287 @Override getCircleColorCount()288 public int getCircleColorCount() { 289 return mCircleColors.size(); 290 } 291 292 /** 293 * Sets the colors that should be used for the circles of this DataSet. 294 * Colors are reused as soon as the number of Entries the DataSet represents 295 * is higher than the size of the colors array. Make sure that the colors 296 * are already prepared (by calling getResources().getColor(...)) before 297 * adding them to the DataSet. 298 * 299 * @param colors 300 */ setCircleColors(List<Integer> colors)301 public void setCircleColors(List<Integer> colors) { 302 mCircleColors = colors; 303 } 304 305 /** 306 * Sets the colors that should be used for the circles of this DataSet. 307 * Colors are reused as soon as the number of Entries the DataSet represents 308 * is higher than the size of the colors array. Make sure that the colors 309 * are already prepared (by calling getResources().getColor(...)) before 310 * adding them to the DataSet. 311 * 312 * @param colors 313 */ setCircleColors(int... colors)314 public void setCircleColors(int... colors) { 315 this.mCircleColors = ColorTemplate.createColors(colors); 316 } 317 318 /** 319 * ets the colors that should be used for the circles of this DataSet. 320 * Colors are reused as soon as the number of Entries the DataSet represents 321 * is higher than the size of the colors array. You can use 322 * "new String[] { R.color.red, R.color.green, ... }" to provide colors for 323 * this method. Internally, the colors are resolved using 324 * getResources().getColor(...) 325 * 326 * @param colors 327 */ setCircleColors(int[] colors, Context c)328 public void setCircleColors(int[] colors, Context c) { 329 330 List<Integer> clrs = mCircleColors; 331 if (clrs == null) { 332 clrs = new ArrayList<>(); 333 } 334 clrs.clear(); 335 336 for (int color : colors) { 337 clrs.add(c.getResources().getColor(color)); 338 } 339 340 mCircleColors = clrs; 341 } 342 343 /** 344 * Sets the one and ONLY color that should be used for this DataSet. 345 * Internally, this recreates the colors array and adds the specified color. 346 * 347 * @param color 348 */ setCircleColor(int color)349 public void setCircleColor(int color) { 350 resetCircleColors(); 351 mCircleColors.add(color); 352 } 353 354 /** 355 * resets the circle-colors array and creates a new one 356 */ resetCircleColors()357 public void resetCircleColors() { 358 if (mCircleColors == null) { 359 mCircleColors = new ArrayList<Integer>(); 360 } 361 mCircleColors.clear(); 362 } 363 364 /** 365 * Sets the color of the inner circle of the line-circles. 366 * 367 * @param color 368 */ setCircleHoleColor(int color)369 public void setCircleHoleColor(int color) { 370 mCircleHoleColor = color; 371 } 372 373 @Override getCircleHoleColor()374 public int getCircleHoleColor() { 375 return mCircleHoleColor; 376 } 377 378 /** 379 * Set this to true to allow drawing a hole in each data circle. 380 * 381 * @param enabled 382 */ setDrawCircleHole(boolean enabled)383 public void setDrawCircleHole(boolean enabled) { 384 mDrawCircleHole = enabled; 385 } 386 387 @Override isDrawCircleHoleEnabled()388 public boolean isDrawCircleHoleEnabled() { 389 return mDrawCircleHole; 390 } 391 392 /** 393 * Sets a custom IFillFormatter to the chart that handles the position of the 394 * filled-line for each DataSet. Set this to null to use the default logic. 395 * 396 * @param formatter 397 */ setFillFormatter(IFillFormatter formatter)398 public void setFillFormatter(IFillFormatter formatter) { 399 400 if (formatter == null) 401 mFillFormatter = new DefaultFillFormatter(); 402 else 403 mFillFormatter = formatter; 404 } 405 406 @Override getFillFormatter()407 public IFillFormatter getFillFormatter() { 408 return mFillFormatter; 409 } 410 411 public enum Mode { 412 LINEAR, 413 STEPPED, 414 CUBIC_BEZIER, 415 HORIZONTAL_BEZIER 416 } 417 } 418