Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 2236:c2b15d9c0f43
Refactoring: moved more base code from XYChartGenerator into its parent class ChartGenerator
flys-artifacts/trunk@3883 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 02 Feb 2012 14:00:40 +0000 |
parents | 46ec09c7f578 |
children | 23c7c51df772 |
comparison
equal
deleted
inserted
replaced
2235:ee5310134463 | 2236:c2b15d9c0f43 |
---|---|
9 | 9 |
10 import java.awt.geom.Rectangle2D; | 10 import java.awt.geom.Rectangle2D; |
11 | 11 |
12 import java.awt.image.BufferedImage; | 12 import java.awt.image.BufferedImage; |
13 | 13 |
14 import java.io.IOException; | |
15 | 14 |
16 import java.text.NumberFormat; | 15 import java.text.NumberFormat; |
17 | 16 |
18 import java.util.ArrayList; | 17 import java.util.ArrayList; |
19 import java.util.HashMap; | 18 import java.util.HashMap; |
44 import org.jfree.data.xy.XYDataset; | 43 import org.jfree.data.xy.XYDataset; |
45 | 44 |
46 import org.jfree.ui.RectangleInsets; | 45 import org.jfree.ui.RectangleInsets; |
47 import org.jfree.ui.TextAnchor; | 46 import org.jfree.ui.TextAnchor; |
48 | 47 |
49 import de.intevation.artifacts.CallContext; | |
50 | 48 |
51 import de.intevation.artifactdatabase.state.Facet; | 49 import de.intevation.artifactdatabase.state.Facet; |
52 import de.intevation.artifactdatabase.state.Settings; | 50 |
53 | 51 |
54 | |
55 import de.intevation.flys.exports.ChartExportHelper; | |
56 import de.intevation.flys.jfree.EnhancedLineAndShapeRenderer; | 52 import de.intevation.flys.jfree.EnhancedLineAndShapeRenderer; |
57 import de.intevation.flys.jfree.FLYSAnnotation; | 53 import de.intevation.flys.jfree.FLYSAnnotation; |
58 import de.intevation.flys.jfree.StableXYDifferenceRenderer; | 54 import de.intevation.flys.jfree.StableXYDifferenceRenderer; |
59 import de.intevation.flys.jfree.StickyAxisAnnotation; | 55 import de.intevation.flys.jfree.StickyAxisAnnotation; |
60 import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation; | 56 import de.intevation.flys.jfree.CollisionFreeXYTextAnnotation; |
440 | 436 |
441 return axis; | 437 return axis; |
442 } | 438 } |
443 | 439 |
444 | 440 |
445 /** Creates Font (Family and size) to use when creating Legend Items. */ | |
446 protected Font createLegendLabelFont() { | |
447 return new Font( | |
448 DEFAULT_FONT_NAME, | |
449 Font.PLAIN, | |
450 getLegendFontSize() | |
451 ); | |
452 } | |
453 | |
454 | |
455 /** | 441 /** |
456 * If no data is visible, draw at least empty axis. | 442 * If no data is visible, draw at least empty axis. |
457 */ | 443 */ |
458 private void recoverEmptyPlot(XYPlot plot) { | 444 private void recoverEmptyPlot(XYPlot plot) { |
459 if (plot.getRangeAxis() == null) { | 445 if (plot.getRangeAxis() == null) { |
627 logger.warn("Range for y" + index + | 613 logger.warn("Range for y" + index + |
628 " axis not set. Using default values: 0 - 1."); | 614 " axis not set. Using default values: 0 - 1."); |
629 ry = new Range(0, 1); | 615 ry = new Range(0, 1); |
630 } | 616 } |
631 return new Range[] {rx, ry}; | 617 return new Range[] {rx, ry}; |
632 } | |
633 | |
634 | |
635 /** | |
636 * This method searches for a specific axis in the <i>settings</i> if | |
637 * <i>settings</i> is set. If the axis was found, this method returns the | |
638 * specified axis range if the axis range is fixed. Otherwise, this method | |
639 * returns null. | |
640 * | |
641 * @param axisId The identifier of an axis. | |
642 * | |
643 * @return the specified axis range from <i>settings</i> if the axis is | |
644 * fixed, otherwise null. | |
645 */ | |
646 public Range getRangeForAxisFromSettings(String axisId) { | |
647 ChartSettings chartSettings = getChartSettings(); | |
648 if (chartSettings == null) { | |
649 return null; | |
650 } | |
651 | |
652 AxisSection as = chartSettings.getAxisSection(axisId); | |
653 Boolean fixed = as.isFixed(); | |
654 | |
655 if (fixed != null && fixed) { | |
656 Double upper = as.getUpperRange(); | |
657 Double lower = as.getLowerRange(); | |
658 | |
659 if (upper != null && lower != null) { | |
660 return lower < upper | |
661 ? new Range(lower, upper) | |
662 : new Range(upper, lower); | |
663 } | |
664 } | |
665 | |
666 return null; | |
667 } | |
668 | |
669 public LegendItem createLegendItem(Document theme, String name) { | |
670 // OPTIMIZE Pass font, parsed Theme items. | |
671 ThemeAccess themeAccess = new ThemeAccess(theme); | |
672 Color color = themeAccess.parseLineColorField(); | |
673 LegendItem li = new LegendItem(name, color); | |
674 li.setLabelFont(createLegendLabelFont()); | |
675 return li; | |
676 } | 618 } |
677 | 619 |
678 | 620 |
679 /** Get color for hyk zones by their type (which is the name). */ | 621 /** Get color for hyk zones by their type (which is the name). */ |
680 public Paint colorForHYKZone(String zoneName) { | 622 public Paint colorForHYKZone(String zoneName) { |
948 | 890 |
949 plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d)); | 891 plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d)); |
950 } | 892 } |
951 | 893 |
952 | 894 |
953 /** Override to handle subtitle adding. */ | |
954 protected void addSubtitles(JFreeChart chart) { | |
955 // override this method in subclasses that need subtitles | |
956 } | |
957 | |
958 | |
959 /** | 895 /** |
960 * This method walks over all axes (domain and range) of <i>plot</i> and | 896 * This method walks over all axes (domain and range) of <i>plot</i> and |
961 * calls localizeDomainAxis() for domain axes or localizeRangeAxis() for | 897 * calls localizeDomainAxis() for domain axes or localizeRangeAxis() for |
962 * range axes. | 898 * range axes. |
963 * | 899 * |
1148 // Add all annotations to our annotation pool. | 1084 // Add all annotations to our annotation pool. |
1149 annotations.setTheme(theme); | 1085 annotations.setTheme(theme); |
1150 if (facet != null) | 1086 if (facet != null) |
1151 annotations.setLabel(facet.getDescription()); | 1087 annotations.setLabel(facet.getDescription()); |
1152 addAnnotations(annotations, visible); | 1088 addAnnotations(annotations, visible); |
1153 } | |
1154 | |
1155 | |
1156 /** | |
1157 * Returns an instance of <i>ChartSettings</i> with a chart specific section | |
1158 * but with no axes settings. | |
1159 * | |
1160 * @return an instance of <i>ChartSettings</i>. | |
1161 */ | |
1162 public Settings getSettings() { | |
1163 if (this.settings != null) { | |
1164 return this.settings; | |
1165 } | |
1166 | |
1167 ChartSettings settings = new ChartSettings(); | |
1168 | |
1169 ChartSection chartSection = buildChartSection(); | |
1170 LegendSection legendSection = buildLegendSection(); | |
1171 ExportSection exportSection = buildExportSection(); | |
1172 | |
1173 settings.setChartSection(chartSection); | |
1174 settings.setLegendSection(legendSection); | |
1175 settings.setExportSection(exportSection); | |
1176 | |
1177 List<AxisSection> axisSections = buildAxisSections(); | |
1178 for (AxisSection axisSection: axisSections) { | |
1179 settings.addAxisSection(axisSection); | |
1180 } | |
1181 | |
1182 return settings; | |
1183 } | |
1184 | |
1185 | |
1186 /** | |
1187 * Creates a new <i>ChartSection</i>. | |
1188 * | |
1189 * @return a new <i>ChartSection</i>. | |
1190 */ | |
1191 protected ChartSection buildChartSection() { | |
1192 ChartSection chartSection = new ChartSection(); | |
1193 chartSection.setTitle(getChartTitle()); | |
1194 chartSection.setSubtitle(getChartSubtitle()); | |
1195 chartSection.setDisplayGird(isGridVisible()); | |
1196 return chartSection; | |
1197 } | |
1198 | |
1199 | |
1200 /** | |
1201 * Creates a new <i>LegendSection</i>. | |
1202 * | |
1203 * @return a new <i>LegendSection</i>. | |
1204 */ | |
1205 protected LegendSection buildLegendSection() { | |
1206 LegendSection legendSection = new LegendSection(); | |
1207 legendSection.setVisibility(isLegendVisible()); | |
1208 legendSection.setFontSize(getLegendFontSize()); | |
1209 return legendSection; | |
1210 } | |
1211 | |
1212 | |
1213 /** | |
1214 * Creates a new <i>ExportSection</i> with default values <b>WIDTH=600</b> | |
1215 * and <b>HEIGHT=400</b>. | |
1216 * | |
1217 * @return a new <i>ExportSection</i>. | |
1218 */ | |
1219 protected ExportSection buildExportSection() { | |
1220 ExportSection exportSection = new ExportSection(); | |
1221 exportSection.setWidth(600); | |
1222 exportSection.setHeight(400); | |
1223 return exportSection; | |
1224 } | |
1225 | |
1226 | |
1227 /** | |
1228 * Creates a list of Sections that contains all axes of the chart (including | |
1229 * X and Y axes). | |
1230 * | |
1231 * @return a list of Sections for each axis in this chart. | |
1232 */ | |
1233 protected List<AxisSection> buildAxisSections() { | |
1234 List<AxisSection> axisSections = new ArrayList<AxisSection>(); | |
1235 | |
1236 axisSections.addAll(buildXAxisSections()); | |
1237 axisSections.addAll(buildYAxisSections()); | |
1238 | |
1239 return axisSections; | |
1240 } | |
1241 | |
1242 | |
1243 /** | |
1244 * Creates a new Section for chart's X axis. | |
1245 * | |
1246 * @return a List that contains a Section for the X axis. | |
1247 */ | |
1248 protected List<AxisSection> buildXAxisSections() { | |
1249 List<AxisSection> axisSections = new ArrayList<AxisSection>(); | |
1250 | |
1251 String identifier = "X"; | |
1252 | |
1253 AxisSection axisSection = new AxisSection(); | |
1254 axisSection.setIdentifier(identifier); | |
1255 axisSection.setLabel(getXAxisLabel()); | |
1256 axisSection.setFontSize(14); | |
1257 axisSection.setFixed(false); | |
1258 | |
1259 // XXX We are able to find better default ranges that [0,0], but the Y | |
1260 // axes currently have no better ranges set. | |
1261 axisSection.setUpperRange(0d); | |
1262 axisSection.setLowerRange(0d); | |
1263 | |
1264 axisSections.add(axisSection); | |
1265 | |
1266 return axisSections; | |
1267 } | |
1268 | |
1269 | |
1270 /** | |
1271 * Creates a list of Section for the chart's Y axes. This method makes use | |
1272 * of <i>getYAxisWalker</i> to be able to access all Y axes defined in | |
1273 * subclasses. | |
1274 * | |
1275 * @return a list of Y axis sections. | |
1276 */ | |
1277 protected List<AxisSection> buildYAxisSections() { | |
1278 List<AxisSection> axisSections = new ArrayList<AxisSection>(); | |
1279 | |
1280 YAxisWalker walker = getYAxisWalker(); | |
1281 for (int i = 0, n = walker.length(); i < n; i++) { | |
1282 AxisSection ySection = new AxisSection(); | |
1283 ySection.setIdentifier(walker.getId(i)); | |
1284 ySection.setLabel(getYAxisLabel(i)); | |
1285 ySection.setFontSize(14); | |
1286 ySection.setFixed(false); | |
1287 | |
1288 // XXX We are able to find better default ranges that [0,0], the | |
1289 // only problem is, that we do NOT have a better range than [0,0] | |
1290 // for each axis, because the initial chart will not have a dataset | |
1291 // for each axis set! | |
1292 ySection.setUpperRange(0d); | |
1293 ySection.setLowerRange(0d); | |
1294 | |
1295 axisSections.add(ySection); | |
1296 } | |
1297 | |
1298 return axisSections; | |
1299 } | 1089 } |
1300 | 1090 |
1301 | 1091 |
1302 /** | 1092 /** |
1303 * Do Points out. | 1093 * Do Points out. |