comparison flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java @ 3242:1dca41dba135

Move annotation code to base class ChartGenerator flys-artifacts/trunk@4874 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Christian Lins <christian.lins@intevation.de>
date Wed, 04 Jul 2012 22:28:44 +0000
parents b0eee4c1eaa0
children c27c4e06dd87
comparison
equal deleted inserted replaced
3241:da3e58694cae 3242:1dca41dba135
1 package de.intevation.flys.exports; 1 package de.intevation.flys.exports;
2 2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Paint;
6 import java.util.ArrayList;
7 import java.util.Date;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 import org.apache.log4j.Logger;
13 import org.jfree.chart.ChartFactory;
14 import org.jfree.chart.JFreeChart;
15 import org.jfree.chart.annotations.XYTextAnnotation;
16 import org.jfree.chart.axis.ValueAxis;
17 import org.jfree.chart.plot.Marker;
18 import org.jfree.chart.plot.XYPlot;
19 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
20 import org.jfree.data.Range;
21 import org.jfree.data.general.Series;
22 import org.jfree.data.time.Day;
23 import org.jfree.data.time.TimeSeries;
24 import org.jfree.data.time.TimeSeriesCollection;
25 import org.jfree.data.xy.XYDataset;
26 import org.jfree.ui.Layer;
27 import org.json.JSONArray;
28 import org.json.JSONException;
29 import org.w3c.dom.Document;
30
3 import de.intevation.artifactdatabase.state.ArtifactAndFacet; 31 import de.intevation.artifactdatabase.state.ArtifactAndFacet;
4 import de.intevation.artifactdatabase.state.Facet;
5
6 import de.intevation.flys.jfree.Bounds; 32 import de.intevation.flys.jfree.Bounds;
7 import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation; 33 import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation;
8 import de.intevation.flys.jfree.DoubleBounds; 34 import de.intevation.flys.jfree.DoubleBounds;
9 import de.intevation.flys.jfree.FLYSAnnotation; 35 import de.intevation.flys.jfree.FLYSAnnotation;
10 import de.intevation.flys.jfree.StyledTimeSeries; 36 import de.intevation.flys.jfree.StyledTimeSeries;
11 import de.intevation.flys.jfree.TimeBounds; 37 import de.intevation.flys.jfree.TimeBounds;
12
13 import de.intevation.flys.themes.ThemeAccess;
14
15 import de.intevation.flys.artifacts.model.HYKFactory;
16
17 import java.awt.BasicStroke;
18 import java.awt.Color;
19 import java.awt.Font;
20 import java.awt.Paint;
21 import java.awt.Stroke;
22
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.log4j.Logger;
30
31 import org.jfree.chart.ChartFactory;
32 import org.jfree.chart.JFreeChart;
33 import org.jfree.chart.LegendItemCollection;
34
35 import org.jfree.chart.annotations.XYBoxAnnotation;
36 import org.jfree.chart.annotations.XYTextAnnotation;
37
38 import org.jfree.chart.axis.ValueAxis;
39
40 import org.jfree.chart.plot.Marker;
41 import org.jfree.chart.plot.XYPlot;
42
43 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
44
45 import org.jfree.data.Range;
46
47 import org.jfree.data.general.Series;
48
49 import org.jfree.data.time.Day;
50 import org.jfree.data.time.TimeSeries;
51 import org.jfree.data.time.TimeSeriesCollection;
52
53 import org.jfree.data.xy.XYDataset;
54
55 import org.jfree.ui.Layer;
56
57 import org.json.JSONArray;
58 import org.json.JSONException;
59
60 import org.w3c.dom.Document;
61 38
62 /** 39 /**
63 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 40 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
64 */ 41 */
65 public abstract class TimeseriesChartGenerator extends ChartGenerator { 42 public abstract class TimeseriesChartGenerator extends ChartGenerator {
181 range = Range.combine(range, subRange); 158 range = Range.combine(range, subRange);
182 } 159 }
183 160
184 } // end of TimeseriesAxisDataset class 161 } // end of TimeseriesAxisDataset class
185 162
186
187 /** List of annotations to insert in plot. */
188 protected List<FLYSAnnotation> annotations;
189
190 protected List<Marker> domainMarker; 163 protected List<Marker> domainMarker;
191 164
192 protected List<Marker> valueMarker; 165 protected List<Marker> valueMarker;
193 166
194 protected Map<String, String> attributes; 167 protected Map<String, String> attributes;
591 doAnnotations(annotations, null, theme, visible); 564 doAnnotations(annotations, null, theme, visible);
592 565
593 addAxisDataset(tsc, axisIndex, visible); 566 addAxisDataset(tsc, axisIndex, visible);
594 } 567 }
595 568
596 /** 569 /** Get color for hyk zones by their type (which is the name). */
597 * Register annotations like MainValues for later plotting
598 *
599 * @param o list of annotations (data of facet).
600 * @param facet The facet. This facet does NOT support any data objects. Use
601 * FLYSArtifact.getNativeFacet() instead to retrieve a Facet which supports
602 * data.
603 * @param theme Theme document for given annotations.
604 * @param visible The visibility of the annotations.
605 */
606 protected void doAnnotations(
607 FLYSAnnotation annotations,
608 ArtifactAndFacet aandf,
609 Document theme,
610 boolean visible
611 ){
612 // Running into trouble here.
613 logger.debug("doAnnotations");
614
615 // Add all annotations to our annotation pool.
616 annotations.setTheme(theme);
617 if (aandf != null) {
618 Facet facet = aandf.getFacet();
619 annotations.setLabel(aandf.getFacetDescription());
620 }
621 else {
622 logger.debug(
623 "Art/Facet for Annotations is null. " +
624 "This should never happen!");
625 }
626
627 addAnnotations(annotations, visible);
628 }
629
630
631
632 /**
633 * Adds annotations to list (if visible is true).
634 */
635 public void addAnnotations(FLYSAnnotation annotation, boolean visible) {
636 if (!visible) {
637 return;
638 }
639
640 if (annotations == null) {
641 annotations = new ArrayList<FLYSAnnotation>();
642 }
643
644 annotations.add(annotation);
645 }
646
647
648 /** Add annotations (Sticky, Text and hyk zones). */
649 /* public void addAnnotationsToRenderer(XYPlot plot) {
650 logger.debug("addAnnotationsToRenderer");
651
652 if (annotations == null) {
653 logger.debug("addAnnotationsToRenderer: no annotations.");
654 return;
655 }
656
657 // Paints for the boxes/lines.
658 Stroke basicStroke = new BasicStroke(1.0f);
659
660 Paint linePaint = new Color(255, 0,0,60);
661 Paint fillPaint = new Color(0, 255,0,60);
662 Paint tranPaint = new Color(0, 0,0, 0);
663
664 // OPTMIMIZE: Pre-calculate positions
665 Area area = new Area(
666 plot.getDomainAxis(0).getRange(),
667 plot.getRangeAxis().getRange());
668
669 // Walk over all Annotation sets.
670 for (FLYSAnnotation fa: annotations) {
671
672 // Access text styling, if any.
673 Document theme = fa.getTheme();
674 ThemeAccess.TextStyle textStyle = null;
675 ThemeAccess.LineStyle lineStyle = null;
676
677 // Get Themeing information and add legend item.
678 if (theme != null) {
679 ThemeAccess themeAccess = new ThemeAccess(theme);
680 textStyle = themeAccess.parseTextStyle();
681 lineStyle = themeAccess.parseLineStyle();
682 if (fa.getLabel() != null) {
683 LegendItemCollection lic = new LegendItemCollection();
684 LegendItemCollection old = plot.getFixedLegendItems();
685 lic.add(createLegendItem(theme, fa.getLabel()));
686 // (Re-)Add prior legend entries.
687 if (old != null) {
688 old.addAll(lic);
689 }
690 else {
691 old = lic;
692 }
693 plot.setFixedLegendItems(old);
694 }
695 }
696
697 // Other Text Annotations (e.g. labels of manual points).
698 for (XYTextAnnotation ta: fa.getTextAnnotations()) {
699 // Style the text.
700 if (textStyle != null) {
701 textStyle.apply(ta);
702 }
703 ta.setY(area.above(0.05d, ta.getY()));
704 plot.getRenderer().addAnnotation(ta, org.jfree.ui.Layer.FOREGROUND);
705 }
706 }
707 }*/
708
709 /**
710 * Add the annotations (Sticky, Text and hyk zones) stored
711 * in the annotations field.
712 */
713 public void addAnnotationsToRenderer(XYPlot plot) {
714 logger.debug("addAnnotationsToRenderer");
715
716 if (annotations == null) {
717 logger.debug("addAnnotationsToRenderer: no annotations.");
718 return;
719 }
720
721 // Paints for the boxes/lines.
722 Stroke basicStroke = new BasicStroke(1.0f);
723
724 Paint linePaint = new Color(255, 0,0,60);
725 Paint fillPaint = new Color(0, 255,0,60);
726 Paint tranPaint = new Color(0, 0,0, 0);
727
728 // OPTMIMIZE: Pre-calculate positions
729 Area area = new Area(
730 plot.getDomainAxis(0).getRange(),
731 plot.getRangeAxis().getRange());
732
733 // Walk over all Annotation sets.
734 for (FLYSAnnotation fa: annotations) {
735
736 // Access text styling, if any.
737 Document theme = fa.getTheme();
738 ThemeAccess.TextStyle textStyle = null;
739 ThemeAccess.LineStyle lineStyle = null;
740
741 // Get Themeing information and add legend item.
742 if (theme != null) {
743 ThemeAccess themeAccess = new ThemeAccess(theme);
744 textStyle = themeAccess.parseTextStyle();
745 lineStyle = themeAccess.parseLineStyle();
746 if (fa.getLabel() != null) {
747 LegendItemCollection lic = new LegendItemCollection();
748 LegendItemCollection old = plot.getFixedLegendItems();
749 lic.add(createLegendItem(theme, fa.getLabel()));
750 // (Re-)Add prior legend entries.
751 if (old != null) {
752 old.addAll(lic);
753 }
754 else {
755 old = lic;
756 }
757 plot.setFixedLegendItems(old);
758 }
759 }
760
761 // The 'Sticky' Annotations (at axis, with line and text).
762 /* for (StickyAxisAnnotation sta: fa.getAxisTextAnnotations()) {
763 addStickyAnnotation(
764 sta, plot, area, lineStyle, textStyle, theme);
765 }*/
766
767 // Other Text Annotations (e.g. labels of manual points).
768 for (XYTextAnnotation ta: fa.getTextAnnotations()) {
769 // Style the text.
770 if (textStyle != null) {
771 textStyle.apply(ta);
772 }
773 ta.setY(area.above(0.05d, ta.getY()));
774 plot.getRenderer().addAnnotation(ta, org.jfree.ui.Layer.FOREGROUND);
775 }
776
777 // Hyks.
778 for (HYKFactory.Zone zone: fa.getBoxes()) {
779 // For each zone, create a box to fill with color, a box to draw
780 // the lines and a text to display the type.
781 fillPaint = colorForHYKZone(zone.getName());
782
783 XYBoxAnnotation boxA = new XYBoxAnnotation(zone.getFrom(), area.atGround(),
784 zone.getTo(), area.ofGround(0.03f), basicStroke, tranPaint, fillPaint);
785 XYBoxAnnotation boxB = new XYBoxAnnotation(zone.getFrom(), area.atGround(),
786 zone.getTo(), area.atTop(), basicStroke, fillPaint, tranPaint);
787
788 XYTextAnnotation tex = new XYTextAnnotation(zone.getName(),
789 zone.getFrom() + (zone.getTo() - zone.getFrom()) / 1.0d,
790 area.ofGround(0.015f));
791 if (textStyle != null) {
792 textStyle.apply(tex);
793 }
794
795 plot.getRenderer().addAnnotation(boxA, org.jfree.ui.Layer.BACKGROUND);
796 plot.getRenderer().addAnnotation(boxB, org.jfree.ui.Layer.BACKGROUND);
797 plot.getRenderer().addAnnotation(tex, org.jfree.ui.Layer.BACKGROUND);
798 }
799 }
800 }
801
802 /** Get color for hyk zones by their type (which is the name). */
803 public Paint colorForHYKZone(String zoneName) { 570 public Paint colorForHYKZone(String zoneName) {
804 if (zoneName.startsWith("R")) { 571 if (zoneName.startsWith("R")) {
805 // Brownish. 572 // Brownish.
806 return new Color(153, 60, 0); 573 return new Color(153, 60, 0);
807 } 574 }

http://dive4elements.wald.intevation.org