1

For line charts I find that if I have multiple y values for a single x, it works fine as long as it isn't the final x. If it's the final x, it only displays the first entry. Is there a known workaround for this?

Example:

//firstTimestamp is earlier than secondTimestamp

data.add(new Entry(firstTimestamp, 10));
data.add(new Entry(firstTimestamp, 20)); //won't show unless you uncomment below
data.add(new Entry(firstTimestamp, 30)); //won't show unless you uncomment below

//data.add(new Entry(secondTimestamp, 40));

Graph when second timestamp commented out:

Graph with second timestamp uncommented (note that the 20 and 30 are now included, whereas they weren't before):

Edit:

I believe I've found the cause of this problem and can fix it in the following way, by changing

public abstract class BarLineScatterCandleBubbleRenderer extends DataRenderer {

    // ... lines removed ... //

    public void set(BarLineScatterCandleBubbleDataProvider chart, IBarLineScatterCandleBubbleDataSet dataSet) {
        float phaseX = Math.max(0.f, Math.min(1.f, mAnimator.getPhaseX()));

        float low = chart.getLowestVisibleX();
        float high = chart.getHighestVisibleX();

        Entry entryFrom = dataSet.getEntryForXValue(low, Float.NaN, DataSet.Rounding.DOWN);
        Entry entryTo = dataSet.getEntryForXValue(high, Float.NaN, DataSet.Rounding.UP);

        min = entryFrom == null ? 0 : dataSet.getEntryIndex(entryFrom);
        max = entryTo == null ? 0 : dataSet.getEntryIndex(entryTo);
        range = (int) ((max - min) * phaseX);
    }

    // ... lines removed ... //
}

to this, which I believe will resolve the issue:

public abstract class BarLineScatterCandleBubbleRenderer extends DataRenderer {

    // ... lines removed ... //

    public void set(BarLineScatterCandleBubbleDataProvider chart, IBarLineScatterCandleBubbleDataSet dataSet) {
        float phaseX = Math.max(0.f, Math.min(1.f, mAnimator.getPhaseX()));

        float low = chart.getLowestVisibleX();
        float high = chart.getHighestVisibleX();

        Entry entryFrom = dataSet.getEntryForXValue(low, Float.NaN, DataSet.Rounding.DOWN);

        //my edits here
        int indexTo = dataset.getEntryIndex(high, Float.NaN, DataSet.Rounding.UP);
        List<Entry> values = dataset.getValues();
        while (indexTo + 1 < values.size() && values.get(indexTo + 1).getX() == high) {
            indexTo++;
        }
        Entry entryTo = values.get(indexTo);
        //my edits end here

        min = entryFrom == null ? 0 : dataSet.getEntryIndex(entryFrom);
        max = entryTo == null ? 0 : dataSet.getEntryIndex(entryTo);
        range = (int) ((max - min) * phaseX);
    }

    // ... lines removed ... //
}

How can I subclass this / use these edits?

1 Answers1

1

Please note that the only supported use case for LineChart entries is adding them ordered ascending. This is documented in the wiki:

Please be aware that this library does not officially support drawing LineChart data from an Entry list not sorted by the x-position of the entries in ascending manner.

The reason for this is that the renderer is optimised for unique ascending order entries.

If you want to get around this, I suggest you have a look at the source for LineChartRenderer. You will have to place breakpoints and find the issue that is causing it to render in the way you have demonstrated. Then you can consider subclassing the renderer to meet your requirement. Essentially you would be removing the optimisation in order to support the extra use case (non-unique values).

EDIT: If you are unwilling to manipulate the existing object graph to obtain the behaviour you want, you might want to consider forking the library with the change. Then you can build a .jar of your fork and include it in your Android project. Refer to the instructions in the answers below for the same:

How to make a .jar from an Android Studio project

How to add a .jar as a library in Android Studio

David Rawson
  • 20,912
  • 7
  • 88
  • 124
  • See also [this question](https://stackoverflow.com/questions/40532564/mpandroidchart-creating-a-closed-chart-circular-line-chart). – David Rawson Feb 20 '17 at 04:06