comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/DefaultHistogram.java @ 767:79401c871da4

Added and repaired javadoc in de.intevation.gnv.chart package. gnv-artifacts/trunk@823 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 24 Mar 2010 14:48:55 +0000
parents d08b9ba148c5
children 9a828e5a2390
comparison
equal deleted inserted replaced
766:a23ce49423d5 767:79401c871da4
12 import org.jfree.chart.plot.XYPlot; 12 import org.jfree.chart.plot.XYPlot;
13 13
14 import org.jfree.data.statistics.HistogramDataset; 14 import org.jfree.data.statistics.HistogramDataset;
15 15
16 /** 16 /**
17 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) 17 * Default implementation of {@link de.intevation.gnv.chart.AbstractHistogram}.
18 *
19 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
18 */ 20 */
19 public class DefaultHistogram 21 public class DefaultHistogram
20 extends AbstractHistogram 22 extends AbstractHistogram
21 { 23 {
22 // TODO take a better default value 24 /**
25 * Default bin count.
26 * TODO find a better default value
27 */
23 public static final int DEFAULT_BINS = 15; 28 public static final int DEFAULT_BINS = 15;
29
30 /**
31 * Constant field for limitating the number of bin in a single histogram.
32 */
24 public static final int MAXIMAL_BINS = 20; 33 public static final int MAXIMAL_BINS = 20;
34
35 /**
36 * Default key to retrieve the number of bins from {@link
37 * #requestParameter}.
38 */
25 public static final String REQUEST_KEY_BIN_COUNT = "bincount"; 39 public static final String REQUEST_KEY_BIN_COUNT = "bincount";
40
41 /**
42 * Default key to retrieve the width of a single bin from {@link
43 * #requestParameter}.
44 */
26 public static final String REQUEST_KEY_BIN_WIDTH = "binwidth"; 45 public static final String REQUEST_KEY_BIN_WIDTH = "binwidth";
46
47 /**
48 * Default key to retrieve the chart width from {@link #requestParameter}.
49 */
27 public static final String REQUEST_KEY_CHART_WIDTH = "width"; 50 public static final String REQUEST_KEY_CHART_WIDTH = "width";
51
52 /**
53 * Default key to retrieve the <code>Locale</code> object from {@link
54 * #requestParameter} used for i18n support.
55 */
28 public static final String REQUEST_KEY_LOCALE = "locale"; 56 public static final String REQUEST_KEY_LOCALE = "locale";
57
58 /**
59 * Default key to retrieve the object from {@link #requestParameter}. It
60 * defines which value this chart has to be used for bin calculation. You
61 * can either adjust the number of bins or the width of a single bin.
62 */
29 public static final String REQUEST_KEY_BIN_CHOICE = "bintype"; 63 public static final String REQUEST_KEY_BIN_CHOICE = "bintype";
30 64
65 /**
66 * Logger used for logging with log4j.
67 */
31 private static Logger logger = Logger.getLogger(DefaultHistogram.class); 68 private static Logger logger = Logger.getLogger(DefaultHistogram.class);
32 69
70 /**
71 * Object storing some further parameter used for chart settings.
72 */
33 protected Map requestParameter; 73 protected Map requestParameter;
34 74
35 75
76 /**
77 * Constructor to create DefaultHistogram objects.
78 *
79 * @param labels Labels to decorate this chart.
80 * @param data Raw data to be displayed in histogram.
81 * @param theme Theme used to adjust the chart look.
82 * @param requestParameter Object which serves some further settings.
83 */
36 public DefaultHistogram( 84 public DefaultHistogram(
37 ChartLabels labels, Object[] data, ChartTheme theme, Map requestParameter 85 ChartLabels labels, Object[] data, ChartTheme theme, Map requestParameter
38 ) { 86 ) {
39 super(labels, data, theme); 87 super(labels, data, theme);
40 this.requestParameter = requestParameter; 88 this.requestParameter = requestParameter;
41 } 89 }
42 90
43 91
92 /**
93 * @see de.intevation.gnv.chart.AbstractHistogram#applyDatasets()
94 */
95 @Override
44 protected void applyDatasets() { 96 protected void applyDatasets() {
45 XYPlot plot = (XYPlot) chart.getPlot(); 97 XYPlot plot = (XYPlot) chart.getPlot();
46 98
47 // prepare data and create add them to histogram dataset 99 // prepare data and create add them to histogram dataset
48 String name = (String) data[0]; 100 String name = (String) data[0];
54 106
55 plot.setDataset(0, dataset); 107 plot.setDataset(0, dataset);
56 } 108 }
57 109
58 110
111 /**
112 * Method which scans the hole bunch of values and returns an array with
113 * contains min and max value. Min value is stored at position 0, max value
114 * is stored at position 1 in that array.
115 *
116 * @param values Array which contains all values
117 *
118 * @return Array which contains min and max value
119 */
59 protected double[] getMinMax(double[] values) { 120 protected double[] getMinMax(double[] values) {
60 double[] minmax = new double[2]; 121 double[] minmax = new double[2];
61 minmax[0] = Double.MAX_VALUE; 122 minmax[0] = Double.MAX_VALUE;
62 minmax[1] = Double.MIN_VALUE; 123 minmax[1] = Double.MIN_VALUE;
63 124
69 130
70 return minmax; 131 return minmax;
71 } 132 }
72 133
73 134
135 /**
136 * Turn a Double[] into a double[].
137 *
138 * @param array Doube[]
139 *
140 * @return double[]
141 */
74 protected double[] toDouble(Double[] array) { 142 protected double[] toDouble(Double[] array) {
75 int length = array.length; 143 int length = array.length;
76 double[] values = new double[length]; 144 double[] values = new double[length];
77 145
78 for(int i = 0; i < length; i++) { 146 for(int i = 0; i < length; i++) {
81 149
82 return values; 150 return values;
83 } 151 }
84 152
85 153
154 /**
155 * Method to calculate the number of bins this chart should be parted into.
156 * The real calculation takes place in {@link #getBinCountByNumber} and
157 * {@link #getBinCountByWidth}. This method switches between these methods
158 * depending on the object stored in {@link #requestParameter}.
159 *
160 * @param values All values used in this histogram
161 *
162 * @return Number of bins
163 */
86 protected int getBinCount(double[] values) { 164 protected int getBinCount(double[] values) {
87 String param = (String) requestParameter.get(REQUEST_KEY_BIN_CHOICE); 165 String param = (String) requestParameter.get(REQUEST_KEY_BIN_CHOICE);
88 166
89 if (param != null && param.equalsIgnoreCase(REQUEST_KEY_BIN_WIDTH)) { 167 if (param != null && param.equalsIgnoreCase(REQUEST_KEY_BIN_WIDTH)) {
90 return getBinCountByWidth(values); 168 return getBinCountByWidth(values);
93 return getBinCountByNumber(); 171 return getBinCountByNumber();
94 } 172 }
95 } 173 }
96 174
97 175
176 /**
177 * Method to retrieve the number of bins. If {@link #requestParameter}
178 * contains a valid <code>Integer</code> at
179 * <code>REQUEST_KEY_BIN_COUNT</code> and this is smaller than or equal
180 * {@link #MAXIMAL_BINS}, this value is used. If no valid
181 * <code>Integer</code> is given or if the value in {@link #requestParameter}
182 * is bigger than {@link #MAXIMAL_BINS}, {@link #DEFAULT_BINS} is used.
183 *
184 * @return Number of bins
185 */
98 protected int getBinCountByNumber() { 186 protected int getBinCountByNumber() {
99 int bins = -1; 187 int bins = -1;
100 String param = (String) requestParameter.get(REQUEST_KEY_BIN_COUNT); 188 String param = (String) requestParameter.get(REQUEST_KEY_BIN_COUNT);
101 189
102 try { 190 try {
113 return DEFAULT_BINS; 201 return DEFAULT_BINS;
114 } 202 }
115 } 203 }
116 204
117 205
206 /**
207 * Serves the number of bins depending on a given width for each bin, but
208 * maximum bin count is limited by {@link MAXIMAL_BINS}.
209 *
210 * @param values All values in this histogram
211 *
212 * @return Number of bins
213 */
118 protected int getBinCountByWidth(double[] values) { 214 protected int getBinCountByWidth(double[] values) {
119 int bins = -1; 215 int bins = -1;
120 String param = (String) requestParameter.get(REQUEST_KEY_BIN_WIDTH); 216 String param = (String) requestParameter.get(REQUEST_KEY_BIN_WIDTH);
121 Locale locale = (Locale) requestParameter.get(REQUEST_KEY_LOCALE); 217 Locale locale = (Locale) requestParameter.get(REQUEST_KEY_LOCALE);
122 NumberFormat format = NumberFormat.getInstance(locale); 218 NumberFormat format = NumberFormat.getInstance(locale);

http://dive4elements.wald.intevation.org