diff artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java @ 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 1dff8e71c4d6
children 25bce6e8beea
line wrap: on
line diff
--- 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) {

http://dive4elements.wald.intevation.org