# HG changeset patch # User Ingo Weinzierl # Date 1309518971 0 # Node ID 7ca4a287cd0e45e9bd5c9aa794197a0980b25471 # Parent 95356252c309bfff5522b3f0da4b209081055dc4 #135 Modified the way to store datasets for different chart axes. flys-artifacts/trunk@2275 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 95356252c309 -r 7ca4a287cd0e flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Fri Jul 01 08:33:52 2011 +0000 +++ b/flys-artifacts/ChangeLog Fri Jul 01 11:16:11 2011 +0000 @@ -1,3 +1,26 @@ +2011-07-01 Ingo Weinzierl + + flys/issue135 (Diagramm: Trotz abgeschalteter Themen bleiben Beschriftungen bestehen) + + * src/main/java/de/intevation/flys/exports/XYChartGenerator.java: The way + to store datasets has changed. Until this revision, the concrete + generators managed their own datasets. E.g. the + DischargeLongitudinalSectionGenerator had three datasets: w, q and + corrected w. Now, there are just two datasets, managed by this base + generator - one dataset for the first Y axis and one dataset for the + second Y axis. This makes it easier to remove axes, that have no data to + be displayed. All concrete chart generators have to add their XYSeries + using two methods: addFirstAxisSeries() and addSecondAxisSeries(). + + * src/main/java/de/intevation/flys/exports/DischargeLongitudinalSectionGenerator.java, + src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java, + src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java, + src/main/java/de/intevation/flys/exports/ComputedDischargeCurveGenerator.java, + src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java: + Those concrete chart generators no longer manage datasets themself but + they use the two methods described above, to plot the data to the first + or second Y axis. + 2011-07-01 Ingo Weinzierl * src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java: diff -r 95356252c309 -r 7ca4a287cd0e flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveGenerator.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveGenerator.java Fri Jul 01 08:33:52 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ComputedDischargeCurveGenerator.java Fri Jul 01 11:16:11 2011 +0000 @@ -6,6 +6,7 @@ import org.jfree.chart.JFreeChart; import org.jfree.chart.title.TextTitle; +import org.jfree.data.xy.XYSeries; import de.intevation.artifacts.Artifact; @@ -91,17 +92,15 @@ protected void doQOut(WQKms wqkms) { int size = wqkms.size(); - double[][] data = new double[2][size]; double[] res = new double[3]; + XYSeries series = new XYSeries(getSeriesName(wqkms)); for (int i = 0; i < size; i++) { res = wqkms.get(i, res); - - data[0][i] = res[1]; - data[1][i] = res[0]; + series.add(res[1], res[0]); } - dataset.addSeries(getSeriesName(wqkms), data); + addFirstAxisSeries(series); } diff -r 95356252c309 -r 7ca4a287cd0e flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java Fri Jul 01 08:33:52 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java Fri Jul 01 11:16:11 2011 +0000 @@ -11,7 +11,7 @@ import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.data.Range; -import org.jfree.data.xy.DefaultXYDataset; +import org.jfree.data.xy.XYSeries; import de.intevation.artifacts.Artifact; @@ -55,14 +55,9 @@ public static final String I18N_YAXIS_LABEL_DEFAULT = "W [cm]"; - /** The Dataset that contains the data for the chart.*/ - protected DefaultXYDataset dataset; - public DischargeCurveGenerator() { super(); - - this.dataset = new DefaultXYDataset(); } @@ -99,12 +94,6 @@ } - protected void addDatasets(JFreeChart chart) { - XYPlot plot = (XYPlot) chart.getPlot(); - plot.setDataset(0, dataset); - } - - public void doOut(Artifact artifact, Facet facet, Document attr) { logger.debug("DischargeCurveGenerator.doOut: " + facet.getName()); @@ -145,7 +134,15 @@ double [][] values = new double [][] { wqkms.getQs(), wqkms.getWs() }; - dataset.addSeries(seriesName, values); + int size = values != null ? values[0].length : 0; + + XYSeries series = new XYSeries(seriesName); + + for (int i = 0; i < size; i++) { + series.add(values[0][i], values[1][i]); + } + + addFirstAxisSeries(series); } diff -r 95356252c309 -r 7ca4a287cd0e flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeLongitudinalSectionGenerator.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeLongitudinalSectionGenerator.java Fri Jul 01 08:33:52 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeLongitudinalSectionGenerator.java Fri Jul 01 11:16:11 2011 +0000 @@ -4,11 +4,9 @@ import org.apache.log4j.Logger; -import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYSeries; -import org.jfree.data.xy.XYSeriesCollection; import org.w3c.dom.Document; @@ -33,24 +31,9 @@ Logger.getLogger(DischargeLongitudinalSectionGenerator.class); - /** The storage for the corrected W series to be drawn in this chart.*/ - protected XYSeriesCollection cw; - public DischargeLongitudinalSectionGenerator() { super(); - - this.cw = new XYSeriesCollection(); - } - - - @Override - public void addDatasets(JFreeChart chart) { - super.addDatasets(chart); - - XYPlot plot = (XYPlot) chart.getPlot(); - - plot.setDataset(2, cw); } @@ -58,36 +41,28 @@ protected void adjustPlot(XYPlot plot) { super.adjustPlot(plot); - // TODO REMOVE THIS CODE, IF WE HAVE INTRODUCED THEMES! - XYLineAndShapeRenderer rw = (XYLineAndShapeRenderer) - plot.getRendererForDataset(w); - - XYLineAndShapeRenderer rcw = null; - try { - rcw = (XYLineAndShapeRenderer) rw.clone(); - } - catch (Exception e) { - logger.error(e, e); - } - - int cwNum = cw.getSeriesCount(); + //// TODO REMOVE THIS CODE, IF WE HAVE INTRODUCED THEMES! + //XYLineAndShapeRenderer rw = (XYLineAndShapeRenderer) + // plot.getRendererForDataset(w); - for (int i = 0; i < cwNum; i++) { - rcw.setSeriesPaint(i, Color.RED); - } - - plot.setRenderer(2, rcw); - } + //XYLineAndShapeRenderer rcw = null; + //try { + // rcw = (XYLineAndShapeRenderer) rw.clone(); + //} + //catch (Exception e) { + // logger.error(e, e); + //} + //int cwNum = cw.getSeriesCount(); - protected void adjustAxes(XYPlot plot) { - super.adjustAxes(plot); + //for (int i = 0; i < cwNum; i++) { + // rcw.setSeriesPaint(i, Color.RED); + //} - plot.mapDatasetToRangeAxis(2, 0); + //plot.setRenderer(2, rcw); } - @Override public void doOut(Artifact artifact, Facet facet, Document attr) { logger.debug("DischargeLongitudinalSectionGenerator.doOut"); @@ -135,7 +110,8 @@ for (int i = 0; i < size; i++) { series.add(wqckms.getKms(i), wqckms.getC(i)); } - cw.addSeries(series); + + addFirstAxisSeries(series); } if (wqckms.guessWaterIncreasing()) { diff -r 95356252c309 -r 7ca4a287cd0e flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java Fri Jul 01 08:33:52 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java Fri Jul 01 11:16:11 2011 +0000 @@ -14,7 +14,6 @@ import org.jfree.chart.title.TextTitle; import org.jfree.data.Range; import org.jfree.data.xy.XYSeries; -import org.jfree.data.xy.XYSeriesCollection; import de.intevation.artifacts.Artifact; @@ -38,12 +37,6 @@ private static Logger logger = Logger.getLogger(DurationCurveGenerator.class); - /** The storage for the W series to be drawn in this chart.*/ - protected XYSeriesCollection w; - - /** The storage for the Q series to be drawn in this chart.*/ - protected XYSeriesCollection q; - public static final String I18N_DURATION_W = "chart.duration.curve.curve.w"; @@ -75,9 +68,6 @@ public DurationCurveGenerator() { super(); - - this.w = new XYSeriesCollection(); - this.q = new XYSeriesCollection(); } @@ -122,42 +112,34 @@ } - public void addDatasets(JFreeChart chart) { - XYPlot plot = (XYPlot) chart.getPlot(); - - plot.setDataset(0, w); - plot.setDataset(1, q); - } - - protected void adjustPlot(XYPlot plot) { super.adjustPlot(plot); // TODO REMOVE THIS CODE, IF WE HAVE INTRODUCED THEMES! - XYLineAndShapeRenderer rw = (XYLineAndShapeRenderer) - plot.getRendererForDataset(w); - - XYLineAndShapeRenderer rq = null; - try { - rq = (XYLineAndShapeRenderer) rw.clone(); - } - catch (Exception e) { - logger.error(e, e); - } + //XYLineAndShapeRenderer rw = (XYLineAndShapeRenderer) + // plot.getRendererForDataset(w); - int wNum = w.getSeriesCount(); - int qNum = q.getSeriesCount(); + //XYLineAndShapeRenderer rq = null; + //try { + // rq = (XYLineAndShapeRenderer) rw.clone(); + //} + //catch (Exception e) { + // logger.error(e, e); + //} - for (int i = 0; i < wNum; i++) { - rw.setSeriesPaint(i, Color.BLUE); - } + //int wNum = w.getSeriesCount(); + //int qNum = q.getSeriesCount(); - for (int i = 0; i < qNum; i++) { - rq.setSeriesPaint(i, Color.GREEN); - } + //for (int i = 0; i < wNum; i++) { + // rw.setSeriesPaint(i, Color.BLUE); + //} - plot.setRenderer(0, rw); - plot.setRenderer(1, rq); + //for (int i = 0; i < qNum; i++) { + // rq.setSeriesPaint(i, Color.GREEN); + //} + + //plot.setRenderer(0, rw); + //plot.setRenderer(1, rq); } @@ -167,7 +149,6 @@ NumberAxis qAxis = new NumberAxis("Q [m\u00b3/s]"); plot.setRangeAxis(1, qAxis); - plot.mapDatasetToRangeAxis(1, 1); } @@ -219,7 +200,7 @@ series.add((double) day, w); } - this.w.addSeries(series); + addFirstAxisSeries(series); } @@ -244,7 +225,7 @@ series.add((double) day, q); } - this.q.addSeries(series); + addSecondAxisSeries(series); } diff -r 95356252c309 -r 7ca4a287cd0e flys-artifacts/src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java Fri Jul 01 08:33:52 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java Fri Jul 01 11:16:11 2011 +0000 @@ -12,7 +12,6 @@ import org.jfree.chart.title.TextTitle; import org.jfree.data.Range; import org.jfree.data.xy.XYSeries; -import org.jfree.data.xy.XYSeriesCollection; import org.w3c.dom.Document; @@ -59,20 +58,12 @@ public static final String I18N_YAXIS_LABEL_DEFAULT = "W [NN + m]"; public static final String I18N_2YAXIS_LABEL_DEFAULT = "Q [m\u00b3/s]"; - /** The storage for the W series to be drawn in this chart.*/ - protected XYSeriesCollection w; - - /** The storage for the Q series to be drawn in this chart.*/ - protected XYSeriesCollection q; protected boolean inverted; public LongitudinalSectionGenerator() { super(); - - this.w = new XYSeriesCollection(); - this.q = new XYSeriesCollection(); } @@ -113,43 +104,34 @@ } - @Override - protected void addDatasets(JFreeChart chart) { - XYPlot plot = (XYPlot) chart.getPlot(); - - plot.setDataset(0, w); - plot.setDataset(1, q); - } - - protected void adjustPlot(XYPlot plot) { super.adjustPlot(plot); - // TODO REMOVE THIS CODE, IF WE HAVE INTRODUCED THEMES! - XYLineAndShapeRenderer rw = (XYLineAndShapeRenderer) - plot.getRendererForDataset(w); - - XYLineAndShapeRenderer rq = null; - try { - rq = (XYLineAndShapeRenderer) rw.clone(); - } - catch (Exception e) { - logger.error(e, e); - } + //// TODO REMOVE THIS CODE, IF WE HAVE INTRODUCED THEMES! + //XYLineAndShapeRenderer rw = (XYLineAndShapeRenderer) + // plot.getRendererForDataset(w); - int wNum = w.getSeriesCount(); - int qNum = q.getSeriesCount(); + //XYLineAndShapeRenderer rq = null; + //try { + // rq = (XYLineAndShapeRenderer) rw.clone(); + //} + //catch (Exception e) { + // logger.error(e, e); + //} - for (int i = 0; i < wNum; i++) { - rw.setSeriesPaint(i, Color.BLUE); - } + //int wNum = w.getSeriesCount(); + //int qNum = q.getSeriesCount(); - for (int i = 0; i < qNum; i++) { - rq.setSeriesPaint(i, Color.GREEN); - } + //for (int i = 0; i < wNum; i++) { + // rw.setSeriesPaint(i, Color.BLUE); + //} - plot.setRenderer(0, rw); - plot.setRenderer(1, rq); + //for (int i = 0; i < qNum; i++) { + // rq.setSeriesPaint(i, Color.GREEN); + //} + + //plot.setRenderer(0, rw); + //plot.setRenderer(1, rq); } @@ -160,7 +142,6 @@ msg(I18N_2YAXIS_LABEL, I18N_2YAXIS_LABEL_DEFAULT)); plot.setRangeAxis(1, qAxis); - plot.mapDatasetToRangeAxis(1, 1); invertXAxis(plot.getDomainAxis()); } @@ -252,7 +233,7 @@ series.add(wqkms.getKms(i), wqkms.getW(i)); } - w.addSeries(series); + addFirstAxisSeries(series); if (wqkms.guessWaterIncreasing()) { setInverted(true); @@ -285,7 +266,7 @@ series.add(wqkms.getKms(i), wqkms.getQ(i)); } - q.addSeries(series); + addSecondAxisSeries(series); if (wqkms.guessWaterIncreasing()) { setInverted(true); diff -r 95356252c309 -r 7ca4a287cd0e flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java Fri Jul 01 08:33:52 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java Fri Jul 01 11:16:11 2011 +0000 @@ -16,6 +16,8 @@ import org.jfree.chart.plot.XYPlot; import org.jfree.data.Range; import org.jfree.data.xy.XYDataset; +import org.jfree.data.xy.XYSeries; +import org.jfree.data.xy.XYSeriesCollection; import org.jfree.ui.RectangleInsets; @@ -33,6 +35,13 @@ private static Logger logger = Logger.getLogger(XYChartGenerator.class); + /** SeriesCollection used for the first axis.*/ + protected XYSeriesCollection first; + + /** SeriesCollection used for the second axis.*/ + protected XYSeriesCollection second; + + public static final Color DEFAULT_GRID_COLOR = Color.GRAY; public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f; @@ -58,14 +67,6 @@ */ protected abstract String getYAxisLabel(); - /** - * This method is called to add all datasets of a concrete XYChartGenerator - * to the JFreeChart. - * - * @param chart The JFreeChart object. - */ - protected abstract void addDatasets(JFreeChart chart); - public void generate() throws IOException @@ -102,16 +103,64 @@ XYPlot plot = (XYPlot) chart.getPlot(); - addDatasets(chart); + addDatasets(plot); addSubtitles(chart); adjustPlot(plot); adjustAxes(plot); + + removeEmptyRangeAxes(plot); + autoZoom(plot); return chart; } + protected void addDatasets(XYPlot plot) { + if (first != null) { + logger.debug("Set the first axis dataset."); + plot.setDataset(0, first); + } + if (second != null) { + logger.debug("Set the second axis dataset."); + plot.setDataset(1, second); + } + } + + + public void addFirstAxisSeries(XYSeries series) { + if (first == null) { + first = new XYSeriesCollection(); + } + + if (series != null) { + first.addSeries(series); + } + } + + + public void addSecondAxisSeries(XYSeries series) { + if (second == null) { + second = new XYSeriesCollection(); + } + + if (series != null) { + second.addSeries(series); + } + } + + + private void removeEmptyRangeAxes(XYPlot plot) { + if (first == null) { + plot.setRangeAxis(0, null); + } + + if (second == null) { + plot.setRangeAxis(1, null); + } + } + + /** * This method zooms the plot to the specified ranges in the attribute * document or to the ranges specified by the min/max values in the @@ -129,6 +178,11 @@ for (int i = 0, num = plot.getDatasetCount(); i < num; i++) { XYDataset dataset = plot.getDataset(i); + + if (dataset == null) { + continue; + } + Range[] ranges = getRangesForDataset(dataset); if (i == 0) { @@ -261,6 +315,14 @@ plot.setRangeGridlinesVisible(true); plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d)); + + if (plot.getDataset(0) != null) { + plot.mapDatasetToRangeAxis(0, 0); + } + + if (plot.getDataset(1) != null) { + plot.mapDatasetToRangeAxis(1, 1); + } }