From dee492bc4f35d02eea56de8cc67ae29c1f86d8e1 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Mon, 3 Oct 2016 23:34:05 +0200 Subject: [PATCH] Fixes regarding xlabels -- must set them at the correct point in time (otherwise we get ArrayIndexOutOfBoundsException when the chart refreshes and the wrong xLabels, potentially with fewer entries are used) --- .../activities/charts/AbstractChartFragment.java | 13 ++++++------- .../charts/ActivitySleepChartFragment.java | 1 + .../gadgetbridge/activities/charts/ChartsData.java | 11 +++++++++++ .../activities/charts/WeekStepsChartFragment.java | 4 ++-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java index 222a93ef..f0a1c7b7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java @@ -85,7 +85,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { }; private boolean mChartDirty = true; private AsyncTask refreshTask; - private XIndexLabelFormatter xIndexFormatter = new XIndexLabelFormatter(); + protected XIndexLabelFormatter xIndexFormatter = new XIndexLabelFormatter(); public boolean isChartDirty() { return mChartDirty; @@ -409,6 +409,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { Date date; String dateStringFrom = ""; String dateStringTo = ""; + ArrayList xLabels = null; LOG.info("" + getTitle() + ": number of samples:" + samples.size()); CombinedData combinedData; @@ -422,7 +423,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { SimpleDateFormat annotationDateFormat = new SimpleDateFormat("HH:mm"); int numEntries = samples.size(); - ArrayList xLabels = new ArrayList<>(numEntries); + xLabels = new ArrayList<>(numEntries); List activityEntries = new ArrayList<>(numEntries); boolean hr = supportsHeartrate(gbDevice); List heartrateEntries = hr ? new ArrayList(numEntries) : null; @@ -507,9 +508,6 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { xLabels.add(xLabel); } - xIndexFormatter.setxLabels(xLabels); -// chart.getXAxis().setValues(xLabels); - BarDataSet activitySet = createActivitySet(activityEntries, colors, "Activity"); // create a data object with the datasets // combinedData = new CombinedData(xLabels); @@ -533,7 +531,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { combinedData = new CombinedData(); } - return new DefaultChartsData(combinedData); + return new DefaultChartsData(combinedData, xLabels); } protected boolean isValidHeartRateValue(int value) { @@ -717,8 +715,9 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { public static class DefaultChartsData extends ChartsData { private final CombinedData combinedData; - public DefaultChartsData(CombinedData combinedData) { + public DefaultChartsData(CombinedData combinedData, ArrayList xLabels) { this.combinedData = combinedData; + setxLabels(xLabels); } public CombinedData getCombinedData() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivitySleepChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivitySleepChartFragment.java index 8bab4941..914c58f7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivitySleepChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivitySleepChartFragment.java @@ -119,6 +119,7 @@ public class ActivitySleepChartFragment extends AbstractChartFragment { DefaultChartsData dcd = (DefaultChartsData) chartsData; mChart.getLegend().setTextColor(LEGEND_TEXT_COLOR); mChart.setData(null); // workaround for https://github.com/PhilJay/MPAndroidChart/issues/2317 + xIndexFormatter.setxLabels(dcd.getXLabels()); mChart.setData(dcd.getCombinedData()); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsData.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsData.java index 4285951f..ecdde59d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsData.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ChartsData.java @@ -1,4 +1,15 @@ package nodomain.freeyourgadget.gadgetbridge.activities.charts; +import java.util.ArrayList; + public abstract class ChartsData { + private ArrayList xLabels; + + public void setxLabels(ArrayList xLabels) { + this.xLabels = xLabels; + } + + public ArrayList getXLabels() { + return xLabels; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java index 8b442776..3cec78de 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java @@ -71,6 +71,7 @@ public class WeekStepsChartFragment extends AbstractChartFragment { mWeekStepsChart.setData(null); // workaround for https://github.com/PhilJay/MPAndroidChart/issues/2317 mWeekStepsChart.setData(mcd.getWeekBeforeStepsData().getCombinedData()); mWeekStepsChart.getLegend().setEnabled(false); + xIndexLabelFormatter.setxLabels(mcd.getWeekBeforeStepsData().getXLabels()); } @Override @@ -97,7 +98,6 @@ public class WeekStepsChartFragment extends AbstractChartFragment { BarDataSet set = new BarDataSet(entries, ""); set.setColor(akActivity.color); - xIndexLabelFormatter.setxLabels(labels); BarData barData = new BarData(set); barData.setValueTextColor(Color.GRAY); //prevent tearing other graph elements with the black text. Another approach would be to hide the values cmpletely with data.setDrawValues(false); @@ -107,7 +107,7 @@ public class WeekStepsChartFragment extends AbstractChartFragment { CombinedData combinedData = new CombinedData(); combinedData.setData(barData); - return new DefaultChartsData(combinedData); + return new DefaultChartsData(combinedData, labels); }