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

File DefaultChartGenerator.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
21
3
1
120
66
6
0.29
7
3
2

Classes

Class Line # Actions
DefaultChartGenerator 52 21 0% 6 2
0.923076992.3%
 

Contributing tests

This file is covered by 8 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.chart.internal;
21   
22    import java.io.IOException;
23    import java.util.List;
24    import java.util.Map;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.jfree.chart.ChartUtilities;
32    import org.jfree.chart.JFreeChart;
33    import org.jfree.chart.plot.Plot;
34    import org.xwiki.chart.ChartCustomizer;
35    import org.xwiki.chart.ChartGenerator;
36    import org.xwiki.chart.ChartGeneratorException;
37    import org.xwiki.chart.internal.plot.PlotGenerator;
38    import org.xwiki.chart.model.ChartModel;
39    import org.xwiki.component.annotation.Component;
40    import org.xwiki.component.manager.ComponentLookupException;
41    import org.xwiki.component.manager.ComponentManager;
42   
43    /**
44    * The default implementation of {@link ChartGenerator} component interface which utilizes the JFreeChart charting
45    * library.
46    *
47    * @version $Id: 07b85af85828ad693a871decbab950e14cb7d73a $
48    * @since 2.0M1
49    */
50    @Component
51    @Singleton
 
52    public class DefaultChartGenerator implements ChartGenerator
53    {
54    /**
55    * Used to lookup plot generators dynamically.
56    */
57    @Inject
58    @Named("context")
59    private Provider<ComponentManager> contextComponentManager;
60   
61    @Inject
62    private Provider<List<ChartCustomizer>> customizerProvider;
63   
 
64  15 toggle @Override
65    public byte[] generate(ChartModel model, Map<String, String> parameters) throws ChartGeneratorException
66    {
67  15 setDefaultParams(parameters);
68  15 String type = parameters.get(TYPE_PARAM);
69  15 String title = parameters.get(TITLE_PARAM);
70   
71  15 PlotGenerator generator;
72  15 try {
73  15 generator = contextComponentManager.get().getInstance(PlotGenerator.class, type);
74    } catch (ComponentLookupException e) {
75  0 throw new ChartGeneratorException(String.format("No such chart type : [%s].", type), e);
76    }
77   
78  15 Plot plot = generator.generate(model, parameters);
79  15 JFreeChart jfchart = new JFreeChart(title, plot);
80   
81    // Run customizations
82  15 for (ChartCustomizer customizer : this.customizerProvider.get()) {
83  30 customizer.customize(jfchart, parameters);
84    }
85   
86  15 int width = Integer.parseInt(parameters.get(WIDTH_PARAM));
87  15 int height = Integer.parseInt(parameters.get(HEIGHT_PARAM));
88  15 try {
89  15 return ChartUtilities.encodeAsPNG(jfchart.createBufferedImage(width, height));
90    } catch (IOException ex) {
91  0 throw new ChartGeneratorException("Error while png encoding the chart image.");
92    }
93    }
94   
95    /**
96    * Sets up default values for certain parameters, if they are not provided.
97    *
98    * @param parameters the parameter set which the values are to be put into
99    */
 
100  15 toggle public void setDefaultParams(Map<String, String> parameters)
101    {
102  15 setParam(SERIES_PARAM, "rows", parameters);
103  15 setParam(WIDTH_PARAM, "400", parameters);
104  15 setParam(HEIGHT_PARAM, "300", parameters);
105    }
106   
107    /**
108    * Sets up values in a map, unless they already exist.
109    *
110    * @param key the key
111    * @param value the corresponding value
112    * @param map the map in which the values are put
113    */
 
114  45 toggle private static void setParam(String key, String value, Map<String, String> map)
115    {
116  45 if (!map.containsKey(key)) {
117  6 map.put(key, value);
118    }
119    }
120    }