【问题标题】:MPAndroidChart, update data from textViewMPAndroidChart,从 textView 更新数据
【发布时间】:2017-06-02 05:32:23
【问题描述】:

我试图在每次 textView 中的值更改时刷新图表。以下是 onCreate 的 sn-p。

希望有任何建议。

final ArrayList<Entry> entriesa= new ArrayList<>();
final LineChart lineChart = (LineChart) findViewById(R.id.chart);
final LineDataSet dataset = new LineDataSet(entriesa,"AWAY");
final LineData data = new LineData(dataset);

entriesa.add(new Entry(0,0));
lineChart.setData(data);
lineChart.invalidate();


final TextView viewa1 = (TextView) findViewById(R.id.sa1);
viewa1.addTextChangedListener(new TextWatcher() {
String s=viewa1.getText().toString();
int iv = Integer.parseInt(s.replaceAll("[\\D]", ""));

    public void beforeTextChanged ( CharSequence s, int start, int count, int after ) {

    }
    public void afterTextChanged ( Editable s ) {

    }
    public void onTextChanged(CharSequence s, int start, int before, int count ) {
    entriesa.set(0,new Entry(0,iv));
    data.notifyDataChanged();
    lineChart.notifyDataSetChanged();
    lineChart.invalidate();}
});

【问题讨论】:

  • 我不知道答案,但也许进入你的最终 ArrayList 也是最终的?但我不确定

标签: android android-studio mpandroidchart


【解决方案1】:

不是添加到数组列表,而是在 onTextChanged 中声明 LineData 并使用 addEntry 使用新条目附加到 LineData。

现在,使用 getData() 在每个 onTextChanged 上声明数据以及先前保存的数据,然后使用 addEntry 添加最新条目。这会创建一个连续的流......就像实时数据一样。我还使用了 ILineDataSet,因为我在同一个图表上有多个数据点。

10 个数据点指向视口,现在我的应用中有一个漂亮的自动滚动折线图。

LineData data = lineChart.getData();
ILineDataSet set = data.getDataSetByIndex(0);
data.addEntry(new Entry(set.getEntryCount(),iv),0);
data.notifyDataChanged();
lineChart.notifyDataSetChanged();
lineChart.setVisibleXRangeMaximum(10);
lineChart.moveViewToX(data.getEntryCount());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    相关资源
    最近更新 更多