Add feature to allow setting different colors for values (#1389, ...)
This commit is contained in:
parent
9615cc2af1
commit
31de4f2a29
11 changed files with 49 additions and 31 deletions
|
@ -107,7 +107,7 @@ public class BarChartPositiveNegative extends DemoBase {
|
|||
BarDataSet set = new BarDataSet(values, "Values");
|
||||
set.setBarSpacePercent(40f);
|
||||
set.setColors(colors);
|
||||
set.setValueTextColor(red);
|
||||
set.setValueTextColors(colors);
|
||||
|
||||
BarData data = new BarData(dates, set);
|
||||
data.setValueTextSize(13f);
|
||||
|
|
|
@ -26,6 +26,11 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
|
|||
*/
|
||||
protected List<Integer> mColors = null;
|
||||
|
||||
/**
|
||||
* List representing all colors that are used for drawing the actual values for this DataSet
|
||||
*/
|
||||
protected List<Integer> mValueColors = null;
|
||||
|
||||
/**
|
||||
* label that describes the DataSet or the data the DataSet represents
|
||||
*/
|
||||
|
@ -46,11 +51,6 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
|
|||
*/
|
||||
protected transient ValueFormatter mValueFormatter;
|
||||
|
||||
/**
|
||||
* the color used for the value-text
|
||||
*/
|
||||
protected int mValueColor = Color.BLACK;
|
||||
|
||||
/**
|
||||
* the typeface used for the value text
|
||||
*/
|
||||
|
@ -76,9 +76,11 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
|
|||
*/
|
||||
public BaseDataSet() {
|
||||
mColors = new ArrayList<Integer>();
|
||||
mValueColors = new ArrayList<Integer>();
|
||||
|
||||
// default color
|
||||
mColors.add(Color.rgb(140, 234, 255));
|
||||
mValueColors.add(Color.BLACK);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -261,7 +263,13 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
|
|||
|
||||
@Override
|
||||
public void setValueTextColor(int color) {
|
||||
mValueColor = color;
|
||||
mValueColors.clear();
|
||||
mValueColors.add(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValueTextColors(List<Integer> colors) {
|
||||
mValueColors = colors;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -275,8 +283,8 @@ public abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int getValueTextColor() {
|
||||
return mValueColor;
|
||||
public int getValueTextColor(int index) {
|
||||
return mValueColors.get(index % mValueColors.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -288,6 +288,13 @@ public interface IDataSet<T extends Entry> {
|
|||
*/
|
||||
void setValueTextColor(int color);
|
||||
|
||||
/**
|
||||
* Sets a list of colors to be used as the colors for the drawn values.
|
||||
*
|
||||
* @param colors
|
||||
*/
|
||||
void setValueTextColors(List<Integer> colors);
|
||||
|
||||
/**
|
||||
* Sets a Typeface for the value-labels of this DataSet.
|
||||
*
|
||||
|
@ -303,11 +310,13 @@ public interface IDataSet<T extends Entry> {
|
|||
void setValueTextSize(float size);
|
||||
|
||||
/**
|
||||
* Returns the color that is used for drawing the values inside the chart
|
||||
* Returns the color at the specified index that is used for drawing the values inside the chart.
|
||||
* Uses modulus internally.
|
||||
*
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
int getValueTextColor();
|
||||
int getValueTextColor(int index);
|
||||
|
||||
/**
|
||||
* Returns the typeface that is used for drawing the values inside the chart
|
||||
|
|
|
@ -223,7 +223,7 @@ public class BarChartRenderer extends DataRenderer {
|
|||
float val = entry.getVal();
|
||||
|
||||
drawValue(c, dataSet.getValueFormatter(), val, entry, i, valuePoints[j],
|
||||
valuePoints[j + 1] + (val >= 0 ? posOffset : negOffset));
|
||||
valuePoints[j + 1] + (val >= 0 ? posOffset : negOffset), dataSet.getValueTextColor(j / 2));
|
||||
}
|
||||
|
||||
// if we have stacks
|
||||
|
@ -248,11 +248,13 @@ public class BarChartRenderer extends DataRenderer {
|
|||
continue;
|
||||
|
||||
drawValue(c, dataSet.getValueFormatter(), entry.getVal(), entry, i, valuePoints[j],
|
||||
valuePoints[j + 1] + (entry.getVal() >= 0 ? posOffset : negOffset));
|
||||
valuePoints[j + 1] + (entry.getVal() >= 0 ? posOffset : negOffset), dataSet.getValueTextColor(j / 2));
|
||||
|
||||
// draw stack values
|
||||
} else {
|
||||
|
||||
int color = dataSet.getValueTextColor(j / 2);
|
||||
|
||||
float[] transformed = new float[vals.length * 2];
|
||||
|
||||
float posY = 0f;
|
||||
|
@ -289,7 +291,7 @@ public class BarChartRenderer extends DataRenderer {
|
|||
|| !mViewPortHandler.isInBoundsLeft(x))
|
||||
continue;
|
||||
|
||||
drawValue(c, dataSet.getValueFormatter(), vals[k / 2], entry, i, x, y);
|
||||
drawValue(c, dataSet.getValueFormatter(), vals[k / 2], entry, i, x, y, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -141,13 +141,6 @@ public class BubbleChartRenderer extends DataRenderer {
|
|||
final float phaseX = mAnimator.getPhaseX();
|
||||
final float phaseY = mAnimator.getPhaseY();
|
||||
|
||||
final float alpha = phaseX == 1 ? phaseY : phaseX;
|
||||
int valueTextColor = dataSet.getValueTextColor();
|
||||
valueTextColor = Color.argb(Math.round(255.f * alpha), Color.red(valueTextColor),
|
||||
Color.green(valueTextColor), Color.blue(valueTextColor));
|
||||
|
||||
mValuePaint.setColor(valueTextColor);
|
||||
|
||||
BubbleEntry entryFrom = dataSet.getEntryForXIndex(mMinX);
|
||||
BubbleEntry entryTo = dataSet.getEntryForXIndex(mMaxX);
|
||||
|
||||
|
@ -159,6 +152,11 @@ public class BubbleChartRenderer extends DataRenderer {
|
|||
|
||||
for (int j = 0; j < positions.length; j += 2) {
|
||||
|
||||
final float alpha = phaseX == 1 ? phaseY : phaseX;
|
||||
int valueTextColor = dataSet.getValueTextColor(j / 2);
|
||||
valueTextColor = Color.argb(Math.round(255.f * alpha), Color.red(valueTextColor),
|
||||
Color.green(valueTextColor), Color.blue(valueTextColor));
|
||||
|
||||
float x = positions[j];
|
||||
float y = positions[j + 1];
|
||||
|
||||
|
@ -171,7 +169,7 @@ public class BubbleChartRenderer extends DataRenderer {
|
|||
BubbleEntry entry = dataSet.getEntryForIndex(j / 2 + minx);
|
||||
|
||||
drawValue(c, dataSet.getValueFormatter(), entry.getSize(), entry, i, x,
|
||||
y + (0.5f * lineHeight));
|
||||
y + (0.5f * lineHeight), valueTextColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -217,7 +217,7 @@ public class CandleStickChartRenderer extends LineScatterCandleRadarRenderer {
|
|||
|
||||
CandleEntry entry = dataSet.getEntryForIndex(j / 2 + minx);
|
||||
|
||||
drawValue(c, dataSet.getValueFormatter(), entry.getHigh(), entry, i, x, y - yOffset);
|
||||
drawValue(c, dataSet.getValueFormatter(), entry.getHigh(), entry, i, x, y - yOffset, dataSet.getValueTextColor(j / 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,10 +9,10 @@ import android.graphics.Paint.Style;
|
|||
|
||||
import com.github.mikephil.charting.animation.ChartAnimator;
|
||||
import com.github.mikephil.charting.data.Entry;
|
||||
import com.github.mikephil.charting.formatter.ValueFormatter;
|
||||
import com.github.mikephil.charting.highlight.Highlight;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
|
||||
import com.github.mikephil.charting.utils.Utils;
|
||||
import com.github.mikephil.charting.formatter.ValueFormatter;
|
||||
import com.github.mikephil.charting.utils.ViewPortHandler;
|
||||
|
||||
/**
|
||||
|
@ -102,7 +102,6 @@ public abstract class DataRenderer extends Renderer {
|
|||
*/
|
||||
protected void applyValueTextStyle(IDataSet set) {
|
||||
|
||||
mValuePaint.setColor(set.getValueTextColor());
|
||||
mValuePaint.setTypeface(set.getValueTypeface());
|
||||
mValuePaint.setTextSize(set.getValueTextSize());
|
||||
}
|
||||
|
@ -138,8 +137,10 @@ public abstract class DataRenderer extends Renderer {
|
|||
* @param dataSetIndex the index of the DataSet the drawn Entry belongs to
|
||||
* @param x position
|
||||
* @param y position
|
||||
* @param color
|
||||
*/
|
||||
public void drawValue(Canvas c, ValueFormatter formatter, float value, Entry entry, int dataSetIndex, float x, float y) {
|
||||
public void drawValue(Canvas c, ValueFormatter formatter, float value, Entry entry, int dataSetIndex, float x, float y, int color) {
|
||||
mValuePaint.setColor(color);
|
||||
c.drawText(formatter.getFormattedValue(value, entry, dataSetIndex, mViewPortHandler), x, y, mValuePaint);
|
||||
}
|
||||
|
||||
|
|
|
@ -461,7 +461,7 @@ public class LineChartRenderer extends LineRadarRenderer {
|
|||
Entry entry = dataSet.getEntryForIndex(j / 2 + minx);
|
||||
|
||||
drawValue(c, dataSet.getValueFormatter(), entry.getVal(), entry, i, x,
|
||||
y - valOffset);
|
||||
y - valOffset, dataSet.getValueTextColor(j / 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -244,7 +244,7 @@ public class PieChartRenderer extends DataRenderer {
|
|||
// draw everything, depending on settings
|
||||
if (drawXVals && drawYVals) {
|
||||
|
||||
drawValue(c, formatter, value, entry, 0, x, y);
|
||||
drawValue(c, formatter, value, entry, 0, x, y, dataSet.getValueTextColor(j));
|
||||
|
||||
if (j < data.getXValCount())
|
||||
c.drawText(data.getXVals().get(j), x, y + lineHeight,
|
||||
|
@ -255,7 +255,7 @@ public class PieChartRenderer extends DataRenderer {
|
|||
c.drawText(data.getXVals().get(j), x, y + lineHeight / 2f, mValuePaint);
|
||||
} else if (drawYVals) {
|
||||
|
||||
drawValue(c, formatter, value, entry, 0, x, y + lineHeight / 2f);
|
||||
drawValue(c, formatter, value, entry, 0, x, y + lineHeight / 2f, dataSet.getValueTextColor(j));
|
||||
}
|
||||
|
||||
xIndex++;
|
||||
|
|
|
@ -162,7 +162,7 @@ public class RadarChartRenderer extends LineRadarRenderer {
|
|||
(entry.getVal() - mChart.getYChartMin()) * factor * phaseY,
|
||||
sliceangle * j * phaseX + mChart.getRotationAngle());
|
||||
|
||||
drawValue(c, dataSet.getValueFormatter(), entry.getVal(), entry, i, p.x, p.y - yoffset);
|
||||
drawValue(c, dataSet.getValueFormatter(), entry.getVal(), entry, i, p.x, p.y - yoffset, dataSet.getValueTextColor(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -232,7 +232,7 @@ public class ScatterChartRenderer extends LineScatterCandleRadarRenderer {
|
|||
Entry entry = dataSet.getEntryForIndex(j / 2);
|
||||
|
||||
drawValue(c, dataSet.getValueFormatter(), entry.getVal(), entry, i, positions[j],
|
||||
positions[j + 1] - shapeSize);
|
||||
positions[j + 1] - shapeSize, dataSet.getValueTextColor(j / 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue