Skip to content

Commit e4ba3cd

Browse files
committed
- multiple gradient color
1 parent a525088 commit e4ba3cd

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.annotation.SuppressLint;
55
import android.graphics.RectF;
66
import android.os.Bundle;
7+
import android.support.v4.content.ContextCompat;
78
import android.util.Log;
89
import android.view.Menu;
910
import android.view.MenuItem;
@@ -16,7 +17,6 @@
1617
import com.github.mikephil.charting.charts.BarChart;
1718
import com.github.mikephil.charting.components.Legend;
1819
import com.github.mikephil.charting.components.Legend.LegendForm;
19-
import com.github.mikephil.charting.components.Legend.LegendPosition;
2020
import com.github.mikephil.charting.components.XAxis;
2121
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
2222
import com.github.mikephil.charting.components.YAxis;
@@ -31,6 +31,7 @@
3131
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
3232
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
3333
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
34+
import com.github.mikephil.charting.model.GradientColor;
3435
import com.github.mikephil.charting.utils.ColorTemplate;
3536
import com.github.mikephil.charting.utils.MPPointF;
3637
import com.xxmassdeveloper.mpchartexample.custom.DayAxisValueFormatter;
@@ -39,6 +40,7 @@
3940
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
4041

4142
import java.util.ArrayList;
43+
import java.util.List;
4244

