Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java @ 2031:8e7e56db96a5
Make ('static') WKms data available for area calculations.
flys-artifacts/trunk@3494 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Tue, 20 Dec 2011 12:06:35 +0000 |
parents | 131842fc6581 |
children | 0318fa6f0844 |
line wrap: on
line source
package de.intevation.flys.exports; import java.awt.Font; import org.apache.log4j.Logger; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.title.TextTitle; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.data.xy.XYSeries; import org.w3c.dom.Document; import de.intevation.artifactdatabase.state.ArtifactAndFacet; import de.intevation.artifactdatabase.state.Facet; import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.model.FacetTypes; import de.intevation.flys.artifacts.model.WKms; import de.intevation.flys.artifacts.model.WQKms; import de.intevation.flys.jfree.FLYSAnnotation; import de.intevation.flys.utils.FLYSUtils; import de.intevation.flys.utils.DataUtil; /** * An OutGenerator that generates discharge curves. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class LongitudinalSectionGenerator extends XYChartGenerator implements FacetTypes { public enum YAXIS { W(0), Q(1), D(2); protected int idx; private YAXIS(int c) { idx = c; } } /** The logger that is used in this generator. */ private static Logger logger = Logger.getLogger(LongitudinalSectionGenerator.class); /** Key to look up internationalized String for annotations label. */ public static final String I18N_ANNOTATIONS_LABEL = "chart.longitudinal.annotations.label"; /** * Key to look up internationalized String for LongitudinalSection diagrams * titles. */ public static final String I18N_CHART_TITLE = "chart.longitudinal.section.title"; /** * Key to look up internationalized String for LongitudinalSection diagrams * subtitles. */ public static final String I18N_CHART_SUBTITLE = "chart.longitudinal.section.subtitle"; public static final String I18N_XAXIS_LABEL = "chart.longitudinal.section.xaxis.label"; public static final String I18N_YAXIS_LABEL = "chart.longitudinal.section.yaxis.label"; public static final String I18N_2YAXIS_LABEL = "chart.longitudinal.section.yaxis.second.label"; public static final String I18N_CHART_TITLE_DEFAULT = "W-L\u00e4ngsschnitt"; public static final String I18N_XAXIS_LABEL_DEFAULT = "km"; public static final String I18N_YAXIS_LABEL_DEFAULT = "W [NN + m]"; public static final String I18N_2YAXIS_LABEL_DEFAULT = "Q [m\u00b3/s]"; public final static String I18N_WDIFF_YAXIS_LABEL = "chart.w_differences.yaxis.label"; public final static String I18N_WDIFF_YAXIS_LABEL_DEFAULT = "m"; /** Whether or not the plot is inverted (left-right). */ protected boolean inverted; public LongitudinalSectionGenerator() { super(); } @Override protected YAxisWalker getYAxisWalker() { return new YAxisWalker() { @Override public int length() { return YAXIS.values().length; } @Override public String getId(int idx) { YAXIS[] yaxes = YAXIS.values(); return yaxes[idx].toString(); } }; } public boolean isInverted() { return inverted; } public void setInverted(boolean inverted) { this.inverted = inverted; } /** * Get internationalized title for chart. */ public String getChartTitle() { return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT); } @Override protected String getChartSubtitle() { double[] dist = getRange(); Object[] args = new Object[] { getRiverName(), dist[0], dist[1] }; return msg(getChartSubtitleKey(), "", args); } /** * Gets key to look up internationalized String for the charts subtitle. * @return key to look up translated subtitle. */ protected String getChartSubtitleKey() { return I18N_CHART_SUBTITLE; } /** * Add (internationalized) subtitle to chart. * @see getChartSubtitleKey */ @Override protected void addSubtitles(JFreeChart chart) { String subtitle = getChartSubtitle(); chart.addSubtitle(new TextTitle(subtitle)); } /** * Get internationalized label for the x axis. */ protected String getXAxisLabel() { FLYSArtifact flys = (FLYSArtifact) master; return msg( I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT, new Object[] { FLYSUtils.getRiver(flys).getName() }); } @Override protected String getYAxisLabel() { return getWAxisLabel(); } /** * Get internationalized label for the y axis. */ protected String getWAxisLabel() { FLYSArtifact flys = (FLYSArtifact) master; String unit = FLYSUtils.getRiver(flys).getWstUnit().getName(); return msg( I18N_YAXIS_LABEL, I18N_YAXIS_LABEL_DEFAULT, new Object[] { unit }); } /** * Create Axis for given index. * @return axis with according internationalized label. */ @Override protected NumberAxis createYAxis(int index) { Font labelFont = new Font("Tahoma", Font.BOLD, 14); String label = getYAxisLabel(index); NumberAxis axis = new NumberAxis(label); // "Q" Axis shall include 0. if (index == YAXIS.Q.idx) { axis.setAutoRangeIncludesZero(true); } else { axis.setAutoRangeIncludesZero(false); } axis.setLabelFont(labelFont); return axis; } protected String getYAxisLabel(int index) { String label = "default"; if (index == YAXIS.W.idx) { label = getWAxisLabel(); } else if (index == YAXIS.Q.idx) { label = msg(getQAxisLabelKey(), getQAxisDefaultLabel()); } else if (index == YAXIS.D.idx) { label = msg(I18N_WDIFF_YAXIS_LABEL, I18N_WDIFF_YAXIS_LABEL_DEFAULT); } return label; } /** * Get default value for the second Y-Axis' label (if no translation was * found). */ protected String getQAxisDefaultLabel() { return I18N_2YAXIS_LABEL_DEFAULT; } /** * Get key for internationalization of the second Y-Axis' label. */ protected String getQAxisLabelKey() { return I18N_2YAXIS_LABEL; } /** * Trigger inversion. */ @Override protected void adjustAxes(XYPlot plot) { super.adjustAxes(plot); invertXAxis(plot.getDomainAxis()); } /** * This method inverts the x-axis based on the kilometer information of the * selected river. If the head of the river is at kilometer 0, the axis is * not inverted, otherwise it is. * * @param xaxis The domain axis. */ protected void invertXAxis(ValueAxis xaxis) { if (inverted) { logger.debug("X-Axis.setInverted(true)"); xaxis.setInverted(true); } } /** * Produce output. * @param facet current facet. * @param attr theme for facet */ public void doOut( ArtifactAndFacet artifactAndFacet, Document attr, boolean visible ) { String name = artifactAndFacet.getFacetName(); logger.debug("LongitudinalSectionGenerator.doOut: " + name); if (name == null) { logger.error("No facet name for doOut(). No output generated!"); return; } Facet facet = artifactAndFacet.getFacet(); if (facet == null) { return; } if (name.equals(LONGITUDINAL_W)) { doWOut((WQKms) artifactAndFacet.getData(context), facet, attr, visible); } else if (name.equals(LONGITUDINAL_Q)) { doQOut((WQKms) artifactAndFacet.getData(context), facet, attr, visible); } else if (name.equals(LONGITUDINAL_ANNOTATION)) { doAnnotations((FLYSAnnotation) artifactAndFacet.getData(context), facet, attr, visible); } else if (name.equals(STATIC_WKMS) || name.equals(HEIGHTMARKS_POINTS) || name.equals(STATIC_WQKMS)) { doWOut((WKms) artifactAndFacet.getData(context), facet, attr, visible); } else if (name.equals(W_DIFFERENCES)) { doWDifferencesOut( (WKms) artifactAndFacet.getData(context), facet, attr, visible); } else if (FacetTypes.IS.AREA(name)) { doArea(artifactAndFacet.getData(context), artifactAndFacet.getFacetDescription(), attr, visible); } else { logger.warn("Unknown facet name: " + name); return; } } /** * Process the output for W facets in a longitudinal section curve. * * @param wqkms An array of WQKms values. * @param facet The facet. This facet does NOT support any data objects. Use * FLYSArtifact.getNativeFacet() instead to retrieve a Facet which supports * data. * @param theme The theme that contains styling information. * @param visible The visibility of the curve. */ protected void doWOut( WKms wkms, Facet facet, Document theme, boolean visible ) { logger.debug("LongitudinalSectionGenerator.doWOut"); XYSeries series = new StyledXYSeries(facet.getDescription(), theme); StyledSeriesBuilder.addPoints(series, wkms); addAxisSeries(series, YAXIS.W.idx, visible); if (wkms instanceof WQKms) { if (needInvertAxis((WQKms) wkms)) { setInverted(true); } } } /** * Add items to dataseries which describes the differences. */ protected void doWDifferencesOut( WKms wkms, Facet facet, Document theme, boolean visible ) { logger.debug("WDifferencesCurveGenerator.doWDifferencesOut"); if (wkms == null) { logger.warn("No data to add to WDifferencesChart."); return; } XYSeries series = new StyledXYSeries(facet.getDescription(), theme); if (logger.isDebugEnabled()) { if (wkms.size() > 0) { logger.debug("Generate series: " + series.getKey()); logger.debug("Start km: " + wkms.getKm(0)); logger.debug("End km: " + wkms.getKm(wkms.size() - 1)); logger.debug("Values : " + wkms.size()); } } StyledSeriesBuilder.addPoints(series, wkms); addAxisSeries(series, YAXIS.D.idx, visible); if (DataUtil.guessWaterIncreasing(wkms.allWs())) { setInverted(true); } } /** * Process the output for Q facets in a longitudinal section curve. * * @param wqkms An array of WQKms values. * @param facet The facet. This facet does NOT support any data objects. Use * FLYSArtifact.getNativeFacet() instead to retrieve a Facet which supports * data. * @param theme The theme that contains styling information. * @param visible The visibility of the curve. */ protected void doQOut( WQKms wqkms, Facet facet, Document theme, boolean visible ) { logger.debug("LongitudinalSectionGenerator.doQOut"); XYSeries series = new StyledXYSeries(facet.getDescription(), theme); StyledSeriesBuilder.addPointsKmQ(series, wqkms); addAxisSeries(series, YAXIS.Q.idx, visible); if (needInvertAxis(wqkms)) { setInverted(true); } } /** * This method determines - taking JFreeCharts auto x value ordering into * account - if the x axis need to be inverted. Waterlines in these charts * should decrease. * * @param wqkms The data object that stores the x and y values used for this * chart. */ public boolean needInvertAxis(WQKms wqkms) { boolean wsUp = wqkms.guessWaterIncreasing(); boolean kmUp = DataUtil.guessWaterIncreasing(wqkms.allKms()); boolean inv = (wsUp && kmUp) || (!wsUp && !kmUp); int size = wqkms.size(); if (logger.isDebugEnabled()) { logger.debug("Values : " + size); if (size > 0) { logger.debug("Start km: " + wqkms.getKm(0)); logger.debug("End km: " + wqkms.getKm(size-1)); } logger.debug("wsUp: " + wsUp); logger.debug("kmUp: " + kmUp); logger.debug("inv: " + inv); } return inv; } /** * Get name of series (displayed in legend). * @return name of the series. */ protected String getSeriesName(WQKms wqkms, String mode) { String name = wqkms.getName(); String prefix = name != null && name.indexOf(mode) >= 0 ? null : mode; return prefix != null && prefix.length() > 0 ? prefix + "(" + name +")" : name; } /** * Do Area out. */ protected void doArea( Object o, String seriesName, Document theme, boolean visible ) { logger.debug("LongitudinalSectionGenerator.doArea"); StyledAreaSeriesCollection area = new StyledAreaSeriesCollection(theme); // TODO make this more stable. Object[] doubles = (Object[]) o; XYSeries up = null; XYSeries down = null; if (doubles[1] != null) { up = new StyledXYSeries(seriesName, false, theme); if (doubles[1] instanceof WKms) { StyledSeriesBuilder.addPoints(up, (WKms) doubles[1]); } else if (doubles[1] instanceof double[][]) { StyledSeriesBuilder.addPoints(up, (double [][]) doubles[1]); } else { logger.error("Do not know how to deal with (up) area info from: " + doubles[1]); } } if (doubles[0] != null) { // TODO: Sort this out: when the two series have the same name, // the renderer (or anything in between) will not work correctly. down = new StyledXYSeries(seriesName + " ", false, theme); if (doubles[0] instanceof WQKms) { StyledSeriesBuilder.addPoints(down, (WKms) doubles[0]); } else if (doubles[0] instanceof double[][]) { StyledSeriesBuilder.addPoints(down, (double[][]) doubles[0]); } else { logger.error("Do not know how to deal with (down) area info from: " + doubles[0]); } } if (up == null && down != null) { area.setMode(StyledAreaSeriesCollection.FILL_MODE.ABOVE); down.setKey(seriesName); area.addSeries(down); } else if (up != null && down == null) { area.setMode(StyledAreaSeriesCollection.FILL_MODE.UNDER); area.addSeries(up); } else if (up != null && down != null) { area.setMode(StyledAreaSeriesCollection.FILL_MODE.BETWEEN); area.addSeries(up); area.addSeries(down); } addAreaSeries(area, 0, visible); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :