Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart fill color gradient

Is it possible with MPAndroidChart (or any other Android chart library) to fill the chart under the draw line with a gradient color? Something like this:

set1.setFillColor(getResources().getColor(R.drawable.chart_fill));

then in chart_fill.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="#FF207a54"
        android:startColor="#FFffffff" />

</shape>
like image 407
David Avatar asked Oct 02 '15 12:10

David


3 Answers

You can use the following two methods:

LineRadarDataSet#setDrawFilled(boolean isFilled); 
LineRadarDataSet#setFillDrawable(Drawable d);

Here's a link to the javadoc for that class.

This is the example from the MPAndroidChart sample project:

set1.setDrawFilled(true);
if (Utils.getSDKInt() >= 18) {
    // fill drawable only supported on api level 18 and above
    Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
    set1.setFillDrawable(drawable);
}
else {
    set1.setFillColor(Color.BLACK);
}

In fade_red.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:startColor="#00ff0000"
        android:endColor="#ffff0000" />
</shape>

Here's a sample of what it looks like on a LineChart:

a line chart with a red gradient fill

like image 107
David Rawson Avatar answered Oct 09 '22 17:10

David Rawson


Kotlin Solution:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    lineDataSet.setDrawFilled(true)
    val fillGradient = ContextCompat.getDrawable(requireContext(), R.drawable.red_gradient)
    lineDataSet.fillDrawable = fillGradient
}

Here is the red_gradient.xml file:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:startColor="#00F81616"
        android:endColor="#ffF81616" />
</shape>
like image 33
Md. Yamin Mollah Avatar answered Oct 09 '22 18:10

Md. Yamin Mollah


Okay i have found a solution:

William Chart and i am using this method:

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

float[] index = { 0, 1 };
dataset.setGradientFill(colors, index);
like image 43
David Avatar answered Oct 09 '22 18:10

David