MPAndroidChart通过按钮单击创建新图表

时间:2017-11-01 23:47:09

标签: java android unit-testing onclick android-espresso

是否可以使用另一个图表覆盖图表,或者在第一个图表下点击单选按钮或任何其他按钮点击?我需要3个条形图,要么全部都在彼此之下,要么需要一个图表,可以通过按钮点击或单选按钮点击刷新新数据。

https://pastebin.com/b1D5aAPr

这就是我的想法。

package com.example.zozo07.mobile;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

import com.concretepage.android.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import java.util.ArrayList;

public class ChartActivity extends AppCompatActivity{

    private BarChart barChart;
    private RadioButton rbA, rbC, rbD;

    private BarDataSet barDataSet;
    private ArrayList<BarEntry> barEntries;
    private ArrayList<String> theTitles;
    private BarData theData;

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

        barChart = (BarChart) findViewById(R.id.bargraph);
        rbA = (RadioButton) findViewById(R.id.rbA);
        rbC = (RadioButton) findViewById(R.id.rbC);
        rbD = (RadioButton) findViewById(R.id.rbD);
        Button btnCreate = (Button) findViewById(R.id.btnCreate);

        btnCreate.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                createBarGraph(new BarEntry(50f, 0), new BarEntry(100f, 1), new BarEntry(20f, 2), barEntries, theTitles, barDataSet, theData);
            }
        });

        registerButtons();

    }

    private void createBarGraph(BarEntry e, BarEntry e2, BarEntry e3, ArrayList<BarEntry> barEntries, ArrayList<String> theTitles, BarDataSet barDataSet, BarData theData) {
        barEntries = new ArrayList<>();
        barEntries.add(e);
        barEntries.add(e2);
        barEntries.add(e3);

        barDataSet = new BarDataSet(barEntries, "Data");

        theTitles = new ArrayList<>();
        theTitles.add("Alarm");
        theTitles.add("Sleep");
        theTitles.add("Vacation");


        theData = new BarData(theTitles, barDataSet);
        barChart.setData(theData);

        barChart.setTouchEnabled(true);
        barChart.setDragEnabled(true);
        barChart.setScaleEnabled(true);
    }

    public void registerButtons() {
        register(R.id.rbA);
        register(R.id.rbC);
        register(R.id.rbD);
    }

    private void register(int buttonResourceId){
        findViewById(buttonResourceId).setOnClickListener(buttonClickListener);
    }

    private OnClickListener buttonClickListener = new OnClickListener() {

        @Override
        public void onClick(View v){
            switch (v.getId()) {
                case R.id.rbA:
                    killBarChart();
                    barA();
                    break;
                case R.id.rbC:
                    killBarChart();
                    barB();
                    break;
                case R.id.rbD:
                    killBarChart();
                    barD();
                    break;
                default:
                    break;
            }
        }
    };

    private void barD() {
        createBarGraph(new BarEntry(30f, 0), new BarEntry(60f, 1), new BarEntry(100f, 2), barEntries, theTitles, barDataSet, theData);
    }

    private void barB() {
        createBarGraph(new BarEntry(20f, 0), new BarEntry(50f, 1), new BarEntry(90f, 2), barEntries, theTitles, barDataSet, theData);
    }

    private void barA() {
        createBarGraph(new BarEntry(80f, 0), new BarEntry(70f, 1), new BarEntry(20f, 2), barEntries, theTitles, barDataSet, theData);
    }

    protected void killBarChart() {
        barEntries = null;
        barDataSet = null;
        theTitles = null;
    }
}

2 个答案:

答案 0 :(得分:0)

是的,一旦您更改数据集,就可以轻松覆盖它。创建一个方法来创建图表,并在按钮单击或无线电选择时调用该方法并将新值传递给它,它将更新您的图表。

答案 1 :(得分:0)

它正在运作。我将USB线更改为另一个,现在我的Android设备被ADB识别,因此不需要模拟器,通过点击图表,它们会发生变化。