changeset 315:63f8b3fb7d9a

Localization of chart axis with locale which fits best to server and browser settings. gnv-artifacts/trunk@373 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 26 Nov 2009 08:47:44 +0000
parents 6f72e335e2c9
children 446301258f34
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java
diffstat 5 files changed, 99 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Thu Nov 26 08:45:04 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Thu Nov 26 08:47:44 2009 +0000
@@ -1,3 +1,18 @@
+2009-11-26  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java: Localized
+	  chart axis. Two method have been added - method to localize y-axis of 
+	  charts, abstract method to localize x-axis of charts.
+
+	* src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java,
+	  src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java: Override
+	  abstract method to localize x-axis.
+
+	* src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java:
+	  Call algorithm to determine the best locale regarding intersection of
+	  supported server and browser locales. This locale is used to create 
+	  localized charts.
+
 2009-11-26  Ingo Weinzierl <ingo.weinzierl@intevation.de>
 
 	* src/main/java/de/intevation/gnv/artifacts/ressource/RessourceFactory.java:
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java	Thu Nov 26 08:45:04 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/AbstractXYLineChart.java	Thu Nov 26 08:47:44 2009 +0000
@@ -1,13 +1,16 @@
 package de.intevation.gnv.chart;
 
 import java.awt.Color;
+import java.text.NumberFormat;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.Locale;
 
 import org.apache.log4j.Logger;
 
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.ChartFactory;
+import org.jfree.chart.axis.Axis;
 import org.jfree.chart.axis.NumberAxis;
 import org.jfree.chart.axis.NumberTickUnit;
 import org.jfree.chart.axis.AxisLocation;
@@ -36,6 +39,7 @@
     protected abstract void initData();
     protected abstract void addValue(Result row, Series series);
     protected abstract void addSeries(Series series, int idx);
+    protected abstract void localizeDomainAxis(Axis axis, Locale locale);
     protected abstract String createSeriesName(
         String breakPoint1,
         String breakPoint2,
@@ -69,20 +73,25 @@
 
     protected void prepareAxis(String seriesKey, int idx) {
         log.debug("prepare axis of xychart");
-        XYPlot plot     = chart.getXYPlot();
-        NumberAxis axis = new NumberAxis(seriesKey);
+
+        XYPlot plot      = chart.getXYPlot();
+        Axis xAxis       = plot.getDomainAxis();
+        NumberAxis yAxis = new NumberAxis(seriesKey);
+
+        localizeDomainAxis(xAxis, locale);
+        localizeRangeAxis(yAxis, locale);
 
         if (seriesKey.contains("richtung")) {
-            axis.setTickUnit(new NumberTickUnit(30.0));
-            axis.setUpperBound(360.0);
-            axis.setLowerBound(0.0);
-            plot.setRangeAxis(idx, axis);
+            yAxis.setTickUnit(new NumberTickUnit(30.0));
+            yAxis.setUpperBound(360.0);
+            yAxis.setLowerBound(0.0);
+            plot.setRangeAxis(idx, yAxis);
         }
         else {
-            axis.setFixedDimension(10.0);
-            axis.setAutoRangeIncludesZero(false);
-            plot.setRangeAxis(idx, axis);
-            axis.configure();
+            yAxis.setFixedDimension(10.0);
+            yAxis.setAutoRangeIncludesZero(false);
+            plot.setRangeAxis(idx, yAxis);
+            yAxis.configure();
         }
 
         if (idx % 2 != 0)
@@ -97,6 +106,20 @@
     }
 
 
+    protected void localizeRangeAxis(Axis axis, Locale locale) {
+        if (locale == null)
+            return;
+
+        log.debug(
+            "Set language of axis [" + axis.getLabel() + "] " +
+            "to " + locale.toString()
+        );
+
+        NumberFormat format = NumberFormat.getInstance(locale);
+        ((NumberAxis) axis).setNumberFormatOverride(format);
+    }
+
+
     protected String findValueTitle(Collection values, String id) {
         log.debug("find description of dataset");
 
@@ -104,7 +127,7 @@
             Iterator it = values.iterator();
             while (it.hasNext()) {
                 KeyValueDescibeData data = (KeyValueDescibeData) it.next();
-    
+
                 if (id.equals(data.getKey()))
                     return data.getValue();
             }
@@ -112,4 +135,4 @@
         return "";
     }
 }
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=latin1 :
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java	Thu Nov 26 08:45:04 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/TimeSeriesChart.java	Thu Nov 26 08:47:44 2009 +0000
@@ -1,5 +1,7 @@
 package de.intevation.gnv.chart;
 
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.Locale;
@@ -9,6 +11,8 @@
 import org.jfree.chart.ChartTheme;
 import org.jfree.chart.ChartFactory;
 import org.jfree.chart.JFreeChart;
