1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro.chart.source

File AxisConfigurator.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

40
81
8
1
302
161
34
0.42
10.12
8
4.25

Classes

Class Line # Actions
AxisConfigurator 47 81 0% 34 28
0.7829457578.3%
 

Contributing tests

This file is covered by 15 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * A configurator object for the axes.
41    *
42    * This super class provides basic parameter validation.
43    *
44    * @version $Id: 55363825ab755a236151a92e1d714116144dbbce $
45    * @since 4.2M1
46    */
 
47    public class AxisConfigurator extends AbstractConfigurator
48    {
49    /**
50    * The name of the domain axis parameter.
51    */
52    public static final String DOMAIN_AXIS_PARAM = "domain_axis_type";
53   
54    /**
55    * The name of the range axis parameter.
56    */
57    public static final String RANGE_AXIS_PARAM = "range_axis_type";
58   
59    /**
60    * The name of the domain axis date format parameter. Used only if the axis type is date.
61    */
62    public static final String DOMAIN_AXIS_DATE_FORMAT_PARAM = "domain_axis_date_format";
63   
64    /**
65    * The name of the range axis date format parameter. Used only if the axis type is date.
66    */
67    public static final String RANGE_AXIS_DATE_FORMAT_PARAM = "range_axis_date_format";
68   
69    /**
70    * The lower limit on the domain axis.
71    */
72    public static final String DOMAIN_AXIS_LOWER_PARAM = "domain_axis_lower";
73   
74    /**
75    * The upper limit on the domain axis.
76    */
77    public static final String DOMAIN_AXIS_UPPER_PARAM = "domain_axis_upper";
78   
79    /**
80    * The lower limit on the range axis.
81    */
82    public static final String RANGE_AXIS_LOWER_PARAM = "range_axis_lower";
83   
84    /**
85    * The upper limit on the range axis.
86    */
87    public static final String RANGE_AXIS_UPPER_PARAM = "range_axis_upper";
88   
89    /**
90    * The configured axis types.
91    */
92    private AxisType[] axisTypes = {null, null, null};
93   
94    /**
95    * The configured axis date formats.
96    */
97    private String[] axisDateFormat = {null, null, null};
98   
99    /**
100    * The locale configuration.
101    */
102    private final LocaleConfiguration localeConfiguration;
103   
104    /**
105    * The lower limits.
106    */
107    private String[] axisLowerLimit = {null, null, null};
108   
109    /**
110    * The upper limits.
111    */
112    private String[] axisUpperLimit = {null, null, null};
113   
114    /**
115    * @param localeConfiguration The locale configuration.
116    */
 
117  25 toggle public AxisConfigurator(LocaleConfiguration localeConfiguration)
118    {
119  25 this.localeConfiguration = localeConfiguration;
120    }
121   
 
122  137 toggle @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    * Claime limit parameters.
152    *
153    * @param key the parameter name.
154    * @param value the parameter value.
155    * @return {@code true} if the parameter was claimed.
156    */
 
157  129 toggle 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    * Set the axis type for the axis at the given index.
178    *
179    * @param index The index.
180    * @param axisType the axis type.
181    */
 
182  5 toggle private void setAxisType(int index, AxisType axisType)
183    {
184  5 axisTypes[index] = axisType;
185    }
186   
187    /**
188    * Set the date format for the axis at the given index.
189    *
190    * @param index The index.
191    * @param dateFormatString the date format.
192    * @throws MacroExecutionException if the date format string is invalid.
193    */
 
194  3 toggle private void setAxisDateFormat(int index, String dateFormatString) throws MacroExecutionException
195    {
196  3 axisDateFormat[index] = dateFormatString;
197    }
198   
199    /**
200    * Set the axes in the chart model.
201    *
202    * @param plotType The target plot type.
203    * @param chartModel The target chart model.
204    * @throws MacroExecutionException if the axes are incorrectly configured.
205    */
 
206  21 toggle 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    * Set the limits of a number axis.
259    *
260    * @param axis The axis.
261    * @param index The index of the axis
262    * @throws MacroExecutionException if the parameters could not be parsed as numbers.
263    */
 
264  12 toggle 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    * Set the limits of a date axis.
282    *
283    * @param axis The axis.
284    * @param index The index of the axis.
285    * @throws MacroExecutionException if the parameters could not be parsed as dates.
286    */
 
287  5 toggle 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    }