Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] set gradient color for bar chart #3759

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Down Expand Up @@ -265,6 +265,9 @@ private void setData(int count, float range) {
set1.setDrawIcons(false);

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);

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,8 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
*/
protected List<Integer> mColors = null;

protected GradientColor gradientColor = null;

/**
* List representing all colors that are used for drawing the actual values for this DataSet
*/
Expand Down Expand Up @@ -139,6 +142,11 @@ public int getColor() {
return mColors.get(0);
}

@Override
public GradientColor getGradientColor() {
return gradientColor;
}

@Override
public int getColor(int index) {
return mColors.get(index % mColors.size());
Expand Down Expand Up @@ -219,6 +227,16 @@ public void setColor(int color) {
mColors.add(color);
}

/**
* Sets the start and end color for gradient colot, 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 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,13 @@ public interface IDataSet<T extends Entry> {
*/
int getColor();

/**
* Returns the Gradient color model
*
* @return
*/
GradientColor getGradientColor();

/**
* 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,20 @@ 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));
}


c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
buffer.buffer[j + 3], mRenderPaint);

Expand Down