要求:
要求每人录入15天的体温数据, 以折线图或柱状图展示个人15天的体温变化情况。
我采用的是MPAndroidChart。
一、过程
1、首先将MPAndroidChart的jar包导入工程中
2、绘制xml布局代码
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="@mipmap/bj3"> 7 8 <com.github.mikephil.charting.charts.BarChart 9 android:id="@+id/show_chart" 10 android:layout_width="match_parent" 11 android:layout_height="500dp" 12 > 13 14 </com.github.mikephil.charting.charts.BarChart> 15 </LinearLayout>
3、编写java代码
思路:
设置外观
1 private void setLegend() { 2 Legend legend = barChart.getLegend(); 3 legend.setFormSize(12f); 4 legend.setTextSize(15f); 5 legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); 6 legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); 7 legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); 8 legend.setYOffset(55f); 9 legend.setXOffset(30f); 10 }
设置坐标轴
private void setAxis() { // 设置x轴 XAxis xAxis = barChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawGridLines(false); xAxis.setLabelCount(list1.size());list1.size() xAxis.setTextSize(12f); xAxis.setGranularity(1f); xAxis.setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float value, AxisBase axis) { if ((int) value < list1.size()) { return list1.get((int) value); } else { return ""; } } }); xAxis.setYOffset(5); //设置y轴 YAxis yAxis_right = barChart.getAxisRight(); yAxis_right.setAxisMaximum(40f); yAxis_right.setAxisMinimum(35f); yAxis_right.setEnabled(false); YAxis yAxis_left = barChart.getAxisLeft(); yAxis_left.setAxisMaximum(40f); yAxis_left.setAxisMinimum(35f); yAxis_left.setTextSize(15f); }
添加数据:
1 private void setData() { 2 myHelper=new DatabaseHelper(this); 3 SQLiteDatabase db=myHelper.getWritableDatabase(); 4 List<IBarDataSet> sets = new ArrayList<>(); 5 List<BarEntry> barEntries = new ArrayList<>(); 6 float[] num = new float[list1.size()]; //存放各班级正常填报的人数 7 8 Cursor cursor = db.query("ALL_information", null, "name=?", new String[]{username}, null, null, null);//读取数据库所有信息 9 if(cursor.moveToFirst()){ 10 float i=0; 11 do{ 12 13 String date=cursor.getString(cursor.getColumnIndex("date")); 14 String temperature = cursor.getString(cursor.getColumnIndex("temperature")); 15 float t=Float.parseFloat(temperature); 16 barEntries.add(new BarEntry(i,t)); 17 i++; 18 19 }while (cursor.moveToNext()); 20 } 21 cursor.close(); 22 23 BarDataSet barDataSet = new BarDataSet(barEntries, "每日体温"); 24 barDataSet.setValueTextColor(Color.BLACK); 25 barDataSet.setValueTextSize(15f); 26 barDataSet.setColor(Color.parseColor("#FFFF00")); 27 28 29 barDataSet.setValueFormatter(new IValueFormatter() { 30 @Override 31 public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { 32 return value + "℃"; 33 } 34 }); 35 36 sets.add(barDataSet); 37 BarData barData = new BarData(sets); 38 barData.setBarWidth(0.6f); 39 barChart.setData(barData); 40 barChart.getXAxis().setLabelRotationAngle(-60); 41 barChart.setVisibleXRange(0,6); 42 43 }