How to change chart background with MPAndroidChart

2020-04-03 12:54发布

I'm using the amazing library MPAndroidChart. It works like a charm, except when I'm trying to change the background color of the BarData. Default color is white, and I want to change it to Transparent.

I've tried this :

Paint p1 = mChart.getPaint(Chart.PAINT_GRID_BACKGROUND);
p1.setColor(Color.RED);

and this:

<com.github.mikephil.charting.charts.BarChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"/>

... but it seems that doesnt works.

Any ideas ?

Thanks =)

4条回答
不美不萌又怎样
2楼-- · 2020-04-03 13:12

Since release v1.6.5, the background of the Chart is transparent by default. Meaning, that everything in the background (chart background, other Views, etc.) that is not overlayed by data drawn into the Chart, will be visible.

If you want to change the background (color, or maybe drawable), you can either do that by changing the chart-background

  • in .xml (android:background="...")
  • by calling setBackgroundColor(...) or setBackgroundResource(...)

Another way could be to change the background of the parent layout that contains the Chart.

查看更多
Fickle 薄情
3楼-- · 2020-04-03 13:12

if you want to Change Whole Screen Background Color

 Barchart chart;
 chart.setBackgroundColor(Color.rgb(0, 0, 0));//Set as a black
 chart.setDrawGridBackground(false);//set this to true to draw the grid background, false if not

Happy to help Thanks

查看更多
该账号已被封号
4楼-- · 2020-04-03 13:20

Code to change Background color:

chart.setBackgroundColor(Color.TRANSPARENT); //set whatever color you prefer
chart.setDrawGridBackground(false);// this is a must 
查看更多
Evening l夕情丶
5楼-- · 2020-04-03 13:24

So, after some research, I've found that it's not possible yet: https://github.com/PhilJay/MPAndroidChart/issues/53

Hope It will be possible soon ! =)

查看更多
登录 后发表回答