Mpandroidchart: Changing xAxis label count with data set change results in old dataset values passed to formatter

Created on 2 Sep 2018  ·  6Comments  ·  Source: PhilJay/MPAndroidChart

Summary
Changing xAxis label count with data set change results in old data set values passed to formatter, but not always.
Setting of params on the first data set:

with(xAxis) {
        position = XAxis.XAxisPosition.BOTTOM
        this.typeface = typeface
        setDrawGridLines(false)
        this.labelCount = labelCount
        valueFormatter = xAxisFormatter
    }
...
barChart.data = barData

on the first load everything is ok.
Then I refresh the data, formatter and label:

...
    val xAxisFormatter = data.xAxisFormatterByRange()
    val labelCount = data.xAxisLabelCountByRange()

    barChart.xAxis.labelCount = labelCount
    barChart.xAxis.valueFormatter = xAxisFormatter

    val dataSet = view.data.getDataSetByIndex(0) as BarDataSet
    dataSet.values = entries

    barChart.data.notifyDataChanged()
    barChart.notifyDataSetChanged()

The first time the data refresh happens, everything goes well.
I get the crash on the second refresh on line barChart.notifyDataSetChanged() because values from old data are passed to a new formatter.

When I remove the setting of labelCount from both the init and refresh, everything works well.
When I leave the labelCount setting only in the init or refresh, I get the same crash.

I need this as I use date ranges. For the period of week, all 7 labels are present without any labelCount set, but for periods of month and year, default 6 labels are not good enough. Label counts are 7, 15 and 12.

Expected Behavior
After barChart.notifyDataSetChanged() chart is refreshed with new data, formatter and label count.

Possible Solution
mEntries in XAxis are not always refreshed on labelCount changes? I could not find where they are set or reset.

Device (please complete the following information):

  • Device: Google Pixel 2
  • Android Version 9
  • Library Version 3.0.3

Other bar chart settings:

        barChart.setDrawValueAboveBar(true)
        barChart.setScaleEnabled(false)
        barChart.legend.isEnabled = false
        barChart.description.isEnabled = false
        barChart.setNoDataText(noDataText)
        barChart.setNoDataTextTypeface(typeface)

Crash log:

org.threeten.bp.DateTimeException: Invalid value for DayOfWeek: 8
        at org.threeten.bp.DayOfWeek.of(DayOfWeek.java:144)
        at com.deividasstr.ui.features.consumedsweetdata.charts.WeekXAxisFormatter.weekDayFromDay(WeekXAxisFormatter.kt:16)
        at com.deividasstr.ui.features.consumedsweetdata.charts.WeekXAxisFormatter.getFormattedValue(WeekXAxisFormatter.kt:12)
        at com.github.mikephil.charting.components.AxisBase.getFormattedLabel(AxisBase.java:472)
        at com.github.mikephil.charting.components.AxisBase.getLongestLabel(AxisBase.java:458)
        at com.github.mikephil.charting.renderer.XAxisRenderer.computeSize(XAxisRenderer.java:79)
        at com.github.mikephil.charting.renderer.XAxisRenderer.computeAxisValues(XAxisRenderer.java:74)
        at com.github.mikephil.charting.renderer.XAxisRenderer.computeAxis(XAxisRenderer.java:67)
        at com.github.mikephil.charting.charts.BarLineChartBase.notifyDataSetChanged(BarLineChartBase.java:332)
...
bug fix

Most helpful comment

Hey, how is this PR pull going?

All 6 comments

I solved this by using forked version of this lib. In it, in AxisRenderer class in method computeAxisValues(float min, float max) I changed

if (mAxis.mEntries.length < n) {
                // Ensure stops contains at least numStops elements.
                mAxis.mEntries = new float[n];
            }

to

mAxis.mEntries = new float[n];

Everything works fine now. I did not issue a PR because the forked version is different than the one distributed as gradle dependency. Let me know if you want me to do this PR.

I suggested the PR for this bug, it would be nice to see the fix in the lib.

I'll do some testing soon and hopefully pull this today or tomorrow. I'm trying to get through all 1000+ issues right now so it may take longer. Thank you for your patience!

No worries, thank you for your amazing job!

On Mon, Oct 29, 2018, 22:14 Mick A. notifications@github.com wrote:

Oof, looks like I missed this in #3940
https://github.com/PhilJay/MPAndroidChart/issues/3940. I'll do some
testing soon and hopefully pull this today or tomorrow. I'm trying to get
through all 1000+ issues right now so it may take longer. Thank you for
your patience!


You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/PhilJay/MPAndroidChart/issues/4214#issuecomment-434061958,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AQjLXV7SWNfP8atlULlP1iM5IvErSC4Nks5up2HAgaJpZM4WWoAf
.

Hey, how is this PR pull going?

Hey, please merge the PR https://github.com/PhilJay/MPAndroidChart/pull/4217, it is quite simple.
The new , 3.1 version does not solve the issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AiTheAnswer picture AiTheAnswer  ·  3Comments

Giammaofwar picture Giammaofwar  ·  3Comments

mrZizik picture mrZizik  ·  3Comments

thanhcly920 picture thanhcly920  ·  3Comments

galex picture galex  ·  3Comments