2015-10-02 267 views
2

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

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> 
+0

任何更新? MPAndroidChart是否包含任何新版本中的渐变填充颜色功能? – VladimirVip

+1

@VladimirVip请检查我的回答 –

+0

@DavidRawson非常感谢你,这回答了这个问题! – VladimirVip

回答

2

好吧,我已经找到了解决办法:

William Chart,我用这个方法:

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

float[] index = { 0, 1 }; 
dataset.setGradientFill(colors, index); 
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