private void collectFuelStats(int v, Cursor fuelCursor) { long MILLIPERDAY = 1000 * 60 * 60 * 24; long DAYSPERYEAR = 365; long MONPERYEAR = 12; float tare = 0; prevDist = 0; Double sumC = 0d; Double sumE = 0d; Double y0Min; Double y0Max; Double y1Min; Double y1Max; float cost_per_day; long days; float total_cost; int count = 0; // need 2 or more datapoints to analyze fuel usage. The first one // we'll use the odometer reading as tare and ignore fuel/qty/price for average. // at the second filling and beyond, we then can calculate fuel usage relative to // cost and price and distance if (fuelCursor.getCount() > 2) { fuelCursor.moveToFirst(); // discard this one except for dist, which is starting int numData = 0; // fuelCursor.getCount()-2; // start at the end since this is time based and fill // backward float dist = fuelCursor.getFloat(fuelCursor.getColumnIndex(mDbHelper.KEY_DIST)); long first_day = fuelCursor.getLong(fuelCursor.getColumnIndex(mDbHelper.KEY_DATE)); long last_day; tare = dist; total_cost = 0; fuelCursor.moveToNext(); do { Long date = fuelCursor.getLong(fuelCursor.getColumnIndex(mDbHelper.KEY_DATE)); int ddate = (int) ((today - date) / millisecPerDay); Log.d("GAS:", "numData: ddate=" + numData + " : " + ddate); float cost = fuelCursor.getFloat(fuelCursor.getColumnIndex(mDbHelper.KEY_COST)); float price = fuelCursor.getFloat(fuelCursor.getColumnIndex(mDbHelper.KEY_PRICE)); float qty = fuelCursor.getFloat(fuelCursor.getColumnIndex(mDbHelper.KEY_QTY)); float thisDist = fuelCursor.getFloat(fuelCursor.getColumnIndex(mDbHelper.KEY_DIST)); last_day = fuelCursor.getLong(fuelCursor.getColumnIndex(mDbHelper.KEY_DATE)); float delta = thisDist - tare; float efficiency = delta / qty; float costperd = (price * qty) / delta; mCurrentCostSeries.add(date, costperd); mCurrentEffSeries.add(date, efficiency); sumC += costperd; sumE += efficiency; total_cost += cost; count++; tare = thisDist; // just get distance per fuel filling numData++; } while (fuelCursor.moveToNext()); String fs, fsm, fsy; float dailyCost = total_cost / (float) ((last_day - first_day) / MILLIPERDAY); float yearlyCost = dailyCost * DAYSPERYEAR; fs = String.format("%.1f /day ", dailyCost); fsy = String.format("%.0f/yr", yearlyCost); mDailyCost.setText("Average Cost " + cunits + fs + cunits + fsy); avgCost = sumC / count; fs = String.format(avgCostTitle + ":" + "%.1f", avgCost); mRenderer.addYTextLabel(avgCost, fs, 0); avgEff = sumE / count; fs = String.format(avgEffTitle + ":" + "%.1f", avgEff); mRenderer.addYTextLabel(avgEff, fs, 1); y0Min = mCurrentCostSeries.getMinY(); y0Max = mCurrentCostSeries.getMaxY(); y1Min = mCurrentEffSeries.getMinY(); y1Max = mCurrentEffSeries.getMaxY(); // set up chart min/max so that charts aren't scrunched together mRenderer.setYAxisMin(y0Min - (y0Max - y0Min) / 2, 0); mRenderer.setYAxisMax(y0Max + (y0Max - y0Min) / 2, 0); // set the efficiency to be lower mRenderer.setYAxisMin(0, 1); mRenderer.setYAxisMax(y1Max + (y1Max - y1Min) / 2, 1); } else { // no data. barf on user Toast.makeText( this, getApplicationContext().getResources().getString(R.string.nodata_err), Toast.LENGTH_SHORT) .show(); } }
private void setGraphicalView(@Nullable PlotBoundaries plotBoundaries) { double minValue = plotBoundaries == null ? DEFAULT_MIN_NUMBER : plotBoundaries.xMin; double maxValue = plotBoundaries == null ? DEFAULT_MAX_NUMBER : plotBoundaries.xMax; final ViewGroup graphContainer = (ViewGroup) findViewById(R.id.plot_view_container); if (graphicalView != null) { graphContainer.removeView(graphicalView); } chart = prepareChart(minValue, maxValue, expression, variable); // reverting boundaries (as in prepareChart() we add some cached values ) double minX = Double.MAX_VALUE; double minY = Double.MAX_VALUE; double maxX = Double.MIN_VALUE; double maxY = Double.MIN_VALUE; for (XYSeries series : chart.getDataset().getSeries()) { minX = Math.min(minX, series.getMinX()); minY = Math.min(minY, series.getMinY()); maxX = Math.max(maxX, series.getMaxX()); maxY = Math.max(maxY, series.getMaxY()); } Log.d( CalculatorPlotActivity.class.getName(), "min x: " + minX + ", min y: " + minY + ", max x: " + maxX + ", max y: " + maxY); Log.d(CalculatorPlotActivity.class.getName(), "Plot boundaries are " + plotBoundaries); if (plotBoundaries == null) { chart.getRenderer().setXAxisMin(Math.max(minX, minValue)); chart.getRenderer().setYAxisMin(Math.max(minY, minValue)); chart.getRenderer().setXAxisMax(Math.min(maxX, maxValue)); chart.getRenderer().setYAxisMax(Math.min(maxY, maxValue)); } else { chart.getRenderer().setXAxisMin(plotBoundaries.xMin); chart.getRenderer().setYAxisMin(plotBoundaries.yMin); chart.getRenderer().setXAxisMax(plotBoundaries.xMax); chart.getRenderer().setYAxisMax(plotBoundaries.yMax); } graphicalView = new GraphicalView(this, chart); graphicalView.addZoomListener( new ZoomListener() { @Override public void zoomApplied(ZoomEvent e) { updateDataSets(chart); } @Override public void zoomReset() { updateDataSets(chart); } }, true, true); graphicalView.addPanListener( new PanListener() { @Override public void panApplied() { Log.d(TAG, "org.achartengine.tools.PanListener.panApplied"); updateDataSets(chart); } }); graphContainer.addView(graphicalView); updateDataSets(chart, 50); }