svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / programming / CreateChartPanelFunction.java @ 46067
History | View | Annotate | Download (5.89 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.util.ArrayList; |
5 |
import java.util.HashMap; |
6 |
import java.util.List; |
7 |
import java.util.Objects; |
8 |
import javax.json.JsonArray; |
9 |
import javax.json.JsonObject; |
10 |
import javax.json.JsonValue; |
11 |
import org.apache.commons.lang3.Range; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.apache.commons.lang3.math.NumberUtils; |
14 |
import org.gvsig.expressionevaluator.Function; |
15 |
import org.gvsig.expressionevaluator.Interpreter; |
16 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
17 |
import org.gvsig.expressionevaluator.spi.JsonUtils; |
18 |
import org.gvsig.tools.util.GetItemByKey; |
19 |
import org.gvsig.tools.util.GetItemWithSize; |
20 |
import org.jfree.chart.ChartFactory; |
21 |
import org.jfree.chart.ChartPanel; |
22 |
import org.jfree.chart.JFreeChart; |
23 |
import org.jfree.chart.plot.PlotOrientation; |
24 |
import org.jfree.data.category.DefaultCategoryDataset; |
25 |
import org.jfree.data.statistics.BoxAndWhiskerItem; |
26 |
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset; |
27 |
import org.jfree.data.statistics.DefaultBoxAndWhiskerXYDataset; |
28 |
|
29 |
public class CreateChartPanelFunction extends AbstractFunction { |
30 |
|
31 |
public CreateChartPanelFunction() {
|
32 |
super(
|
33 |
Function.GROUP_PROGRAMMING, |
34 |
"CREATE_CHARTPANEL",
|
35 |
Range.between(1, 2), |
36 |
"",
|
37 |
"CREATE_CHARTPANEL" + "({{string}})", |
38 |
null,
|
39 |
"ChartPanel",
|
40 |
false
|
41 |
); |
42 |
} |
43 |
|
44 |
@Override
|
45 |
public boolean allowConstantFolding() { |
46 |
return false; |
47 |
} |
48 |
|
49 |
@Override
|
50 |
public Object call(Interpreter interpreter, final Object[] args) throws Exception { |
51 |
Object obj = getObject(args, 0); |
52 |
if (obj == null) { |
53 |
return null; |
54 |
//throw new ExpressionRuntimeException("Null is not valid in "+FUNCTION_GETITEM+" function.");
|
55 |
} |
56 |
|
57 |
if (!(obj instanceof JsonObject)) { |
58 |
obj = JsonUtils.toJsonObject(obj); |
59 |
} |
60 |
Object obj1 = getObject(args, 1); |
61 |
GetItemWithSize dataset = null;
|
62 |
if ((obj1 instanceof GetItemWithSize)) { |
63 |
dataset = ((GetItemWithSize) obj1); |
64 |
} |
65 |
|
66 |
JsonObject jsonobj = ((JsonObject) obj); |
67 |
ChartPanel graph = createGraph(jsonobj, dataset); |
68 |
return graph;
|
69 |
|
70 |
} |
71 |
|
72 |
public ChartPanel createGraph(JsonObject json, GetItemWithSize<GetItemByKey> dataset) {
|
73 |
String chartType = json.getString("chartType").toLowerCase(); |
74 |
switch (chartType) {
|
75 |
case "boxandwhisker": { |
76 |
String valueField = json.getString("valueField"); |
77 |
HashMap<String, HashMap<String, List<Number>>> tree = new HashMap<String, HashMap<String, List<Number>>>(); |
78 |
String field1 = json.getString("field1"); |
79 |
String field2 = json.getString("field2"); |
80 |
for (int i = 0; i < dataset.size(); i++) { |
81 |
GetItemByKey element = dataset.get(i); |
82 |
String rowValue = Objects.toString(element.get(field1), null); |
83 |
String colValue = Objects.toString(element.get(field2), null); |
84 |
double elementValue = NumberUtils.toDouble(Objects.toString(element.get(valueField), null)); |
85 |
|
86 |
if (tree.containsKey(rowValue)) {
|
87 |
HashMap<String, List<Number>> colTree = tree.get(rowValue); |
88 |
if (colTree.containsKey(colValue)) {
|
89 |
colTree.get(colValue).add(elementValue); |
90 |
} else {
|
91 |
ArrayList<Number> list = new ArrayList<Number>(); |
92 |
list.add(elementValue); |
93 |
colTree.put(colValue, list); |
94 |
} |
95 |
} else {
|
96 |
HashMap<String, List<Number>> e = new HashMap<String, List<Number>>(); |
97 |
ArrayList<Number> list = new ArrayList<Number>(); |
98 |
list.add(elementValue); |
99 |
e.put(colValue, list); |
100 |
tree.put(rowValue, e); |
101 |
} |
102 |
} |
103 |
DefaultBoxAndWhiskerCategoryDataset ds = new DefaultBoxAndWhiskerCategoryDataset();
|
104 |
for (String rowKey : tree.keySet()) { |
105 |
for (String colKey : tree.get(rowKey).keySet()) { |
106 |
ds.add(tree.get(rowKey).get(colKey), rowKey, colKey); |
107 |
} |
108 |
} |
109 |
try {
|
110 |
|
111 |
} catch (Exception ex) { |
112 |
throw new RuntimeException("Not able to retrieve values", ex); |
113 |
} |
114 |
|
115 |
JFreeChart boxAndWhiskerChart = ChartFactory.createBoxAndWhiskerChart( |
116 |
json.getString("title", ""), |
117 |
json.getString("categoryAxisLabel", ""), |
118 |
json.getString("valueAxisLabel", ""), |
119 |
ds, |
120 |
json.getBoolean("legend", true)); |
121 |
|
122 |
ChartPanel chartPanel = new ChartPanel(boxAndWhiskerChart);
|
123 |
chartPanel.setPreferredSize(new Dimension(json.getInt("witdh", 360), json.getInt("height", 200))); |
124 |
return chartPanel;
|
125 |
} |
126 |
|
127 |
case "barchart": { |
128 |
DefaultCategoryDataset ds = new DefaultCategoryDataset();
|
129 |
if (dataset == null) { |
130 |
JsonArray datasetJson = json.getJsonArray("dataset");
|
131 |
for (JsonValue jsonValue : datasetJson) {
|
132 |
JsonArray element = (JsonArray) jsonValue; |
133 |
double value = element.getJsonNumber(0).doubleValue(); |
134 |
String field1 = element.getString(1, null); |
135 |
String field2 = element.getString(2, null); |
136 |
ds.addValue(value, field1, field2); |
137 |
} |
138 |
} else {
|
139 |
String valueField = json.getString("valueField"); |
140 |
String field1 = json.getString("field1"); |
141 |
String field2 = json.getString("field2"); |
142 |
for (int i = 0; i < dataset.size(); i++) { |
143 |
GetItemByKey element = dataset.get(i); |
144 |
|
145 |
try {
|
146 |
ds.addValue(NumberUtils.toDouble(Objects.toString(element.get(valueField), null), Double.NaN), |
147 |
Objects.toString(element.get(field1), null),
|
148 |
Objects.toString(element.get(field2), null));
|
149 |
} catch (Exception ex) { |
150 |
throw new RuntimeException("Not able to retrieve values", ex); |
151 |
} |
152 |
} |
153 |
|
154 |
} |
155 |
JFreeChart barChart = ChartFactory.createBarChart( |
156 |
json.getString("title", ""), |
157 |
json.getString("categoryAxisLabel", ""), |
158 |
json.getString("valueAxisLabel", ""), |
159 |
ds, |
160 |
StringUtils.equalsIgnoreCase(json.getString("orientation", null), "HORIZONTAL") ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, |
161 |
json.getBoolean("legend", true), |
162 |
false,
|
163 |
false);
|
164 |
|
165 |
ChartPanel chartPanel = new ChartPanel(barChart);
|
166 |
chartPanel.setPreferredSize(new Dimension(json.getInt("witdh", 360), json.getInt("height", 200))); |
167 |
return chartPanel;
|
168 |
} |
169 |
default:
|
170 |
throw new IllegalArgumentException("Unknown chart type:" + chartType); |
171 |
|
172 |
} |
173 |
} |
174 |
|
175 |
} |