changeset 7597:fca46ce8e4f5

(issue1225) Implement Magic labels. There is now a new value in the chartsettings "Suggested Label" which is hidden in the property editor. A suggested label is the label that combines the label's of all processors that wrote data to an axis. This suggested label is set as the label when the user has not overwritten the label.
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 25 Nov 2013 14:58:14 +0100
parents 12c4c8d0ac41
children 8580f222dfb2
files artifacts/src/main/java/org/dive4elements/river/exports/AxisSection.java artifacts/src/main/java/org/dive4elements/river/exports/ChartSettings.java artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.java gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.properties gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_de.properties gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_en.properties gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/ChartPropertiesEditor.java
diffstat 8 files changed, 90 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/AxisSection.java	Mon Nov 25 14:53:50 2013 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/AxisSection.java	Mon Nov 25 14:58:14 2013 +0100
@@ -21,12 +21,13 @@
  */
 public class AxisSection extends TypeSection {
 
-    public static final String IDENTIFIER_ATTR = "id";
-    public static final String LABEL_ATTR      = "label";
-    public static final String FONTSIZE_ATTR   = "font-size";
-    public static final String FIXATION_ATTR   = "fixation";
-    public static final String UPPERRANGE_ATTR = "upper";
-    public static final String LOWERRANGE_ATTR = "lower";
+    public static final String IDENTIFIER_ATTR           = "id";
+    public static final String LABEL_ATTR                = "label";
+    public static final String SUGGESTED_LABEL_ATTR      = "suggested-label";
+    public static final String FONTSIZE_ATTR             = "font-size";
+    public static final String FIXATION_ATTR             = "fixation";
+    public static final String UPPERRANGE_ATTR           = "upper";
+    public static final String LOWERRANGE_ATTR           = "lower";
 
 
     public AxisSection() {
@@ -54,6 +55,15 @@
     }
 
 
+    public void setSuggestedLabel(String label) {
+        setStringValue(SUGGESTED_LABEL_ATTR, label);
+    }
+
+
+    public String getSuggestedLabel() {
+        return getStringValue(SUGGESTED_LABEL_ATTR);
+    }
+
     public void setFontSize(int fontSize) {
         if (fontSize <= 0) {
             return;
--- a/artifacts/src/main/java/org/dive4elements/river/exports/ChartSettings.java	Mon Nov 25 14:53:50 2013 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/ChartSettings.java	Mon Nov 25 14:58:14 2013 +0100
@@ -211,14 +211,16 @@
         String fixation = XMLUtils.xpathString(axis, "fixation", null);
         String low      = XMLUtils.xpathString(axis, "lower", null);
         String up       = XMLUtils.xpathString(axis, "upper", null);
+        String sugLabel = XMLUtils.xpathString(axis, "suggested-label", null);
 
         if (logger.isDebugEnabled()) {
-            logger.debug("Fount axis id:        '" + id + "'");
-            logger.debug("Fount axis label:     '" + label + "'");
-            logger.debug("Fount axis font size: '" + fSize + "'");
-            logger.debug("Fount axis fixation:  '" + fixation + "'");
-            logger.debug("Fount axis lower:     '" + low + "'");
-            logger.debug("Fount axis upper:     '" + up + "'");
+            logger.debug("Found axis id:        '" + id + "'");
+            logger.debug("Found axis label:     '" + label + "'");
+            logger.debug("Found axis font size: '" + fSize + "'");
+            logger.debug("Found axis fixation:  '" + fixation + "'");
+            logger.debug("Found axis lower:     '" + low + "'");
+            logger.debug("Found axis upper:     '" + up + "'");
+            logger.debug("Found axis sug. label:'" + sugLabel + "'");
         }
 
         section.setIdentifier(id);
@@ -227,6 +229,7 @@
         section.setFixed(Boolean.valueOf(fixation));
         section.setLowerRange(Double.parseDouble(low.length() > 0 ? low : "0"));
         section.setUpperRange(Double.parseDouble(up.length() > 0 ? up : "0"));
+        section.setSuggestedLabel(sugLabel);
 
         target.addAxisSection(section);
     }
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Mon Nov 25 14:53:50 2013 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Mon Nov 25 14:58:14 2013 +0100
@@ -15,8 +15,13 @@
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
 
 import java.io.OutputStream;
 
@@ -67,6 +72,8 @@
 
 import org.w3c.dom.Document;
 
+import org.apache.commons.lang.StringUtils;
+
 
 /**
  * The main diagram creation class.
@@ -105,11 +112,17 @@
     /** Whether or not the plot is inverted (left-right). */
     private boolean inverted;
 
+    private static final Pattern UNIT_PATTERN =
+        Pattern.compile("\\s*\\[[\\w\\s\\+\\-]*\\]\\s*");
+
+    protected Map<Integer, LinkedHashSet<String>> axesLabels;
+
     protected DiagramAttributes.Instance diagramAttributes;
 
     public DiagramGenerator() {
         super();
 
+        axesLabels = new HashMap<Integer, LinkedHashSet<String>>();
         xBounds  = new HashMap<Integer, Bounds>();
         yBounds  = new HashMap<Integer, Bounds>();
     }
@@ -1056,9 +1069,20 @@
 
     @Override
     protected String getDefaultYAxisLabel(String axisName) {
-        String label;
+        Set labelSet = axesLabels.get(diagramAttributes.getAxisIndex(axisName));
+        logger.debug("Labels for axis: " + labelSet);
+        if (labelSet != null && labelSet.size() >= 2) {
+            String label = StringUtils.join(labelSet, ", ");
+            Matcher units = UNIT_PATTERN.matcher(label);
+            if (units.find()) {
+                String firstUnit = units.group();
+                label = units.replaceAll("");
+                label += firstUnit;
+            }
+            return label;
+        }
         for (Processor pr: diagramAttributes.getProcessorsForAxisName(axisName)) {
-            label = pr.getAxisLabel(this);
+            String label = pr.getAxisLabel(this);
             if (label != null) {
                 return label;
             }
@@ -1082,6 +1106,7 @@
             String axisName = diagramAttributes.getAxisName(i);
             ySection.setIdentifier(axisName);
             ySection.setLabel(getYAxisLabel(axisName));
+            ySection.setSuggestedLabel(getDefaultYAxisLabel(axisName));
             ySection.setFontSize(14);
             ySection.setFixed(false);
 
@@ -1115,7 +1140,10 @@
         AxisSection as = chartSettings.getAxisSection(axisName);
         if (as != null) {
             String label = as.getLabel();
-            if (label != null) {
+            if (label != null && !label.equals(as.getSuggestedLabel())) {
+                // Only if the suggested label is not the current label
+                // the user has modified the label. Otherwise lets
+                // recalculate the label
                 return label;
             }
         }
@@ -1154,6 +1182,17 @@
             if (pr.canHandle(facetName)) {
                 found = true;
                 pr.doOut(this, bundle, theme, visible);
+
+                if (visible) {
+                    // Save the label that should be added for this processor
+                    int axisIdx = diagramAttributes.getAxisIndex(pr.getAxisName());
+                    LinkedHashSet<String> curLabels = axesLabels.get(axisIdx);
+                    if (curLabels == null) {
+                        curLabels = new LinkedHashSet<String>(5);
+                    }
+                    curLabels.add(pr.getAxisLabel(this));
+                    axesLabels.put(axisIdx, curLabels);
+                }
             }
         }
         if (!found) {
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.java	Mon Nov 25 14:53:50 2013 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.java	Mon Nov 25 14:58:14 2013 +0100
@@ -1363,5 +1363,7 @@
     String historical_discharge_curves();
 
     String current_gauge();
+
+    String suggested_label();
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.properties	Mon Nov 25 14:53:50 2013 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.properties	Mon Nov 25 14:58:14 2013 +0100
@@ -565,6 +565,7 @@
 display_logo = Display logo
 logo_placeh = Horiz. Place for logo
 logo_placev = Vertic. Place for logo
+suggested_label = Suggested Label
 top = top
 bottom = bottom
 center = center
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_de.properties	Mon Nov 25 14:53:50 2013 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_de.properties	Mon Nov 25 14:58:14 2013 +0100
@@ -569,6 +569,7 @@
 display_logo = Logo anzeigen
 logo_placeh = Horizontale Ausrichtung Logo
 logo_placev = Vertikale Ausrichtung Logo
+suggested_label = Vorgeschlagener Titel
 top = oben
 bottom = unten
 center = mittig
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_en.properties	Mon Nov 25 14:53:50 2013 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_en.properties	Mon Nov 25 14:58:14 2013 +0100
@@ -544,6 +544,7 @@
 display_logo = Display logo
 logo_placeh = Horiz. Place for logo
 logo_placev = Vertic. Place for logo
+suggested_label = Suggested Label
 top = top
 bottom = bottom
 center = center
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/ChartPropertiesEditor.java	Mon Nov 25 14:53:50 2013 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/ChartPropertiesEditor.java	Mon Nov 25 14:58:14 2013 +0100
@@ -188,6 +188,12 @@
         PropertyGroup origPg = (PropertyGroup)orig;
 
         if (pg.getName().equals("axis")) {
+            for (Property prop: origPg.getProperties()) {
+                GWT.log("Found: " + prop.getName());
+            }
+            for (Property prop: pg.getProperties()) {
+                GWT.log("Found new: " + prop.getName());
+            }
             // Certain axis shall be skipped (W/Q-Diagrams cm-axis especially).
             String outputName = tab.getOutputName();
             if (outputName.equals("fix_wq_curve") || outputName.equals("computed_discharge_curve")
@@ -213,6 +219,18 @@
             title.setValue(
                 ((StringProperty)origPg.getPropertyByName("label")).getValue());
 
+            StringProperty suggestedLabel =
+                (StringProperty)pg.getPropertyByName("suggested-label");
+            FormItem sugLabel = null;
+
+            if (suggestedLabel != null) {
+                // X Axis does not have a suggested label
+                // otherwise add an hidden property for suggestedLabel
+                sugLabel = createStringProperty(suggestedLabel);
+                sugLabel.setValue(
+                    ((StringProperty)origPg.getPropertyByName("suggested-label")).getValue());
+            }
+
             IntegerProperty fontsize =
                 (IntegerProperty)pg.getPropertyByName("font-size");
             FormItem fs = createIntegerProperty(fontsize);

http://dive4elements.wald.intevation.org