comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java @ 2238:23c7c51df772

Some more refactoring in XYChartGenerator and ChartGenerator; implemented necessary stuff in TimeseriesChartGenerator and return new empty instances of timeseries charts. flys-artifacts/trunk@3885 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 02 Feb 2012 15:44:13 +0000
parents c2b15d9c0f43
children 7e8e1d5384c0
comparison
equal deleted inserted replaced
2237:60615235e951 2238:23c7c51df772
7 import java.io.OutputStream; 7 import java.io.OutputStream;
8 8
9 import java.util.ArrayList; 9 import java.util.ArrayList;
10 import java.util.List; 10 import java.util.List;
11 import java.util.Locale; 11 import java.util.Locale;
12 import java.util.TreeMap;
13 import java.util.SortedMap;
12 14
13 import javax.xml.xpath.XPathConstants; 15 import javax.xml.xpath.XPathConstants;
14 16
15 import org.apache.log4j.Logger; 17 import org.apache.log4j.Logger;
16 18
19 21
20 import org.jfree.chart.JFreeChart; 22 import org.jfree.chart.JFreeChart;
21 import org.jfree.chart.LegendItem; 23 import org.jfree.chart.LegendItem;
22 import org.jfree.chart.axis.NumberAxis; 24 import org.jfree.chart.axis.NumberAxis;
23 import org.jfree.data.Range; 25 import org.jfree.data.Range;
26 import org.jfree.data.xy.XYDataset;
24 27
25 import de.intevation.artifacts.Artifact; 28 import de.intevation.artifacts.Artifact;
26 import de.intevation.artifacts.CallContext; 29 import de.intevation.artifacts.CallContext;
27 import de.intevation.artifacts.CallMeta; 30 import de.intevation.artifacts.CallMeta;
28 import de.intevation.artifacts.PreferredLocale; 31 import de.intevation.artifacts.PreferredLocale;
86 protected Artifact master; 89 protected Artifact master;
87 90
88 /** The settings that should be used during output creation.*/ 91 /** The settings that should be used during output creation.*/
89 protected Settings settings; 92 protected Settings settings;
90 93
94 /** Map of datasets ("index"). */
95 protected SortedMap<Integer, AxisDataset> datasets;
96
97
91 98
92 /** 99 /**
93 * A mini interface that allows to walk over the YAXIS enums defined in 100 * A mini interface that allows to walk over the YAXIS enums defined in
94 * subclasses. 101 * subclasses.
95 */ 102 */
96 public interface YAxisWalker { 103 public interface YAxisWalker {
104
97 int length(); 105 int length();
106
98 String getId(int idx); 107 String getId(int idx);
99 } 108 } // end of YAxisWalker interface
109
110
111
112 public interface AxisDataset {
113
114 void addDataset(XYDataset dataset);
115
116 boolean isEmpty();
117
118 } // end of AxisDataset interface
119
120
121
122 /**
123 * Default constructor that initializes internal data structures.
124 */
125 public ChartGenerator() {
126 datasets = new TreeMap<Integer, AxisDataset>();
127 }
128
100 129
101 130
102 /** 131 /**
103 * This method needs to be implemented by concrete subclasses to create new 132 * This method needs to be implemented by concrete subclasses to create new
104 * instances of JFreeChart. 133 * instances of JFreeChart.
140 * @param pos The position of an Y axis. 169 * @param pos The position of an Y axis.
141 * 170 *
142 * @return the default Y axis label at position <i>pos</i>. 171 * @return the default Y axis label at position <i>pos</i>.
143 */ 172 */
144 protected abstract String getDefaultYAxisLabel(int pos); 173 protected abstract String getDefaultYAxisLabel(int pos);
174
175
176 /**
177 * This method is used to create new AxisDataset instances which may differ
178 * in concrete subclasses.
179 *
180 * @param idx The index of an axis.
181 */
182 protected abstract AxisDataset createAxisDataset(int idx);
183
184
185 /**
186 * Combines the ranges of the X axis at index <i>idx</i>.
187 *
188 * @param range A new range.
189 * @param idx The index of the X axis that should be comined with
190 * <i>range</i>.
191 */
192 protected abstract void combineXRanges(Range range, int idx);
145 193
146 194
147 /** 195 /**
148 * This method should be used by concrete subclasses to add subtitle to 196 * This method should be used by concrete subclasses to add subtitle to
149 * <i>chart</i>. <b>The method in this implementation is empty</b>. 197 * <i>chart</i>. <b>The method in this implementation is empty</b>.
725 : new Range(upper, lower); 773 : new Range(upper, lower);
726 } 774 }
727 } 775 }
728 776
729 return null; 777 return null;
778 }
779
780
781 /**
782 * Adds a new AxisDataset which contains <i>dataset</i> at index <i>idx</i>.
783 *
784 * @param dataset An XYDataset.
785 * @param idx The axis index.
786 * @param visible Determines, if the dataset should be visible or not.
787 */
788 public void addAxisDataset(XYDataset dataset, int idx, boolean visible) {
789 if (dataset == null || idx < 0) {
790 return;
791 }
792
793 AxisDataset axisDataset = getAxisDataset(idx);
794
795 Range[] xyRanges = ChartHelper.getRanges(dataset);
796
797 if (visible) {
798 logger.debug("Add new AxisDataset at index: " + idx);
799 axisDataset.addDataset(dataset);
800 combineXRanges(xyRanges[0], 0);
801 }
802 else {
803 combineXRanges(xyRanges[0], 0);
804
805 // TODO
806 // Expand y range provided by 'timeseries' to have a proper range
807 // set which includes all data.
808 // iw: I am not sure if we still need this
809 }
810 }
811
812
813 /**
814 * This method grants access to the AxisDatasets stored in <i>datasets</i>.
815 * If no AxisDataset exists for index <i>idx</i>, a new AxisDataset is
816 * created using <i>createAxisDataset()</i>.
817 *
818 * @param idx The index of the desired AxisDataset.
819 *
820 * @return an existing or new AxisDataset.
821 */
822 public AxisDataset getAxisDataset(int idx) {
823 AxisDataset axisDataset = datasets.get(idx);
824
825 if (axisDataset == null) {
826 axisDataset = createAxisDataset(idx);
827 datasets.put(idx, axisDataset);
828 }
829
830 return axisDataset;
730 } 831 }
731 832
732 833
733 /** 834 /**
734 * This helper mehtod is used to extract the current locale from instance 835 * This helper mehtod is used to extract the current locale from instance
945 return new IdentifiableNumberAxis(walker.getId(idx), label); 1046 return new IdentifiableNumberAxis(walker.getId(idx), label);
946 } 1047 }
947 1048
948 1049
949 /** 1050 /**
1051 * Create Y (range) axis for given index.
1052 * Shall be overriden by subclasses.
1053 */
1054 protected NumberAxis createYAxis(int index) {
1055 YAxisWalker walker = getYAxisWalker();
1056
1057 Font labelFont = new Font(
1058 DEFAULT_FONT_NAME,
1059 Font.BOLD,
1060 getYAxisFontSize(index));
1061
1062 IdentifiableNumberAxis axis = new IdentifiableNumberAxis(
1063 walker.getId(index),
1064 getYAxisLabel(index));
1065
1066 axis.setAutoRangeIncludesZero(false);
1067 axis.setLabelFont(labelFont);
1068
1069 return axis;
1070 }
1071
1072
1073 /**
950 * Creates a new LegendItem with <i>name</i> and font provided by 1074 * Creates a new LegendItem with <i>name</i> and font provided by
951 * <i>createLegendLabelFont()</i>. 1075 * <i>createLegendLabelFont()</i>.
952 * 1076 *
953 * @param theme The theme of the chart line. 1077 * @param theme The theme of the chart line.
954 * @param The displayed name of the item. 1078 * @param The displayed name of the item.

http://dive4elements.wald.intevation.org