Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/XYChartGenerator.java @ 7034:557cb3a3d772 generator-refactoring
Combine AxisDataset implementations and remove / simplify handling code.
Tested with longitudinal section and hist. discharges should be
ok.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Wed, 18 Sep 2013 10:12:47 +0200 |
parents | 1b35b2ddfc28 |
children | 06a9a241faac |
comparison
equal
deleted
inserted
replaced
7033:0d91a6598a89 | 7034:557cb3a3d772 |
---|---|
44 import org.dive4elements.river.jfree.CollisionFreeXYTextAnnotation; | 44 import org.dive4elements.river.jfree.CollisionFreeXYTextAnnotation; |
45 import org.dive4elements.river.jfree.DoubleBounds; | 45 import org.dive4elements.river.jfree.DoubleBounds; |
46 import org.dive4elements.river.jfree.RiverAnnotation; | 46 import org.dive4elements.river.jfree.RiverAnnotation; |
47 import org.dive4elements.river.jfree.StyledAreaSeriesCollection; | 47 import org.dive4elements.river.jfree.StyledAreaSeriesCollection; |
48 import org.dive4elements.river.jfree.StyledXYSeries; | 48 import org.dive4elements.river.jfree.StyledXYSeries; |
49 import org.dive4elements.river.jfree.AxisDataset; | |
49 import org.dive4elements.river.themes.ThemeDocument; | 50 import org.dive4elements.river.themes.ThemeDocument; |
50 | 51 |
51 | 52 |
52 /** | 53 /** |
53 * An abstract base class for creating XY charts. | 54 * An abstract base class for creating XY charts. |
64 * | 65 * |
65 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 66 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
66 */ | 67 */ |
67 public abstract class XYChartGenerator extends ChartGenerator { | 68 public abstract class XYChartGenerator extends ChartGenerator { |
68 | 69 |
69 public class XYAxisDataset implements AxisDataset { | |
70 /** Symbolic integer, but also coding the priority (0 goes first). */ | |
71 protected int axisSymbol; | |
72 | |
73 /** List of assigned datasets (in order). */ | |
74 protected List<XYDataset> datasets; | |
75 | |
76 /** Range to use to include all given datasets. */ | |
77 protected Range range; | |
78 | |
79 /** Index of axis in plot. */ | |
80 protected int plotAxisIndex; | |
81 | |
82 /** Create AxisDataset. */ | |
83 public XYAxisDataset(int symb) { | |
84 this.axisSymbol = symb; | |
85 datasets = new ArrayList<XYDataset>(); | |
86 } | |
87 | |
88 /** Merge (or create given range with range so far (if any). */ | |
89 private void mergeRanges(Range subRange) { | |
90 // Avoid merging NaNs, as they take min/max place forever. | |
91 if (subRange == null || | |
92 Double.isNaN(subRange.getLowerBound()) || | |
93 Double.isNaN(subRange.getUpperBound())) { | |
94 return; | |
95 } | |
96 if (range == null) { | |
97 range = subRange; | |
98 return; | |
99 } | |
100 range = Range.combine(range, subRange); | |
101 } | |
102 | |
103 | |
104 /** Add a dataset to internal list for this axis. */ | |
105 @Override | |
106 public void addDataset(XYDataset dataset) { | |
107 datasets.add(dataset); | |
108 includeYRange(((XYSeriesCollection) dataset).getSeries(0)); | |
109 } | |
110 | |
111 /** Add a dataset, include its range. */ | |
112 public void addDataset(XYSeries series) { | |
113 addDataset(new XYSeriesCollection(series)); | |
114 } | |
115 | |
116 | |
117 /** Set Range for this axis. */ | |
118 @Override | |
119 public void setRange(Range range) { | |
120 this.range = range; | |
121 } | |
122 | |
123 | |
124 /** Get Range for this axis. */ | |
125 @Override | |
126 public Range getRange() { | |
127 return range; | |
128 } | |
129 | |
130 | |
131 /** Get Array of Datasets. */ | |
132 @Override | |
133 public XYDataset[] getDatasets() { | |
134 return datasets.toArray(new XYDataset[datasets.size()]); | |
135 } | |
136 | |
137 | |
138 /** Add a Dataset that describes an area. */ | |
139 public void addArea(StyledAreaSeriesCollection series) { | |
140 this.datasets.add(series); | |
141 List<?> allSeries = series.getSeries(); | |
142 /* We do not include the bounds/ranges, if the area includes | |
143 * points at "infinity"/BIG_DOUBLE_VALUE, the charts extents are | |
144 * expanded to include these very small/big value. | |
145 * This is especially used when showing "area above axis". */ | |
146 } | |
147 | |
148 /** True if to be rendered as area. */ | |
149 @Override | |
150 public boolean isArea(XYDataset series) { | |
151 return (series instanceof StyledAreaSeriesCollection); | |
152 } | |
153 | |
154 /** Adjust range to include given dataset. */ | |
155 public void includeYRange(XYSeries dataset) { | |
156 mergeRanges(new Range(dataset.getMinY(), dataset.getMaxY())); | |
157 } | |
158 | |
159 /** True if no datasets given. */ | |
160 @Override | |
161 public boolean isEmpty() { | |
162 return this.datasets.isEmpty(); | |
163 } | |
164 | |
165 /** Set the 'real' axis index that this axis is mapped to. */ | |
166 @Override | |
167 public void setPlotAxisIndex(int axisIndex) { | |
168 this.plotAxisIndex = axisIndex; | |
169 } | |
170 | |
171 /** Get the 'real' axis index that this axis is mapped to. */ | |
172 @Override | |
173 public int getPlotAxisIndex() { | |
174 return this.plotAxisIndex; | |
175 } | |
176 } // class AxisDataset | |
177 | |
178 /** Enumerator over existing axes. */ | 70 /** Enumerator over existing axes. */ |
179 @Override | 71 @Override |
180 protected abstract YAxisWalker getYAxisWalker(); | 72 protected abstract YAxisWalker getYAxisWalker(); |
181 | 73 |
182 public static final int AXIS_SPACE = 5; | 74 public static final int AXIS_SPACE = 5; |
413 } | 305 } |
414 | 306 |
415 | 307 |
416 @Override | 308 @Override |
417 protected AxisDataset createAxisDataset(int idx) { | 309 protected AxisDataset createAxisDataset(int idx) { |
418 logger.debug("Create new XYAxisDataset for index: " + idx); | 310 logger.debug("Create new AxisDataset for index: " + idx); |
419 return new XYAxisDataset(idx); | 311 return new AxisDataset(idx); |
420 } | 312 } |
421 | 313 |
422 | 314 |
423 /** | 315 /** |
424 * Put debug output about datasets. | 316 * Put debug output about datasets. |
479 if (area == null) { | 371 if (area == null) { |
480 logger.warn("Cannot yet render above/under curve."); | 372 logger.warn("Cannot yet render above/under curve."); |
481 return; | 373 return; |
482 } | 374 } |
483 | 375 |
484 XYAxisDataset axisDataset = (XYAxisDataset) getAxisDataset(index); | 376 AxisDataset axisDataset = (AxisDataset) getAxisDataset(index); |
485 | 377 |
486 if (visible) { | 378 if (visible) { |
487 axisDataset.addArea(area); | 379 axisDataset.addArea(area); |
488 } | 380 } |
489 else { | 381 else { |
508 logger.debug("Y Range of XYSeries: " + | 400 logger.debug("Y Range of XYSeries: " + |
509 series.getMinY() + " | " + series.getMaxY()); | 401 series.getMinY() + " | " + series.getMaxY()); |
510 | 402 |
511 addAxisDataset(new XYSeriesCollection(series), index, visible); | 403 addAxisDataset(new XYSeriesCollection(series), index, visible); |
512 | 404 |
513 XYAxisDataset axisDataset = (XYAxisDataset) getAxisDataset(index); | 405 AxisDataset axisDataset = (AxisDataset) getAxisDataset(index); |
514 | |
515 if (!visible) { | |
516 // Do this also when not visible to have axis scaled by default such | |
517 // that every data-point could be seen (except for annotations). | |
518 axisDataset.includeYRange(series); | |
519 } | |
520 } | 406 } |
521 | 407 |
522 | 408 |
523 /** | 409 /** |
524 * Add the given vertical marker to the chart. | 410 * Add the given vertical marker to the chart. |