teichmann@8165: /* Copyright (C) 2014 by Bundesanstalt für Gewässerkunde teichmann@8165: * Software engineering by Intevation GmbH teichmann@8165: * teichmann@8165: * This file is Free Software under the GNU AGPL (>=v3) teichmann@8165: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@8165: * documentation coming with Dive4Elements River for details. teichmann@8165: */ teichmann@8165: package org.dive4elements.river.exports; teichmann@8165: teichmann@8320: import java.awt.Font; teichmann@8320: teichmann@8320: import org.apache.log4j.Logger; teichmann@8320: import org.dive4elements.river.exports.injector.InjectorConstants; teichmann@8320: import org.dive4elements.river.jfree.AxisDataset; tom@8338: import org.dive4elements.river.jfree.DoubleBounds; tom@8738: teichmann@8320: import org.jfree.chart.axis.NumberAxis; tom@8738: import org.jfree.chart.axis.ValueAxis; teichmann@8320: import org.jfree.chart.plot.XYPlot; teichmann@8320: import org.jfree.data.Range; teichmann@8320: teichmann@8320: public class DischargeGenerator teichmann@8320: extends DiagramGenerator teichmann@8320: implements InjectorConstants teichmann@8165: { teichmann@8323: private static Logger log = Logger.getLogger(DischargeGenerator.class); teichmann@8320: rrenkert@8353: private String I18N_AXIS_LABEL = "chart.discharge.curve.yaxis.cm.label"; rrenkert@8353: andre@8571: private int wAxisIndex; andre@8571: private int wInCmAxisIndex; andre@8571: double pnpValue; andre@8571: teichmann@8165: public DischargeGenerator() { teichmann@8165: } teichmann@8320: teichmann@8320: @Override gernotbelger@9555: public void addDatasets(XYPlot plot, final LegendAggregator legendBuilder) { gernotbelger@9555: super.addDatasets(plot, legendBuilder); teichmann@8320: gernotbelger@9123: Object pnp = getContext().getContextValue(PNP); teichmann@8320: if (!(pnp instanceof Number)) { teichmann@8320: return; teichmann@8320: } teichmann@8320: andre@8571: pnpValue = ((Number)pnp).doubleValue(); teichmann@8320: andre@8571: wAxisIndex = diagramAttributes.getAxisIndex("W"); teichmann@8320: if (wAxisIndex == -1) { teichmann@8320: log.warn("No W axis found."); teichmann@8320: return; teichmann@8320: } teichmann@8320: gernotbelger@9123: AxisDataset data = getDatasets().get(wAxisIndex); teichmann@8320: if (data == null) { teichmann@8320: // No W axis teichmann@8320: return; teichmann@8320: } teichmann@8320: tom@8330: if (data.getRange() == null) { tom@8330: // No active datasets tom@8330: return; tom@8330: } tom@8330: tom@8338: Range axisRange = inCm( tom@8338: plot.getRangeAxis(wAxisIndex).getRange(), tom@8338: pnpValue tom@8338: ); tom@8338: Range dataRange = inCm(data.getRange(), pnpValue); teichmann@8320: teichmann@8320: // Do we have an index for W in cm? teichmann@8320: NumberAxis wInCmAxis = createWinCMAxis(wAxisIndex); tom@8338: wInCmAxis.setRange(axisRange); teichmann@8320: andre@8571: wInCmAxisIndex = plot.getRangeAxisCount(); tom@8338: plot.setRangeAxis(wInCmAxisIndex, wInCmAxis); tom@8338: combineYBounds(new DoubleBounds(dataRange), wInCmAxisIndex); teichmann@8320: } teichmann@8320: teichmann@8320: private static Range inCm(Range r, double pnpValue) { teichmann@8320: double l = r.getLowerBound(); teichmann@8320: double u = r.getUpperBound(); teichmann@8320: l = (l - pnpValue)*100d; teichmann@8320: u = (u - pnpValue)*100d; teichmann@8320: return new Range(l, u); teichmann@8320: } teichmann@8320: teichmann@8320: private NumberAxis createWinCMAxis(int wAxisIndex) { teichmann@8320: teichmann@8320: Font labelFont = new Font( teichmann@8320: DEFAULT_FONT_NAME, teichmann@8320: Font.BOLD, teichmann@8320: getYAxisFontSize(wAxisIndex)); teichmann@8320: teichmann@8320: String axisName = "W.in.cm"; gernotbelger@9123: String axisLabel = msg( I18N_AXIS_LABEL, "W [cm]" ); teichmann@8320: teichmann@8320: IdentifiableNumberAxis axis = new IdentifiableNumberAxis( rrenkert@8353: axisName, axisLabel); teichmann@8320: teichmann@8320: axis.setAutoRangeIncludesZero(false); teichmann@8320: axis.setLabelFont(labelFont); teichmann@8320: axis.setTickLabelFont(labelFont); teichmann@8320: teichmann@8320: return axis; teichmann@8320: } andre@8571: andre@8571: /** We need to override this to keep both axis synced. */ andre@8571: @Override andre@8571: protected void autoZoom(XYPlot plot) { andre@8571: super.autoZoom(plot); tom@8738: tom@8738: ValueAxis wAxis = plot.getRangeAxis(wAxisIndex); tom@8738: if (wAxis instanceof IdentifiableNumberAxis) { tom@8738: IdentifiableNumberAxis idA = (IdentifiableNumberAxis)wAxis; tom@8738: Range fixedRange = getRangeForAxisFromSettings(idA.getId()); tom@8738: if (fixedRange == null) { tom@8738: return; tom@8738: } tom@8738: tom@8738: log.debug("Adjusting helper centimeter axis to fixed range."); tom@8738: Range adjustedRange = inCm( tom@8738: fixedRange, tom@8738: pnpValue tom@8738: ); tom@8738: IdentifiableNumberAxis wInCmAxis= tom@8738: (IdentifiableNumberAxis) plot.getRangeAxis(wInCmAxisIndex); tom@8738: wInCmAxis.setRange(adjustedRange); andre@8571: } andre@8571: } teichmann@8165: } teichmann@8165: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :