使用MPAndroidChart在 安卓中绘制一张多线图,分别为温度,湿度,烟雾
时间: 2024-02-21 21:59:45 浏览: 28
下面是使用MPAndroidChart库在Android中绘制一张多线图的示例代码,分别为温度、湿度、烟雾:
首先,你需要在你的 `build.gradle` 文件中添加以下依赖项:
```gradle
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
```
然后,在你的布局文件中添加一个 `LineChart` 视图:
```xml
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
接下来,在你的 Java 文件中,获取 `LineChart` 对象并设置一些基本属性:
```java
// 获取 LineChart 对象
LineChart chart = findViewById(R.id.chart);
// 设置一些基本属性
chart.setDrawGridBackground(false);
chart.getDescription().setEnabled(false);
chart.setTouchEnabled(true);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
```
然后,你需要创建 `LineDataSet` 对象,并将其添加到 `LineData` 对象中:
```java
// 创建温度数据
List<Entry> temperatureEntries = new ArrayList<>();
temperatureEntries.add(new Entry(0, 22));
temperatureEntries.add(new Entry(1, 23));
temperatureEntries.add(new Entry(2, 24));
temperatureEntries.add(new Entry(3, 25));
temperatureEntries.add(new Entry(4, 26));
// 创建湿度数据
List<Entry> humidityEntries = new ArrayList<>();
humidityEntries.add(new Entry(0, 50));
humidityEntries.add(new Entry(1, 51));
humidityEntries.add(new Entry(2, 52));
humidityEntries.add(new Entry(3, 53));
humidityEntries.add(new Entry(4, 54));
// 创建烟雾数据
List<Entry> smokeEntries = new ArrayList<>();
smokeEntries.add(new Entry(0, 100));
smokeEntries.add(new Entry(1, 101));
smokeEntries.add(new Entry(2, 98));
smokeEntries.add(new Entry(3, 102));
smokeEntries.add(new Entry(4, 99));
// 创建温度 LineDataSet 对象
LineDataSet temperatureDataSet = new LineDataSet(temperatureEntries, "Temperature");
temperatureDataSet.setColor(Color.RED);
temperatureDataSet.setLineWidth(2f);
temperatureDataSet.setDrawCircles(false);
// 创建湿度 LineDataSet 对象
LineDataSet humidityDataSet = new LineDataSet(humidityEntries, "Humidity");
humidityDataSet.setColor(Color.BLUE);
humidityDataSet.setLineWidth(2f);
humidityDataSet.setDrawCircles(false);
// 创建烟雾 LineDataSet 对象
LineDataSet smokeDataSet = new LineDataSet(smokeEntries, "Smoke");
smokeDataSet.setColor(Color.GREEN);
smokeDataSet.setLineWidth(2f);
smokeDataSet.setDrawCircles(false);
// 将 LineDataSet 对象添加到 LineData 对象中
LineData lineData = new LineData(temperatureDataSet, humidityDataSet, smokeDataSet);
```
最后,将 `LineData` 对象设置到 `LineChart` 中:
```java
// 将 LineData 对象设置到 LineChart 中
chart.setData(lineData);
// 刷新视图
chart.invalidate();
```
这样你就可以在 Android 中绘制一张多线图,分别为温度、湿度、烟雾了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)