如何使用MPAndroidChart库更改图表背景?

8

我正在使用令人惊叹的库MPAndroidChart。它的表现十分出色,但是当我尝试更改BarData的背景颜色时遇到了问题。默认颜色是白色,而我想要将其改为透明。

我已经尝试过以下方法:

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

以及这个:

<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"/>

...但似乎不起作用。

有什么想法吗?

谢谢=)

4个回答

14

自从v1.6.5版本以来,Chart的背景默认是透明的。这意味着,背景中的所有内容(图表背景,其他Views等)如果不被绘制到Chart上,则可见。

如果您想更改背景(颜色或者图片),可以通过以下方式之一:

  • 在.xml文件中更改chart-background(android:background="...")
  • 调用setBackgroundColor(...) 或者 setBackgroundResource(...)

另一种方法是更改包含Chart的父布局的背景。


我只是在测试你们的新版本,透明背景效果非常好!再次感谢。 - Phil
@Philipp 我们能否仅为范围设置背景。比如-30及以下为红色,-10以下为黄色,10以上为黄色,30以上为红色。 - Prasad

3

更改背景颜色的代码:

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

1

如果您想要更改整个屏幕的背景颜色

 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

很高兴能帮忙,谢谢。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接