Mercurial > dive4elements > gnv-client
annotate gnv-artifacts/src/main/java/de/intevation/gnv/chart/DefaultHistogram.java @ 629:d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
gnv-artifacts/trunk@706 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 24 Feb 2010 14:30:52 +0000 |
parents | 65f09139e9b3 |
children | 79401c871da4 |
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 |
629
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
3 import java.text.NumberFormat; |
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.text.ParseException; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
5 import java.util.Locale; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
6 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
|
7 |
617
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
8 import org.apache.log4j.Logger; |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
9 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
10 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
|
11 |
617
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
12 import org.jfree.chart.plot.XYPlot; |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
13 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
14 import org.jfree.data.statistics.HistogramDataset; |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
15 |
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 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
18 */ |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
19 public class DefaultHistogram |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
20 extends AbstractHistogram |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
21 { |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
22 // TODO take a better default value |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
23 public static final int DEFAULT_BINS = 15; |
629
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
24 public static final int MAXIMAL_BINS = 20; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
25 public static final String REQUEST_KEY_BIN_COUNT = "bincount"; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
26 public static final String REQUEST_KEY_BIN_WIDTH = "binwidth"; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
27 public static final String REQUEST_KEY_CHART_WIDTH = "width"; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
28 public static final String REQUEST_KEY_LOCALE = "locale"; |
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 String REQUEST_KEY_BIN_CHOICE = "bintype"; |
617
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
30 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
31 private static Logger logger = Logger.getLogger(DefaultHistogram.class); |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
32 |
629
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
33 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
|
34 |
617
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
35 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
36 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
|
37 ChartLabels labels, Object[] data, ChartTheme theme, Map requestParameter |
617
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
38 ) { |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
39 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
|
40 this.requestParameter = requestParameter; |
617
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
41 } |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
42 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
43 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
44 protected void applyDatasets() { |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
45 XYPlot plot = (XYPlot) chart.getPlot(); |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
46 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
47 // prepare data and create add them to histogram dataset |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
48 String name = (String) data[0]; |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
49 double[] values = toDouble((Double[]) data[1]); |
629
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
50 int bins = getBinCount(values); |
617
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
51 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
52 HistogramDataset dataset = new HistogramDataset(); |
629
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
53 dataset.addSeries(name, values, bins); |
617
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
54 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
55 plot.setDataset(0, dataset); |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
56 } |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
57 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
58 |
629
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
59 protected double[] getMinMax(double[] values) { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
60 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
|
61 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
|
62 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
|
63 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 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
|
68 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
69 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
70 return minmax; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
71 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
72 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
73 |
617
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
74 protected double[] toDouble(Double[] array) { |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
75 int length = array.length; |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
76 double[] values = new double[length]; |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
77 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
78 for(int i = 0; i < length; i++) { |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
79 values[i] = array[i].doubleValue(); |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
80 } |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
81 |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
82 return values; |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
83 } |
629
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
84 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
85 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
86 protected int getBinCount(double[] values) { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
87 String param = (String) requestParameter.get(REQUEST_KEY_BIN_CHOICE); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
88 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
89 if (param != null && param.equalsIgnoreCase(REQUEST_KEY_BIN_WIDTH)) { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
90 return getBinCountByWidth(values); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
91 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
92 else { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
93 return getBinCountByNumber(); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
94 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
95 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
96 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
97 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
98 protected int getBinCountByNumber() { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
99 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
|
100 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
|
101 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
102 try { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
103 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
|
104 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
|
105 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
|
106 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
107 return bins; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
108 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
109 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
|
110 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
|
111 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
|
112 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
113 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
|
114 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
115 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
116 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
117 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
118 protected int getBinCountByWidth(double[] values) { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
119 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
|
120 String param = (String) requestParameter.get(REQUEST_KEY_BIN_WIDTH); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
121 Locale locale = (Locale) requestParameter.get(REQUEST_KEY_LOCALE); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
122 NumberFormat format = NumberFormat.getInstance(locale); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
123 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
124 try { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
125 double[] minmax = getMinMax(values); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
126 double totalWidth = minmax[1] - minmax[0]; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
127 Number number = format.parse(param); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
128 double binWidth = -1d; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
129 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
130 if (number instanceof Double) { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
131 binWidth = ((Double) number).doubleValue(); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
132 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
133 else if (number instanceof Long) { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
134 binWidth = ((Long) number).doubleValue(); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
135 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
136 else if (number instanceof Integer) { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
137 binWidth = ((Integer) number).doubleValue(); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
138 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
139 else { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
140 logger.warn("Invalid bin width 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
|
141 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
|
142 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
143 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
|
144 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
145 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
146 double tmpBins = totalWidth / binWidth; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
147 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
148 bins = (int) Math.round(tmpBins); |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
149 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
|
150 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
|
151 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
152 return bins; |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
153 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
154 catch (ParseException pe) { |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
155 logger.warn("Invalid bin width 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
|
156 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
|
157 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
158 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
|
159 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
160 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
|
161 logger.warn("Invalid bin width 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
|
162 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
|
163 |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
164 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
|
165 } |
d08b9ba148c5
Implemented logic to adjust number of bins corresponding to user input.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
623
diff
changeset
|
166 } |
617
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
167 } |
3d13fa281a7e
Added new ouput mode: histogram.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
168 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |