1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
45 |
|
|
46 |
|
|
47 |
|
@version |
48 |
|
@since |
49 |
|
|
50 |
|
@Component |
51 |
|
@Singleton |
|
|
| 92.3% |
Uncovered Elements: 2 (26) |
Complexity: 6 |
Complexity Density: 0.29 |
|
52 |
|
public class DefaultChartGenerator implements ChartGenerator |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@Inject |
58 |
|
@Named("context") |
59 |
|
private Provider<ComponentManager> contextComponentManager; |
60 |
|
|
61 |
|
@Inject |
62 |
|
private Provider<List<ChartCustomizer>> customizerProvider; |
63 |
|
|
|
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 3 |
Complexity Density: 0.19 |
|
64 |
15 |
@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 |
|
|
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 |
|
|
97 |
|
|
98 |
|
@param |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
100 |
15 |
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 |
|
|
109 |
|
|
110 |
|
@param |
111 |
|
@param |
112 |
|
@param |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
114 |
45 |
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 |
|
} |