@Override public void renderGridLines(Canvas c) { if (!mXAxis.isDrawGridLinesEnabled() || !mXAxis.isEnabled()) return; float[] position = new float[] {0f, 0f}; mGridPaint.setColor(mXAxis.getGridColor()); mGridPaint.setStrokeWidth(mXAxis.getGridLineWidth()); BarData bd = mChart.getData(); int step = bd.getDataSetCount(); for (int i = mMinX; i < mMaxX; i += mXAxis.mAxisLabelModulus) { position[0] = i * step + i * bd.getGroupSpace() - 0.5f; mTrans.pointValuesToPixel(position); if (mViewPortHandler.isInBoundsX(position[0])) { c.drawLine( position[0], mViewPortHandler.offsetTop(), position[0], mViewPortHandler.contentBottom(), mGridPaint); } } }
private void addData() { ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); for (int i = 0; i < yData.length; i++) yVals1.add(new BarEntry(yData[i], i)); ArrayList<String> xVals = new ArrayList<String>(); for (int i = 0; i < xData.length; i++) xVals.add(xData[i]); // create pie data set BarDataSet dataSet = new BarDataSet(yVals1, "Stock"); // dataSet.setSliceSpace(5); // dataSet.setSelectionShift(5); // add many colors ArrayList<Integer> colors = new ArrayList<Integer>(); colors.add(Color.rgb(0, 204, 68)); colors.add(Color.rgb(230, 0, 0)); dataSet.setColors(colors); // instantiate pie data object BarData data = new BarData(xVals, dataSet); data.setValueTextSize(16); data.setValueTextColor(Color.BLACK); mChart.setData(data); // undo highlights mChart.highlightValue(null); // update pie chart mChart.invalidate(); }
private void refreshWeekBeforeSteps( DBHandler db, BarLineChartBase barChart, Calendar day, GBDevice device) { ActivityAnalysis analysis = new ActivityAnalysis(); day = (Calendar) day.clone(); // do not modify the caller's argument day.add(Calendar.DATE, -7); List<BarEntry> entries = new ArrayList<>(); List<String> labels = new ArrayList<>(); for (int counter = 0; counter < 7; counter++) { entries.add( new BarEntry(analysis.calculateTotalSteps(getSamplesOfDay(db, day, device)), counter)); labels.add(day.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, mLocale)); day.add(Calendar.DATE, 1); } BarDataSet set = new BarDataSet(entries, ""); set.setColor(akActivity.color); BarData data = new BarData(labels, set); data.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); LimitLine target = new LimitLine(mTargetSteps); barChart.getAxisLeft().getLimitLines().clear(); barChart.getAxisLeft().addLimitLine(target); setupLegend(barChart); barChart.setData(data); barChart.getLegend().setEnabled(false); }
private void setData(int count, float range) { ArrayList<String> xVals = new ArrayList<String>(); for (int i = 0; i < count; i++) { xVals.add(mMonths[i % 12]); } ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); for (int i = 0; i < count; i++) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); calendar.set(year, month, day, 2 * i, 0, 0); Long nowTime = calendar.getTime().getTime(); calendar.set(year, month, day, 2 * (i + 1), 0, 0); Long nextTime = calendar.getTime().getTime(); // 数据库查询 Float val; List<State> states = cupboard() .withDatabase(db) .query(State.class) .withSelection( "time_point > ? AND time_point < ?", nowTime.toString(), nextTime.toString()) .list(); if (states.isEmpty()) { val = 0.0f; } else if (states.size() == 1) { val = 0.0f; } else { State firstState = states.get(0); State lastState = states.get(states.size() - 1); val = Float.parseFloat(lastState.getVentilation_volume()) - Float.parseFloat(firstState.getVentilation_volume()); } yVals1.add(new BarEntry(val, i)); } BarDataSet set1 = new BarDataSet(yVals1, "单位时间吸入的空气量"); set1.setBarSpacePercent(35f); ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>(); dataSets.add(set1); BarData data = new BarData(xVals, dataSets); data.setValueTextSize(10f); data.setValueTypeface(mTf); mChart.setData(data); }
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); }
/** * draws the x-labels on the specified y-position * * @param pos */ @Override protected void drawLabels(Canvas c, float pos) { // pre allocate to save performance (dont allocate in loop) float[] position = new float[] {0f, 0f}; BarData bd = mChart.getData(); int step = bd.getDataSetCount(); for (int i = mMinX; i <= mMaxX; i += mXAxis.mAxisLabelModulus) { position[0] = i * step + i * bd.getGroupSpace() + bd.getGroupSpace() / 2f; // consider groups (center label for each group) if (step > 1) { position[0] += ((float) step - 1f) / 2f; } mTrans.pointValuesToPixel(position); if (mViewPortHandler.isInBoundsX(position[0]) && i >= 0 && i < mXAxis.getValues().size()) { String label = mXAxis.getValues().get(i); int color = mXAxis.getTextColor(i); mAxisLabelPaint.setColor(color); if (mXAxis.isAvoidFirstLastClippingEnabled()) { // avoid clipping of the last if (i == mXAxis.getValues().size() - 1) { float width = Utils.calcTextWidth(mAxisLabelPaint, label); if (width > mViewPortHandler.offsetRight() * 2 && position[0] + width > mViewPortHandler.getChartWidth()) position[0] -= width / 2; // avoid clipping of the first } else if (i == 0) { float width = Utils.calcTextWidth(mAxisLabelPaint, label); position[0] += width / 2; } } c.drawText(label, position[0], pos, mAxisLabelPaint); } } }
public BarData horizontalBarDataGastos(List<ParseObject> gastos) { // mChart.animateY(2500); ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); ArrayList<String> xVals = new ArrayList<String>(); for (int i = 0; i < gastos.size(); i++) { xVals.add(gastos.get(i).getNumber("mes").toString()); yVals1.add(new BarEntry(gastos.get(i).getNumber("mes").floatValue(), i)); } BarDataSet set1 = new BarDataSet(yVals1, "Gastos"); BarData data = new BarData(xVals, set1); data.setValueTextSize(10f); data.setValueTypeface(tf); return data; }
private void setData(List<Integer> valueList, String format) { mChart.setVisibility(View.VISIBLE); ArrayList<String> xVals = new ArrayList<String>(); ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); if (valueList != null) { for (int i = 0; i < valueList.size(); i++) { xVals.add(i + "s"); yVals1.add(new BarEntry(valueList.get(i), i)); } } String legend = getResources().getString(R.string.caption_receptin_rate); if (!format.equals("%")) legend = getResources().getString(R.string.caption_packet_count); BarDataSet set1 = new BarDataSet(yVals1, legend); set1.setBarSpacePercent(35f); set1.setColor(Color.parseColor("#0288D1")); ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>(); dataSets.add(set1); BarData data = new BarData(xVals, dataSets); data.setValueTextSize(10f); data.setDrawValues(false); YAxisValueFormatter custom = new DataAxisFormatter(format); YAxis leftAxis = mChart.getAxisLeft(); YAxis rightAxis = mChart.getAxisRight(); leftAxis.setValueFormatter(custom); rightAxis.setValueFormatter(custom); mChart.setData(data); }
/** * Transforms an List of Entry into a float array containing the x and y values transformed with * all matrices for the BARCHART. * * @param entries * @param dataSet the dataset index * @return */ public float[] generateTransformedValuesHorizontalBarChart( List<? extends Entry> entries, int dataSet, BarData bd, float phaseY) { float[] valuePoints = new float[entries.size() * 2]; int setCount = bd.getDataSetCount(); float space = bd.getGroupSpace(); for (int j = 0; j < valuePoints.length; j += 2) { Entry e = entries.get(j / 2); // calculate the x-position, depending on datasetcount float x = e.getXIndex() + (j / 2 * (setCount - 1)) + dataSet + space * (j / 2) + space / 2f; float y = e.getVal(); valuePoints[j] = y * phaseY; valuePoints[j + 1] = x; } pointValuesToPixel(valuePoints); return valuePoints; }
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(); }
/** Get the selected data and show it. */ private void drawData() { // Get shopping list in the dates range Select selectSLInRange = Select.from(ShoppingList.class) .whereOr( Condition.prop("date").gt(minDate.getTime().getTime()), Condition.prop("date").eq(minDate.getTime().getTime())) .and(Condition.prop("date").lt(maxDate.getTime().getTime())); List<ShoppingList> shoppingLists = selectSLInRange.list(); // Show number of shopping lists retrieved Toast.makeText(getActivity(), shoppingLists.size() + " shopping lists", Toast.LENGTH_SHORT) .show(); // If there's one or more, draw the chart // Each year is painted in one color. All the shopping list of one month must be // merge to get the total spend money amount of that month if (shoppingLists.size() > 0) { String[] mMonths = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; Collections.sort(shoppingLists); Map<String, List<BarEntry>> entrysYear = new HashMap<>(); List<String> labels = new ArrayList<>(); // First item Date date = shoppingLists.get(0).getDate(); Calendar cal = getInstance(); cal.setTime(date); int year = cal.get(YEAR); int month = cal.get(MONTH); entrysYear.put(year + "", new ArrayList<BarEntry>()); labels.add(mMonths[month % 12]); float totalPrice = 0; int newYear; int newMonth; for (int i = 0; i < shoppingLists.size(); i++) { Date d = shoppingLists.get(i).getDate(); Calendar c = getInstance(); c.setTime(d); newYear = c.get(YEAR); newMonth = c.get(MONTH); if (newMonth == month && newYear == year) { totalPrice += shoppingLists.get(i).getTotalPrice(); } else { // If not same year -> add new one if (newYear != year) { entrysYear.put(newYear + "", new ArrayList<BarEntry>()); } // If not same month -> save previous month BarEntry be = new BarEntry(totalPrice, labels.size() - 1); entrysYear.get(year + "").add(be); // Add new month labels.add(mMonths[newMonth % 12]); totalPrice = shoppingLists.get(i).getTotalPrice(); } year = newYear; month = newMonth; } // Save last one BarEntry be = new BarEntry(totalPrice, labels.size() - 1); entrysYear.get(year + "").add(be); BarData data = new BarData(labels); int i = 0; for (String name : entrysYear.keySet()) { BarDataSet dataset = new BarDataSet(entrysYear.get(name), name); dataset.setColor(ColorTemplate.COLORFUL_COLORS[i % ColorTemplate.COLORFUL_COLORS.length]); dataset.setBarSpacePercent(35f); data.addDataSet(dataset); i++; } data.setValueTextSize(10f); chart1.setData(data); chart1.notifyDataSetChanged(); // let the chart know it's data changed chart1.invalidate(); // refresh chart1.animateXY(1000, 1000); } }