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; rrenkert@8353: import org.dive4elements.river.artifacts.resources.Resources; 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; teichmann@8320: import org.jfree.chart.axis.NumberAxis; 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 teichmann@8320: public void addDatasets(XYPlot plot) { teichmann@8320: super.addDatasets(plot); teichmann@8320: teichmann@8320: Object pnp = context.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: teichmann@8320: AxisDataset data = datasets.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"; rrenkert@8353: String axisLabel = Resources.getMsg(context.getMeta(), rrenkert@8353: 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); andre@8571: IdentifiableNumberAxis idA = (IdentifiableNumberAxis) plot.getRangeAxis(wAxisIndex); andre@8571: Range fixedRange = getRangeForAxisFromSettings(idA.getId()); andre@8571: if (fixedRange == null) { andre@8571: return; andre@8571: } andre@8571: log.debug("Adjusting helper centimeter axis to fixed range."); andre@8571: Range adjustedRange = inCm( andre@8571: fixedRange, andre@8571: pnpValue andre@8571: ); andre@8571: IdentifiableNumberAxis wInCmAxis= andre@8571: (IdentifiableNumberAxis) plot.getRangeAxis(wInCmAxisIndex); andre@8571: wInCmAxis.setRange(adjustedRange); andre@8571: } teichmann@8165: } teichmann@8165: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :