-->

App_显示图表内容

2020-02-19 21:29发布

今天在之前记账本的基础上增加了图标的显示功能,在本次课程中它以折线图为例讲述。但是课程中给出了多有图的代码案例。

 https://github.com/lecho/hellocharts-android

但是结果并不是很理想,中途进行了很多色测试,没有出现问题,但是在做后图表的显示中出现了闪退的问题,根据网上所说的我进行了配置的更改,但是依旧无效,继续明天的努力,解决闪退bug的问题把

这里并没有下载和复制图标源码,而是在build.gradle里面做了引用

 并在这里对以上的内容在chart_view.xml中做了引用

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

    <lecho.lib.hellocharts.view.LineChartView
        android:id="@+id/chart"
        android:padding="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

并且新建了相对应的ChartsActivity.java:

package com.example.familybook;

import android.app.Activity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.LineChartView;

public class ChartsActivity extends Activity {
    private LineChartView mChart;
    private Map<String,Integer> table=new TreeMap<>();
    private LineChartData mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.chart_view);
        mChart=(LineChartView) findViewById(R.id.chart);
        List<CostBean> allDate= (List<CostBean>) getIntent().getSerializableExtra("cost_list");
        generateValues(allDate);
        generateData();
        mData=new LineChartData();
    }

    private void generateData() {
        List<Line> lines=new ArrayList<>();
        List<PointValue> values=new ArrayList<>();
        int indeX=0;
        for(Integer value:table.values()){
            values.add(new PointValue(indeX,value));
            indeX++;
        }
        Line line=new Line(values);
        line.setColor(ChartUtils.COLORS[0]);
        line.setShape(ValueShape.CIRCLE);
        line.setPointColor(ChartUtils.COLORS[1]);
        lines.add(line);
        mData=new LineChartData(lines);
        mData.setLines(lines);
        mChart.setLineChartData(mData);
    }

    private void generateValues(List<CostBean> allDate) {
        if(allDate!=null){
            for(int i=0;i<allDate.size();i++){
                CostBean costBean=allDate.get(i);
                String costDate=costBean.costDate;
                int costMoney=Integer.parseInt(costBean.costMoney);
                if(!table.containsKey(costDate)){
                    table.put(costDate,costMoney);
                }
                else{
                    int originMoney=table.get(costDate);
                    table.put(costDate, originMoney + costMoney);
                }
            }
        }
    }
}

 

标签: