Skip to content

Commit

Permalink
Merge pull request PhilJay#4001 from oatrice/feature/muti_gradient
Browse files Browse the repository at this point in the history
[Feature] Multiple gradient color for barchart
  • Loading branch information
almic authored May 5, 2018
2 parents 689d484 + e4ba3cd commit c08ec81
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand All @@ -16,7 +17,6 @@
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;
Expand All @@ -31,6 +31,7 @@
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;
Expand All @@ -39,6 +40,7 @@
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

import java.util.ArrayList;
import java.util.List;

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

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -144,6 +149,21 @@ public int getColor(int index) {
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 ##### ######
*/
Expand Down Expand Up @@ -219,6 +239,25 @@ public void setColor(int color) {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
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;

Expand Down Expand Up @@ -163,6 +165,32 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
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);

Expand Down

0 comments on commit c08ec81

Please sign in to comment.