Merge pull request #4001 from oatrice/feature/muti_gradient
[Feature] Multiple gradient color for barchart
This commit is contained in:
commit
c08ec81fc0
5 changed files with 146 additions and 2 deletions
|
@ -4,6 +4,7 @@ package com.xxmassdeveloper.mpchartexample;
|
|||
import android.annotation.SuppressLint;
|
||||
import android.graphics.RectF;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
@ -16,7 +17,6 @@ import android.widget.Toast;
|
|||
import com.github.mikephil.charting.charts.BarChart;
|
||||
import com.github.mikephil.charting.components.Legend;
|
||||
import com.github.mikephil.charting.components.Legend.LegendForm;
|
||||
import com.github.mikephil.charting.components.Legend.LegendPosition;
|
||||
import com.github.mikephil.charting.components.XAxis;
|
||||
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
|
||||
import com.github.mikephil.charting.components.YAxis;
|
||||
|
@ -31,6 +31,7 @@ import com.github.mikephil.charting.highlight.Highlight;
|
|||
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
|
||||
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
|
||||
import com.github.mikephil.charting.model.GradientColor;
|
||||
import com.github.mikephil.charting.utils.ColorTemplate;
|
||||
import com.github.mikephil.charting.utils.MPPointF;
|
||||
import com.xxmassdeveloper.mpchartexample.custom.DayAxisValueFormatter;
|
||||
|
@ -39,6 +40,7 @@ import com.xxmassdeveloper.mpchartexample.custom.XYMarkerView;
|
|||
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BarChartActivity extends DemoBase implements OnSeekBarChangeListener,
|
||||
OnChartValueSelectedListener {
|
||||
|
@ -264,7 +266,31 @@ public class BarChartActivity extends DemoBase implements OnSeekBarChangeListene
|
|||
|
||||
set1.setDrawIcons(false);
|
||||
|
||||
set1.setColors(ColorTemplate.MATERIAL_COLORS);
|
||||
// set1.setColors(ColorTemplate.MATERIAL_COLORS);
|
||||
|
||||
/*int startColor = ContextCompat.getColor(this, android.R.color.holo_blue_dark);
|
||||
int endColor = ContextCompat.getColor(this, android.R.color.holo_blue_bright);
|
||||
set1.setGradientColor(startColor, endColor);*/
|
||||
|
||||
int startColor1 = ContextCompat.getColor(this, android.R.color.holo_orange_light);
|
||||
int startColor2 = ContextCompat.getColor(this, android.R.color.holo_blue_light);
|
||||
int startColor3 = ContextCompat.getColor(this, android.R.color.holo_orange_light);
|
||||
int startColor4 = ContextCompat.getColor(this, android.R.color.holo_green_light);
|
||||
int startColor5 = ContextCompat.getColor(this, android.R.color.holo_red_light);
|
||||
int endColor1 = ContextCompat.getColor(this, android.R.color.holo_blue_dark);
|
||||
int endColor2 = ContextCompat.getColor(this, android.R.color.holo_purple);
|
||||
int endColor3 = ContextCompat.getColor(this, android.R.color.holo_green_dark);
|
||||
int endColor4 = ContextCompat.getColor(this, android.R.color.holo_red_dark);
|
||||
int endColor5 = ContextCompat.getColor(this, android.R.color.holo_orange_dark);
|
||||
|
||||
List<GradientColor> gradientColors = new ArrayList<>();
|
||||
gradientColors.add(new GradientColor(startColor1, endColor1));
|
||||
gradientColors.add(new GradientColor(startColor2, endColor2));
|
||||
gradientColors.add(new GradientColor(startColor3, endColor3));
|
||||
gradientColors.add(new GradientColor(startColor4, endColor4));
|
||||
gradientColors.add(new GradientColor(startColor5, endColor5));
|
||||
|
||||
set1.setGradientColors(gradientColors);
|
||||
|
||||
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
|
||||
dataSets.add(set1);
|
||||
|
|
|
@ -13,6 +13,7 @@ import com.github.mikephil.charting.interfaces.datasets.IDataSet;
|
|||
import com.github.mikephil.charting.utils.ColorTemplate;
|
||||
import com.github.mikephil.charting.utils.MPPointF;
|
||||
import com.github.mikephil.charting.utils.Utils;
|
||||
import com.github.mikephil.charting.model.GradientColor;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Inherited;
|
||||
|
@ -31,6 +32,10 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
|
|||
*/
|
||||
protected List<Integer> mColors = null;
|
||||
|
||||
protected GradientColor gradientColor = null;
|
||||
|
||||
protected List<GradientColor> gradientColors = null;
|
||||
|
||||
/**
|
||||
* List representing all colors that are used for drawing the actual values for this DataSet
|
||||
*/
|
||||
|
@ -144,6 +149,21 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
|
|||
return mColors.get(index % mColors.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
public GradientColor getGradientColor() {
|
||||
return gradientColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GradientColor> getGradientColors() {
|
||||
return gradientColors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GradientColor getGradientColor(int index) {
|
||||
return gradientColors.get(index % gradientColors.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* ###### ###### COLOR SETTING RELATED METHODS ##### ######
|
||||
*/
|
||||
|
@ -219,6 +239,25 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
|
|||
mColors.add(color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the start and end color for gradient color, ONLY color that should be used for this DataSet.
|
||||
*
|
||||
* @param startColor
|
||||
* @param endColor
|
||||
*/
|
||||
public void setGradientColor(int startColor, int endColor) {
|
||||
gradientColor = new GradientColor(startColor, endColor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the start and end color for gradient colors, ONLY color that should be used for this DataSet.
|
||||
*
|
||||
* @param gradientColors
|
||||
*/
|
||||
public void setGradientColors(List<GradientColor> gradientColors) {
|
||||
this.gradientColors = gradientColors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a color with a specific alpha value.
|
||||
*
|
||||
|
|
|
@ -10,6 +10,7 @@ import com.github.mikephil.charting.data.DataSet;
|
|||
import com.github.mikephil.charting.data.Entry;
|
||||
import com.github.mikephil.charting.formatter.IValueFormatter;
|
||||
import com.github.mikephil.charting.utils.MPPointF;
|
||||
import com.github.mikephil.charting.model.GradientColor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -285,6 +286,28 @@ public interface IDataSet<T extends Entry> {
|
|||
*/
|
||||
int getColor();
|
||||
|
||||
/**
|
||||
* Returns the Gradient color model
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
GradientColor getGradientColor();
|
||||
|
||||
/**
|
||||
* Returns the Gradient colors
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<GradientColor> getGradientColors();
|
||||
|
||||
/**
|
||||
* Returns the Gradient colors
|
||||
*
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
GradientColor getGradientColor(int index);
|
||||
|
||||
/**
|
||||
* Returns the color at the given index of the DataSet's color array.
|
||||
* Performs a IndexOutOfBounds check by modulus.
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.github.mikephil.charting.model;
|
||||
|
||||
public class GradientColor {
|
||||
|
||||
private int startColor;
|
||||
private int endColor;
|
||||
|
||||
public GradientColor(int startColor, int endColor) {
|
||||
this.startColor = startColor;
|
||||
this.endColor = endColor;
|
||||
}
|
||||
|
||||
public int getStartColor() {
|
||||
return startColor;
|
||||
}
|
||||
|
||||
public void setStartColor(int startColor) {
|
||||
this.startColor = startColor;
|
||||
}
|
||||
|
||||
public int getEndColor() {
|
||||
return endColor;
|
||||
}
|
||||
|
||||
public void setEndColor(int endColor) {
|
||||
this.endColor = endColor;
|
||||
}
|
||||
}
|
|
@ -19,6 +19,8 @@ import com.github.mikephil.charting.utils.MPPointF;
|
|||
import com.github.mikephil.charting.utils.Transformer;
|
||||
import com.github.mikephil.charting.utils.Utils;
|
||||
import com.github.mikephil.charting.utils.ViewPortHandler;
|
||||
import android.graphics.LinearGradient;
|
||||
import com.github.mikephil.charting.model.GradientColor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -163,6 +165,32 @@ public class BarChartRenderer extends BarLineScatterCandleBubbleRenderer {
|
|||
mRenderPaint.setColor(dataSet.getColor(j / 4));
|
||||
}
|
||||
|
||||
if (dataSet.getGradientColor() != null) {
|
||||
GradientColor gradientColor = dataSet.getGradientColor();
|
||||
mRenderPaint.setShader(
|
||||
new LinearGradient(
|
||||
buffer.buffer[j],
|
||||
buffer.buffer[j + 3],
|
||||
buffer.buffer[j],
|
||||
buffer.buffer[j + 1],
|
||||
gradientColor.getStartColor(),
|
||||
gradientColor.getEndColor(),
|
||||
android.graphics.Shader.TileMode.MIRROR));
|
||||
}
|
||||
|
||||
if (dataSet.getGradientColors() != null) {
|
||||
mRenderPaint.setShader(
|
||||
new LinearGradient(
|
||||
buffer.buffer[j],
|
||||
buffer.buffer[j + 3],
|
||||
buffer.buffer[j],
|
||||
buffer.buffer[j + 1],
|
||||
dataSet.getGradientColor(j / 4).getStartColor(),
|
||||
dataSet.getGradientColor(j / 4).getEndColor(),
|
||||
android.graphics.Shader.TileMode.MIRROR));
|
||||
}
|
||||
|
||||
|
||||
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
|
||||
buffer.buffer[j + 3], mRenderPaint);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue