如何在MPAndroid图表中动态设置X轴和Y轴的数据

时间:2018-09-29 05:16:48

标签: android

有人可以告诉我如何动态地将数据设置为具有不同数据点的折线图吗?

我的代码->

within(com.dux.secondwallet.api.v3.pay.merchant.*)

预先感谢

1 个答案:

答案 0 :(得分:0)

对于BarChart,创建一个列表并像这样添加数据->

List<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0f, 30f));
entries.add(new BarEntry(1f, 80f));
entries.add(new BarEntry(2f, 60f));
entries.add(new BarEntry(3f, 50f)); 
                                    // gap of 2f
entries.add(new BarEntry(5f, 70f));
entries.add(new BarEntry(6f, 60f));

BarDataSet set = new BarDataSet(entries, "BarDataSet");

对于“其他图表”,过程相同,只是对不同类型的图表使用不同的对象。请参阅this更多信息。

您的整个代码是这样->

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

    // in this example, a LineChart is initialized from xml
    LineChart chart = findViewById(R.id.chart);

    //  Adding Data to show it inside the Graph..
    EntryClass entryClass = new EntryClass();

    for (int u = 0; u < dataList.size(); u++) {
        Log.e("Data_List_Size", dataList.size() + "");
        for (int j = 0; j < trafficFilter.dataPoints.size(); j++) {
            entryClass.AddintoList(trafficFilter.dataPoints.get(j).getX(),trafficFilter.dataPoints.get(u).getY(), u));
        }
    }

    LineDataSet lineDataSet = new LineDataSet(entryClass.getEntryList(),"Sample Data");
    LineData lineData = new LineData(lineDataSet);
    chart.setData(lineData);
    chart.invalidate();
}

这是您的EntryClass的外观->

import com.github.mikephil.charting.data.Entry;

import java.util.ArrayList;
import java.util.List;

public class EntryClass {
    private List<Entry> entryList;

    EntryClass(){
        entryList = new ArrayList<>();
    }

    public List<Entry> getEntryList() {
        return entryList;
    }

    public void AddintoList(float X,float Y) {
        entryList.add(new Entry(X,Y));
    }
}