个人作业阶段二 4
在xml文件中部署一个 echarts 的容器
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ShowActivity1"> <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:textSize="16sp" android:textColor="#000000" android:layout_marginHorizontal="20dp" android:gravity="center"/> <com.github.mikephil.charting.charts.BarChart android:id ="@+id/bar" android:layout_width="match_parent" android:layout_height="500dp" android:layout_below="@+id/text1" android:layout_marginTop="20dp"> </com.github.mikephil.charting.charts.BarChart> </RelativeLayout>
activity.java
package com.example.myapplication; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.WindowManager; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.components.Legend; import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.formatter.IValueFormatter; import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; import com.github.mikephil.charting.utils.ViewPortHandler; import org.litepal.LitePal; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ShowActivity1 extends AppCompatActivity { private BarChart barChart; private static List<String> list= new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 去除状态栏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_show1); TextView text1 = findViewById(R.id.text1); //所查询的日期传值 Intent intent = getIntent(); String TEXT = intent.getStringExtra("DATE"); List<Infomation>infomations = LitePal.findAll(Infomation.class); List<User>users = org.litepal.LitePal.findAll(User.class); for(Infomation infomation:infomations) { for (User user : users) { if(infomation.getZhu_date().equals(TEXT)){ list.add(user.getUser_class()); } } } list = joinList(list); initBarChart(); text1.setText(TEXT+"的各班级情况"); } //合并list中相同元素 private static List joinList(List list){ List list2= new ArrayList(); for(int i=0;i<list.size();i++){ Object obj = list.get(i); //如果当前元素不在list2中,则添加 if(list2.indexOf(obj)==-1){ list2.add(0,obj); } } Collections.reverse(list2); //倒序list return list2; } private void initBarChart(){ barChart = findViewById(R.id.bar); barChart.getDescription().setEnabled(false); // 不显示描述 barChart.setExtraOffsets(5, 5, 5, 5); // 设置图的偏移量,类似于内边距 ,设置视图窗口大小 setAxis(); // 设置坐标轴 setLegend(); // 设置图例 setData(); // 设置数据 } private void setData() { List<IBarDataSet> sets = new ArrayList<>(); List<BarEntry> barEntries1 = new ArrayList<>(); int[] num1 = new int[list.size()]; //存放各班级正常填报的人数 //获取填写得查询日期 Intent intent = getIntent(); String TEXT2 = intent.getStringExtra("DATE"); List<User> users = LitePal.findAll(User.class); for (int i = 0; i < list.size(); i++) { for (User user1 : users) { if (user1.getUser_class().equals(list.get(i))) { List<Infomation> infomations = LitePal.where("zhu_name=?", user1.getUser_name()).find(Infomation.class); for (Infomation infomation1 : infomations) { if (infomation1.getZhu_date().equals(TEXT2)) { num1[i]++; } } } } barEntries1.add(new BarEntry(i, (float) num1[i])); } BarDataSet barDataSet1 = new BarDataSet(barEntries1, "正常上报"); barDataSet1.setValueTextColor(Color.BLACK); // 值的颜色 barDataSet1.setValueTextSize(15f); // 值的大小 barDataSet1.setColor(Color.parseColor("#FFFF00")); // 柱子的颜色 //barDataSet1.setLabel("已填报"); // 设置标签之后,图例的内容默认会以设置的标签显示 // 设置柱子上数据显示的格式 barDataSet1.setValueFormatter(new IValueFormatter() { @Override public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { return (int) value + "人"; } }); sets.add(barDataSet1); List<BarEntry> barEntries2 = new ArrayList<>(); int[] num2 = new int[list.size()]; //存放各班级体温异常的人数 for (int i = 0; i < list.size(); i++) { for (User user2 : users) { if (user2.getUser_class().equals(list.get(i))) { List<Infomation> infomations = LitePal.where("zhu_name=?", user2.getUser_name()).find(Infomation.class); for (Infomation infomation2 : infomations) { if (infomation2.getZhu_date().equals(TEXT2) && Double.valueOf(infomation2.getZhu_tiwen()).intValue() > 37) { num2[i]++; } } } } barEntries2.add(new BarEntry((float) (i+0.3), (float) num2[i])); } BarDataSet barDataSet2 = new BarDataSet(barEntries2, "体温异常"); barDataSet2.setValueTextColor(Color.BLACK); // 值的颜色 barDataSet2.setValueTextSize(15f); // 值的大小 barDataSet2.setDrawValues(true); barDataSet2.setColor(Color.parseColor("#BBFFFF")); // 设置柱子上数据显示的格式 barDataSet2.setValueFormatter(new IValueFormatter() { @Override public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { return (int) value + "人"; } }); sets.add(barDataSet2); int[] num = new int [list.size()]; //存放各班级注册的总人数 List<Infomation> infomations1 = LitePal.where("zhu_date=?",TEXT2).find(Infomation.class); for(int i=0;i<list.size();i++){ for (User user4 : users) { if(user4.getUser_class().equals(list.get(i))){ num[i]++; } } } List<BarEntry> barEntries3 = new ArrayList<>(); int[] num3 = new int[list.size()]; //存放各班级没上报数据的人数 for (int i = 0; i < list.size(); i++) { for (User user3 : users) { if (user3.getUser_class().equals(list.get(i))){ num3[i] = num[i] - num1[i]; } } if(num3[i]<0){ num3[i]=0; } barEntries3.add(new BarEntry((float) (i + 0.6), (float) num3[i])); } BarDataSet barDataSet3 = new BarDataSet(barEntries3, "未上报体温"); barDataSet3.setValueTextColor(Color.BLACK); // 值的颜色 barDataSet3.setValueTextSize(15f); // 值的大小 barDataSet3.setColor(Color.parseColor("#FFE4E1")); // 柱子的颜色 // 设置柱子上数据显示的格式 barDataSet3.setValueFormatter(new IValueFormatter() { @Override public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { return (int) value + "人"; } }); sets.add(barDataSet3); BarData barData = new BarData(sets); barData.setBarWidth(0.3f); // 设置柱子的宽度 barChart.setData(barData); } private void setLegend() { Legend legend = barChart.getLegend(); legend.setFormSize(12f); // 图例的图形大小 legend.setTextSize(15f); // 图例的文字大小 legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); // 图例的方向为垂直 legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); //显示位置,水平右对齐 legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); // 显示位置,垂直上对齐 // 设置水平与垂直方向的偏移量 legend.setYOffset(55f); legend.setXOffset(30f); } private void setAxis() { // 设置x轴 XAxis xAxis = barChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置x轴显示在下方,默认在上方 xAxis.setDrawGridLines(false); // 将此设置为true,绘制该轴的网格线。 xAxis.setLabelCount(list.size()); // 设置x轴上的标签个数 xAxis.setTextSize(15f); // x轴上标签的大小 // 设置x轴每最小刻度 interval xAxis.setGranularity(1f); // 设置x轴显示的值的格式 xAxis.setValueFormatter((value, axis) -> { if ((int) value < list.size()) { return list.get((int) value); } else { return ""; } }); xAxis.setYOffset(5); // 设置标签对x轴的偏移量,垂直方向 // 设置y轴,y轴有两条,分别为左和右 YAxis yAxis_right = barChart.getAxisRight(); yAxis_right.setAxisMaximum(50f); // 设置y轴的最大值 yAxis_right.setAxisMinimum(0f); // 设置y轴的最小值 yAxis_right.setEnabled(false); // 不显示右边的y轴 YAxis yAxis_left = barChart.getAxisLeft(); yAxis_left.setAxisMaximum(50f); yAxis_left.setAxisMinimum(0f); yAxis_left.setTextSize(15f); // 设置y轴的标签大小 } }