Saad Saadi
Saad Saadi

Reputation: 1061

How to fill LineDataSet in MPAndroidChart with LinearGradiant or Array of colors?

I am making one app in which i need to display visible spectrum (400nm to 780nm). I am using MPAndroidchart. I converted wavelength to color and rendered the spectrum. Below is the screenshot of the app. I can display the rendered spectrum on the background grid but how can i display in the lineDataset. LineDataSet only have one function SetFillColor(int). I want to fill the lineDataset with this Paint. This is my code.

Paint paint = new Paint();// = chart.setPaint();
int[] colors =new int[7];
float[] pos = {0.0f, 0.15f, 0.275f, 0.325f, 0.5f,0.6625f,1};
final float[] bands = { 380, 440, 490, 510, 580, 645, 780};
for(int i =0;i<bands.length;i++) {
colors[i]=Wavelength.wvColor(bands[i], gamma); 
//Wavelength.wvColor is the function which returns the `int`.
}
paint.setShader(new LinearGradient(0, 0, chart.getWidht(), 0, colors, pos, Shader.TileMode.CLAMP));

In chart, i can easily display it with the below code

chart.setPaint(paint, Chart.PAINT_GRID_BACKGROUND);

Question: How can i fill my LineDataset with the linearGradient or fill with array of colors?

enter image description here

Upvotes: 0

Views: 2249

Answers (1)

Ricardo
Ricardo

Reputation: 2291

You can find the solution here: https://github.com/PhilJay/MPAndroidChart/issues/1076

public class LineChartRenderer extends com.github.mikephil.charting.renderer.LineChartRenderer {

public LineChartRenderer(LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
    super(chart, animator, viewPortHandler);
}

@Override
protected void drawLinearFill(Canvas c, LineDataSet dataSet, List<Entry> entries, int minx, int maxx, Transformer trans) {

    mRenderPaint.setStyle(Paint.Style.FILL);

    mRenderPaint.setColor(dataSet.getFillColor());
    // filled is drawn with less alpha
    mRenderPaint.setAlpha(dataSet.getFillAlpha());

    Path filled = generateFilledPath(entries, dataSet.getFillFormatter().getFillLinePosition(dataSet, mChart), minx, maxx);

    trans.pathValueToPixel(filled);

    // GRADIENT BG - SET SHADER
    ALog.d(this, "drawLinearFill @LineChartRenderer - c.getHeight()=" + c.getHeight());
    mRenderPaint.setShader(new LinearGradient(0, 0, 0, c.getHeight(), AConstant.COLOR_CHART_LINE, AConstant.COLOR_CHART_BG,
            Shader.TileMode.CLAMP));

    c.drawPath(filled, mRenderPaint);

    // restore alpha
    mRenderPaint.setAlpha(255);

    // GRADIENT BG - REMOVE SHADER
    mRenderPaint.setShader(null);
}

/**
 * Generates the path that is used for filled drawing.
 * 
 * @param entries
 * @return
 */
private Path generateFilledPath(List<Entry> entries, float fillMin, int from, int to) {
    ALog.d(this, "generateFilledPath @LineChartRenderer");

    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();

    Path filled = new Path();
    filled.moveTo(entries.get(from).getXIndex(), fillMin);
    filled.lineTo(entries.get(from).getXIndex(), entries.get(from).getVal() * phaseY);

    // create a new path
    for (int x = from + 1, count = (int) Math.ceil((to - from) * phaseX + from); x < count; x++) {

        Entry e = entries.get(x);
        filled.lineTo(e.getXIndex(), e.getVal() * phaseY);
    }

    // close up
    filled.lineTo(entries.get(Math.max(Math.min((int) Math.ceil((to - from) * phaseX + from) - 1, entries.size() - 1), 0))
            .getXIndex(), fillMin);

    filled.close();

    return filled;
}

}

Upvotes: 1

Related Questions