Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 923:7ca4a287cd0e
#135 Modified the way to store datasets for different chart axes.
flys-artifacts/trunk@2275 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 01 Jul 2011 11:16:11 +0000 |
parents | f3fd8c9b7f51 |
children | f7761914f745 |
comparison
equal
deleted
inserted
replaced
922:95356252c309 | 923:7ca4a287cd0e |
---|---|
14 import org.jfree.chart.axis.ValueAxis; | 14 import org.jfree.chart.axis.ValueAxis; |
15 import org.jfree.chart.plot.PlotOrientation; | 15 import org.jfree.chart.plot.PlotOrientation; |
16 import org.jfree.chart.plot.XYPlot; | 16 import org.jfree.chart.plot.XYPlot; |
17 import org.jfree.data.Range; | 17 import org.jfree.data.Range; |
18 import org.jfree.data.xy.XYDataset; | 18 import org.jfree.data.xy.XYDataset; |
19 import org.jfree.data.xy.XYSeries; | |
20 import org.jfree.data.xy.XYSeriesCollection; | |
19 | 21 |
20 import org.jfree.ui.RectangleInsets; | 22 import org.jfree.ui.RectangleInsets; |
21 | 23 |
22 import de.intevation.flys.exports.ChartExportHelper; | 24 import de.intevation.flys.exports.ChartExportHelper; |
23 | 25 |
31 | 33 |
32 /** The logger that is used in this generator.*/ | 34 /** The logger that is used in this generator.*/ |
33 private static Logger logger = Logger.getLogger(XYChartGenerator.class); | 35 private static Logger logger = Logger.getLogger(XYChartGenerator.class); |
34 | 36 |
35 | 37 |
38 /** SeriesCollection used for the first axis.*/ | |
39 protected XYSeriesCollection first; | |
40 | |
41 /** SeriesCollection used for the second axis.*/ | |
42 protected XYSeriesCollection second; | |
43 | |
44 | |
36 public static final Color DEFAULT_GRID_COLOR = Color.GRAY; | 45 public static final Color DEFAULT_GRID_COLOR = Color.GRAY; |
37 public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f; | 46 public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f; |
38 | 47 |
39 | 48 |
40 /** | 49 /** |
55 * Returns the Y-Axis label of a chart. | 64 * Returns the Y-Axis label of a chart. |
56 * | 65 * |
57 * @return the Y-Axis label of a chart. | 66 * @return the Y-Axis label of a chart. |
58 */ | 67 */ |
59 protected abstract String getYAxisLabel(); | 68 protected abstract String getYAxisLabel(); |
60 | |
61 /** | |
62 * This method is called to add all datasets of a concrete XYChartGenerator | |
63 * to the JFreeChart. | |
64 * | |
65 * @param chart The JFreeChart object. | |
66 */ | |
67 protected abstract void addDatasets(JFreeChart chart); | |
68 | 69 |
69 | 70 |
70 public void generate() | 71 public void generate() |
71 throws IOException | 72 throws IOException |
72 { | 73 { |
100 chart.setBackgroundPaint(Color.WHITE); | 101 chart.setBackgroundPaint(Color.WHITE); |
101 chart.getPlot().setBackgroundPaint(Color.WHITE); | 102 chart.getPlot().setBackgroundPaint(Color.WHITE); |
102 | 103 |
103 XYPlot plot = (XYPlot) chart.getPlot(); | 104 XYPlot plot = (XYPlot) chart.getPlot(); |
104 | 105 |
105 addDatasets(chart); | 106 addDatasets(plot); |
106 addSubtitles(chart); | 107 addSubtitles(chart); |
107 adjustPlot(plot); | 108 adjustPlot(plot); |
108 adjustAxes(plot); | 109 adjustAxes(plot); |
110 | |
111 removeEmptyRangeAxes(plot); | |
112 | |
109 autoZoom(plot); | 113 autoZoom(plot); |
110 | 114 |
111 return chart; | 115 return chart; |
116 } | |
117 | |
118 | |
119 protected void addDatasets(XYPlot plot) { | |
120 if (first != null) { | |
121 logger.debug("Set the first axis dataset."); | |
122 plot.setDataset(0, first); | |
123 } | |
124 if (second != null) { | |
125 logger.debug("Set the second axis dataset."); | |
126 plot.setDataset(1, second); | |
127 } | |
128 } | |
129 | |
130 | |
131 public void addFirstAxisSeries(XYSeries series) { | |
132 if (first == null) { | |
133 first = new XYSeriesCollection(); | |
134 } | |
135 | |
136 if (series != null) { | |
137 first.addSeries(series); | |
138 } | |
139 } | |
140 | |
141 | |
142 public void addSecondAxisSeries(XYSeries series) { | |
143 if (second == null) { | |
144 second = new XYSeriesCollection(); | |
145 } | |
146 | |
147 if (series != null) { | |
148 second.addSeries(series); | |
149 } | |
150 } | |
151 | |
152 | |
153 private void removeEmptyRangeAxes(XYPlot plot) { | |
154 if (first == null) { | |
155 plot.setRangeAxis(0, null); | |
156 } | |
157 | |
158 if (second == null) { | |
159 plot.setRangeAxis(1, null); | |
160 } | |
112 } | 161 } |
113 | 162 |
114 | 163 |
115 /** | 164 /** |
116 * This method zooms the plot to the specified ranges in the attribute | 165 * This method zooms the plot to the specified ranges in the attribute |
127 Range xrange = getDomainAxisRange(); | 176 Range xrange = getDomainAxisRange(); |
128 Range yrange = getValueAxisRange(); | 177 Range yrange = getValueAxisRange(); |
129 | 178 |
130 for (int i = 0, num = plot.getDatasetCount(); i < num; i++) { | 179 for (int i = 0, num = plot.getDatasetCount(); i < num; i++) { |
131 XYDataset dataset = plot.getDataset(i); | 180 XYDataset dataset = plot.getDataset(i); |
181 | |
182 if (dataset == null) { | |
183 continue; | |
184 } | |
185 | |
132 Range[] ranges = getRangesForDataset(dataset); | 186 Range[] ranges = getRangesForDataset(dataset); |
133 | 187 |
134 if (i == 0) { | 188 if (i == 0) { |
135 ValueAxis xaxis = plot.getDomainAxis(); | 189 ValueAxis xaxis = plot.getDomainAxis(); |
136 zoomX(plot, xaxis, ranges[0], xrange); | 190 zoomX(plot, xaxis, ranges[0], xrange); |
259 plot.setRangeGridlineStroke(gridStroke); | 313 plot.setRangeGridlineStroke(gridStroke); |
260 plot.setRangeGridlinePaint(DEFAULT_GRID_COLOR); | 314 plot.setRangeGridlinePaint(DEFAULT_GRID_COLOR); |
261 plot.setRangeGridlinesVisible(true); | 315 plot.setRangeGridlinesVisible(true); |
262 | 316 |
263 plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d)); | 317 plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d)); |
318 | |
319 if (plot.getDataset(0) != null) { | |
320 plot.mapDatasetToRangeAxis(0, 0); | |
321 } | |
322 | |
323 if (plot.getDataset(1) != null) { | |
324 plot.mapDatasetToRangeAxis(1, 1); | |
325 } | |
264 } | 326 } |
265 | 327 |
266 | 328 |
267 protected void addSubtitles(JFreeChart chart) { | 329 protected void addSubtitles(JFreeChart chart) { |
268 // override this method in subclasses that need subtitles | 330 // override this method in subclasses that need subtitles |