Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java @ 9283:55e2155ab52d
Introduced new option 'tick-units' for axes. Configured the count Axis to only allow for integer ticks.
author | gernotbelger |
---|---|
date | Fri, 20 Jul 2018 15:24:05 +0200 |
parents | 87a242425467 |
children |
comparison
equal
deleted
inserted
replaced
9282:7efb27005d0d | 9283:55e2155ab52d |
---|---|
13 import java.util.Collections; | 13 import java.util.Collections; |
14 import java.util.List; | 14 import java.util.List; |
15 | 15 |
16 import org.w3c.dom.Element; | 16 import org.w3c.dom.Element; |
17 import org.w3c.dom.NodeList; | 17 import org.w3c.dom.NodeList; |
18 | 18 import org.dive4elements.river.exports.DiagramAttributes.AxisAttributes.TickUnits; |
19 import org.dive4elements.river.exports.process.Processor; | 19 import org.dive4elements.river.exports.process.Processor; |
20 | 20 import org.jfree.chart.axis.NumberAxis; |
21 import org.jfree.chart.axis.TickUnitSource; | |
21 import org.apache.log4j.Logger; | 22 import org.apache.log4j.Logger; |
22 | 23 |
23 import org.dive4elements.artifacts.CallContext; | 24 import org.dive4elements.artifacts.CallContext; |
24 | 25 |
25 import org.dive4elements.river.artifacts.resources.Resources; | 26 import org.dive4elements.river.artifacts.resources.Resources; |
113 return DiagramAttributes.this.getAxesAttributes(); | 114 return DiagramAttributes.this.getAxesAttributes(); |
114 } | 115 } |
115 } // class Instance | 116 } // class Instance |
116 | 117 |
117 public static class AxisAttributes { | 118 public static class AxisAttributes { |
119 | |
120 /** Possible point of extension in order to support different tick units per axis.*/ | |
121 public static enum TickUnits { | |
122 standard { | |
123 @Override | |
124 public TickUnitSource createTickUnits() { | |
125 return NumberAxis.createStandardTickUnits(); | |
126 } | |
127 }, integer { | |
128 @Override | |
129 public TickUnitSource createTickUnits() { | |
130 return NumberAxis.createIntegerTickUnits(); | |
131 } | |
132 }; | |
133 | |
134 public abstract TickUnitSource createTickUnits(); | |
135 } | |
136 | |
118 private final String name; | 137 private final String name; |
119 private final boolean isLeftAlign; // TODO: Remove! | 138 private final boolean isLeftAlign; // TODO: Remove! |
120 private final boolean forceAlign; // TODO: Remove! | 139 private final boolean forceAlign; // TODO: Remove! |
121 private final boolean includeZero; // TODO: Use Evaluator | 140 private final boolean includeZero; // TODO: Use Evaluator |
122 | 141 |
123 private final Evaluator isInverted; | 142 private final Evaluator isInverted; |
124 private final Evaluator isLog; | 143 private final Evaluator isLog; |
125 private final double lowerMargin; | 144 private final double lowerMargin; |
126 private final double upperMargin; | 145 private final double upperMargin; |
146 private TickUnits tickUnits; | |
127 | 147 |
128 public AxisAttributes( | 148 public AxisAttributes( |
129 String name, | 149 String name, |
130 boolean isLeftAlign, | 150 boolean isLeftAlign, |
131 boolean forceAlign, | 151 boolean forceAlign, |
132 boolean includeZero, | 152 boolean includeZero, |
133 Evaluator isInverted, | 153 Evaluator isInverted, |
134 Evaluator isLog, | 154 Evaluator isLog, |
155 TickUnits tickUnits, | |
135 double lowerMargin, | 156 double lowerMargin, |
136 double upperMargin | 157 double upperMargin |
137 ) { | 158 ) { |
138 this.name = name; | 159 this.name = name; |
139 this.isLeftAlign = isLeftAlign; | 160 this.isLeftAlign = isLeftAlign; |
140 this.forceAlign = forceAlign; | 161 this.forceAlign = forceAlign; |
141 this.includeZero = includeZero; | 162 this.includeZero = includeZero; |
142 this.isInverted = isInverted; | 163 this.isInverted = isInverted; |
143 this.isLog = isLog; | 164 this.isLog = isLog; |
165 this.tickUnits = tickUnits; | |
144 this.lowerMargin = lowerMargin; | 166 this.lowerMargin = lowerMargin; |
145 this.upperMargin = upperMargin; | 167 this.upperMargin = upperMargin; |
146 } | 168 } |
147 | 169 |
148 public String getName() { | 170 public String getName() { |
173 return this.lowerMargin; | 195 return this.lowerMargin; |
174 } | 196 } |
175 | 197 |
176 public double getUpperMargin() { | 198 public double getUpperMargin() { |
177 return this.upperMargin; | 199 return this.upperMargin; |
200 } | |
201 | |
202 public TickUnits getTickUnits() { | |
203 return this.tickUnits; | |
178 } | 204 } |
179 } // class AxisAttributes | 205 } // class AxisAttributes |
180 | 206 |
181 public class DomainAxisAttributes extends AxisAttributes { | 207 public class DomainAxisAttributes extends AxisAttributes { |
182 | 208 |
190 Evaluator isInverted, | 216 Evaluator isInverted, |
191 Evaluator isLog, | 217 Evaluator isLog, |
192 Title title | 218 Title title |
193 ) { | 219 ) { |
194 super(name, isLeftAlign, forceAlign, includeZero, isInverted, | 220 super(name, isLeftAlign, forceAlign, includeZero, isInverted, |
195 isLog, 0.0, 0.0); | 221 isLog, TickUnits.standard, 0.0, 0.0); |
196 this.title = title; | 222 this.title = title; |
197 } | 223 } |
198 | 224 |
199 public Title getTitle() { | 225 public Title getTitle() { |
200 return title; | 226 return title; |
389 axisElement.getAttribute("include-zero").trim(); | 415 axisElement.getAttribute("include-zero").trim(); |
390 | 416 |
391 String isInverted = axisElement.getAttribute("inverted"); | 417 String isInverted = axisElement.getAttribute("inverted"); |
392 String isLog = axisElement.getAttribute("logarithmic"); | 418 String isLog = axisElement.getAttribute("logarithmic"); |
393 | 419 |
420 final TickUnits tickUnits = parseTickUnits(axisElement); | |
421 | |
394 final double lowerMargin = parseDouble( axisElement.getAttribute("lowerMargin"), 0.0 ); | 422 final double lowerMargin = parseDouble( axisElement.getAttribute("lowerMargin"), 0.0 ); |
395 final double upperMargin = parseDouble( axisElement.getAttribute("upperMargin"), 0.0 ); | 423 final double upperMargin = parseDouble( axisElement.getAttribute("upperMargin"), 0.0 ); |
396 | 424 |
397 if (name.isEmpty()) { | 425 if (name.isEmpty()) { |
398 continue; | 426 continue; |
404 if ("left" .equals(part)) isleftAlign = true; | 432 if ("left" .equals(part)) isleftAlign = true; |
405 else if ("right".equals(part)) isleftAlign = false; | 433 else if ("right".equals(part)) isleftAlign = false; |
406 else if ("force".equals(part)) forceAlign = true; | 434 else if ("force".equals(part)) forceAlign = true; |
407 } | 435 } |
408 | 436 |
437 | |
409 Evaluator isInvertedE = parseEvaluator(isInverted, FALSE); | 438 Evaluator isInvertedE = parseEvaluator(isInverted, FALSE); |
410 | 439 |
411 Evaluator isLogE = parseEvaluator(isLog, FALSE); | 440 Evaluator isLogE = parseEvaluator(isLog, FALSE); |
412 | 441 |
413 axesAttrs.add(new AxisAttributes( | 442 axesAttrs.add(new AxisAttributes( |
414 name, isleftAlign, forceAlign, | 443 name, isleftAlign, forceAlign, |
415 includeZero.equals("true"), | 444 includeZero.equals("true"), |
416 isInvertedE, isLogE, | 445 isInvertedE, isLogE, tickUnits, |
417 lowerMargin,upperMargin)); | 446 lowerMargin,upperMargin)); |
418 } | 447 } |
419 } | 448 } |
420 | 449 |
450 private TickUnits parseTickUnits(Element axisElement) { | |
451 final String attribute = axisElement.getAttribute("tick-units"); | |
452 if( attribute == null || attribute.trim().isEmpty()) | |
453 return TickUnits.standard; | |
454 | |
455 try { | |
456 return TickUnits.valueOf(attribute); | |
457 } | |
458 catch (Exception e) { | |
459 log.error(String.format("Invalid tick-units: %s", attribute), e); | |
460 return TickUnits.standard; | |
461 } | |
462 } | |
463 | |
421 private static double parseDouble( final String text, final double defaultValue ) { | 464 private static double parseDouble( final String text, final double defaultValue ) { |
422 if( text == null || text.isEmpty() ) | 465 if( text == null || text.isEmpty() ) |
423 return defaultValue; | 466 return defaultValue; |
424 | 467 |
425 try { | 468 try { |
426 return Double.parseDouble(text); | 469 return Double.parseDouble(text); |
427 } | 470 } |
428 catch (final NumberFormatException e) { | 471 catch (final NumberFormatException e) { |
429 e.printStackTrace(); | 472 e.printStackTrace(); |
430 log.error(String.format("Invalid double attribute: ", text), e); | 473 log.error(String.format("Invalid double attribute: %s", text), e); |
431 return defaultValue; | 474 return defaultValue; |
432 } | 475 } |
433 } | 476 } |
434 | 477 |
435 private static Evaluator createDynamicEvaluator( | 478 private static Evaluator createDynamicEvaluator( |