[PATCH] Introduced new option 'tick-units' for axes. Configured the count Axis to only allow for integer ticks

Wald Commits scm-commit at wald.intevation.org
Fri Jul 20 15:24:11 CEST 2018


# HG changeset patch
# User gernotbelger
# Date 1532093045 -7200
# Node ID 55e2155ab52d8d59a5e142236c7a444fd9cb812e
# Parent  7efb27005d0dc654bd1f70c3f11365b3ae6061f1
Introduced new option 'tick-units' for axes. Configured the count Axis to only allow for integer ticks.

diff -r 7efb27005d0d -r 55e2155ab52d artifacts/doc/conf/generators/longitudinal-diagram-defaults.xml
--- a/artifacts/doc/conf/generators/longitudinal-diagram-defaults.xml	Fri Jul 20 14:58:34 2018 +0200
+++ b/artifacts/doc/conf/generators/longitudinal-diagram-defaults.xml	Fri Jul 20 15:24:05 2018 +0200
@@ -18,7 +18,7 @@
     <axis name="tkhAxis" include-zero="true" />
     <axis name="flowdepthDevelopmentAxis"/>
     <axis name="flowdepthDevelopmentPerYearAxis"/>
-    <axis name="countAxis" include-zero="true"/>
+    <axis name="countAxis" include-zero="true" tick-units="integer"/>
     <axis name="durationAxis" include-zero="true"/>
 
     <domain-axis key="chart.longitudinal.section.xaxis.label" default="Fluss-Km" inverted="org.dive4elements.river.exports.IsKmUpEvaluator()">
diff -r 7efb27005d0d -r 55e2155ab52d artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Fri Jul 20 14:58:34 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Fri Jul 20 15:24:05 2018 +0200
@@ -15,9 +15,10 @@
 
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
-
+import org.dive4elements.river.exports.DiagramAttributes.AxisAttributes.TickUnits;
 import org.dive4elements.river.exports.process.Processor;
-
+import org.jfree.chart.axis.NumberAxis;
+import org.jfree.chart.axis.TickUnitSource;
 import org.apache.log4j.Logger;
 
 import org.dive4elements.artifacts.CallContext;
@@ -115,6 +116,24 @@
     } // class Instance
 
     public static class AxisAttributes {
+        
+        /** Possible point of extension in order to support different tick units per axis.*/
+        public static enum TickUnits {
+            standard {
+                @Override
+                public TickUnitSource createTickUnits() {
+                    return NumberAxis.createStandardTickUnits();
+                }
+            }, integer {
+                @Override
+                public TickUnitSource createTickUnits() {
+                    return NumberAxis.createIntegerTickUnits();
+                }
+            };
+
+            public abstract TickUnitSource createTickUnits();
+        }
+        
         private final String  name;
         private final boolean isLeftAlign; // TODO: Remove!
         private final boolean forceAlign;  // TODO: Remove!
@@ -124,6 +143,7 @@
         private final Evaluator isLog;
         private final double lowerMargin;
         private final double upperMargin;
+        private TickUnits tickUnits;
 
         public AxisAttributes(
             String    name,
@@ -132,6 +152,7 @@
             boolean   includeZero,
             Evaluator isInverted,
             Evaluator isLog,
+            TickUnits tickUnits,
             double lowerMargin,
             double upperMargin
         ) {
@@ -141,6 +162,7 @@
             this.includeZero = includeZero;
             this.isInverted  = isInverted;
             this.isLog       = isLog;
+            this.tickUnits = tickUnits;
             this.lowerMargin = lowerMargin;
             this.upperMargin = upperMargin;
         }
@@ -176,6 +198,10 @@
         public double getUpperMargin() {
             return this.upperMargin;
         }
+        
+        public TickUnits getTickUnits() {
+            return this.tickUnits;
+        }
     } // class AxisAttributes
 
     public class DomainAxisAttributes extends AxisAttributes {
@@ -192,7 +218,7 @@
             Title     title
         ) {
             super(name, isLeftAlign, forceAlign, includeZero, isInverted,
-                    isLog, 0.0, 0.0);
+                    isLog, TickUnits.standard, 0.0, 0.0);
             this.title = title;
         }
 
@@ -391,6 +417,8 @@
             String isInverted = axisElement.getAttribute("inverted");
             String isLog = axisElement.getAttribute("logarithmic");
 
+            final TickUnits tickUnits = parseTickUnits(axisElement);
+
             final double lowerMargin = parseDouble( axisElement.getAttribute("lowerMargin"), 0.0 );
             final double upperMargin = parseDouble( axisElement.getAttribute("upperMargin"), 0.0 );
             
@@ -406,6 +434,7 @@
                 else if ("force".equals(part)) forceAlign  = true;
             }
 
+
             Evaluator isInvertedE = parseEvaluator(isInverted, FALSE);
 
             Evaluator isLogE = parseEvaluator(isLog, FALSE);
@@ -413,11 +442,25 @@
             axesAttrs.add(new AxisAttributes(
                 name, isleftAlign, forceAlign,
                 includeZero.equals("true"),
-                isInvertedE, isLogE,
+                isInvertedE, isLogE, tickUnits,
                 lowerMargin,upperMargin));
         }
     }
     
+    private TickUnits parseTickUnits(Element axisElement) {
+        final String attribute = axisElement.getAttribute("tick-units");
+        if( attribute == null || attribute.trim().isEmpty())
+            return TickUnits.standard;
+
+        try {
+            return TickUnits.valueOf(attribute);
+        }
+        catch (Exception e) {
+            log.error(String.format("Invalid tick-units: %s", attribute), e);
+            return TickUnits.standard;
+        }
+    }
+
     private static double parseDouble( final String text, final double defaultValue ) {
         if( text == null || text.isEmpty() )
             return defaultValue;
@@ -427,7 +470,7 @@
         }
         catch (final NumberFormatException e) {
             e.printStackTrace();
-            log.error(String.format("Invalid double attribute: ", text), e);
+            log.error(String.format("Invalid double attribute: %s", text), e);
             return defaultValue;
         }
     }
diff -r 7efb27005d0d -r 55e2155ab52d artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Fri Jul 20 14:58:34 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Fri Jul 20 15:24:05 2018 +0200
@@ -31,6 +31,7 @@
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.river.artifacts.D4EArtifact;
 import org.dive4elements.river.exports.DiagramAttributes.AxisAttributes;
+import org.dive4elements.river.exports.DiagramAttributes.AxisAttributes.TickUnits;
 import org.dive4elements.river.exports.process.Processor;
 import org.dive4elements.river.jfree.AxisDataset;
 import org.dive4elements.river.jfree.Bounds;
@@ -1294,7 +1295,11 @@
         
         axis.setLowerMargin(axisAttributes.getLowerMargin());
         axis.setUpperMargin(axisAttributes.getUpperMargin());
-        
+
+        TickUnits tickUnits = axisAttributes.getTickUnits();
+        if( tickUnits != TickUnits.standard )
+            axis.setStandardTickUnits(tickUnits.createTickUnits());
+
         return axis;
     }
 


More information about the Dive4Elements-commits mailing list