annotate gnv-artifacts/src/main/java/de/intevation/gnv/chart/DefaultHistogram.java @ 1107:86ca3c10523f

Adjust the format of axes labels in histograms corresponding the specified locale (issue316). gnv-artifacts/trunk@1238 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 29 Jun 2010 07:37:36 +0000
parents bb2679624c6a
children f953c9a559d8
rev   line source
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
1 package de.intevation.gnv.chart;
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
2
1107
86ca3c10523f Adjust the format of axes labels in histograms corresponding the specified locale (issue316).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1055
diff changeset
3 import java.util.Locale;
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
4 import java.util.Map;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
5
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
6 import org.apache.log4j.Logger;
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
7
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
8 import org.jfree.chart.ChartTheme;
623
65f09139e9b3 Fixed file handle leak when writing meta data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 617
diff changeset
9
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
10 import org.jfree.chart.plot.XYPlot;
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
11
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
12 /**
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
13 * Default implementation of {@link de.intevation.gnv.chart.AbstractHistogram}.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
14 *
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
15 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
16 */
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
17 public class DefaultHistogram
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
18 extends AbstractHistogram
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
19 {
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
20 /**
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
21 * Default bin count.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
22 * TODO find a better default value
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
23 */
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
24 public static final int DEFAULT_BINS = 15;
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
25
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
26 /**
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
27 * Constant field for limitating the number of bin in a single histogram.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
28 */
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
29 public static final int MAXIMAL_BINS = 20;
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
30
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
31 /**
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
32 * Default key to retrieve the number of bins from {@link
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
33 * #requestParameter}.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
34 */
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
35 public static final String REQUEST_KEY_BIN_COUNT = "bincount";
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
36
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
37 /**
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
38 * Default key to retrieve the width of a single bin from {@link
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
39 * #requestParameter}.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
40 */
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
41 public static final String REQUEST_KEY_BIN_WIDTH = "binwidth";
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
42
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
43 /**
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
44 * Default key to retrieve the chart width from {@link #requestParameter}.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
45 */
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
46 public static final String REQUEST_KEY_CHART_WIDTH = "width";
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
47
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
48 /**
778
9a828e5a2390 Removed trailing whitespace
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 767
diff changeset
49 * Default key to retrieve the object from {@link #requestParameter}. It
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
50 * defines which value this chart has to be used for bin calculation. You
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
51 * can either adjust the number of bins or the width of a single bin.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
52 */
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
53 public static final String REQUEST_KEY_BIN_CHOICE = "bintype";
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
54
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
55 /**
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
56 * Logger used for logging with log4j.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
57 */
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
58 private static Logger logger = Logger.getLogger(DefaultHistogram.class);
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
59
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
60 /**
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
61 * Object storing some further parameter used for chart settings.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
62 */
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
63 protected Map requestParameter;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
64
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
65 protected double[] minmax = null;
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
66
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
67
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
68 /**
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
69 * Constructor to create DefaultHistogram objects.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
70 *
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
71 * @param labels Labels to decorate this chart.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
72 * @param data Raw data to be displayed in histogram.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
73 * @param theme Theme used to adjust the chart look.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
74 * @param requestParameter Object which serves some further settings.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
75 */
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
76 public DefaultHistogram(
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
77 ChartLabels labels, Object[] data, ChartTheme theme, Map requestParameter
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
78 ) {
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
79 super(labels, data, theme);
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
80 this.requestParameter = requestParameter;
1107
86ca3c10523f Adjust the format of axes labels in histograms corresponding the specified locale (issue316).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1055
diff changeset
81 this.locale = (Locale) requestParameter.get("locale");
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
82 }
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
83
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
84
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
85 @Override
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
86 protected void applyDatasets() {
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
87 XYPlot plot = (XYPlot) chart.getPlot();
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
88
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
89 // prepare data and create add them to histogram dataset
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
90 String name = (String) data[0];
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
91 double[] values = toDouble((Double[]) data[1]);
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
92
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
93 double binWidth = getBinWidth(values);
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
94 int binCount = getBinCount();
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
95
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
96 AdvancedHistogramDataset dataset =
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
97 new AdvancedHistogramDataset(binCount, binWidth);
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
98 dataset.addSeries(name, values);
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
99
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
100 plot.setDataset(0, dataset);
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
101 }
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
102
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
103
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
104 /**
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
105 * Method which scans the hole bunch of values and returns an array with
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
106 * contains min and max value. Min value is stored at position 0, max value
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
107 * is stored at position 1 in that array.
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
108 *
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
109 * @param values Array which contains all values
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
110 *
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
111 * @return Array which contains min and max value
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
112 */
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
113 protected double[] getMinMax(double[] values) {
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
114 if (minmax != null)
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
115 return minmax;
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
116
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
117 double[] minmax = new double[2];
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
118 minmax[0] = Double.MAX_VALUE;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
119 minmax[1] = Double.MIN_VALUE;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
120
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
121 int length = values.length;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
122 for (int i = 0; i < length; i++) {
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
123 minmax[0] = values[i] < minmax[0] ? values[i] : minmax[0];
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
124 minmax[1] = values[i] > minmax[1] ? values[i] : minmax[1];
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
125 }
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
126
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
127 this.minmax = minmax;
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
128
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
129 return minmax;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
130 }
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
131
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
132
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
133 /**
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
134 * Turn a Double[] into a double[].
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
135 *
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
136 * @param array Doube[]
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
137 *
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
138 * @return double[]
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
139 */
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
140 protected double[] toDouble(Double[] array) {
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
141 int length = array.length;
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
142 double[] values = new double[length];
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
143
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
144 for(int i = 0; i < length; i++) {
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
145 values[i] = array[i].doubleValue();
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
146 }
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
147
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
148 return values;
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
149 }
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
150
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
151
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
152 /**
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
153 * Method to retrieve the number of bins.
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
154 *
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
155 * @return the number of bins that is specified in <i>requestParameter</i>
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
156 * or -1 if the number of bins is not the dominant value to calculate the
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
157 * width of a single bin.
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
158 */
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
159 protected int getBinCount() {
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
160 // Return -1 to trigger a calculation of the number of bins in
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
161 // AdvancedHistogramDataset if the user chose the bin width as dominant
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
162 // value.
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
163 String choice = (String) requestParameter.get(REQUEST_KEY_BIN_CHOICE);
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
164 if (choice != null && choice.equalsIgnoreCase(REQUEST_KEY_BIN_WIDTH)) {
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
165 return -1;
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
166 }
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
167
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
168 int bins = -1;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
169 String param = (String) requestParameter.get(REQUEST_KEY_BIN_COUNT);
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
170
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
171 try {
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
172 bins = Integer.parseInt(param);
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
173 bins = bins <= 0 ? DEFAULT_BINS : bins;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
174 bins = bins > MAXIMAL_BINS ? MAXIMAL_BINS : bins;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
175
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
176 return bins;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
177 }
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
178 catch (NumberFormatException nfe) {
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
179 logger.warn("Invalid number of bins for histogram chart: " + param);
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
180 logger.warn("Return default bins: " + DEFAULT_BINS);
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
181
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
182 return DEFAULT_BINS;
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
183 }
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
184 }
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
185
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
186
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
187 /**
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
188 * Serves width of a single bin.
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
189 *
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
190 * @param values All values in this histogram
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
191 *
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
192 * @return The bin width that is given in <i>requestParameter</i> or -1 if
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
193 * the bin width is not the dominant value for calculating the number of
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
194 * bins in the histogram.
767
79401c871da4 Added and repaired javadoc in de.intevation.gnv.chart package.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 629
diff changeset
195 */
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
196 protected double getBinWidth(double[] values) {
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
197 // Return -1 to trigger a calculation of the bin width in
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
198 // AdvancedHistogramDataset if the user chose the number of bins as
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
199 // dominant value.
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
200 String choice = (String) requestParameter.get(REQUEST_KEY_BIN_CHOICE);
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
201 if (choice == null || !choice.equalsIgnoreCase(REQUEST_KEY_BIN_WIDTH)) {
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
202 return -1;
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
203 }
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
204
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
205 int bins = -1;
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
206 String param = (String) requestParameter.get(REQUEST_KEY_BIN_WIDTH);
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
207
1054
8430269ec73b Removed the parsing for double/integer values specifying the bin width or the number of bins in DefaultHistogram, because these inserted values are no longer i18n strings (issue289).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 815
diff changeset
208 double[] minmax = getMinMax(values);
8430269ec73b Removed the parsing for double/integer values specifying the bin width or the number of bins in DefaultHistogram, because these inserted values are no longer i18n strings (issue289).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 815
diff changeset
209 double totalWidth = minmax[1] - minmax[0];
8430269ec73b Removed the parsing for double/integer values specifying the bin width or the number of bins in DefaultHistogram, because these inserted values are no longer i18n strings (issue289).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 815
diff changeset
210 double binWidth = Double.parseDouble(param);
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
211
1054
8430269ec73b Removed the parsing for double/integer values specifying the bin width or the number of bins in DefaultHistogram, because these inserted values are no longer i18n strings (issue289).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 815
diff changeset
212 double tmpBins = totalWidth / binWidth;
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
213
1054
8430269ec73b Removed the parsing for double/integer values specifying the bin width or the number of bins in DefaultHistogram, because these inserted values are no longer i18n strings (issue289).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 815
diff changeset
214 bins = (int) Math.round(tmpBins);
8430269ec73b Removed the parsing for double/integer values specifying the bin width or the number of bins in DefaultHistogram, because these inserted values are no longer i18n strings (issue289).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 815
diff changeset
215 bins = bins <= 0 ? DEFAULT_BINS : bins;
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
216
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
217 // the calculated number of bins with the given width exceed the maximum
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
218 // number of bins.
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
219 if (bins > MAXIMAL_BINS) {
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
220 return totalWidth / (MAXIMAL_BINS);
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
221 }
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
222
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).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1054
diff changeset
223 return binWidth;
629
d08b9ba148c5 Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 623
diff changeset
224 }
617
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
225 }
3d13fa281a7e Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
226 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org