faeze saghafi
faeze saghafi

Reputation: 658

MPAndroidChart - adding entries dynamically does not work

I want to use MPAndroidChart and sometimes that user will be able add dynamic Entries and notify the changes and this is my code:

public void addEntry(int x , int y) {
    if (data != null)
    {
        ILineDataSet set = data.getDataSetByIndex(6);
        set.addEntry(new Entry(x, y));
        data.notifyDataChanged();
        chart.notifyDataSetChanged();
        chart.moveViewToX(data.getEntryCount());
        chart.invalidate();
    }

}

but it does not work. How can I solve this problem?

Upvotes: 0

Views: 1974

Answers (1)

sauvik
sauvik

Reputation: 2254

You can try something like this. This is how entries were added dynamically in the examples provided in the library.

private void addEntry(int x , int y) {
        LineData data = mChart.getData();
        ILineDataSet set = data.getDataSetByIndex(yourDataSetIndex);
        if (set == null) {
            set = createSet();
            data.addDataSet(set);
        }             
        data.addEntry(new Entry(x, y), yourDataSetIndex);
        data.notifyDataChanged();
        mChart.notifyDataSetChanged(); 

}

For more information, you can check this class.

Upvotes: 1

Related Questions