Mercurial > dive4elements > river
changeset 7079:48e30e2c5db9 generator-refactoring
ChartGenerator2 can be used as ChartInfoGenerator, too.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 20 Sep 2013 17:00:59 +0200 (2013-09-20) |
parents | b780cf1148c3 |
children | 02a704a37323 |
files | artifacts/src/main/java/org/dive4elements/river/exports/ChartGenerator2.java |
diffstat | 1 files changed, 126 insertions(+), 32 deletions(-) [+] |
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/ChartGenerator2.java Fri Sep 20 16:34:37 2013 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/exports/ChartGenerator2.java Fri Sep 20 17:00:59 2013 +0200 @@ -8,43 +8,22 @@ package org.dive4elements.river.exports; -import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; -import org.dive4elements.artifactdatabase.state.Settings; -import org.dive4elements.artifacts.Artifact; -import org.dive4elements.artifacts.ArtifactNamespaceContext; -import org.dive4elements.artifacts.CallContext; -import org.dive4elements.artifacts.CallMeta; -import org.dive4elements.artifacts.PreferredLocale; -import org.dive4elements.artifacts.common.utils.XMLUtils; -import org.dive4elements.river.artifacts.access.RangeAccess; -import org.dive4elements.river.artifacts.D4EArtifact; -import org.dive4elements.river.artifacts.resources.Resources; -import org.dive4elements.river.collections.D4EArtifactCollection; -import org.dive4elements.river.jfree.Bounds; -import org.dive4elements.river.jfree.DoubleBounds; -import org.dive4elements.river.jfree.EnhancedLineAndShapeRenderer; -import org.dive4elements.river.jfree.RiverAnnotation; -import org.dive4elements.river.jfree.StableXYDifferenceRenderer; -import org.dive4elements.river.jfree.Style; -import org.dive4elements.river.jfree.StyledAreaSeriesCollection; -import org.dive4elements.river.jfree.StyledSeries; -import org.dive4elements.river.jfree.AxisDataset; -import org.dive4elements.river.model.River; -import org.dive4elements.river.themes.LineStyle; -import org.dive4elements.river.themes.TextStyle; -import org.dive4elements.river.themes.ThemeDocument; -import org.dive4elements.river.utils.RiverUtils; - import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; +import java.awt.Graphics2D; import java.awt.Paint; import java.awt.Stroke; import java.awt.TexturePaint; +import java.awt.Transparency; + import java.awt.geom.Rectangle2D; + import java.awt.image.BufferedImage; + import java.io.IOException; import java.io.OutputStream; + import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -55,23 +34,72 @@ import javax.xml.xpath.XPathConstants; import org.apache.log4j.Logger; + +import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; +import org.dive4elements.artifactdatabase.state.Settings; + +import org.dive4elements.artifacts.Artifact; +import org.dive4elements.artifacts.ArtifactNamespaceContext; +import org.dive4elements.artifacts.CallContext; +import org.dive4elements.artifacts.CallMeta; +import org.dive4elements.artifacts.PreferredLocale; + +import org.dive4elements.artifacts.common.utils.XMLUtils; + +import org.dive4elements.river.artifacts.D4EArtifact; + +import org.dive4elements.river.artifacts.access.RangeAccess; + +import org.dive4elements.river.artifacts.resources.Resources; + +import org.dive4elements.river.collections.D4EArtifactCollection; + +import org.dive4elements.river.java2d.NOPGraphics2D; + +import org.dive4elements.river.jfree.AxisDataset; +import org.dive4elements.river.jfree.Bounds; +import org.dive4elements.river.jfree.DoubleBounds; +import org.dive4elements.river.jfree.EnhancedLineAndShapeRenderer; +import org.dive4elements.river.jfree.RiverAnnotation; +import org.dive4elements.river.jfree.StableXYDifferenceRenderer; +import org.dive4elements.river.jfree.Style; +import org.dive4elements.river.jfree.StyledAreaSeriesCollection; +import org.dive4elements.river.jfree.StyledSeries; + +import org.dive4elements.river.model.River; + +import org.dive4elements.river.themes.LineStyle; +import org.dive4elements.river.themes.TextStyle; +import org.dive4elements.river.themes.ThemeDocument; + +import org.dive4elements.river.utils.Formatter; +import org.dive4elements.river.utils.RiverUtils; + +import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.JFreeChart; import org.jfree.chart.LegendItem; import org.jfree.chart.LegendItemCollection; + import org.jfree.chart.axis.NumberAxis; + import org.jfree.chart.plot.XYPlot; + import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; + import org.jfree.chart.title.TextTitle; + import org.jfree.data.Range; + import org.jfree.data.general.Series; + import org.jfree.data.xy.XYDataset; + import org.jfree.ui.RectangleInsets; import org.jfree.ui.TextAnchor; + import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.dive4elements.river.utils.Formatter; - /** * Implementation of the OutGenerator interface for charts. * It should provide some basic things that equal in all chart types. @@ -81,6 +109,10 @@ private static Logger logger = Logger.getLogger(ChartGenerator2.class); + public static final boolean USE_NOP_GRAPHICS = + Boolean.getBoolean("info.rendering.nop.graphics"); + + public static final int DEFAULT_CHART_WIDTH = 600; public static final int DEFAULT_CHART_HEIGHT = 400; public static final String DEFAULT_CHART_FORMAT = "png"; @@ -256,11 +288,73 @@ * Generate chart. */ @Override - public void generate() - throws IOException - { + public void generate() throws IOException { + logger.debug("ChartGenerator2.generate"); + if (outName.indexOf("chartinfo") > 0) { + generateInfo(); + } + else { + generateImage(); + } + } + + + /** Generate only meta infos */ + private void generateInfo() throws IOException { + + logger.debug("ChartInfoGenerator2.generateInfo"); + + JFreeChart chart = generateChart(); + + int[] size = getSize(); + if (size == null) { + size = getDefaultSize(); + } + + ChartRenderingInfo info = new ChartRenderingInfo(); + + long startTime = System.currentTimeMillis(); + + if (USE_NOP_GRAPHICS) { + BufferedImage image = + new BufferedImage(size[0], size[1], Transparency.BITMASK); + + Graphics2D g2d = image.createGraphics(); + Graphics2D nop = new NOPGraphics2D(g2d); + + chart.draw( + nop, + new Rectangle2D.Double(0, 0, size[0], size[1]), + null, + info); + + nop.dispose(); + } + else { + chart.createBufferedImage( + size[0], size[1], Transparency.BITMASK, info); + } + + long stopTime = System.currentTimeMillis(); + + if (logger.isDebugEnabled()) { + logger.debug("Rendering info took: " + + (stopTime-startTime) + "ms"); + } + + + InfoGeneratorHelper helper = new InfoGeneratorHelper(this); + Document doc = helper.createInfoDocument(chart, info); + + XMLUtils.toStream(doc, out); + } + + /** Generate the diagram as an image. */ + private void generateImage() throws IOException { + logger.debug("ChartGenerator2.generateImage"); + JFreeChart chart = generateChart(); String format = getFormat();