Statistics
| Revision:

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 @ 46052

History | View | Annotate | Download (3.44 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.BufferedImage;
6
import javax.json.JsonArray;
7
import javax.json.JsonObject;
8
import javax.json.JsonValue;
9 43512 jjdelcerro
import org.apache.commons.lang3.Range;
10 46052 omartinez
import org.apache.commons.lang3.StringUtils;
11 44138 jjdelcerro
import org.gvsig.expressionevaluator.Function;
12 43521 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
13 43512 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractFunction;
14 46052 omartinez
import org.gvsig.expressionevaluator.spi.JsonUtils;
15
import org.jfree.chart.ChartFactory;
16
import org.jfree.chart.ChartPanel;
17
import org.jfree.chart.ChartUtilities;
18
import org.jfree.chart.JFreeChart;
19
import org.jfree.chart.plot.PlotOrientation;
20
import org.jfree.data.category.DefaultCategoryDataset;
21 43512 jjdelcerro
22 46052 omartinez
public class CreateChartPanelFunction extends AbstractFunction {
23 43512 jjdelcerro
24 46052 omartinez
    public CreateChartPanelFunction() {
25 43512 jjdelcerro
        super(
26 44138 jjdelcerro
            Function.GROUP_PROGRAMMING,
27 46052 omartinez
            "CREATE_CHARTPANEL",
28 44269 omartinez
            Range.is(1),
29 44138 jjdelcerro
            "",
30 46052 omartinez
            "CREATE_CHARTPANEL"+"({{string}})",
31 44138 jjdelcerro
            null,
32 46052 omartinez
            "ChartPanel",
33 44207 jjdelcerro
            false
34 43512 jjdelcerro
        );
35
    }
36 44138 jjdelcerro
37 43512 jjdelcerro
    @Override
38 44009 jjdelcerro
    public boolean allowConstantFolding() {
39 44138 jjdelcerro
        return false;
40
    }
41 44009 jjdelcerro
42
    @Override
43 44138 jjdelcerro
    public Object call(Interpreter interpreter, final Object[] args) throws Exception {
44 46052 omartinez
        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 44269 omartinez
50 46052 omartinez
        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 43512 jjdelcerro
    }
59 44138 jjdelcerro
60 43512 jjdelcerro
61 46052 omartinez
    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
    }
94
95 43512 jjdelcerro
}