1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.chart.source; |
21 |
|
|
22 |
|
import java.text.DateFormat; |
23 |
|
import java.text.ParseException; |
24 |
|
import java.text.SimpleDateFormat; |
25 |
|
import java.util.Date; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.StringUtils; |
28 |
|
import org.apache.commons.lang3.math.NumberUtils; |
29 |
|
import org.jfree.chart.axis.Axis; |
30 |
|
import org.jfree.chart.axis.CategoryAxis; |
31 |
|
import org.jfree.chart.axis.DateAxis; |
32 |
|
import org.jfree.chart.axis.DateTickMarkPosition; |
33 |
|
import org.jfree.chart.axis.NumberAxis; |
34 |
|
import org.jfree.chart.axis.ValueAxis; |
35 |
|
import org.xwiki.chart.axis.AxisType; |
36 |
|
import org.xwiki.chart.plot.PlotType; |
37 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
|
|
| 78.3% |
Uncovered Elements: 28 (129) |
Complexity: 34 |
Complexity Density: 0.42 |
|
47 |
|
public class AxisConfigurator extends AbstractConfigurator |
48 |
|
{ |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
public static final String DOMAIN_AXIS_PARAM = "domain_axis_type"; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
public static final String RANGE_AXIS_PARAM = "range_axis_type"; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
public static final String DOMAIN_AXIS_DATE_FORMAT_PARAM = "domain_axis_date_format"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public static final String RANGE_AXIS_DATE_FORMAT_PARAM = "range_axis_date_format"; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
public static final String DOMAIN_AXIS_LOWER_PARAM = "domain_axis_lower"; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
public static final String DOMAIN_AXIS_UPPER_PARAM = "domain_axis_upper"; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
public static final String RANGE_AXIS_LOWER_PARAM = "range_axis_lower"; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
public static final String RANGE_AXIS_UPPER_PARAM = "range_axis_upper"; |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
private AxisType[] axisTypes = {null, null, null}; |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
private String[] axisDateFormat = {null, null, null}; |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
private final LocaleConfiguration localeConfiguration; |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
private String[] axisLowerLimit = {null, null, null}; |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
private String[] axisUpperLimit = {null, null, null}; |
113 |
|
|
114 |
|
|
115 |
|
@param |
116 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
117 |
25 |
public AxisConfigurator(LocaleConfiguration localeConfiguration)... |
118 |
|
{ |
119 |
25 |
this.localeConfiguration = localeConfiguration; |
120 |
|
} |
121 |
|
|
|
|
| 65.6% |
Uncovered Elements: 11 (32) |
Complexity: 8 |
Complexity Density: 0.44 |
|
122 |
137 |
@Override... |
123 |
|
public boolean setParameter(String key, String value) throws MacroExecutionException |
124 |
|
{ |
125 |
137 |
boolean claimed = true; |
126 |
|
|
127 |
137 |
if (DOMAIN_AXIS_PARAM.equals(key)) { |
128 |
5 |
AxisType type = AxisType.forName(value); |
129 |
5 |
if (type == null) { |
130 |
0 |
invalidParameterValue(DOMAIN_AXIS_PARAM, value); |
131 |
|
} |
132 |
5 |
setAxisType(0, type); |
133 |
132 |
} else if (DOMAIN_AXIS_DATE_FORMAT_PARAM.equals(key)) { |
134 |
3 |
setAxisDateFormat(0, value); |
135 |
129 |
} else if (RANGE_AXIS_PARAM.equals(key)) { |
136 |
0 |
AxisType type = AxisType.forName(value); |
137 |
0 |
if (type == null) { |
138 |
0 |
invalidParameterValue(RANGE_AXIS_PARAM, value); |
139 |
|
} |
140 |
0 |
setAxisType(1, type); |
141 |
129 |
} else if (RANGE_AXIS_DATE_FORMAT_PARAM.equals(key)) { |
142 |
0 |
setAxisDateFormat(0, value); |
143 |
129 |
} else if (!claimLimitParameters(key, value)) { |
144 |
123 |
claimed = false; |
145 |
|
} |
146 |
|
|
147 |
137 |
return claimed; |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
@param |
154 |
|
@param |
155 |
|
@return |
156 |
|
|
|
|
| 78.9% |
Uncovered Elements: 4 (19) |
Complexity: 5 |
Complexity Density: 0.45 |
|
157 |
129 |
private boolean claimLimitParameters(String key, String value)... |
158 |
|
{ |
159 |
129 |
boolean claimed = true; |
160 |
|
|
161 |
129 |
if (DOMAIN_AXIS_LOWER_PARAM.equals(key)) { |
162 |
0 |
axisLowerLimit[0] = value; |
163 |
129 |
} else if (DOMAIN_AXIS_UPPER_PARAM.equals(key)) { |
164 |
0 |
axisUpperLimit[0] = value; |
165 |
129 |
} else if (RANGE_AXIS_LOWER_PARAM.equals(key)) { |
166 |
3 |
axisLowerLimit[1] = value; |
167 |
126 |
} else if (RANGE_AXIS_UPPER_PARAM.equals(key)) { |
168 |
3 |
axisUpperLimit[1] = value; |
169 |
|
} else { |
170 |
123 |
claimed = false; |
171 |
|
} |
172 |
|
|
173 |
129 |
return claimed; |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
@param |
180 |
|
@param |
181 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
182 |
5 |
private void setAxisType(int index, AxisType axisType)... |
183 |
|
{ |
184 |
5 |
axisTypes[index] = axisType; |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
@param |
191 |
|
@param |
192 |
|
@throws |
193 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
194 |
3 |
private void setAxisDateFormat(int index, String dateFormatString) throws MacroExecutionException... |
195 |
|
{ |
196 |
3 |
axisDateFormat[index] = dateFormatString; |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
@param |
203 |
|
@param |
204 |
|
@throws |
205 |
|
|
|
|
| 88.4% |
Uncovered Elements: 5 (43) |
Complexity: 10 |
Complexity Density: 0.3 |
|
206 |
21 |
public void setAxes(PlotType plotType, SimpleChartModel chartModel) throws MacroExecutionException... |
207 |
|
{ |
208 |
21 |
AxisType[] defaultAxisTypes = plotType.getDefaultAxisTypes(); |
209 |
|
|
210 |
84 |
for (int i = 0; i < axisTypes.length; i++) { |
211 |
63 |
AxisType type = axisTypes[i]; |
212 |
63 |
if (i >= defaultAxisTypes.length) { |
213 |
39 |
if (type != null) { |
214 |
0 |
throw new MacroExecutionException("To many axes for plot type."); |
215 |
|
} |
216 |
39 |
continue; |
217 |
|
} |
218 |
24 |
if (type == null) { |
219 |
19 |
type = defaultAxisTypes[i]; |
220 |
|
} |
221 |
|
|
222 |
24 |
Axis axis; |
223 |
|
|
224 |
24 |
switch (type) { |
225 |
12 |
case NUMBER: |
226 |
12 |
NumberAxis numberAxis = new NumberAxis(); |
227 |
12 |
axis = numberAxis; |
228 |
12 |
setNumberLimits(numberAxis, i); |
229 |
12 |
break; |
230 |
7 |
case CATEGORY: |
231 |
7 |
axis = new CategoryAxis(); |
232 |
7 |
break; |
233 |
5 |
case DATE: |
234 |
5 |
DateAxis dateAxis = new DateAxis(); |
235 |
5 |
axis = dateAxis; |
236 |
5 |
dateAxis.setTickMarkPosition(DateTickMarkPosition.END); |
237 |
5 |
if (axisDateFormat[i] != null) { |
238 |
3 |
try { |
239 |
3 |
DateFormat dateFormat = new SimpleDateFormat(axisDateFormat[i], |
240 |
|
localeConfiguration.getLocale()); |
241 |
3 |
dateAxis.setDateFormatOverride(dateFormat); |
242 |
|
} catch (IllegalArgumentException e) { |
243 |
0 |
throw new MacroExecutionException(String.format("Invalid date format [%s].", |
244 |
|
axisDateFormat[i])); |
245 |
|
} |
246 |
|
} |
247 |
5 |
setDateLimits(dateAxis, i); |
248 |
5 |
break; |
249 |
0 |
default: |
250 |
0 |
throw new MacroExecutionException(String.format("Unsupported axis type [%s]", type.getName())); |
251 |
|
} |
252 |
|
|
253 |
24 |
chartModel.setAxis(i, axis); |
254 |
|
} |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
@param |
261 |
|
@param |
262 |
|
@throws |
263 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
264 |
12 |
private void setNumberLimits(ValueAxis axis, int index) throws MacroExecutionException... |
265 |
|
{ |
266 |
12 |
try { |
267 |
12 |
if (axisLowerLimit[index] != null) { |
268 |
3 |
Number number = NumberUtils.createNumber(StringUtils.trim(axisLowerLimit[index])); |
269 |
3 |
axis.setLowerBound(number.doubleValue()); |
270 |
|
} |
271 |
12 |
if (axisUpperLimit[index] != null) { |
272 |
3 |
Number number = NumberUtils.createNumber(StringUtils.trim(axisUpperLimit[index])); |
273 |
3 |
axis.setUpperBound(number.doubleValue()); |
274 |
|
} |
275 |
|
} catch (NumberFormatException e) { |
276 |
0 |
throw new MacroExecutionException("Invalid number in axis bound.", e); |
277 |
|
} |
278 |
|
} |
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
@param |
284 |
|
@param |
285 |
|
@throws |
286 |
|
|
|
|
| 41.7% |
Uncovered Elements: 7 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
287 |
5 |
private void setDateLimits(DateAxis axis, int index) throws MacroExecutionException... |
288 |
|
{ |
289 |
5 |
try { |
290 |
5 |
if (axisLowerLimit[index] != null) { |
291 |
0 |
Date date = localeConfiguration.getDateFormat().parse(StringUtils.trim(axisLowerLimit[index])); |
292 |
0 |
axis.setMinimumDate(date); |
293 |
|
} |
294 |
5 |
if (axisUpperLimit[index] != null) { |
295 |
0 |
Date date = localeConfiguration.getDateFormat().parse(StringUtils.trim(axisUpperLimit[index])); |
296 |
0 |
axis.setMaximumDate(date); |
297 |
|
} |
298 |
|
} catch (ParseException e) { |
299 |
0 |
throw new MacroExecutionException("Invalid date in axis bound.", e); |
300 |
|
} |
301 |
|
} |
302 |
|
} |