How to use doubao Write CUI corrosion rate APP

Publish Time: Author: Site Editor Visit: 30

1. 项目准备

 

build.gradle(Module: app)中添加 MPAndroidChart 库依赖:

 

收起

groovy

dependencies {
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}

2. 布局文件(activity_main.xml)

 

收起

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/title_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保温层ER探头腐蚀率"
        android:textSize="20sp"
        android:gravity="center"
        android:padding="16dp"/>

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/line_chart"
        android:layout_width="match_parent"
        android:layout_height="300dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="16dp">

        <Button
            android:id="@+id/refresh_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="刷新"/>

        <Button
            android:id="@+id/alarm_status_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="报警状况"/>
    </LinearLayout>

</LinearLayout>

3. 主活动代码(MainActivity.java)

 

收起

java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private LineChart lineChart;
    private Button refreshButton;
    private Button alarmStatusButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lineChart = findViewById(R.id.line_chart);
        refreshButton = findViewById(R.id.refresh_button);
        alarmStatusButton = findViewById(R.id.alarm_status_button);

        // 初始化图表数据
        setupChartData();

        refreshButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 这里可以添加刷新数据逻辑,比如重新获取数据并更新图表
                setupChartData();
            }
        });

        alarmStatusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 这里添加跳转到报警状况页面等逻辑
            }
        });
    }

    private void setupChartData() {
        // 示例数据
        List<Entry> entryList1 = new ArrayList<>();
        entryList1.add(new Entry(0, 1));
        entryList1.add(new Entry(1, 1));
        entryList1.add(new Entry(2, 1));
        entryList1.add(new Entry(3, 1));
        entryList1.add(new Entry(4, 1));
        entryList1.add(new Entry(5, 0.9));
        entryList1.add(new Entry(6, 1.8));
        entryList1.add(new Entry(7, 2));

        List<Entry> entryList2 = new ArrayList<>();
        entryList2.add(new Entry(0, 0.9));
        entryList2.add(new Entry(1, 0.9));
        entryList2.add(new Entry(2, 0.9));
        entryList2.add(new Entry(3, 0.9));
        entryList2.add(new Entry(4, 0.9));
        entryList2.add(new Entry(5, 0.8));
        entryList2.add(new Entry(6, 1.6));
        entryList2.add(new Entry(7, 1.9));

        LineDataSet dataSet1 = new LineDataSet(entryList1, "腐蚀率曲线1");
        dataSet1.setColor(getResources().getColor(R.color.red));
        dataSet1.setValueTextColor(getResources().getColor(R.color.red));

        LineDataSet dataSet2 = new LineDataSet(entryList2, "腐蚀率曲线2");
        dataSet2.setColor(getResources().getColor(R.color.blue));
        dataSet2.setValueTextColor(getResources().getColor(R.color.blue));

        LineData lineData = new LineData(dataSet1, dataSet2);
        lineChart.setData(lineData);

        // 设置X轴标签
        XAxis xAxis = lineChart.getXAxis();
        xAxis.setValueFormatter(new ValueFormatter() {
            private String[] months = {"3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月"};
            @Override
            public String getAxisLabel(float value, AxisBase axis) {
                return months[(int) value];
            }
        });

        lineChart.invalidate();
    }
}
Recommend Products