首页 文章

MPAndroidChart填充颜色渐变

提问于
浏览
5

是否可以使用MPAndroidChart(或任何其他Android图表库)以渐变颜色填充绘制线下的图表?像这样的东西:

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

然后在 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>

2 回答

  • 2

    您可以使用以下两种方法:

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

    这是该类的javadoc的链接 .

    这是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);
    }
    

    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>
    

    以下是 LineChart 上的示例:

    a line chart with a red gradient fill

  • 8

    好的,我找到了一个解决方案:

    William Chart我正在使用这种方法:

    int[] colors = { getResources().getColor(R.color.menu_text),
     getResources().getColor(android.R.color.white) };
    
    float[] index = { 0, 1 };
    dataset.setGradientFill(colors, index);
    

相关问题