Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart Linechart unconnected data points

I am investigating MPAndroidChart for usage in my company's Android and iOS application and I have found a problem that I need a solution to in order to be able to use this framework.

The application will mostly use the Line Chart functionality and the supplied data can contain NULL entries. I have seen other posts that discusses this matter and apparently there is no solution to showing NULL values yet. MPAndroidChart with null values

The author suggests simply not adding the data point to the set, but in my case it's very important that there is a "hole" in the graph were there is more than two consecutive NULL values (or however to represent it) i.e that the graph is not continous between two points with NULL values in between. Is there any way I can accomplish this with this framework?

I have been looking into the possibility of separating the data points into different data sets, but it seems like kind of a hack.

Thank you!

Dataset example:

[1 2 10 NULL NULL NULL 20 25 30]
The Line must NOT connect the numbers 10 and 20.
like image 528
Joakim Avatar asked Aug 11 '15 10:08

Joakim


1 Answers

This is what I ended up coming up with to solve this - for anyone in the future. It iterates through and creates new entries in a data set until it hits a null value, and then it creates data sets with "fake" entries that use a boolean in the entry constructor. Boolean can be found from

"entry.getData()"

and you can then you can use this to set that Dataset to not be visible

"mLineDataSet.setVisible(false);"

Note: Do not try to set Dataset Color to transparent - the library has a bug where if certain entries are null the graph doesn't even appear.

    private void createDataSets() {
          for (int index = 0; index < mGraph.getGraphDataSets().size(); index++) {
                    lastIndexCreated = 0;
                    final GraphDataSet mDataSet = mGraph.getGraphDataSets().get(index);
                    final ArrayList<Entry> mEntries = getEntries(mDataSet.getYValues(), lastIndexCreated);
                    final LineDataSet mLineDataSet = getDataSet(mEntries, mDataSet, color);
                    mGraphLineData.addDataSet(mLineDataSet);
                    lastIndexCreated = mEntries.size() - 1;
                    while (lastIndexCreated < mDataSet.getYValues().size() - 1) {

                        final LineDataSet set = getDataSet(mEntriesSet, mDataSet, colorSecondary);
                        if (mEntriesSet.size() != 0)
                            mGraphLineData.addDataSet(set);
                        lastIndexCreated = (int) mEntriesSet.get(mEntriesSet.size() - 1).getX();
                    }
                }
    }

    private ArrayList<Entry> getEntries(final List<Float> yValues, final int firstValueIndex) {
            final ArrayList<Entry> mEntries = new ArrayList<>();
            for (int i = firstValueIndex; i < yValues.size(); i++) {
                if (yValues.get(i) != null)
                 //boolean here is false means that dataset is not fake and should be shown
                    mEntries.add(new Entry(i, yValues.get(i), false));
                else if (firstValueIndex == i) {
                //add a "Fake" data entry, and use mEntry.getData to set line to not be visible.
                    mEntries.add(new Entry(i, 0, true));
                    break;
                } else {
                    break;
                }
            }
            return mEntries;
        }
like image 130
HannahCarney Avatar answered Sep 20 '22 14:09

HannahCarney