@Override public void onDataFetched(List<Meter> meters) { mMeters = meters; mSwipeRefreshLayout.setRefreshing(false); showProgress(false); // only reading one meter. // TODO: graph multiple meters? Meter meter = mMeters.get(0); setData(meter.getReadings().size(), 1000); }
private void setData(int count, float range) { // only using first meter for now. // TODO: graph multiple meters? Meter meter = mMeters.get(0); List<MeterReading> readings = meter.getReadings(); ArrayList<Entry> averageEntries = new ArrayList<Entry>(); // build up X-axis labels; e.g., dates. // build up Y-axis with values; // skip first date as we can't determine consumption. ArrayList<String> xVals = new ArrayList<String>(); ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); float runningTotal = 0; float dailyAverage; for (int lv = 1; lv < count; lv++) { MeterReading prevReading = readings.get(lv - 1); MeterReading currReading = readings.get(lv); xVals.add(removeYearFromDate(currReading.getTimeStamp())); float val = (currReading.getConsumption() - prevReading.getConsumption()) * 10; yVals1.add(new BarEntry(val, lv - 1)); // calc & store daily average. runningTotal += val; dailyAverage = runningTotal / lv; averageEntries.add(new Entry(dailyAverage, lv - 1)); } // TODO: allow name to be updated. BarDataSet set1 = new BarDataSet(yVals1, "mine"); set1.setBarSpacePercent(35f); ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>(); dataSets.add(set1); // bar chart data BarData barData = new BarData(xVals, dataSets); barData.setValueFormatter(new MyValueFormatter()); barData.setValueTextSize(10f); // line chart data LineData lineData = new LineData(); LineDataSet set = new LineDataSet(averageEntries, "average"); int lineColor = Color.rgb(251, 169, 165); set.setColor(lineColor); set.setLineWidth(1.5f); set.setDrawCircles(false); set.setFillColor(lineColor); set.setDrawCubic(true); set.setDrawValues(false); set.setAxisDependency(YAxis.AxisDependency.LEFT); lineData.addDataSet(set); CombinedData combinedData = new CombinedData(getDates(meter.getReadings())); combinedData.setData(barData); combinedData.setData(lineData); mChart.setData(combinedData); mChart.invalidate(); }