Revision 46067
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 | ||
---|---|---|
1 | 1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
2 | 2 |
|
3 | 3 |
import java.awt.Dimension; |
4 |
import java.awt.Graphics2D; |
|
5 |
import java.awt.image.BufferedImage; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.HashMap; |
|
6 |
import java.util.List; |
|
7 |
import java.util.Objects; |
|
6 | 8 |
import javax.json.JsonArray; |
7 | 9 |
import javax.json.JsonObject; |
8 | 10 |
import javax.json.JsonValue; |
9 | 11 |
import org.apache.commons.lang3.Range; |
10 | 12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.apache.commons.lang3.math.NumberUtils; |
|
11 | 14 |
import org.gvsig.expressionevaluator.Function; |
12 | 15 |
import org.gvsig.expressionevaluator.Interpreter; |
13 | 16 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
14 | 17 |
import org.gvsig.expressionevaluator.spi.JsonUtils; |
18 |
import org.gvsig.tools.util.GetItemByKey; |
|
19 |
import org.gvsig.tools.util.GetItemWithSize; |
|
15 | 20 |
import org.jfree.chart.ChartFactory; |
16 | 21 |
import org.jfree.chart.ChartPanel; |
17 |
import org.jfree.chart.ChartUtilities; |
|
18 | 22 |
import org.jfree.chart.JFreeChart; |
19 | 23 |
import org.jfree.chart.plot.PlotOrientation; |
20 | 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; |
|
21 | 28 |
|
22 | 29 |
public class CreateChartPanelFunction extends AbstractFunction { |
23 | 30 |
|
24 |
public CreateChartPanelFunction() { |
|
25 |
super( |
|
26 |
Function.GROUP_PROGRAMMING, |
|
27 |
"CREATE_CHARTPANEL", |
|
28 |
Range.is(1), |
|
29 |
"", |
|
30 |
"CREATE_CHARTPANEL"+"({{string}})", |
|
31 |
null, |
|
32 |
"ChartPanel", |
|
33 |
false |
|
34 |
); |
|
35 |
} |
|
36 |
|
|
37 |
@Override |
|
38 |
public boolean allowConstantFolding() { |
|
39 |
return false; |
|
40 |
} |
|
41 |
|
|
42 |
@Override |
|
43 |
public Object call(Interpreter interpreter, final Object[] args) throws Exception { |
|
44 |
Object obj = getObject(args, 0); |
|
45 |
if( obj == null ) { |
|
46 |
return null; |
|
47 |
//throw new ExpressionRuntimeException("Null is not valid in "+FUNCTION_GETITEM+" function."); |
|
48 |
} |
|
49 |
|
|
50 |
if( !(obj instanceof JsonObject )) { |
|
51 |
obj = JsonUtils.toJsonObject(obj); |
|
52 |
} |
|
53 |
|
|
54 |
JsonObject jsonobj = ((JsonObject)obj); |
|
55 |
ChartPanel graph = createGraph(jsonobj); |
|
56 |
return graph; |
|
57 |
|
|
58 |
} |
|
59 |
|
|
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 |
} |
|
60 | 43 |
|
61 |
public ChartPanel createGraph(JsonObject json) { |
|
62 |
String chartType = json.getString("chartType").toLowerCase(); |
|
63 |
switch(chartType){ |
|
64 |
case "barchart": { |
|
65 |
JsonArray datasetJson = json.getJsonArray("dataset"); |
|
66 |
DefaultCategoryDataset ds = new DefaultCategoryDataset(); |
|
67 |
for (JsonValue jsonValue : datasetJson) { |
|
68 |
JsonArray element = (JsonArray) jsonValue; |
|
69 |
double value = element.getJsonNumber(0).doubleValue(); |
|
70 |
String field1 = element.getString(1, null); |
|
71 |
String field2 = element.getString(2, null); |
|
72 |
ds.addValue(value, field1, field2); |
|
73 |
} |
|
74 |
JFreeChart barChart = ChartFactory.createBarChart( |
|
75 |
json.getString("title", ""), |
|
76 |
json.getString("categoryAxisLabel",""), |
|
77 |
json.getString("valueAxisLabel",""), |
|
78 |
ds, |
|
79 |
StringUtils.equalsIgnoreCase(json.getString("orientation",null),"HORIZONTAL")? PlotOrientation.HORIZONTAL:PlotOrientation.VERTICAL, |
|
80 |
json.getBoolean("legend", true), |
|
81 |
false, |
|
82 |
false); |
|
83 |
|
|
84 |
ChartPanel chartPanel = new ChartPanel(barChart); |
|
85 |
chartPanel.setPreferredSize(new Dimension( json.getInt("witdh",360) , json.getInt("height",200) )); |
|
86 |
return chartPanel; |
|
87 |
} |
|
88 |
default: |
|
89 |
throw new IllegalArgumentException("Unknown chart type:" + chartType); |
|
90 |
|
|
91 |
|
|
92 |
} |
|
93 |
} |
|
44 |
@Override |
|
45 |
public boolean allowConstantFolding() { |
|
46 |
return false; |
|
47 |
} |
|
94 | 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 |
|
|
95 | 175 |
} |
Also available in: Unified diff