changeset 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 bfe33e658576
children a72ecacccc91
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/chart/DefaultHistogram.java gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java
diffstat 3 files changed, 152 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Tue Feb 23 15:50:15 2010 +0000
+++ b/gnv-artifacts/ChangeLog	Wed Feb 24 14:30:52 2010 +0000
@@ -1,3 +1,24 @@
+2010-02-24  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java:
+	  Store request parameter in a map and put that map into other
+	  classes/methods instead of putting each request parameter into an own
+	  variable.
+
+	* src/main/java/de/intevation/gnv/chart/DefaultHistogram.java: Implemented
+	  logic to adjust number of bins in histogram charts. The user is able to
+	  choose between the number of bins or the size of a single bin.
+
+2010-02-23  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* contrib/palette2polygonVM.xsl: New. XSLT transformation to transform a
+	  palette file into a style definition template used for mapfile creation.
+	  Usage:
+
+	  $ xsltproc contrib/palette2polygonVM.xsl  \
+	    doc/conf/palette/water-temperature.xml  \
+		> water-temperature.vm
+
 2010-02-23  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	* src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java:
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/DefaultHistogram.java	Tue Feb 23 15:50:15 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/DefaultHistogram.java	Wed Feb 24 14:30:52 2010 +0000
@@ -1,5 +1,10 @@
 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;
 
 import org.jfree.chart.ChartTheme;
@@ -16,14 +21,23 @@
 {
     // TODO take a better default value
     public static final int DEFAULT_BINS = 15;
+    public static final int MAXIMAL_BINS = 20;
+    public static final String REQUEST_KEY_BIN_COUNT   = "bincount";
+    public static final String REQUEST_KEY_BIN_WIDTH   = "binwidth";
+    public static final String REQUEST_KEY_CHART_WIDTH = "width";
+    public static final String REQUEST_KEY_LOCALE      = "locale";
+    public static final String REQUEST_KEY_BIN_CHOICE  = "bintype";
 
     private static Logger logger = Logger.getLogger(DefaultHistogram.class);
 
+    protected Map requestParameter;
+
 
     public DefaultHistogram(
-        ChartLabels labels, Object[] data, ChartTheme theme
+        ChartLabels labels, Object[] data, ChartTheme theme, Map requestParameter
     ) {
         super(labels, data, theme);
+        this.requestParameter = requestParameter;
     }
 
 
@@ -33,14 +47,30 @@
         // 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, DEFAULT_BINS);
+        dataset.addSeries(name, values, bins);
 
         plot.setDataset(0, dataset);
     }
 
 
+    protected double[] getMinMax(double[] values) {
+        double[] minmax = new double[2];
+        minmax[0] = Double.MAX_VALUE;
+        minmax[1] = Double.MIN_VALUE;
+
+        int length = values.length;
+        for (int i = 0; i < length; i++) {
+            minmax[0] = values[i] < minmax[0] ? values[i] : minmax[0];
+            minmax[1] = values[i] > minmax[1] ? values[i] : minmax[1];
+        }
+
+        return minmax;
+    }
+
+
     protected double[] toDouble(Double[] array) {
         int length      = array.length;
         double[] values = new double[length];
@@ -51,5 +81,88 @@
 
         return values;
     }
+
+
+    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);
+        }
+        else {
+            return getBinCountByNumber();
+        }
+    }
+
+
+    protected int getBinCountByNumber() {
+        int    bins  = -1;
+        String param = (String) requestParameter.get(REQUEST_KEY_BIN_COUNT);
+
+        try {
+            bins = Integer.parseInt(param);
+            bins = bins <= 0 ? DEFAULT_BINS : bins;
+            bins = bins >  MAXIMAL_BINS ? MAXIMAL_BINS : bins;
+
+            return bins;
+        }
+        catch (NumberFormatException nfe) {
+            logger.warn("Invalid number of bins for histogram chart: " + param);
+            logger.warn("Return default bins: " + DEFAULT_BINS);
+
+            return DEFAULT_BINS;
+        }
+    }
+
+
+    protected int getBinCountByWidth(double[] values) {
+        int    bins   = -1;
+        String param  = (String) requestParameter.get(REQUEST_KEY_BIN_WIDTH);
+        Locale locale = (Locale) requestParameter.get(REQUEST_KEY_LOCALE);
+        NumberFormat format = NumberFormat.getInstance(locale);
+
+        try {
+            double[] minmax     = getMinMax(values);
+            double   totalWidth = minmax[1] - minmax[0];
+            Number   number     = format.parse(param);
+            double   binWidth   = -1d;
+
+            if (number instanceof Double) {
+                binWidth = ((Double) number).doubleValue();
+            }
+            else if (number instanceof Long) {
+                binWidth = ((Long) number).doubleValue();
+            }
+            else if (number instanceof Integer) {
+                binWidth = ((Integer) number).doubleValue();
+            }
+            else {
+                logger.warn("Invalid bin width for histogram chart: " + param);
+                logger.warn("Return default bins: " + DEFAULT_BINS);
+
+                return DEFAULT_BINS;
+            }
+
+            double tmpBins = totalWidth / binWidth;
+
+            bins = (int) Math.round(tmpBins);
+            bins = bins <= 0 ? DEFAULT_BINS : bins;
+            bins = bins >  MAXIMAL_BINS ? MAXIMAL_BINS : bins;
+
+            return bins;
+        }
+        catch (ParseException pe) {
+            logger.warn("Invalid bin width for histogram chart: " + param);
+            logger.warn("Return default bins: " + DEFAULT_BINS);
+
+            return DEFAULT_BINS;
+        }
+        catch (NumberFormatException nfe) {
+            logger.warn("Invalid bin width for histogram chart: " + param);
+            logger.warn("Return default bins: " + DEFAULT_BINS);
+
+            return DEFAULT_BINS;
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java	Tue Feb 23 15:50:15 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java	Wed Feb 24 14:30:52 2010 +0000
@@ -67,9 +67,11 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.Vector;
 
 import org.apache.log4j.Logger;
@@ -224,6 +226,10 @@
         int chartWidth   = 600;
         int chartHeight  = 400;
         boolean sVisible = false;
+        int binCount     = 0;
+        int binWidth     = 0;
+
+        Map requestParameter = new HashMap();
 
         // lines are always visible. if lines should be configurable we need a
         // parameter in the user interface
@@ -235,6 +241,8 @@
                 while (it.hasNext()) {
                     InputData ip      = it.next();
                     String optionName = ip.getName().trim();
+                    log.debug("FOUND [" + optionName + "] with value {"+ ip.getValue()+"}");
+                    requestParameter.put(optionName, ip.getValue());
 
                     if (optionName.equals("width")) {
                         chartWidth = Integer.parseInt(ip.getValue());
@@ -305,6 +313,13 @@
                 Object[][] data = HistogramHelper.prepareHistogramData(
                     results, parameters, measurements, dates);
 
+                PreferredLocale[] locales = callMeta.getLanguages();
+                Locale[] serverLocales    =
+                    RessourceFactory.getInstance().getLocales();
+                Locale locale             =
+                    callMeta.getPreferredLocale(serverLocales);
+                requestParameter.put("locale", locale);
+
                 int size           = data.length;
                 Chart[] histograms = new Chart[size];
 
@@ -315,7 +330,7 @@
                     ChartTheme theme   = createStyle(callContext);
 
                     histograms[i] = new DefaultHistogram(
-                        labels, data[i], theme);
+                        labels, data[i], theme, requestParameter);
                 }
 
                 ChartExportHelper.exportHistograms(

http://dive4elements.wald.intevation.org