Skip to content

Commit bfae450

Browse files
committed
Fill line chart with a Drawable
can be used for gradients, pictures, etc.
1 parent 2dee57a commit bfae450

8 files changed

Lines changed: 105 additions & 9 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
<gradient
4+
android:angle="90"
5+
android:startColor="#00ff0000"
6+
android:endColor="#ffff0000" />
7+
</shape>

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
import android.graphics.Color;
55
import android.graphics.Typeface;
6+
import android.graphics.drawable.Drawable;
67
import android.os.Bundle;
8+
import android.support.v4.content.ContextCompat;
79
import android.util.Log;
810
import android.view.Menu;
911
import android.view.MenuItem;
@@ -358,11 +360,9 @@ private void setData(int count, float range) {
358360
set1.setCircleRadius(3f);
359361
set1.setDrawCircleHole(false);
360362
set1.setValueTextSize(9f);
361-
set1.setFillAlpha(65);
362-
set1.setFillColor(Color.BLACK);
363-
// set1.setDrawFilled(true);
364-
// set1.setShader(new LinearGradient(0, 0, 0, mChart.getHeight(),
365-
// Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
363+
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
364+
set1.setFillDrawable(drawable);
365+
set1.setDrawFilled(true);
366366

367367
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
368368
dataSets.add(set1); // add the datasets

‎MPChartLib/src/com/github/mikephil/charting/data/LineDataSet.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.Context;
55
import android.graphics.Color;
66
import android.graphics.DashPathEffect;
7+
import android.graphics.drawable.Drawable;
78

89
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
910
import com.github.mikephil.charting.utils.ColorTemplate;
@@ -42,6 +43,10 @@ public class LineDataSet extends LineRadarDataSet<Entry> implements ILineDataSet
4243

4344
private boolean mDrawCircleHole = true;
4445

46+
/** the drawable to be used for filling the line surface*/
47+
protected Drawable mFillDrawable;
48+
49+
4550
public LineDataSet(List<Entry> yVals, String label) {
4651
super(yVals, label);
4752

@@ -326,4 +331,32 @@ public void setFillFormatter(FillFormatter formatter) {
326331
public FillFormatter getFillFormatter() {
327332
return mFillFormatter;
328333
}
334+
335+
336+
/**
337+
* sets the color that is used for filling the line surface
338+
* disables filling with a drawable
339+
* @param color
340+
*/
341+
@Override
342+
public void setFillColor(int color) {
343+
super.setFillColor(color);
344+
mFillDrawable = null;
345+
}
346+
347+
/**
348+
* sets the drawable to be used for filling the line surface. The drawable is used
349+
* instead of the solid color
350+
* @param drawable
351+
*/
352+
@Override
353+
public void setFillDrawable(Drawable drawable) {
354+
mFillDrawable = drawable;
355+
}
356+
357+
@Override
358+
public Drawable getFillDrawable() {
359+
return mFillDrawable;
360+
}
361+
329362
}

‎MPChartLib/src/com/github/mikephil/charting/data/realm/implementation/RealmLineDataSet.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import android.content.Context;
44
import android.graphics.Color;
55
import android.graphics.DashPathEffect;
6+
import android.graphics.drawable.Drawable;
67

7-
import com.github.mikephil.charting.data.Entry;
88
import com.github.mikephil.charting.data.realm.base.RealmLineRadarDataSet;
99
import com.github.mikephil.charting.formatter.DefaultFillFormatter;
1010
import com.github.mikephil.charting.formatter.FillFormatter;
@@ -17,7 +17,6 @@
1717

1818
import io.realm.RealmObject;
1919
import io.realm.RealmResults;
20-
import io.realm.dynamic.DynamicRealmObject;
2120

2221
/**
2322
* Created by Philipp Jahoda on 21/10/15.
@@ -66,6 +65,8 @@ public class RealmLineDataSet<T extends RealmObject> extends RealmLineRadarDataS
6665

6766
private boolean mDrawCircleHole = true;
6867

68+
/** the drawable to be used for filling the line surface*/
69+
protected Drawable mFillDrawable;
6970

7071
/**
7172
* Constructor for creating a LineDataSet with realm data.
@@ -332,4 +333,15 @@ public void setFillFormatter(FillFormatter formatter) {
332333
public FillFormatter getFillFormatter() {
333334
return mFillFormatter;
334335
}
336+
337+
@Override
338+
public void setFillDrawable(Drawable drawable) {
339+
mFillDrawable = drawable;
340+
}
341+
342+
@Override
343+
public Drawable getFillDrawable() {
344+
return mFillDrawable;
345+
}
346+
335347
}

‎MPChartLib/src/com/github/mikephil/charting/interfaces/datasets/ILineDataSet.java‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.mikephil.charting.interfaces.datasets;
22

33
import android.graphics.DashPathEffect;
4+
import android.graphics.drawable.Drawable;
45

56
import com.github.mikephil.charting.data.Entry;
67
import com.github.mikephil.charting.formatter.FillFormatter;
@@ -81,4 +82,16 @@ public interface ILineDataSet extends ILineRadarDataSet<Entry> {
8182
* @return
8283
*/
8384
FillFormatter getFillFormatter();
84-
}
85+
86+
87+
/**
88+
* Sets the drawable to be used for filling
89+
* @param drawable
90+
*/
91+
void setFillDrawable(Drawable drawable);
92+
93+
/**
94+
* @return the drawable used for filling
95+
*/
96+
Drawable getFillDrawable();
97+
}

‎MPChartLib/src/com/github/mikephil/charting/renderer/LineChartRenderer.java‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.graphics.Color;
66
import android.graphics.Paint;
77
import android.graphics.Path;
8+
import android.graphics.drawable.Drawable;
89

910
import com.github.mikephil.charting.animation.ChartAnimator;
1011
import com.github.mikephil.charting.buffer.CircleBuffer;
@@ -85,7 +86,7 @@ public void drawData(Canvas c) {
8586

8687
if (width > 0 && height > 0) {
8788

88-
mDrawBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
89+
mDrawBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
8990
mBitmapCanvas = new Canvas(mDrawBitmap);
9091
} else
9192
return;
@@ -125,6 +126,22 @@ protected void drawDataSet(Canvas c, ILineDataSet dataSet) {
125126
mRenderPaint.setPathEffect(null);
126127
}
127128

129+
/**
130+
* Draws the provided path in filled mode with the provided drawable.
131+
*/
132+
protected void drawFilledPath(Canvas c, Path filledPath, Drawable drawable) {
133+
c.save();
134+
c.clipPath(filledPath);
135+
136+
drawable.setBounds((int) mViewPortHandler.contentLeft(),
137+
(int) mViewPortHandler.contentTop(),
138+
(int) mViewPortHandler.contentRight(),
139+
(int) mViewPortHandler.contentBottom());
140+
drawable.draw(c);
141+
142+
c.restore();
143+
}
144+
128145
/**
129146
* Draws a cubic line.
130147
*
@@ -252,6 +269,11 @@ protected void drawCubicFill(Canvas c, ILineDataSet dataSet, Path spline, Transf
252269

253270
trans.pathValueToPixel(spline);
254271

272+
final Drawable drawable = dataSet.getFillDrawable();
273+
if (dataSet.getFillDrawable() != null) {
274+
drawFilledPath(c, spline, drawable);
275+
return;
276+
}
255277
drawFilledPath(c, spline, dataSet.getFillColor(), dataSet.getFillAlpha());
256278
}
257279

@@ -349,6 +371,12 @@ protected void drawLinearFill(Canvas c, ILineDataSet dataSet, int minx,
349371

350372
trans.pathValueToPixel(filled);
351373

374+
final Drawable drawable = dataSet.getFillDrawable();
375+
if (drawable != null) {
376+
drawFilledPath(c, filled, drawable);
377+
return;
378+
}
379+
352380
drawFilledPath(c, filled, dataSet.getFillColor(), dataSet.getFillAlpha());
353381
}
354382

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ Features
9393
- **LineChart (cubic lines)**
9494
![alt tag](https://raw.github.com/PhilJay/MPChart/master/screenshots/cubiclinechart.png)
9595

96+
- **LineChart (gradient fill)**
97+
![alt tag](https://raw.github.com/PhilJay/MPChart/master/screenshots/line_chart_gradient.png)
98+
9699
- **Combined-Chart (bar- and linechart in this case)**
97100
![alt tag](https://raw.github.com/PhilJay/MPChart/master/screenshots/combined_chart.png)
98101

45.3 KB
Loading

0 commit comments

Comments
 (0)