Mercurial > dive4elements > river
diff artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixDeltaWtGenerator.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixDeltaWtGenerator.java@bd047b71ab37 |
children | 4897a58c8746 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixDeltaWtGenerator.java Thu Apr 25 15:23:37 2013 +0200 @@ -0,0 +1,460 @@ +package org.dive4elements.river.exports.fixings; + +import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; +import org.dive4elements.artifacts.CallContext; +import org.dive4elements.river.artifacts.FLYSArtifact; +import org.dive4elements.river.artifacts.access.FixAnalysisAccess; +import org.dive4elements.river.artifacts.model.DateRange; +import org.dive4elements.river.artifacts.model.FacetTypes; +import org.dive4elements.river.artifacts.model.QWDDateRange; +import org.dive4elements.river.artifacts.model.fixings.QWD; +import org.dive4elements.river.artifacts.resources.Resources; +import org.dive4elements.river.exports.TimeseriesChartGenerator; +import org.dive4elements.river.jfree.CollisionFreeXYTextAnnotation; +import org.dive4elements.river.jfree.FLYSAnnotation; +import org.dive4elements.river.jfree.StyledDomainMarker; +import org.dive4elements.river.jfree.StyledTimeSeries; +import org.dive4elements.river.jfree.StyledValueMarker; +import org.dive4elements.river.utils.ThemeUtil; + +import java.io.OutputStream; +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import org.apache.log4j.Logger; +import org.jfree.chart.annotations.XYTextAnnotation; +import org.jfree.data.time.Day; +import org.jfree.data.time.RegularTimePeriod; +import org.jfree.data.time.TimeSeries; +import org.jfree.data.time.TimeSeriesCollection; +import org.w3c.dom.Document; + + +/** + * Generator for Delta W(t) charts. + * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> + */ +public class FixDeltaWtGenerator +extends TimeseriesChartGenerator +implements FacetTypes +{ + /** Private logger. */ + private static Logger logger = + Logger.getLogger(FixDeltaWtGenerator.class); + + public static final String I18N_CHART_TITLE = + "chart.fix.deltawt.title"; + + public static final String I18N_CHART_SUBTITLE = + "chart.fix.deltawt.subtitle"; + + public static final String I18N_XAXIS_LABEL = + "chart.fix.deltawt.xaxis.label"; + + public static final String I18N_YAXIS_LABEL = + "chart.fix.deltawt.yaxis.label"; + + public static final String I18N_YAXIS_SECOND_LABEL = + "chart.fix.deltawt.yaxis.second.label"; + + + public static enum YAXIS { + dW(0); + protected int idx; + private YAXIS(int c) { + idx = c; + } + } + + + private FLYSArtifact artifact = null; + + + @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(); + } + }; + } + + + @Override + protected String getDefaultChartTitle() { + return msg(I18N_CHART_TITLE, I18N_CHART_TITLE); + } + + + @Override + protected String getChartTitle() { + return Resources.format( + context.getMeta(), + I18N_CHART_TITLE, + "", + FixChartGenerator + .getCurrentKmFromRequest(request).doubleValue()); + } + + + @Override + protected String getDefaultChartSubtitle() { + FixAnalysisAccess access = new FixAnalysisAccess(artifact, context); + DateRange dateRange = access.getDateRange(); + DateRange refRange = access.getReferencePeriod(); + return Resources.format( + context.getMeta(), + I18N_CHART_SUBTITLE, + "", + access.getRiver(), + dateRange.getFrom(), + dateRange.getTo(), + refRange.getFrom(), + refRange.getTo()); + } + + + @Override + protected String getDefaultXAxisLabel() { + return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL); + } + + @Override + protected String getDefaultYAxisLabel(int pos) { + if (pos == 0) { + return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL); + } + else if (pos == 1) { + return msg(I18N_YAXIS_SECOND_LABEL, I18N_YAXIS_SECOND_LABEL); + } + else { + return "NO TITLE FOR Y AXIS: " + pos; + } + } + + + @Override + public void doOut( + ArtifactAndFacet artifactFacet, + Document theme, + boolean visible + ) { + String name = artifactFacet.getFacetName(); + logger.debug("FixDeltaWtGenerator.doOut: " + name); + logger.debug("Theme description is: " + artifactFacet.getFacetDescription()); + + this.artifact = (FLYSArtifact)artifactFacet.getArtifact(); + + if (name.contains(FIX_SECTOR_AVERAGE_DWT)) { + doSectorAverageOut( + (FLYSArtifact) artifactFacet.getArtifact(), + artifactFacet.getData(context), + artifactFacet.getFacetDescription(), + theme, + visible); + } + else if (name.equals(FIX_REFERENCE_EVENTS_DWT)) { + doReferenceEventsOut( + (FLYSArtifact) artifactFacet.getArtifact(), + artifactFacet.getData(context), + artifactFacet.getFacetDescription(), + theme, + visible); + } + else if (name.equals(FIX_ANALYSIS_EVENTS_DWT)) { + doAnalysisEventsOut( + (FLYSArtifact) artifactFacet.getArtifact(), + artifactFacet.getData(context), + artifactFacet.getFacetDescription(), + theme, + visible); + } + else if (name.equals(FIX_DEVIATION_DWT)) { + doDeviationOut( + (FLYSArtifact) artifactFacet.getArtifact(), + artifactFacet.getData(context), + artifactFacet.getFacetDescription(), + theme, + visible); + } + else if (name.equals(FIX_ANALYSIS_PERIODS_DWT)) { + doAnalysisPeriodsOut( + (FLYSArtifact) artifactFacet.getArtifact(), + artifactFacet.getData(context), + artifactFacet.getFacetDescription(), + theme, + visible); + } + else if (name.equals(FIX_REFERENCE_PERIOD_DWT)) { + doReferencePeriodsOut( + (FLYSArtifact) artifactFacet.getArtifact(), + artifactFacet.getData(context), + artifactFacet.getFacetDescription(), + theme, + visible); + } + else if (FacetTypes.IS.MANUALPOINTS(name)) { + doPoints (artifactFacet.getData(context), + artifactFacet, + theme, visible, YAXIS.dW.idx); + } + else { + logger.warn("doOut(): unknown facet name: " + name); + return; + } + } + + + protected void doReferencePeriodsOut( + FLYSArtifact artifact, + Object data, + String desc, + Document theme, + boolean visible) + { + logger.debug("doReferencePeriodsOut()"); + + if (visible) { + FixAnalysisAccess access = new FixAnalysisAccess(artifact, context); + DateRange refRange = access.getReferencePeriod(); + + RegularTimePeriod start = new Day(refRange.getFrom()); + RegularTimePeriod end = new Day(refRange.getTo()); + StyledDomainMarker marker = new StyledDomainMarker( + start.getMiddleMillisecond(), + end.getMiddleMillisecond(), + theme); + domainMarker.add(marker); + } + } + + + protected void doSectorAverageOut( + FLYSArtifact artifact, + Object data, + String desc, + Document theme, + boolean visible) + { + logger.debug("doSectorAverageOut(): description = " + desc); + + QWDDateRange qwd = (QWDDateRange) data; + TimeSeriesCollection tsc = new TimeSeriesCollection(); + TimeSeries series = new StyledTimeSeries(desc, theme); + + if (qwd == null || qwd.qwd == null || qwd.dateRange == null) { + return; + } + RegularTimePeriod rtp = new Day(qwd.qwd.getDate()); + double value = qwd.qwd.getDeltaW(); + + // Draw a line spanning the analysis time. + series.add(rtp, value); + rtp = new Day(qwd.dateRange.getFrom()); + series.addOrUpdate(rtp, value); + rtp = new Day(qwd.dateRange.getTo()); + series.addOrUpdate(rtp, value); + + tsc.addSeries(series); + + addAxisDataset(tsc, 0, visible); + + if (visible && ThemeUtil.parseShowLineLabel(theme)) { + List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>(); + XYTextAnnotation anno = new CollisionFreeXYTextAnnotation( + "\u0394 W(t) [cm] " + (float)Math.round(qwd.qwd.getDeltaW() * 10000) / 10000, + tsc.getXValue(0, 0), + qwd.qwd.getDeltaW()); + textAnnos.add(anno); + + FLYSAnnotation flysAnno = new FLYSAnnotation(null, null, null, theme); + flysAnno.setTextAnnotations(textAnnos); + addAnnotations(flysAnno); + } + } + + + protected void doAnalysisEventsOut( + FLYSArtifact artifact, + Object data, + String desc, + Document theme, + boolean visible + ) { + logger.debug("doAnalysisEventsOut: desc = " + desc); + + QWD qwd = (QWD) data; + doQWDEventsOut(qwd, desc, theme, visible); + } + + + protected void doQWDEventsOut(QWD qwd, String desc, Document theme, boolean visible) + { + TimeSeriesCollection tsc = new TimeSeriesCollection(); + + TimeSeries series = new StyledTimeSeries(desc, theme); + TimeSeries interpol = new StyledTimeSeries(desc + "interpol", theme); + + if (qwd == null) { + logger.debug("doQWDEventsOut: qwd == null"); + return; + } + + Map<Integer, int[]> annoIdxMap = new HashMap<Integer, int[]>(); + + int idxInterpol = 0; + int idxRegular = 0; + RegularTimePeriod rtp = new Day(qwd.getDate()); + double value = qwd.getDeltaW(); + boolean interpolate = qwd.getInterpolated(); + if (interpolate) { + if(interpol.addOrUpdate(rtp, value) == null) { + annoIdxMap.put( + 0, + new int[]{1, idxInterpol}); + idxInterpol++; + } + } + else { + if(series.addOrUpdate(rtp, value) == null) { + annoIdxMap.put( + 0, + new int[]{0, idxRegular}); + idxRegular++; + } + } + + tsc.addSeries(series); + tsc.addSeries(interpol); + addAxisDataset(tsc, 0, visible); + addAttribute(desc + "interpol", "interpolate"); + addAttribute(desc, "outline"); + + doQWDTextAnnotations(annoIdxMap, tsc, qwd, theme, visible); + } + + + /** + * @param annoIdxMap map of index in qwds to series/data item indices in tsc. + */ + protected void doQWDTextAnnotations(Map<Integer, int[]> annoIdxMap, + TimeSeriesCollection tsc, QWD qwd, Document theme, + boolean visible) { + logger.debug("doQWDTextAnnotation()"); + + if (!visible || !ThemeUtil.parseShowPointLabel(theme)) { + logger.debug("doQWDTextAnnotation: annotation not visible"); + return; + } + + Locale locale = Resources.getLocale(context.getMeta()); + NumberFormat nf = NumberFormat.getInstance(locale); + + List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>(); + + for (int[] idxs: annoIdxMap.values()) { + + double x = tsc.getXValue(idxs[0], idxs[1]); + + XYTextAnnotation anno = new CollisionFreeXYTextAnnotation( + nf.format(qwd.getQ()) + " m\u00B3/s", + x, + qwd.getDeltaW()); + textAnnos.add(anno); + } + + FLYSAnnotation flysAnno = new FLYSAnnotation(null, null, null, theme); + flysAnno.setTextAnnotations(textAnnos); + addAnnotations(flysAnno); + } + + + protected void doReferenceEventsOut( + FLYSArtifact artifact, + Object data, + String desc, + Document theme, + boolean visible + ) { + logger.debug("doReferenceEventsOut: desc = " + desc); + + QWD qwd = (QWD) data; + doQWDEventsOut(qwd, desc, theme, visible); + } + + + protected void doDeviationOut( + FLYSArtifact artifact, + Object data, + String desc, + Document theme, + boolean visible + ) { + logger.debug("doDeviationOut: desc = " + desc); + + if (data == null || !visible) { + logger.debug("no standard deviation"); + return; + } + double[] value = (double[]) data; + StyledDomainMarker lower = new StyledDomainMarker((value[0] * -1), 0, theme); + StyledDomainMarker upper = new StyledDomainMarker(0, value[0], theme); + + valueMarker.add(lower); + valueMarker.add(upper); + } + + + protected void doAnalysisPeriodsOut( + FLYSArtifact artifact, + Object data, + String desc, + Document theme, + boolean visible) + { + DateRange[] ranges = (DateRange[]) data; + if (ranges == null || !visible) { + return; + } + for (int i = 0; i < ranges.length; i++) { + logger.debug("creating domain marker"); + RegularTimePeriod start = new Day(ranges[i].getFrom()); + RegularTimePeriod end = new Day(ranges[i].getTo()); + StyledDomainMarker marker = + new StyledDomainMarker(start.getMiddleMillisecond(), + end.getMiddleMillisecond(), theme); + marker.useSecondColor(i % 2 == 0); + domainMarker.add(marker); + } + logger.debug("domainmarkers: " + domainMarker.size()); + } + + + @Override + public void init(Document request, OutputStream out, CallContext context) { + super.init(request, out, context); + + Double currentKm = FixChartGenerator.getCurrentKmFromRequest(request); + + if (logger.isDebugEnabled()) { + logger.debug("currentKm = " + currentKm); + } + + context.putContextValue("currentKm", currentKm); + + StyledValueMarker marker = new StyledValueMarker(0, request); + valueMarker.add(marker); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :