MPAndroidChart with null values
Asked Answered
S

2

5

I'm using the MPAndroidChart and am really enjoying it.

A 'little' need I have is that I can put null values to the 'entrys'. I'm monitoring the apache conections on servers of my system, and I would to see if they is down (where I put the null value) or if they just no conections (0).

I tried, but the Entry class don't accept 'null' as value showing the message: 'The constructor Entry(null, int) is undefined'

Thanks!

Swane answered 15/8, 2014 at 14:22 Comment(1)
G
4

A possible solution for you could be to check weather the object you received is null, or not. If the object is null, you don't even create an Entry object instead of just setting it's value to null.

Example:

// array that contains the information you want to display
ConnectionHolder[] connectionHolders = ...;

ArrayList<Entry> entries = new ArrayList<Entry>();
int cnt = 0;

for(ConnectionHolder ch : connectionHolders) {

    if(ch != null) entries.add(new Entry(ch.getNrOfConnections(), cnt));
    else {
        // do nothing
    }

    cnt++; // always increment
}

This would create e.g. a LineChart where no circles are drawn on indices where the ConnectionHolder object was null.

For a future release of the library, I will try to add the feature so that null values are supported.

Guaiacol answered 15/8, 2014 at 15:40 Comment(3)
Thanks for your answer, but doesn't work... he just make a 'big line' between the two points... I need that this null point show a empty space. So, while you support this I'll try make a circle with a different color.. it's possible, alright? Would you like that I post as issue in github?Swane
Exactly, It's not working. I'm sending the right xIndex and corresponding value, and it's drawing a line between the xIndex'sInca
I am still not able to find a solution for this, its doesn't support null values also if i ignore those items from entry it ignore them from labels as well.Bootblack
B
1

My solution is to draw another DataSet with TRANSPARENT (or arbitrary) color: - chart with fixed number of X values - Y values are updated periodically - boolean flag indicate transparent part (or another color)

private static final int SERIES_SIZE = 360; 
int xIndex = -1;
float xIndexVal;
private LineChart chart;
private boolean currentFlag;

public void createChart(LineDataSet dataSet) {
    LineData chartData = new LineData();
    prepareDataSet(dataSet);
    chartData.addDataSet(dataSet);
    for (int i = 0; i < SERIES_SIZE; i++) {
        chartData.addXValue("" /*+ i*/);
    }
    chart.setData(chartData);

}

private void prepareDataSet(LineDataSet dataSet, YAxis axis, int color) {
    // configure set 
}


public void update(Float val, boolean flag) {
    List<ILineDataSet> dsl = chart.getData().getDataSets();
    Log.d("chart", String.format("%s --- %d sets, index %d", descr,  dsl.size(), xIndex));

    if (xIndex == SERIES_SIZE - 1) {

        // remove all entries at X index 0
        for (int i = 0; i < chart.getData().getDataSetCount(); i++) {
            Entry entry0 = chart.getData().getDataSetByIndex(i).getEntryForIndex(0);
            if (entry0 != null && entry0.getXIndex() == 0) {
                chart.getData().removeEntry(entry0, i);
                Log.d("chart", String.format("entry 0 removed from dataset %d, %d entries in the set", i, chart.getData().getDataSetByIndex(i).getEntryCount()));
            }
            else {
                Log.d("chart", String.format("all %d entries in the set kept", chart.getData().getDataSetByIndex(i).getEntryCount()));
            }
        }

        // remove empty set, if any
        for (Iterator<ILineDataSet> mit = dsl.iterator(); mit.hasNext(); ) {
            if (mit.next().getEntryCount() == 0) {
                mit.remove();
                Log.d("chart", String.format("set removed, %d sets", dsl.size()));
            }
        }

        // move all entries by -1
        for (ILineDataSet ds : dsl) {
            for (Entry entry : ((LineDataSet)ds).getYVals()) {
                entry.setXIndex(entry.getXIndex() - 1);
            }
        }
    }
    else {
        xIndex++;
    }

    if (currentFlag != flag) {
        currentFlag = !currentFlag;
        LineDataSet set = new LineDataSet(null, "");
        prepareDataSet(set, chart.getAxisLeft(), currentFlag ?  Color.TRANSPARENT : Color.BLUE);
        chart.getData().addDataSet(set);

        if (xIndex != 0) {
            chart.getData().addEntry(new Entry(xIndexVal, xIndex - 1), dsl.size() - 1);
        }
    }

    xIndexVal = val;

    chart.getData().addEntry(new Entry(val, xIndex), dsl.size() - 1);

    chart.notifyDataSetChanged();
    chart.invalidate();
}
Brindabrindell answered 23/2, 2016 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.