How to change the background of the chart using the MPAndroidChart library?

I use the awesome MPAndroidChart library. It works like a charm, except when I try to change the background color of BarData. The default color is white, and I want to change it to transparent.

I 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 doesn't seem to work.

Any ideas?

Thanks =)

+5
source share
4 answers

Since release v1.6.5 , the Chart background is transparent by default. This means that everything in the background (the background of the chart, other Views , etc.) that are not overlapping with the data pulled into the Chart will be visible.

If you want to change the background (color or possibly paint), you can either do this by changing the background

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

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

+6
source

Code to change Background color:

 chart.setBackgroundColor(Color.TRANSPARENT); //set whatever color you prefer chart.setDrawGridBackground(false);// this is a must 
+2
source

if you want to change the background color of the whole screen

  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

+1
source

So, after some research, I found that this is not yet possible: https://github.com/PhilJay/MPAndroidChart/issues/53

I hope soon! =)

0
source

Source: https://habr.com/ru/post/1204046/


All Articles