4345
public class BarChartActivity extends DemoBase implements OnSeekBarChangeListener,
4446
OnChartValueSelectedListener {
@@ -264,7 +266,31 @@ private void setData(int count, float range) {
264266

265267
set1.setDrawIcons(false);
266268

267-
set1.setColors(ColorTemplate.MATERIAL_COLORS);
269+
// set1.setColors(ColorTemplate.MATERIAL_COLORS);
270+
271+
/*int startColor = ContextCompat.getColor(this, android.R.color.holo_blue_dark);
272+
int endColor = ContextCompat.getColor(this, android.R.color.holo_blue_bright);
273+
set1.setGradientColor(startColor, endColor);*/
274+
275+
int startColor1 = ContextCompat.getColor(this, android.R.color.holo_orange_light);
276+
int startColor2 = ContextCompat.getColor(this, android.R.color.holo_blue_light);
277+
int startColor3 = ContextCompat.getColor(this, android.R.color.holo_orange_light);
278+
int startColor4 = ContextCompat.getColor(this, android.R.color.holo_green_light);
279+
int startColor5 = ContextCompat.getColor(this, android.R.color.holo_red_light);
280+
int endColor1 = ContextCompat.getColor(this, android.R.color.holo_blue_dark);
281+
int endColor2 = ContextCompat.getColor(this, android.R.color.holo_purple);
282+
int endColor3 = ContextCompat.getColor(this, android.R.color.holo_green_dark);
283+
int endColor4 = ContextCompat.getColor(this, android.R.color.holo_red_dark);
284+
int endColor5 = ContextCompat.getColor(this, android.R.color.holo_orange_dark);
285+
286+
List<GradientColor> gradientColors = new ArrayList<>();
287+
gradientColors.add(new GradientColor(startColor1, endColor1));
288+
gradientColors.add(new GradientColor(startColor2, endColor2));
289+
gradientColors.add(new GradientColor(startColor3, endColor3));
290+
gradientColors.add(new GradientColor(startColor4, endColor4));
291+
gradientColors.add(new GradientColor(startColor5, endColor5));
292+
293+
set1.setGradientColors(gradientColors);
268294

269295
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
270296
dataSets.add(set1);

MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.github.mikephil.charting.utils.ColorTemplate;
1414
import com.github.mikephil.charting.utils.MPPointF;
1515
import com.github.mikephil.charting.utils.Utils;
16+
import com.github.mikephil.charting.model.GradientColor;
1617

1718
import java.lang.annotation.Documented;
1819
import java.lang.annotation.Inherited;
@@ -31,6 +32,10 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
3132
*/
3233
protected List<Integer> mColors = null;
3334

35+
protected GradientColor gradientColor = null;
36+
37+
protected List<GradientColor> gradientColors = null;
38+
3439
/**
3540
* List representing all colors that are used for drawing the actual values for this DataSet
3641
*/
@@ -144,6 +149,21 @@ public int getColor(int index) {
144149
return mColors.get(index % mColors.size());
145150
}
146151

152+
@Override
153+
public GradientColor getGradientColor() {
154+
return gradientColor;
155+
}
156+
157+
@Override
158+
public List<GradientColor> getGradientColors() {
159+
return gradientColors;
160+
}
161+
162+
@Override
163+
public GradientColor getGradientColor(int index) {
164+
return gradientColors.get(index % gradientColors.size());
165+
}
166+
147167
/**
148168
* ###### ###### COLOR SETTING RELATED METHODS ##### ######
149169
*/
@@ -219,6 +239,25 @@ public void setColor(int color) {
219239
mColors.add(color);
220240
}
221241

242+
/**
243+
* Sets the start and end color for gradient color, ONLY color that should be used for this DataSet.
244+
*
245+
* @param startColor
246+
* @param endColor
247+
*/
248+
public void setGradientColor(int startColor, int endColor) {
249+
gradientColor = new GradientColor(startColor, endColor);
250+
}
251+
252+
/**
253+
* Sets the start and end color for gradient colors, ONLY color that should be used for this DataSet.
254+
*
255+
* @param gradientColors
256+
*/
257+
public void setGradientColors(List<GradientColor> gradientColors) {
258+
this.gradientColors = gradientColors;
259+
}
260+
222261
/**
223262
* Sets a color with a specific alpha value.
224263
*

MPChartLib/src/main/java/com/github/mikephil/charting/interfaces/datasets/IDataSet.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.github.mikephil.charting.data.Entry;
1111
import com.github.mikephil.charting.formatter.IValueFormatter;
1212
import com.github.mikephil.charting.utils.MPPointF;
13+
import com.github.mikephil.charting.model.GradientColor;
1314

1415
import java.util.List;
1516

@@ -285,6 +286,28 @@ public interface IDataSet<T extends Entry> {
285286
*/
286287
int getColor();
287288

289+
/**
290+
* Returns the Gradient color model
291+
*
292+
* @return
293+
*/
294+
GradientColor getGradientColor();
295+
296+
/**
297+
* Returns the Gradient colors
298+
*
299+
* @return
300+
*/
301+
List<GradientColor> getGradientColors();
302+
303+
/**
304+
* Returns the Gradient colors
305+
*
306+
* @param index
307+
* @return
308+
*/
309+
GradientColor getGradientColor(int index);
310+
288311
/**
289312
* Returns the color at the given index of the DataSet's color array.
290313
* Performs a IndexOutOfBounds check by modulus.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.mikephil.charting.model;
2+
3+
public class GradientColor {
4+
5+
private int startColor;
6+
private int endColor;
7+
8+
public GradientColor(int startColor, int endColor) {
9+
this.startColor = startColor;
10+
this.endColor = endColor;
11+
}
12+
13+
public int getStartColor() {
14+
return startColor;
15+
}
16+
17+
public void setStartColor(int startColor) {
18+
this.startColor = startColor;
19+
}
20+
21+
public int getEndColor() {
22+
return endColor;
23+
}
24+
25+
public void setEndColor(int endColor) {
26+
this.endColor = endColor;
27+
}
28+
}

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.github.mikephil.charting.utils.Transformer;
2020
import com.github.mikephil.charting.utils.Utils;
2121
import com.github.mikephil.charting.utils.ViewPortHandler;
22+
import android.graphics.LinearGradient;
23+
import com.github.mikephil.charting.model.GradientColor;
2224

2325
import java.util.List;
2426

@@ -163,6 +165,32 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
163165
mRenderPaint.setColor(dataSet.getColor(j / 4));
164166
}
165167

168+
if (dataSet.getGradientColor() != null) {
169+
GradientColor gradientColor = dataSet.getGradientColor();
170+
mRenderPaint.setShader(
171+
new LinearGradient(
172+
buffer.buffer[j],
173+
buffer.buffer[j + 3],
174+
buffer.buffer[j],
175+
buffer.buffer[j + 1],
176+
gradientColor.getStartColor(),
177+
gradientColor.getEndColor(),
178+
android.graphics.Shader.TileMode.MIRROR));
179+
}
180+
181+
if (dataSet.getGradientColors() != null) {
182+
mRenderPaint.setShader(
183+
new LinearGradient(
184+
buffer.buffer[j],
185+
buffer.buffer[j + 3],
186+
buffer.buffer[j],
187+
buffer.buffer[j + 1],
188+
dataSet.getGradientColor(j / 4).getStartColor(),
189+
dataSet.getGradientColor(j / 4).getEndColor(),
190+
android.graphics.Shader.TileMode.MIRROR));
191+
}
192+
193+
166194
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
167195
buffer.buffer[j + 3], mRenderPaint);
168196

0 commit comments

Comments
 (0)