ingo@617: package de.intevation.gnv.chart; ingo@617: ingo@629: import java.text.NumberFormat; ingo@629: import java.text.ParseException; ingo@629: import java.util.Locale; ingo@629: import java.util.Map; ingo@629: ingo@617: import org.apache.log4j.Logger; ingo@617: ingo@617: import org.jfree.chart.ChartTheme; sascha@623: ingo@617: import org.jfree.chart.plot.XYPlot; ingo@617: ingo@617: import org.jfree.data.statistics.HistogramDataset; ingo@617: ingo@617: /** ingo@617: * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) ingo@617: */ ingo@617: public class DefaultHistogram ingo@617: extends AbstractHistogram ingo@617: { ingo@617: // TODO take a better default value ingo@617: public static final int DEFAULT_BINS = 15; ingo@629: public static final int MAXIMAL_BINS = 20; ingo@629: public static final String REQUEST_KEY_BIN_COUNT = "bincount"; ingo@629: public static final String REQUEST_KEY_BIN_WIDTH = "binwidth"; ingo@629: public static final String REQUEST_KEY_CHART_WIDTH = "width"; ingo@629: public static final String REQUEST_KEY_LOCALE = "locale"; ingo@629: public static final String REQUEST_KEY_BIN_CHOICE = "bintype"; ingo@617: ingo@617: private static Logger logger = Logger.getLogger(DefaultHistogram.class); ingo@617: ingo@629: protected Map requestParameter; ingo@629: ingo@617: ingo@617: public DefaultHistogram( ingo@629: ChartLabels labels, Object[] data, ChartTheme theme, Map requestParameter ingo@617: ) { ingo@617: super(labels, data, theme); ingo@629: this.requestParameter = requestParameter; ingo@617: } ingo@617: ingo@617: ingo@617: protected void applyDatasets() { ingo@617: XYPlot plot = (XYPlot) chart.getPlot(); ingo@617: ingo@617: // prepare data and create add them to histogram dataset ingo@617: String name = (String) data[0]; ingo@617: double[] values = toDouble((Double[]) data[1]); ingo@629: int bins = getBinCount(values); ingo@617: ingo@617: HistogramDataset dataset = new HistogramDataset(); ingo@629: dataset.addSeries(name, values, bins); ingo@617: ingo@617: plot.setDataset(0, dataset); ingo@617: } ingo@617: ingo@617: ingo@629: protected double[] getMinMax(double[] values) { ingo@629: double[] minmax = new double[2]; ingo@629: minmax[0] = Double.MAX_VALUE; ingo@629: minmax[1] = Double.MIN_VALUE; ingo@629: ingo@629: int length = values.length; ingo@629: for (int i = 0; i < length; i++) { ingo@629: minmax[0] = values[i] < minmax[0] ? values[i] : minmax[0]; ingo@629: minmax[1] = values[i] > minmax[1] ? values[i] : minmax[1]; ingo@629: } ingo@629: ingo@629: return minmax; ingo@629: } ingo@629: ingo@629: ingo@617: protected double[] toDouble(Double[] array) { ingo@617: int length = array.length; ingo@617: double[] values = new double[length]; ingo@617: ingo@617: for(int i = 0; i < length; i++) { ingo@617: values[i] = array[i].doubleValue(); ingo@617: } ingo@617: ingo@617: return values; ingo@617: } ingo@629: ingo@629: ingo@629: protected int getBinCount(double[] values) { ingo@629: String param = (String) requestParameter.get(REQUEST_KEY_BIN_CHOICE); ingo@629: ingo@629: if (param != null && param.equalsIgnoreCase(REQUEST_KEY_BIN_WIDTH)) { ingo@629: return getBinCountByWidth(values); ingo@629: } ingo@629: else { ingo@629: return getBinCountByNumber(); ingo@629: } ingo@629: } ingo@629: ingo@629: ingo@629: protected int getBinCountByNumber() { ingo@629: int bins = -1; ingo@629: String param = (String) requestParameter.get(REQUEST_KEY_BIN_COUNT); ingo@629: ingo@629: try { ingo@629: bins = Integer.parseInt(param); ingo@629: bins = bins <= 0 ? DEFAULT_BINS : bins; ingo@629: bins = bins > MAXIMAL_BINS ? MAXIMAL_BINS : bins; ingo@629: ingo@629: return bins; ingo@629: } ingo@629: catch (NumberFormatException nfe) { ingo@629: logger.warn("Invalid number of bins for histogram chart: " + param); ingo@629: logger.warn("Return default bins: " + DEFAULT_BINS); ingo@629: ingo@629: return DEFAULT_BINS; ingo@629: } ingo@629: } ingo@629: ingo@629: ingo@629: protected int getBinCountByWidth(double[] values) { ingo@629: int bins = -1; ingo@629: String param = (String) requestParameter.get(REQUEST_KEY_BIN_WIDTH); ingo@629: Locale locale = (Locale) requestParameter.get(REQUEST_KEY_LOCALE); ingo@629: NumberFormat format = NumberFormat.getInstance(locale); ingo@629: ingo@629: try { ingo@629: double[] minmax = getMinMax(values); ingo@629: double totalWidth = minmax[1] - minmax[0]; ingo@629: Number number = format.parse(param); ingo@629: double binWidth = -1d; ingo@629: ingo@629: if (number instanceof Double) { ingo@629: binWidth = ((Double) number).doubleValue(); ingo@629: } ingo@629: else if (number instanceof Long) { ingo@629: binWidth = ((Long) number).doubleValue(); ingo@629: } ingo@629: else if (number instanceof Integer) { ingo@629: binWidth = ((Integer) number).doubleValue(); ingo@629: } ingo@629: else { ingo@629: logger.warn("Invalid bin width for histogram chart: " + param); ingo@629: logger.warn("Return default bins: " + DEFAULT_BINS); ingo@629: ingo@629: return DEFAULT_BINS; ingo@629: } ingo@629: ingo@629: double tmpBins = totalWidth / binWidth; ingo@629: ingo@629: bins = (int) Math.round(tmpBins); ingo@629: bins = bins <= 0 ? DEFAULT_BINS : bins; ingo@629: bins = bins > MAXIMAL_BINS ? MAXIMAL_BINS : bins; ingo@629: ingo@629: return bins; ingo@629: } ingo@629: catch (ParseException pe) { ingo@629: logger.warn("Invalid bin width for histogram chart: " + param); ingo@629: logger.warn("Return default bins: " + DEFAULT_BINS); ingo@629: ingo@629: return DEFAULT_BINS; ingo@629: } ingo@629: catch (NumberFormatException nfe) { ingo@629: logger.warn("Invalid bin width for histogram chart: " + param); ingo@629: logger.warn("Return default bins: " + DEFAULT_BINS); ingo@629: ingo@629: return DEFAULT_BINS; ingo@629: } ingo@629: } ingo@617: } ingo@617: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :