comparison artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java @ 7597:fca46ce8e4f5

(issue1225) Implement Magic labels. There is now a new value in the chartsettings "Suggested Label" which is hidden in the property editor. A suggested label is the label that combines the label's of all processors that wrote data to an axis. This suggested label is set as the label when the user has not overwritten the label.
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 25 Nov 2013 14:58:14 +0100
parents 1dff8e71c4d6
children 25bce6e8beea
comparison
equal deleted inserted replaced
7596:12c4c8d0ac41 7597:fca46ce8e4f5
13 13
14 import java.text.NumberFormat; 14 import java.text.NumberFormat;
15 15
16 import java.util.ArrayList; 16 import java.util.ArrayList;
17 import java.util.HashMap; 17 import java.util.HashMap;
18 import java.util.LinkedHashSet;
18 import java.util.List; 19 import java.util.List;
19 import java.util.Map; 20 import java.util.Map;
21 import java.util.Set;
22
23 import java.util.regex.Pattern;
24 import java.util.regex.Matcher;
20 25
21 import java.io.OutputStream; 26 import java.io.OutputStream;
22 27
23 import javax.swing.ImageIcon; 28 import javax.swing.ImageIcon;
24 29
64 import org.jfree.data.xy.XYDataset; 69 import org.jfree.data.xy.XYDataset;
65 import org.jfree.data.xy.XYSeries; 70 import org.jfree.data.xy.XYSeries;
66 import org.jfree.data.xy.XYSeriesCollection; 71 import org.jfree.data.xy.XYSeriesCollection;
67 72
68 import org.w3c.dom.Document; 73 import org.w3c.dom.Document;
74
75 import org.apache.commons.lang.StringUtils;
69 76
70 77
71 /** 78 /**
72 * The main diagram creation class. 79 * The main diagram creation class.
73 * 80 *
103 protected Map<Integer, Bounds> yBounds; 110 protected Map<Integer, Bounds> yBounds;
104 111
105 /** Whether or not the plot is inverted (left-right). */ 112 /** Whether or not the plot is inverted (left-right). */
106 private boolean inverted; 113 private boolean inverted;
107 114
115 private static final Pattern UNIT_PATTERN =
116 Pattern.compile("\\s*\\[[\\w\\s\\+\\-]*\\]\\s*");
117
118 protected Map<Integer, LinkedHashSet<String>> axesLabels;
119
108 protected DiagramAttributes.Instance diagramAttributes; 120 protected DiagramAttributes.Instance diagramAttributes;
109 121
110 public DiagramGenerator() { 122 public DiagramGenerator() {
111 super(); 123 super();
112 124
125 axesLabels = new HashMap<Integer, LinkedHashSet<String>>();
113 xBounds = new HashMap<Integer, Bounds>(); 126 xBounds = new HashMap<Integer, Bounds>();
114 yBounds = new HashMap<Integer, Bounds>(); 127 yBounds = new HashMap<Integer, Bounds>();
115 } 128 }
116 129
117 @Override 130 @Override
1054 return "Domain Axis Title not configured in conf.xml"; 1067 return "Domain Axis Title not configured in conf.xml";
1055 } 1068 }
1056 1069
1057 @Override 1070 @Override
1058 protected String getDefaultYAxisLabel(String axisName) { 1071 protected String getDefaultYAxisLabel(String axisName) {
1059 String label; 1072 Set labelSet = axesLabels.get(diagramAttributes.getAxisIndex(axisName));
1073 logger.debug("Labels for axis: " + labelSet);
1074 if (labelSet != null && labelSet.size() >= 2) {
1075 String label = StringUtils.join(labelSet, ", ");
1076 Matcher units = UNIT_PATTERN.matcher(label);
1077 if (units.find()) {
1078 String firstUnit = units.group();
1079 label = units.replaceAll("");
1080 label += firstUnit;
1081 }
1082 return label;
1083 }
1060 for (Processor pr: diagramAttributes.getProcessorsForAxisName(axisName)) { 1084 for (Processor pr: diagramAttributes.getProcessorsForAxisName(axisName)) {
1061 label = pr.getAxisLabel(this); 1085 String label = pr.getAxisLabel(this);
1062 if (label != null) { 1086 if (label != null) {
1063 return label; 1087 return label;
1064 } 1088 }
1065 } 1089 }
1066 return "No configured axis label"; 1090 return "No configured axis label";
1080 for (int i = 0, n = axesAttrs.size(); i < n; i++) { 1104 for (int i = 0, n = axesAttrs.size(); i < n; i++) {
1081 AxisSection ySection = new AxisSection(); 1105 AxisSection ySection = new AxisSection();
1082 String axisName = diagramAttributes.getAxisName(i); 1106 String axisName = diagramAttributes.getAxisName(i);
1083 ySection.setIdentifier(axisName); 1107 ySection.setIdentifier(axisName);
1084 ySection.setLabel(getYAxisLabel(axisName)); 1108 ySection.setLabel(getYAxisLabel(axisName));
1109 ySection.setSuggestedLabel(getDefaultYAxisLabel(axisName));
1085 ySection.setFontSize(14); 1110 ySection.setFontSize(14);
1086 ySection.setFixed(false); 1111 ySection.setFixed(false);
1087 1112
1088 // XXX We are able to find better default ranges that [0,0], the 1113 // XXX We are able to find better default ranges that [0,0], the
1089 // only problem is, that we do NOT have a better range than [0,0] 1114 // only problem is, that we do NOT have a better range than [0,0]
1113 return getDefaultYAxisLabel(axisName); 1138 return getDefaultYAxisLabel(axisName);
1114 } 1139 }
1115 AxisSection as = chartSettings.getAxisSection(axisName); 1140 AxisSection as = chartSettings.getAxisSection(axisName);
1116 if (as != null) { 1141 if (as != null) {
1117 String label = as.getLabel(); 1142 String label = as.getLabel();
1118 if (label != null) { 1143 if (label != null && !label.equals(as.getSuggestedLabel())) {
1144 // Only if the suggested label is not the current label
1145 // the user has modified the label. Otherwise lets
1146 // recalculate the label
1119 return label; 1147 return label;
1120 } 1148 }
1121 } 1149 }
1122 1150
1123 return getDefaultYAxisLabel(axisName); 1151 return getDefaultYAxisLabel(axisName);
1152 List<Processor> prL = diagramAttributes.getProcessors(); 1180 List<Processor> prL = diagramAttributes.getProcessors();
1153 for (Processor pr: prL) { 1181 for (Processor pr: prL) {
1154 if (pr.canHandle(facetName)) { 1182 if (pr.canHandle(facetName)) {
1155 found = true; 1183 found = true;
1156 pr.doOut(this, bundle, theme, visible); 1184 pr.doOut(this, bundle, theme, visible);
1185
1186 if (visible) {
1187 // Save the label that should be added for this processor
1188 int axisIdx = diagramAttributes.getAxisIndex(pr.getAxisName());
1189 LinkedHashSet<String> curLabels = axesLabels.get(axisIdx);
1190 if (curLabels == null) {
1191 curLabels = new LinkedHashSet<String>(5);
1192 }
1193 curLabels.add(pr.getAxisLabel(this));
1194 axesLabels.put(axisIdx, curLabels);
1195 }
1157 } 1196 }
1158 } 1197 }
1159 if (!found) { 1198 if (!found) {
1160 logger.warn("No processor found for: " + facetName); 1199 logger.warn("No processor found for: " + facetName);
1161 if (logger.isDebugEnabled()) { 1200 if (logger.isDebugEnabled()) {

http://dive4elements.wald.intevation.org