+import org.jfree.chart.axis.Axis;
+import org.jfree.chart.axis.DateAxis;
 import org.jfree.chart.plot.XYPlot;
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.data.general.Series;
@@ -26,6 +30,8 @@
 extends      AbstractXYLineChart
 {
 
+    private static final String DATE_FORMAT = "dd-MMM";
+
     private static Logger log = Logger.getLogger(TimeSeriesChart.class);
 
 
@@ -152,6 +158,20 @@
     }
 
 
+    protected void localizeDomainAxis(Axis axis, Locale locale) {
+        if (locale == null)
+            return;
+
+        log.debug(
+            "Set language of axis [" + axis.getLabel() + "] " +
+            "to " + locale.toString()
+        );
+
+        DateFormat format = new SimpleDateFormat(DATE_FORMAT, locale);
+        ((DateAxis) axis).setDateFormatOverride(format);
+    }
+
+
     protected String createSeriesName(
         String breakPoint1,
         String breakPoint2,
@@ -164,4 +184,4 @@
             "m";
     }
 }
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java	Thu Nov 26 08:45:04 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/VerticalProfileChart.java	Thu Nov 26 08:47:44 2009 +0000
@@ -7,11 +7,13 @@
 import org.apache.log4j.Logger;
 
 import org.jfree.chart.ChartTheme;
+import org.jfree.chart.axis.Axis;
+import org.jfree.chart.axis.NumberAxis;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.data.xy.XYSeries;
 import org.jfree.data.general.Series;
 import org.jfree.data.xy.XYSeriesCollection;
-import org.jfree.chart.plot.XYPlot;
-import org.jfree.chart.plot.PlotOrientation;
 
 import de.intevation.gnv.geobackend.base.Result;
 
@@ -124,6 +126,12 @@
     }
 
 
+    protected void localizeDomainAxis(Axis axis, Locale locale) {
+        // call localizeRangeAxis from superclass which formats NumberAxis
+        super.localizeRangeAxis(axis, locale);
+    }
+
+
     protected String createSeriesName(
         String breakPoint1,
         String breakPoint2,
@@ -136,4 +144,4 @@
             "m";
     }
 }
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=latin1 :
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Thu Nov 26 08:45:04 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Thu Nov 26 08:47:44 2009 +0000
@@ -215,7 +215,18 @@
                 ChartLabels chartLables = new ChartLabels(this.getFisName(callMeta.getLanguages())+" "+this
                         .getSelectedFeatureName(uuid), this.domainLable);
 
-                String exportFormat = getExportFormat(mimeType);
+                String exportFormat       = getExportFormat(mimeType);
+
+                PreferredLocale[] locales = callMeta.getLanguages();
+                Locale[] serverLocales    =
+                    RessourceFactory.getInstance().getLocales();
+                Locale locale             =
+                    callMeta.getPreferredLocale(serverLocales);
+
+                log.debug(
+                    "Best locale - regarding intersection of server and " +
+                    "browser locales -  is " + locale.toString()
+                );
 
                 this.createChart(
                     outputStream,
@@ -225,6 +236,7 @@
                     chartLables,
                     uuid,
                     exportFormat,
+                    locale,
                     chartWidth,
                     chartHeight
                 );
@@ -399,6 +411,7 @@
         ChartLabels  chartLables,
         String       uuid,
         String       exportFormat,
+        Locale       locale,
         int          width,
         int          height
     )
@@ -411,7 +424,7 @@
             measurements,
             dates,
             getChartResult(uuid),
-            null, // Locale
+            locale, // Locale
             uuid
         );
 
@@ -461,7 +474,7 @@
             dates,
             result,
             timeGapDefinitions,
-            null
+            locale
         );
         chart.generateChart();
 
@@ -591,3 +604,4 @@
         return null;
     }
 }
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org