Mercurial > dive4elements > gnv-client
diff gnv-artifacts/src/main/java/de/intevation/gnv/chart/DefaultHistogram.java @ 1055:bb2679624c6a
Implemented a new histogram dataset that takes the width of a single bin as well as the number of bins for the histogram (issue288).
gnv-artifacts/trunk@1130 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 27 May 2010 07:41:14 +0000 |
parents | 8430269ec73b |
children | 86ca3c10523f |
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/DefaultHistogram.java Wed May 26 17:01:29 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/DefaultHistogram.java Thu May 27 07:41:14 2010 +0000 @@ -1,9 +1,5 @@ package de.intevation.gnv.chart; -import java.text.NumberFormat; -import java.text.ParseException; - -import java.util.Locale; import java.util.Map; import org.apache.log4j.Logger; @@ -12,8 +8,6 @@ import org.jfree.chart.plot.XYPlot; -import org.jfree.data.statistics.HistogramDataset; - /** * Default implementation of {@link de.intevation.gnv.chart.AbstractHistogram}. * @@ -67,6 +61,8 @@ */ protected Map requestParameter; + protected double[] minmax = null; + /** * Constructor to create DefaultHistogram objects. @@ -91,10 +87,13 @@ // prepare data and create add them to histogram dataset String name = (String) data[0]; double[] values = toDouble((Double[]) data[1]); - int bins = getBinCount(values); - HistogramDataset dataset = new HistogramDataset(); - dataset.addSeries(name, values, bins); + double binWidth = getBinWidth(values); + int binCount = getBinCount(); + + AdvancedHistogramDataset dataset = + new AdvancedHistogramDataset(binCount, binWidth); + dataset.addSeries(name, values); plot.setDataset(0, dataset); } @@ -110,6 +109,9 @@ * @return Array which contains min and max value */ protected double[] getMinMax(double[] values) { + if (minmax != null) + return minmax; + double[] minmax = new double[2]; minmax[0] = Double.MAX_VALUE; minmax[1] = Double.MIN_VALUE; @@ -120,6 +122,8 @@ minmax[1] = values[i] > minmax[1] ? values[i] : minmax[1]; } + this.minmax = minmax; + return minmax; } @@ -144,38 +148,21 @@ /** - * Method to calculate the number of bins this chart should be parted into. - * The real calculation takes place in {@link #getBinCountByNumber} and - * {@link #getBinCountByWidth}. This method switches between these methods - * depending on the object stored in {@link #requestParameter}. - * - * @param values All values used in this histogram + * Method to retrieve the number of bins. * - * @return Number of bins + * @return the number of bins that is specified in <i>requestParameter</i> + * or -1 if the number of bins is not the dominant value to calculate the + * width of a single bin. */ - protected int getBinCount(double[] values) { - String param = (String) requestParameter.get(REQUEST_KEY_BIN_CHOICE); - - if (param != null && param.equalsIgnoreCase(REQUEST_KEY_BIN_WIDTH)) { - return getBinCountByWidth(values); + protected int getBinCount() { + // Return -1 to trigger a calculation of the number of bins in + // AdvancedHistogramDataset if the user chose the bin width as dominant + // value. + String choice = (String) requestParameter.get(REQUEST_KEY_BIN_CHOICE); + if (choice != null && choice.equalsIgnoreCase(REQUEST_KEY_BIN_WIDTH)) { + return -1; } - else { - return getBinCountByNumber(); - } - } - - /** - * Method to retrieve the number of bins. If {@link #requestParameter} - * contains a valid <code>Integer</code> at - * <code>REQUEST_KEY_BIN_COUNT</code> and this is smaller than or equal - * {@link #MAXIMAL_BINS}, this value is used. If no valid - * <code>Integer</code> is given or if the value in {@link #requestParameter} - * is bigger than {@link #MAXIMAL_BINS}, {@link #DEFAULT_BINS} is used. - * - * @return Number of bins - */ - protected int getBinCountByNumber() { int bins = -1; String param = (String) requestParameter.get(REQUEST_KEY_BIN_COUNT); @@ -196,16 +183,25 @@ /** - * Serves the number of bins depending on a given width for each bin, but - * maximum bin count is limited by {@link #MAXIMAL_BINS}. + * Serves width of a single bin. * * @param values All values in this histogram * - * @return Number of bins + * @return The bin width that is given in <i>requestParameter</i> or -1 if + * the bin width is not the dominant value for calculating the number of + * bins in the histogram. */ - protected int getBinCountByWidth(double[] values) { - int bins = -1; - String param = (String) requestParameter.get(REQUEST_KEY_BIN_WIDTH); + protected double getBinWidth(double[] values) { + // Return -1 to trigger a calculation of the bin width in + // AdvancedHistogramDataset if the user chose the number of bins as + // dominant value. + String choice = (String) requestParameter.get(REQUEST_KEY_BIN_CHOICE); + if (choice == null || !choice.equalsIgnoreCase(REQUEST_KEY_BIN_WIDTH)) { + return -1; + } + + int bins = -1; + String param = (String) requestParameter.get(REQUEST_KEY_BIN_WIDTH); double[] minmax = getMinMax(values); double totalWidth = minmax[1] - minmax[0]; @@ -215,9 +211,14 @@ bins = (int) Math.round(tmpBins); bins = bins <= 0 ? DEFAULT_BINS : bins; - bins = bins > MAXIMAL_BINS ? MAXIMAL_BINS : bins; - return bins; + // the calculated number of bins with the given width exceed the maximum + // number of bins. + if (bins > MAXIMAL_BINS) { + return totalWidth / (MAXIMAL_BINS); + } + + return binWidth; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :