public static Month getMonth(final String id) { for (Month aMonth : Month.values()) { if (aMonth.toString().equals(id)) { return aMonth; } } throw new IllegalArgumentException("invalid calendar value " + id); }
/** * DESCRIPTION: Obtains (x,y) values from the current data set for plotting. Also calculates the * range (min/max) of x-axis and y-axis values for the series. * * @return a SimpleXYSeries instance containing (x,y) values to plot. */ private SimpleXYSeries getPlotSeries() { final String tag = TAG + ".getPlotSeries()"; // create lists of x-axis, and y-axis numbers to plot List<Number> xNumbers = new LinkedList<Number>(); List<Number> yNumbers = new LinkedList<Number>(); // get numbers to plot from gas record monthly data, where (x,y) is: // x = sequential index [0..n] with labels mapped to specific months // y = calculated gallons used for that month sumy = 0; miny = Float.MAX_VALUE; maxy = Float.MIN_VALUE; minx = Long.MAX_VALUE; maxx = Long.MIN_VALUE; xlabels.clear(); long x = 0L; float y = 0f; for (Month month : PlotActivity.monthly) { y = PlotActivity.monthly.getTrips(month).getGallons(); Log.d(tag, "month=" + month.toString() + " x=" + x + " y=" + y); minx = Math.min(minx, x); maxx = Math.max(maxx, x); miny = Math.min(miny, y); maxy = Math.max(maxy, y); xNumbers.add(x); yNumbers.add(y); sumy += y; xlabels.put(x++, month.getLabel()); } // adjust min/max values if no data if (xNumbers.isEmpty()) minx = maxx = 0; if (yNumbers.isEmpty()) miny = maxy = 0; // calculate average for the series average = 0; if (!yNumbers.isEmpty()) { average = sumy / yNumbers.size(); } Log.d(tag, "minx=" + minx + " maxx=" + maxx); Log.d(tag, "miny=" + miny + " maxy=" + maxy); Log.d(tag, "sumy=" + sumy + " size=" + yNumbers.size() + " average=" + average); // create a new series from the x and y axis numbers String title = ""; return new SimpleXYSeries(xNumbers, yNumbers, title); }
/** * @param date * @throws DateParseException */ public void parseDate(String date) throws DateParseException { String[] dateChunks = date.split(DELIMETER); if (dateChunks.length != 3) { throw new DateParseException( "Date not properly formatted, " + date + " should have been in the form DD-MM-YYYY"); } try { year = Integer.parseInt(dateChunks[2]); if (year > YEAR_MAX || year < YEAR_MIN) { throw new DateParseException( "Year field for " + date + " is beyond the valid range of " + YEAR_MIN + " and " + YEAR_MAX); } } catch (NumberFormatException nfe) { throw new DateParseException("Year field for " + date + " did not parse properly"); } try { month = Month.values()[Integer.parseInt(dateChunks[1])]; } catch (Exception ex) { // covers Array Index Out of Bounds throw new DateParseException("Month field for " + date + " did not parse properly"); } try { dayOfMonth = Integer.parseInt(dateChunks[0]); month.setYear(this.year); if (dayOfMonth > month.daysInMonth()) { throw new DateParseException( "Day field for " + date + " was out of range for month " + month.toString()); } } catch (NumberFormatException nfe) { throw new DateParseException("Day field for " + date + " did not parse properly"); } }