Android统计图
时间: 2023-11-19 08:57:41 浏览: 90
Android中绘制统计图可以使用第三方库MPAndroidChart,它支持多种类型的统计图,包括折线图、柱状图、饼图等等。下面是一个简单的柱状图的例子:
1.在build.gradle文件中添加依赖:
```gradle
dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}
```
2.在布局文件中添加一个BarChart控件:
```xml
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/bar_chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
3.在代码中设置数据和样式:
```java
BarChart barChart = findViewById(R.id.bar_chart);
List<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0, 10));
entries.add(new BarEntry(1, 20));
entries.add(new BarEntry(2, 30));
entries.add(new BarEntry(3, 40));
entries.add(new BarEntry(4, 50));
BarDataSet dataSet = new BarDataSet(entries, "Label");
dataSet.setColor(Color.BLUE);
BarData barData = new BarData(dataSet);
barData.setBarWidth(0.9f);
barChart.setData(barData);
barChart.setFitBars(true);
barChart.invalidate();
```
这段代码会生成一个包含5个柱形的柱状图,每个柱形的高度分别为10、20、30、40、50。
阅读全文
相关推荐
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)