android 饼图代码
时间: 2023-10-03 10:06:54 浏览: 55
以下是一个简单的Android饼图代码示例,使用MPAndroidChart库来实现:
1. 添加依赖
在项目的build.gradle文件中添加以下依赖:
```
dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}
```
2. 在布局文件中添加PieChart视图
```
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp" />
```
3. 在Activity或Fragment中配置饼图数据和属性
```
PieChart chart = findViewById(R.id.chart);
List<PieEntry> entries = new ArrayList<>();
entries.add(new PieEntry(20, "Item 1"));
entries.add(new PieEntry(30, "Item 2"));
entries.add(new PieEntry(50, "Item 3"));
PieDataSet dataSet = new PieDataSet(entries, "Pie Chart");
dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
dataSet.setValueTextSize(16f);
dataSet.setValueTextColor(Color.WHITE);
PieData data = new PieData(dataSet);
chart.setData(data);
chart.getDescription().setEnabled(false);
chart.setDrawHoleEnabled(false);
chart.setUsePercentValues(true);
chart.animateY(1000);
chart.invalidate();
```
上述代码创建了一个包含三个数据项的饼图,使用COLORFUL_COLORS来设置颜色,设置了数据项文本的大小和颜色,以及禁用了描述、饼图中心空洞和使用百分比。最后,通过调用animateY()方法和invalidate()方法来启用动画和刷新图表。
以上是一个简单的Android饼图代码示例,你可以根据自己的需求进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)