MPAndroidChart with null values

孤街浪徒 提交于 2019-12-18 06:49:06

问题


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!


回答1:


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.




回答2:


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();
}


来源:https://stackoverflow.com/questions/25328151/mpandroidchart-with-null-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!