2

I'm using MPAndroidChart library to make charts in my app. I need to create chart like the following one. I need gradient line on LineChart and it's color must be based on values. For example Red for high, Yellow for medium, Green for low.

enter image description here

As far as I tried I managed to get like the following.

enter image description here

I searched for a day to find a solution, but I can't find any solution. So if any of you know how to fix this or have any idea about this please do share with me. Thanks.

0

2 Answers 2

4

Solution from codereview.stackexchange.com worked for me.

_

protected void drawCubicBezier(ILineDataSet dataSet) {
...
// Get screen coordinates for min Y value
MPPointD pixelForValues = trans.getPixelForValues(0, minDy);
minDy = (float) pixelForValues.y;
MPPointD.recycleInstance(pixelForValues);

// Get screen coordinates for max Y value
pixelForValues = trans.getPixelForValues(0, maxDy);
maxDy = (float) pixelForValues.y;
MPPointD.recycleInstance(pixelForValues);

// Get screen coordinates for 0 value
pixelForValues = trans.getPixelForValues(0, 0);
float zeroDy = (float) pixelForValues.y;
MPPointD.recycleInstance(pixelForValues);

float range = minDy - maxDy;
float zeroPointNormalized = Math.max((zeroDy - maxDy) / range, 0);

linearGradient = new LinearGradient(
        0, maxDy, 0, minDy,
        new int[]{Color.RED, Color.RED, Color.BLUE, Color.BLUE},
        new float[]{0, zeroPointNormalized, zeroPointNormalized, 1f},
        Shader.TileMode.REPEAT);
paint.setShader(linearGradient);
...
mBitmapCanvas.drawPath(cubicPath, paint);

}

Sign up to request clarification or add additional context in comments.

1 Comment

from where you invoked this method. can you share some more details.
0

Try William Chart and use this method:

int[] colors = { getResources().getColor(R.color.menu_text),
 getResources().getColor(android.R.color.white) };

float[] index = { 0, 1 };
dataset.setGradientFill(colors, index);

Edit 1: In MPChart, You can use

Paint paint = mChart.getRenderer().getPaintRender();
            paint.setShader(new LinearGradient(0, 0, 0, 40, Color.YELLOW, Color.RED, Shader.TileMode.REPEAT));

2 Comments

Will give a try. Thanks.
It only fills the area under the line. I need the line to be gradient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.