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 / CreateChartFunction.java @ 46065
History | View | Annotate | Download (3.77 KB)
1 | 44138 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|---|---|
2 | 43512 | jjdelcerro | |
3 | 46052 | omartinez | import java.awt.Dimension; |
4 | import java.awt.Graphics2D; |
||
5 | import java.awt.Image; |
||
6 | import java.awt.image.BufferedImage; |
||
7 | import javax.json.JsonArray; |
||
8 | import javax.json.JsonObject; |
||
9 | import javax.json.JsonValue; |
||
10 | 43512 | jjdelcerro | import org.apache.commons.lang3.Range; |
11 | 46052 | omartinez | import org.apache.commons.lang3.StringUtils; |
12 | 44138 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
13 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
14 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
15 | 46052 | omartinez | import org.gvsig.expressionevaluator.spi.JsonUtils; |
16 | import org.gvsig.tools.swing.api.SimpleImage; |
||
17 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
18 | import org.jfree.chart.ChartFactory; |
||
19 | import org.jfree.chart.ChartPanel; |
||
20 | import org.jfree.chart.JFreeChart; |
||
21 | import org.jfree.chart.plot.PlotOrientation; |
||
22 | import org.jfree.data.category.DefaultCategoryDataset; |
||
23 | 43512 | jjdelcerro | |
24 | 46052 | omartinez | public class CreateChartFunction extends AbstractFunction { |
25 | 43512 | jjdelcerro | |
26 | 46052 | omartinez | public CreateChartFunction() {
|
27 | 43512 | jjdelcerro | super(
|
28 | 44138 | jjdelcerro | Function.GROUP_PROGRAMMING, |
29 | 46052 | omartinez | "CREATE_CHART",
|
30 | 44269 | omartinez | Range.is(1),
|
31 | 44138 | jjdelcerro | "",
|
32 | 46052 | omartinez | "CREATE_CHART"+"({{string}})", |
33 | 44138 | jjdelcerro | null,
|
34 | 46052 | omartinez | "SimpleImage",
|
35 | 44207 | jjdelcerro | false
|
36 | 43512 | jjdelcerro | ); |
37 | } |
||
38 | 44138 | jjdelcerro | |
39 | 43512 | jjdelcerro | @Override
|
40 | 44009 | jjdelcerro | public boolean allowConstantFolding() { |
41 | 44138 | jjdelcerro | return false; |
42 | } |
||
43 | 44009 | jjdelcerro | |
44 | @Override
|
||
45 | 44138 | jjdelcerro | public Object call(Interpreter interpreter, final Object[] args) throws Exception { |
46 | 46052 | omartinez | Object obj = getObject(args, 0); |
47 | if( obj == null ) { |
||
48 | return null; |
||
49 | //throw new ExpressionRuntimeException("Null is not valid in "+FUNCTION_GETITEM+" function.");
|
||
50 | } |
||
51 | 44269 | omartinez | |
52 | 46052 | omartinez | if( !(obj instanceof JsonObject )) { |
53 | obj = JsonUtils.toJsonObject(obj); |
||
54 | } |
||
55 | |||
56 | JsonObject jsonobj = ((JsonObject)obj); |
||
57 | SimpleImage graph = ToolsSwingLocator.getToolsSwingManager().createSimpleImage(createGraph(jsonobj)); |
||
58 | return graph;
|
||
59 | |||
60 | 43512 | jjdelcerro | } |
61 | 44138 | jjdelcerro | |
62 | 43512 | jjdelcerro | |
63 | 46052 | omartinez | public Image createGraph(JsonObject json) { |
64 | String chartType = json.getString("chartType").toLowerCase(); |
||
65 | switch(chartType){
|
||
66 | case "barchart": { |
||
67 | JsonArray datasetJson = json.getJsonArray("dataset");
|
||
68 | DefaultCategoryDataset ds = new DefaultCategoryDataset();
|
||
69 | for (JsonValue jsonValue : datasetJson) {
|
||
70 | JsonArray element = (JsonArray) jsonValue; |
||
71 | double value = element.getJsonNumber(0).doubleValue(); |
||
72 | String field1 = element.getString(1, null); |
||
73 | String field2 = element.getString(2, null); |
||
74 | ds.addValue(value, field1, field2); |
||
75 | } |
||
76 | JFreeChart barChart = ChartFactory.createBarChart( |
||
77 | json.getString("title", ""), |
||
78 | json.getString("categoryAxisLabel",""), |
||
79 | json.getString("valueAxisLabel",""), |
||
80 | ds, |
||
81 | StringUtils.equalsIgnoreCase(json.getString("orientation",null),"HORIZONTAL")? PlotOrientation.HORIZONTAL:PlotOrientation.VERTICAL, |
||
82 | json.getBoolean("legend", true), |
||
83 | false,
|
||
84 | false);
|
||
85 | |||
86 | ChartPanel chartPanel = new ChartPanel(barChart);
|
||
87 | chartPanel.setPreferredSize(new Dimension( json.getInt("witdh",360) , json.getInt("height",200) )); |
||
88 | 46065 | omartinez | Dimension d = chartPanel.getPreferredSize();
|
89 | 46052 | omartinez | BufferedImage image = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB); |
90 | Graphics2D g2d = image.createGraphics();
|
||
91 | chartPanel.print( g2d ); |
||
92 | g2d.dispose(); |
||
93 | return image;
|
||
94 | } |
||
95 | default:
|
||
96 | throw new IllegalArgumentException("Unknown chart type:" + chartType); |
||
97 | |||
98 | |||
99 | } |
||
100 | } |
||
101 | |||
102 | 43512 | jjdelcerro | } |