comparison flys-artifacts/src/main/java/de/intevation/flys/exports/InfoGeneratorHelper.java @ 686:3dc61e00385e facets-slt

Merged with trunk and introduced hashing of computed values. flys-artifacts/branches/facets-slt@2126 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 15 Jun 2011 15:28:54 +0000
parents 51172d56e8bc
children bdb05dc9b763
comparison
equal deleted inserted replaced
667:434146596838 686:3dc61e00385e
12 import org.jfree.chart.ChartRenderingInfo; 12 import org.jfree.chart.ChartRenderingInfo;
13 import org.jfree.chart.JFreeChart; 13 import org.jfree.chart.JFreeChart;
14 import org.jfree.chart.axis.ValueAxis; 14 import org.jfree.chart.axis.ValueAxis;
15 import org.jfree.chart.plot.XYPlot; 15 import org.jfree.chart.plot.XYPlot;
16 import org.jfree.data.Range; 16 import org.jfree.data.Range;
17 import org.jfree.data.xy.XYDataset;
17 18
18 import de.intevation.artifacts.common.ArtifactNamespaceContext; 19 import de.intevation.artifacts.common.ArtifactNamespaceContext;
19 import de.intevation.artifacts.common.utils.XMLUtils; 20 import de.intevation.artifacts.common.utils.XMLUtils;
20 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; 21 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
21 22
29 30
30 private static final Logger logger = 31 private static final Logger logger =
31 Logger.getLogger(InfoGeneratorHelper.class); 32 Logger.getLogger(InfoGeneratorHelper.class);
32 33
33 34
34 private InfoGeneratorHelper() { 35 protected XYChartGenerator generator;
36
37
38 public InfoGeneratorHelper(XYChartGenerator generator) {
39 this.generator = generator;
35 } 40 }
36 41
37 42
38 /** 43 /**
39 * Triggers the creation of the chart info document. 44 * Triggers the creation of the chart info document.
41 * @param chart The JFreeChart chart. 46 * @param chart The JFreeChart chart.
42 * @param info An info object that has been created while chart creation. 47 * @param info An info object that has been created while chart creation.
43 * 48 *
44 * @return the info document. 49 * @return the info document.
45 */ 50 */
46 public static Document createInfoDocument( 51 public Document createInfoDocument(
47 JFreeChart chart, 52 JFreeChart chart,
48 ChartRenderingInfo info) 53 ChartRenderingInfo info)
49 { 54 {
50 logger.debug("InfoGeneratorHelper.createInfoDocument"); 55 logger.debug("InfoGeneratorHelper.createInfoDocument");
51 56
57 ArtifactNamespaceContext.NAMESPACE_PREFIX); 62 ArtifactNamespaceContext.NAMESPACE_PREFIX);
58 63
59 Element chartinfo = cr.create("chartinfo"); 64 Element chartinfo = cr.create("chartinfo");
60 65
61 chartinfo.appendChild(createAxesElements(cr, chart)); 66 chartinfo.appendChild(createAxesElements(cr, chart));
62 chartinfo.appendChild(createTransformationElement(cr, chart, info)); 67 chartinfo.appendChild(createTransformationElements(cr, chart, info));
63 68
64 doc.appendChild(chartinfo); 69 doc.appendChild(chartinfo);
65 70
66 return doc; 71 return doc;
67 } 72 }
74 * @param cr The ElementCreator. 79 * @param cr The ElementCreator.
75 * @param chart The chart that provides range information of its axes. 80 * @param chart The chart that provides range information of its axes.
76 * 81 *
77 * @return an element with axes information. 82 * @return an element with axes information.
78 */ 83 */
79 protected static Element createAxesElements( 84 protected Element createAxesElements(
80 ElementCreator cr, 85 ElementCreator cr,
81 JFreeChart chart) 86 JFreeChart chart)
82 { 87 {
83 logger.debug("InfoGeneratorHelper.createRangeElements"); 88 logger.debug("InfoGeneratorHelper.createRangeElements");
84 89
87 XYPlot plot = (XYPlot) chart.getPlot(); 92 XYPlot plot = (XYPlot) chart.getPlot();
88 93
89 int dAxisCount = plot.getDomainAxisCount(); 94 int dAxisCount = plot.getDomainAxisCount();
90 for (int i = 0; i < dAxisCount; i++) { 95 for (int i = 0; i < dAxisCount; i++) {
91 ValueAxis axis = plot.getDomainAxis(i); 96 ValueAxis axis = plot.getDomainAxis(i);
97 XYDataset data = plot.getDataset(i);
98
92 if (axis != null) { 99 if (axis != null) {
93 Element e = createAxisElement(cr, axis, "domain", i); 100 Element e = createAxisElement(cr, axis, data, "domain", i);
94 axes.appendChild(e); 101 axes.appendChild(e);
95 } 102 }
96 } 103 }
97 104
98 int rAxisCount = plot.getRangeAxisCount(); 105 int rAxisCount = plot.getRangeAxisCount();
99 for (int i = 0; i < rAxisCount; i++) { 106 for (int i = 0; i < rAxisCount; i++) {
100 ValueAxis axis = plot.getRangeAxis(i); 107 ValueAxis axis = plot.getRangeAxis(i);
101 if (axis != null) { 108 XYDataset data = plot.getDataset(i);
102 Element e = createAxisElement(cr, axis, "range", i); 109
103 axes.appendChild(e); 110 if (axis == null || data == null) {
111 logger.warn("Axis or dataset is empty at pos: " + i);
112 continue;
104 } 113 }
114
115 Element e = createAxisElement(cr, axis, data, "range", i);
116 axes.appendChild(e);
105 } 117 }
106 118
107 return axes; 119 return axes;
108 } 120 }
109 121
112 * This method create a axis element for a given <i>axis</i> and 124 * This method create a axis element for a given <i>axis</i> and
113 * <i>type</i>. Type can be one of 'domain' or 'range'. 125 * <i>type</i>. Type can be one of 'domain' or 'range'.
114 * 126 *
115 * @param cr The ElementCreator 127 * @param cr The ElementCreator
116 * @param axis The axis that provides range information. 128 * @param axis The axis that provides range information.
129 * @param dataset The dataset for min/max determination.
117 * @param type The axis type ('domain' or 'range'). 130 * @param type The axis type ('domain' or 'range').
118 * @param pos The position in the chart. 131 * @param pos The position in the chart.
119 * 132 *
120 * @return An element that contains range information of a given axis. 133 * @return An element that contains range information of a given axis.
121 */ 134 */
122 protected static Element createAxisElement( 135 protected Element createAxisElement(
123 ElementCreator cr, 136 ElementCreator cr,
124 ValueAxis axis, 137 ValueAxis axis,
138 XYDataset dataset,
125 String type, 139 String type,
126 int pos) 140 int pos)
127 { 141 {
128 Range range = axis.getRange(); 142 Range range = axis.getRange();
129 143
130 Element e = cr.create(type); 144 Element e = cr.create(type);
131 cr.addAttr(e, "pos", String.valueOf(pos), true); 145 cr.addAttr(e, "pos", String.valueOf(pos), true);
132 cr.addAttr(e, "from", String.valueOf(range.getLowerBound()), true); 146 cr.addAttr(e, "from", String.valueOf(range.getLowerBound()), true);
133 cr.addAttr(e, "to", String.valueOf(range.getUpperBound()), true); 147 cr.addAttr(e, "to", String.valueOf(range.getUpperBound()), true);
134 148
149 Range[] rs = generator.getRangesForDataset(dataset);
150 Range r = null;
151
152 if (type.equals("range")) {
153 r = rs[1];
154 }
155 else {
156 r = rs[0];
157 }
158
159 cr.addAttr(e, "min", String.valueOf(r.getLowerBound()), true);
160 cr.addAttr(e, "max", String.valueOf(r.getUpperBound()), true);
161
135 return e; 162 return e;
136 } 163 }
137 164
138 165
139 /** 166 /**
144 * @param chart The chart object. 171 * @param chart The chart object.
145 * @param info The ChartRenderingInfo that is filled while chart creation. 172 * @param info The ChartRenderingInfo that is filled while chart creation.
146 * 173 *
147 * @return an element that contains one or more transformation matrix. 174 * @return an element that contains one or more transformation matrix.
148 */ 175 */
149 protected static Element createTransformationElement( 176 protected Element createTransformationElements(
150 ElementCreator cr, 177 ElementCreator cr,
151 JFreeChart chart, 178 JFreeChart chart,
152 ChartRenderingInfo info) 179 ChartRenderingInfo info)
153 { 180 {
154 logger.debug("InfoGeneratorHelper.createTransformationElement"); 181 logger.debug("InfoGeneratorHelper.createTransformationElements");
155 182
156 Element tf = cr.create("transformation-matrix"); 183 Element tf = cr.create("transformation-matrix");
157 184
158 Rectangle2D dataArea = info.getPlotInfo().getDataArea(); 185 Rectangle2D dataArea = info.getPlotInfo().getDataArea();
159 186
160 XYPlot plot = (XYPlot) chart.getPlot(); 187 XYPlot plot = (XYPlot) chart.getPlot();
161 ValueAxis xAxis = plot.getDomainAxis(); 188 ValueAxis xAxis = plot.getDomainAxis();
162 ValueAxis yAxis = plot.getRangeAxis(); 189
163 190 if (xAxis == null) {
191 logger.error("There is no x axis in the chart!");
192 return null;
193 }
194
195 for (int i = 0, num = plot.getRangeAxisCount(); i < num; i++) {
196 ValueAxis yAxis = plot.getRangeAxis(i);
197
198 if (yAxis == null) {
199 logger.warn("No y axis at pos " + i + " existing.");
200 continue;
201 }
202
203 Element matrix = createTransformationElement(
204 cr, xAxis, yAxis, dataArea, i);
205
206 tf.appendChild(matrix);
207 }
208
209 return tf;
210 }
211
212
213 /**
214 * Creates an element that contains values used to transform coordinates
215 * of a coordinate system A into a coordinate system B.
216 *
217 * @param cr The ElementCreator.
218 * @param xAxis The x axis of the target coordinate system.
219 * @param yAxis The y axis of the target coordinate system.
220 * @param dataArea The pixel coordinates of the chart image.
221 * @param pos The dataset position.
222 *
223 * @return an element that contains transformation matrix values.
224 */
225 protected Element createTransformationElement(
226 ElementCreator cr,
227 ValueAxis xAxis,
228 ValueAxis yAxis,
229 Rectangle2D dataArea,
230 int pos)
231 {
164 double[] tm = createTransformationMatrix(dataArea, xAxis, yAxis); 232 double[] tm = createTransformationMatrix(dataArea, xAxis, yAxis);
165 233
166 cr.addAttr(tf, "sx", String.valueOf(tm[0]), true); 234 Element matrix = cr.create("matrix");
167 cr.addAttr(tf, "sy", String.valueOf(tm[1]), true); 235
168 cr.addAttr(tf, "tx", String.valueOf(tm[2]), true); 236 cr.addAttr(matrix, "pos", String.valueOf(pos), true);
169 cr.addAttr(tf, "ty", String.valueOf(tm[3]), true); 237 cr.addAttr(matrix, "sx", String.valueOf(tm[0]), true);
170 238 cr.addAttr(matrix, "sy", String.valueOf(tm[1]), true);
171 return tf; 239 cr.addAttr(matrix, "tx", String.valueOf(tm[2]), true);
240 cr.addAttr(matrix, "ty", String.valueOf(tm[3]), true);
241
242 return matrix;
172 } 243 }
173 244
174 245
175 /** 246 /**
176 * This method determines a transformation matrix to transform pixel 247 * This method determines a transformation matrix to transform pixel

http://dive4elements.wald.intevation.org