tim@82: /** tim@82: * Title: ChartFactory, $Header: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/output/chart/ChartFactory.java,v 1.8 2007/12/21 12:31:15 blume Exp $ tim@82: * Source: $Source: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/output/chart/ChartFactory.java,v $ tim@82: * created by: Stefan Blume (blume) tim@82: * erstellt am: 06.12.2007 tim@82: * Copyright: con terra GmbH, 2005 tim@82: * tim@82: * modified by: $Author: blume $ tim@82: * modified on: $Date: 2007/12/21 12:31:15 $ tim@82: * Version: $Revision: 1.8 $ tim@82: * TAG: $Name: $ tim@82: * locked from: $Locker: $ tim@82: * CVS State: $State: Exp $ tim@82: * Project: $ProjectName$ tim@82: */ tim@82: package de.intevation.gnv.chart; tim@82: tim@82: import java.awt.Color; tim@82: import java.awt.Font; tim@82: import java.awt.image.BufferedImage; tim@82: import java.io.IOException; tim@82: import java.io.OutputStream; tim@82: import java.util.Collection; tim@82: import java.util.Iterator; tim@82: tim@82: import org.apache.log4j.Logger; tim@82: import org.jfree.chart.JFreeChart; tim@82: import org.jfree.chart.axis.AxisLocation; tim@82: import org.jfree.chart.axis.NumberAxis; tim@82: import org.jfree.chart.axis.NumberTickUnit; tim@82: import org.jfree.chart.encoders.KeypointPNGEncoderAdapter; tim@82: import org.jfree.chart.plot.PlotOrientation; tim@82: import org.jfree.chart.plot.XYPlot; tim@82: import org.jfree.chart.renderer.xy.StandardXYItemRenderer; tim@82: import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; tim@82: import org.jfree.data.xy.XYDataset; tim@82: import org.jfree.data.xy.XYSeries; tim@82: import org.jfree.data.xy.XYSeriesCollection; tim@82: import org.jfree.ui.RectangleInsets; tim@82: tim@82: import de.intevation.gnv.chart.exception.TechnicalChartException; tim@82: import de.intevation.gnv.geobackend.base.Result; tim@82: import de.intevation.gnv.transition.describedata.KeyValueDescibeData; tim@82: tim@82: /** tim@82: * tim@86: * @author Tim Englich tim@86: * tim@82: */ tim@82: public class VerticalProfileChartFactory { tim@82: tim@82: /** tim@82: * Default Logging instance tim@82: */ tim@82: private static Logger sLogger = Logger.getLogger(VerticalProfileChartFactory.class); tim@82: private static boolean sDebug = sLogger.isDebugEnabled(); tim@113: protected PlotOrientation plotOrientation = PlotOrientation.HORIZONTAL; tim@82: tim@113: public synchronized void createProfileChart(ChartLabels pLabels, ChartStyle pStyle, Collection parameters, Collection measurements, OutputStream outputStream, Collection resultSet) throws IOException, TechnicalChartException { tim@82: if (sDebug) tim@82: sLogger.debug("createSimpleTimeSeriesChart()"); tim@82: int lLowerLevel = Integer.MIN_VALUE; tim@82: int lUpperLevel = Integer.MAX_VALUE; tim@82: if (pStyle.isUseUpperDataLevel() tim@82: && pStyle.getUpperLevel() < Integer.MAX_VALUE) { tim@82: lUpperLevel = pStyle.getUpperLevel(); tim@82: } tim@82: if (pStyle.isUseLowerDataLevel() tim@82: && pStyle.getLowerLevel() > Integer.MIN_VALUE) { tim@82: lLowerLevel = pStyle.getLowerLevel(); tim@82: } tim@82: if (sDebug) tim@82: sLogger.debug(" vor createDataset()"); tim@86: XYDataset lSet = this.createDataset(resultSet, lUpperLevel, tim@82: lLowerLevel,parameters,measurements); tim@82: if (sDebug) tim@82: sLogger.debug(" nach createDataset()"); tim@82: final Color[] color = {Color.black, Color.red, Color.green, Color.blue}; tim@86: NumberAxis domain = new NumberAxis(pLabels.getDomainAxisLabel()); tim@82: NumberAxis axis; tim@82: StandardXYItemRenderer renderer = new StandardXYItemRenderer(); tim@82: XYPlot plot = new XYPlot(); tim@82: //Global settings tim@82: tim@113: plot.setOrientation(plotOrientation); tim@82: plot.setBackgroundPaint(Color.lightGray); tim@82: plot.setDomainGridlinePaint(Color.white); tim@82: plot.setRangeGridlinePaint(Color.white); tim@82: plot.setAxisOffset(new RectangleInsets(5.0,5.0,5.0,5.0)); tim@82: //plot.getRangeAxis().setFixedDimension(10.0); tim@82: plot.setDomainAxis(domain); tim@82: plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT); tim@82: if (parameters.size() == 1) { tim@82: KeyValueDescibeData parameter = parameters.iterator().next(); tim@82: axis = new NumberAxis(parameter.getValue()); tim@82: if(parameter.getValue().contains("richtung")){ tim@82: NumberAxis axis1 = new NumberAxis( tim@82: ((String) parameter.getValue()));//,new Range(0.0,360.0)); tim@82: axis1.setTickUnit(new NumberTickUnit(30.0)); tim@82: axis1.setUpperBound(360.0); tim@82: axis1.setLowerBound(0.0); tim@82: //axis1.setDisplayRange(0.0,360.0); tim@82: plot.setRangeAxis( axis1); tim@82: }else{ tim@82: axis.setFixedDimension(10.0); tim@82: axis.setAutoRangeIncludesZero(false); tim@82: plot.setRangeAxis(axis); tim@82: } tim@82: axis.configure(); tim@82: plot.setRangeAxisLocation( AxisLocation.BOTTOM_OR_LEFT); tim@82: plot.setRenderer(renderer); tim@82: plot.setDataset(lSet); tim@82: } else { tim@82: // Individual settings for different parameters tim@82: for (int i = 0; i < lSet.getSeriesCount(); i++) { tim@82: tim@82: plot.setDataset(i, getDataset((XYSeriesCollection) lSet, i)); tim@82: Color mColor=color[i % color.length]; // zyklische Farbvergabe tim@82: mColor = color[0]; tim@82: // if ( pParameterId.length==1){ tim@82: tim@82: if(((String) lSet.getSeriesKey(i)).contains("richtung")){ tim@82: NumberAxis axis1 = new NumberAxis(((String) lSet.getSeriesKey(i)));//,new Range(0.0,360.0)); tim@82: axis1.setTickUnit(new NumberTickUnit(30.0)); tim@82: //axis1.setDisplayRange(0.0,360.0); tim@82: axis1.setLabelPaint(mColor); tim@82: axis1.setTickLabelPaint(mColor); tim@82: axis1.setUpperBound(360.0); tim@82: axis1.setLowerBound(0.0); tim@82: plot.setRangeAxis(i, axis1); tim@82: tim@82: tim@82: } tim@82: else { tim@82: axis = new NumberAxis((String) lSet.getSeriesKey(i)); tim@82: axis.setFixedDimension(10.0); tim@82: axis.setAutoRangeIncludesZero(false); tim@82: axis.setLabelPaint(mColor); tim@82: axis.setTickLabelPaint(mColor); tim@82: plot.setRangeAxis(i, axis); tim@82: axis.configure(); tim@82: } tim@82: if (i % 2 != 0) tim@82: plot.setRangeAxisLocation(i, AxisLocation.BOTTOM_OR_RIGHT); tim@82: else tim@82: plot.setRangeAxisLocation(i, AxisLocation.BOTTOM_OR_LEFT); tim@82: plot.mapDatasetToRangeAxis(i, i); tim@82: // } tim@82: renderer = new StandardXYItemRenderer(); tim@82: renderer.setSeriesPaint(i, mColor); tim@82: // renderer.setSeriesStroke(i,stroke[j]); tim@82: plot.setRenderer(i, renderer); tim@82: } tim@82: } tim@82: JFreeChart chart = new JFreeChart( tim@82: pLabels.getTitle(), tim@82: new Font ("SansSerif",Font.BOLD,24), tim@82: plot,true); tim@82: tim@82: tim@82: setStyle(chart, pStyle); tim@82: configureRenderingOptions(chart); tim@82: if (sDebug) tim@82: sLogger.debug(" vor encodeChart()"); tim@82: tim@82: encodeChart(chart, pStyle, outputStream); tim@82: } tim@82: private static XYDataset getDataset(XYSeriesCollection T, int pIndex){ //throws TechnicalChartException{ tim@82: //if (T.getSeriesCount() < pIndex) throw TechnicalChartException(); tim@82: XYSeriesCollection XYSC = new XYSeriesCollection(); tim@82: XYSC.addSeries(T.getSeries(pIndex)); tim@82: return (XYDataset) XYSC; tim@82: } tim@82: tim@82: private void configureRenderingOptions(JFreeChart pJfreechart) { tim@82: org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = ((XYPlot) pJfreechart tim@82: .getPlot()).getRenderer(); tim@82: if (xyitemrenderer instanceof XYLineAndShapeRenderer) { tim@82: XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyitemrenderer; tim@82: xylineandshaperenderer.setBaseShapesVisible(true); tim@82: xylineandshaperenderer.setBaseShapesFilled(true); tim@82: } tim@82: } tim@82: tim@82: private void setStyle(JFreeChart pJfreechart, ChartStyle pStyle) { tim@82: if (sDebug) tim@82: sLogger.debug("setStyle()"); tim@82: pJfreechart.setBackgroundPaint(pStyle.getCanvasColor()); tim@82: XYPlot xyplot = (XYPlot) pJfreechart.getPlot(); tim@82: xyplot.setBackgroundPaint(pStyle.getPlotBackgroundColor()); tim@82: xyplot.setDomainGridlinePaint(pStyle.getDomainGridlineColor()); tim@82: xyplot.setRangeGridlinePaint(pStyle.getRangeGridlineColor()); tim@82: tim@82: Insets lOffsets = pStyle.getAxisOffset(); tim@82: RectangleInsets lRectangleInsets = new RectangleInsets(lOffsets.mUpper, tim@82: lOffsets.mLeft, lOffsets.mLower, lOffsets.mRight); tim@82: xyplot.setAxisOffset(lRectangleInsets); tim@82: xyplot.setDomainCrosshairVisible(pStyle.isDomainCrosshairVisible()); tim@82: xyplot.setRangeCrosshairVisible(pStyle.isRangeCrosshairVisible()); tim@82: tim@82: } tim@82: tim@82: tim@82: protected XYSeries createXYSeries(String seriesName, Collection resultSet, tim@82: int lUpperCut, int lLowerCut,int pStart,int pEnd) throws TechnicalChartException{ tim@82: if (sDebug) tim@82: sLogger.debug("createXYSeries()"); tim@82: XYSeries series = new XYSeries(seriesName); tim@82: try { tim@82: double xValue=0; tim@82: double yValue=0; tim@82: int i = 0; tim@82: Iterator resultIterator = resultSet.iterator(); tim@82: while (resultIterator.hasNext()){ tim@82: Result lRow = resultIterator.next(); tim@82: if (i >= pStart && i <= pEnd ){ tim@82: xValue = lRow.getDouble("XORDINATE"); tim@82: yValue = lRow.getDouble("YORDINATE"); tim@82: series.add(xValue, yValue); tim@82: sLogger.debug(seriesName+" Added Value "+xValue+" / "+yValue); tim@82: tim@82: }else if (i > pEnd){ tim@82: return series; tim@82: } tim@82: i++; tim@82: } tim@82: } catch (OutOfMemoryError e) { tim@82: sLogger.error(e.getMessage(), e); tim@82: return series; tim@82: tim@82: } catch (Exception e) { //TechnicalChartException tim@82: sLogger.error(e.getMessage(), e); tim@82: } tim@82: finally { tim@82: } tim@82: tim@82: return series; tim@82: } tim@82: tim@86: private XYDataset createDataset(Collection resultSet, tim@82: int lUpperCut, int lLowerCut,Collection parameters, Collection measurements) throws TechnicalChartException { tim@82: tim@82: XYSeriesCollection xyDataset = new XYSeriesCollection(); tim@82: tim@82: try{ tim@82: String break1, break2, break3; tim@82: int mStart = 0; tim@82: int mEnd = 0; tim@82: tim@82: tim@82: Iterator resultIterator = resultSet.iterator(); tim@82: if (resultIterator.hasNext()){ tim@82: // Row row = new Row(sArrayStrLine); tim@82: Result row = resultIterator.next(); tim@82: tim@82: break1 = row.getString("GROUP1"); // 2 tim@82: break2 = row.getString("GROUP2"); //3 tim@82: break3 = row.getString("GROUP3"); // 4 tim@83: int i = 1; tim@82: while (resultIterator.hasNext()) { tim@82: row = resultIterator.next(); tim@82: if (!break1.equals(row.getString("GROUP1")) tim@82: || !break2 .equals(row.getString("GROUP2")) tim@82: || !break3.equals(row.getString("GROUP3"))){ tim@82: String seriesName = findValueTitle(parameters,break1)+" "+ tim@82: findValueTitle(measurements,break2); tim@82: sLogger.debug("Neuer Datensatz "+seriesName+ "von Datensätzen "+mStart+" / "+mEnd); tim@82: xyDataset.addSeries(createXYSeries(seriesName, tim@82: resultSet, lUpperCut, lLowerCut, mStart, mEnd)); tim@82: mStart = i; tim@82: tim@82: break1 = row.getString("GROUP1"); tim@82: break2 = row.getString("GROUP2"); //3 tim@82: break3 = row.getString("GROUP3"); // 4 tim@82: tim@82: } tim@82: mEnd = i; tim@82: //mEnd ++; tim@82: i = i + 1; tim@82: } tim@82: tim@82: String seriesName = findValueTitle(parameters,break1)+" "+ tim@82: findValueTitle(measurements,break2); tim@82: sLogger.debug("Neuer Datensatz "+seriesName+ "von Datensätzen "+mStart+" / "+mEnd); tim@82: xyDataset.addSeries(createXYSeries(seriesName, tim@82: resultSet, lUpperCut, lLowerCut, mStart, mEnd)); tim@82: } tim@82: } tim@82: catch (Exception e){ tim@82: sLogger.error(e.getMessage(), e); tim@82: } tim@82: finally{ tim@82: } tim@82: return xyDataset; tim@82: } tim@82: tim@82: tim@82: tim@82: private void encodeChart(JFreeChart pChart, ChartStyle pStyle, OutputStream outputStream) tim@82: throws IOException { tim@82: if (sDebug) tim@82: sLogger.debug("encodeChart()"); tim@82: KeypointPNGEncoderAdapter lEncoder = new KeypointPNGEncoderAdapter(); tim@82: lEncoder.setEncodingAlpha(true); tim@82: tim@82: int lWidth = (int) pStyle.getChartSize().getWidth(); tim@82: int lHeight = (int) pStyle.getChartSize().getHeight(); tim@82: tim@82: BufferedImage lImage = pChart.createBufferedImage(lWidth, lHeight, tim@82: BufferedImage.BITMASK, null); tim@82: tim@82: lEncoder.encode(lImage, outputStream); tim@82: tim@82: } tim@82: tim@110: private String findValueTitle(Collection values, String id){ tim@110: if (values != null){ tim@110: Iterator it = values.iterator(); tim@110: while(it.hasNext()){ tim@110: KeyValueDescibeData data = it.next(); tim@110: if (id.equals(data.getKey())){ tim@110: return data.getValue(); tim@110: } tim@110: } tim@82: } tim@110: return id; tim@82: } tim@82: }