今天上手做了一下数据可视化图表,写了一下图表的代码,但是在数据传递上遇到了问题,也因为这个问题耽搁了一下午,还是没有实现想要的功能,不过大概框架已经成型。
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" android:background="#FFFDFD" android:orientation="vertical"> <RelativeLayout android:id="@+id/tab_title" android:layout_width="match_parent" android:layout_height="48dp" android:background="#DCFFC107" app:layout_constraintBottom_toTopOf="@+id/guideline3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <TextView android:id="@+id/txt_shouru" android:layout_width="40dp" android:layout_height="match_parent" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginStart="160dp" android:layout_marginLeft="160dp" android:layout_marginTop="15dp" android:layout_marginEnd="211dp" android:layout_marginRight="211dp" android:text="图表" android:textColor="#000000" android:textSize="18sp" /> <View android:layout_width="match_parent" android:layout_height="2px" android:layout_alignParentBottom="true" android:background="#E91E63" /> </RelativeLayout> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/textview1" android:textSize="24sp" app:layout_constraintBottom_toTopOf="@+id/textView2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.105" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确认" app:layout_constraintBottom_toTopOf="@+id/guideline" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/editText" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.737" /> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="请输入月份——4月" android:inputType="textPersonName" app:layout_constraintBottom_toTopOf="@+id/guideline" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toStartOf="@+id/button" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.703" /> <lecho.lib.hellocharts.view.LineChartView android:id="@+id/chart" android:layout_width="wrap_content" android:layout_height="0dp" android:padding="20dp" app:layout_constraintBottom_toTopOf="@+id/guideline2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/guideline" app:layout_constraintVertical_bias="0.0" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_begin="109dp" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_begin="444dp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/textview" android:textSize="24sp" app:layout_constraintBottom_toTopOf="@+id/textView3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.11" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/chart" app:layout_constraintVertical_bias="0.26" /> <TextView android:id="@+id/zhichu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/zhichu" android:textSize="24sp" app:layout_constraintBottom_toTopOf="@+id/shouru" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="@+id/shouru" app:layout_constraintTop_toBottomOf="@+id/chart" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/textview2" android:textSize="24sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.105" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView3" /> <TextView android:id="@+id/shouru" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/shouru" android:textSize="24sp" app:layout_constraintBottom_toTopOf="@+id/yue" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.706" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/zhichu" /> <TextView android:id="@+id/yue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/yue" android:textSize="24sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.706" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/shouru" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_begin="48dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
private void generateData(){ List<Line> lines = new ArrayList<>(); List<PointValue> values = new ArrayList<>(); int indexX = 0; for(Integer value : table.values()){ values.add(new PointValue(indexX, value)); indexX++; } Line line = new Line(values); line.setColor(ChartUtils.COLORS[0]); line.setShape(ValueShape.CIRCLE); line.setPointColor(ChartUtils.COLORS[1]); lines.add(line); mData.setLines(lines); setAxis(); mChart.setLineChartData(mData); } private void generateValues(List<CostBean> mCostBeanList) { if(mCostBeanList != null){ for (int i = 0; i < mCostBeanList.size(); i++) { CostBean costBean = mCostBeanList.get(i); String costDate = costBean.getCostDate(); int costMoney = Integer.parseInt(costBean.getCostMoney()); mAxisXValues.add(new AxisValue(i).setLabel(costDate)); if(!table.containsKey(costDate)){ table.put(costDate,costMoney); //mAxisXValues.add(new AxisValue(i).setLabel(costDate)); }else { int originMoney = table.get(costDate); table.put(costDate,originMoney + costMoney); } } } } private void setAxis() { //坐标轴 Axis axisX = new Axis(); //X轴 axisX.setHasTiltedLabels(true); //X坐标轴字体是斜的显示还是直的,true是斜的显示 axisX.setTextColor(Color.GRAY); //设置字体颜色 // axisX.setName("日期"); //表格名称 axisX.setTextSize(10);//设置字体大小 axisX.setMaxLabelChars(7); //最多几个X轴坐标,意思就是你的缩放让X轴上数据的个数7<=x<=mAxisXValues.length axisX.setValues(mAxisXValues); //填充X轴的坐标名称 mData.setAxisXBottom(axisX); //x 轴在底部 //data.setAxisXTop(axisX); //x 轴在顶部 axisX.setHasLines(true); //x 轴分割线 Axis axisY = new Axis().setHasLines(true); axisY.setMaxLabelChars(6);//max label length, for example 60 for(int i = 0; i < 100; i+=5 ){//Y轴的坐标范围 10 -15 AxisValue value = new AxisValue(i); String label = i + "¥"; value.setLabel(label); values.add(value); } axisY.setValues(values); // axisY.setName("钱");//y轴标注 axisY.setTextSize(10);//设置字体大小 mData.setAxisYLeft(axisY); //Y轴设置在左边 //data.setAxisYRight(axisY); //y轴设置在右边 }