[PATCH 1 of 2] Allow to configure lower and upper margin of a chart axis

Wald Commits scm-commit at wald.intevation.org
Wed Feb 14 18:45:48 CET 2018


# HG changeset patch
# User gernotbelger
# Date 1518630301 -3600
# Node ID f87f435df856c2fe303480cc96cc30ab8ec77e6b
# Parent  caa52aec08c0e5c3561117b8504ddf23e91ef7f3
Allow to configure lower and upper margin of a chart axis

diff -r caa52aec08c0 -r f87f435df856 artifacts/doc/conf/generators/longitudinal-diagram-defaults.xml
--- a/artifacts/doc/conf/generators/longitudinal-diagram-defaults.xml	Wed Feb 14 18:16:03 2018 +0100
+++ b/artifacts/doc/conf/generators/longitudinal-diagram-defaults.xml	Wed Feb 14 18:45:01 2018 +0100
@@ -15,7 +15,7 @@
     <axis name="Tau"/>
     <axis name="Q" include-zero="true"/>
     <axis name="FlowdepthAxis" include-zero="true"/>
-    <axis name="tkhAxis" include-zero="true" />
+    <axis name="tkhAxis" include-zero="true" upperMargin="4.0" />
     <domain-axis key="chart.longitudinal.section.xaxis.label" default="Fluss-Km" inverted="org.dive4elements.river.exports.IsKmUpEvaluator()">
         <arg expr="artifact.river"/>
     </domain-axis>
diff -r caa52aec08c0 -r f87f435df856 artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Wed Feb 14 18:16:03 2018 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Wed Feb 14 18:45:01 2018 +0100
@@ -109,16 +109,15 @@
     } // class Instance
 
     public static class AxisAttributes {
-        private String  name;
-        private boolean isLeftAlign; // TODO: Remove!
-        private boolean forceAlign;  // TODO: Remove!
-        private boolean includeZero; // TODO: Use Evaluator
+        private final String  name;
+        private final boolean isLeftAlign; // TODO: Remove!
+        private final boolean forceAlign;  // TODO: Remove!
+        private final boolean includeZero; // TODO: Use Evaluator
 
-        private Evaluator isInverted;
-        private Evaluator isLog;
-
-        public AxisAttributes() {
-        }
+        private final Evaluator isInverted;
+        private final Evaluator isLog;
+        private final double lowerMargin;
+        private final double upperMargin;
 
         public AxisAttributes(
             String    name,
@@ -126,7 +125,9 @@
             boolean   forceAlign,
             boolean   includeZero,
             Evaluator isInverted,
-            Evaluator isLog
+            Evaluator isLog,
+            double lowerMargin,
+            double upperMargin
         ) {
             this.name        = name;
             this.isLeftAlign = isLeftAlign;
@@ -134,6 +135,8 @@
             this.includeZero = includeZero;
             this.isInverted  = isInverted;
             this.isLog       = isLog;
+            this.lowerMargin = lowerMargin;
+            this.upperMargin = upperMargin;
         }
 
         public String getName() {
@@ -159,15 +162,20 @@
         public Evaluator isLog() {
             return isLog;
         }
+        
+        public double getLowerMargin() {
+            return this.lowerMargin;
+        }
+        
+        public double getUpperMargin() {
+            return this.upperMargin;
+        }
     } // class AxisAttributes
 
     public class DomainAxisAttributes extends AxisAttributes {
 
         private Title title;
 
-        public DomainAxisAttributes() {
-        }
-
         public DomainAxisAttributes(
             String    name,
             boolean   isLeftAlign,
@@ -178,7 +186,7 @@
             Title     title
         ) {
             super(name, isLeftAlign, forceAlign, includeZero, isInverted,
-                    isLog);
+                    isLog, 0.0, 0.0);
             this.title = title;
         }
 
@@ -375,6 +383,9 @@
             String isInverted = axisElement.getAttribute("inverted");
             String isLog = axisElement.getAttribute("logarithmic");
 
+            final double lowerMargin = parseDouble( axisElement.getAttribute("lowerMargin"), 0.0 );
+            final double upperMargin = parseDouble( axisElement.getAttribute("upperMargin"), 0.0 );
+            
             if (name.isEmpty()) {
                 continue;
             }
@@ -394,7 +405,22 @@
             axesAttrs.add(new AxisAttributes(
                 name, isleftAlign, forceAlign,
                 includeZero.equals("true"),
-                isInvertedE, isLogE));
+                isInvertedE, isLogE,
+                lowerMargin,upperMargin));
+        }
+    }
+    
+    private static double parseDouble( final String text, final double defaultValue ) {
+        if( text == null || text.isEmpty() )
+            return defaultValue;
+        
+        try {
+            return Double.parseDouble(text);
+        }
+        catch (final NumberFormatException e) {
+            e.printStackTrace();
+            log.error(String.format("Invalid double attribute: ", text), e);
+            return defaultValue;
         }
     }
 
diff -r caa52aec08c0 -r f87f435df856 artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Wed Feb 14 18:16:03 2018 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Wed Feb 14 18:45:01 2018 +0100
@@ -36,7 +36,7 @@
 import org.dive4elements.artifacts.CallContext;
 
 import org.dive4elements.river.artifacts.D4EArtifact;
-
+import org.dive4elements.river.exports.DiagramAttributes.AxisAttributes;
 import org.dive4elements.river.exports.process.Processor;
 
 import org.dive4elements.river.jfree.RiverAnnotation;
@@ -1270,19 +1270,25 @@
 
     @Override
     protected NumberAxis createYAxis(int index) {
+        
+        final AxisAttributes axisAttributes = diagramAttributes.getAxesAttributes().get(index);
+        
+        boolean logarithmic = (Boolean)axisAttributes.isLog().evaluate((D4EArtifact)getMaster(), context);
+
         NumberAxis axis;
-        boolean logarithmic = (Boolean)diagramAttributes.getAxesAttributes().
-            get(index).isLog().evaluate((D4EArtifact)getMaster(), context);
-
         if (logarithmic) {
             axis = new LogarithmicAxis(getYAxisLabel(index));
         } else {
             axis = super.createYAxis(index);
         }
 
-        if (diagramAttributes.getAxesAttributes().get(index).includeZero()) {
+        if (axisAttributes.includeZero()) {
             axis.setAutoRangeIncludesZero(true);
         }
+        
+        axis.setLowerMargin(axisAttributes.getLowerMargin());
+        axis.setUpperMargin(axisAttributes.getUpperMargin());
+        
         return axis;
     }
 


More information about the Dive4Elements-commits mailing list