private void setChartData(ClimbingArea ClimbingArea) { HashMap<Integer, Integer> difficultyMap = new HashMap<>(); for (int i = AppConstants.MIN_UIAA_LEVEL; i <= AppConstants.MAX_UIAA_LEVEL; ++i) { difficultyMap.put(i, 0); } for (ClimbingRoute route : ClimbingArea.getRoutes()) { String difficulty = route.getUIAARank(); int routeDifficulty = Utils.trimUIAARank(difficulty); if (difficultyMap.containsKey(routeDifficulty)) { difficultyMap.put(routeDifficulty, difficultyMap.get(routeDifficulty) + 1); } } ArrayList<BarEntry> routeDifficulties = new ArrayList<BarEntry>(); String[] xVals = new String[difficultyMap.size()]; List<Integer> orderedList = new ArrayList<>(difficultyMap.keySet()); Collections.sort(orderedList); int indexCounter = 0; for (Integer difficulty : orderedList) { xVals[indexCounter] = difficulty + ""; // BarEntry entry = new BarEntry(difficultyMap.get(difficulty), indexCounter++ , difficulty // +""); BarEntry entry = new BarEntry(difficulty, difficultyMap.get(difficulty), indexCounter++); routeDifficulties.add(entry); } mChart.getXAxis().setLabelCount(xVals.length); BarDataSet set1 = new BarDataSet(routeDifficulties, "Schwierigkeiten"); // set1.setColor(getResources().getColor(R.color.primary_700)); int[] uiaaChartColors = new int[AppConstants.UIAA_CHART_COLORS.length]; for (int i = 0; i < uiaaChartColors.length; ++i) { uiaaChartColors[i] = getResources().getColor(AppConstants.UIAA_CHART_COLORS[i]); } set1.setColors(uiaaChartColors); ArrayList<IBarDataSet> dataSets = new ArrayList<>(); dataSets.add(set1); BarData data = new BarData(dataSets); data.setValueFormatter(new MyValueFormatter()); data.setValueTextSize(10f); mChart.setData(data); mChart.animateX(1000); mChart.animateY(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(); }