Index: 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
===================================================================
--- 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	(revision 46066)
+++ 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	(revision 46067)
@@ -1,95 +1,175 @@
 package org.gvsig.expressionevaluator.impl.function.programming;
 
 import java.awt.Dimension;
-import java.awt.Graphics2D;
-import java.awt.image.BufferedImage;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Objects;
 import javax.json.JsonArray;
 import javax.json.JsonObject;
 import javax.json.JsonValue;
 import org.apache.commons.lang3.Range;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
 import org.gvsig.expressionevaluator.Function;
 import org.gvsig.expressionevaluator.Interpreter;
 import org.gvsig.expressionevaluator.spi.AbstractFunction;
 import org.gvsig.expressionevaluator.spi.JsonUtils;
+import org.gvsig.tools.util.GetItemByKey;
+import org.gvsig.tools.util.GetItemWithSize;
 import org.jfree.chart.ChartFactory;
 import org.jfree.chart.ChartPanel;
-import org.jfree.chart.ChartUtilities;
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.data.category.DefaultCategoryDataset;
+import org.jfree.data.statistics.BoxAndWhiskerItem;
+import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;
+import org.jfree.data.statistics.DefaultBoxAndWhiskerXYDataset;
 
 public class CreateChartPanelFunction extends AbstractFunction {
 
-    public CreateChartPanelFunction() {
-        super(
-            Function.GROUP_PROGRAMMING, 
-            "CREATE_CHARTPANEL", 
-            Range.is(1),
-            "",
-            "CREATE_CHARTPANEL"+"({{string}})",
-            null,
-            "ChartPanel",
-            false
-        );
-    }
-    
-    @Override
-    public boolean allowConstantFolding() {
-        return false;
-    }    
-    
-    @Override
-    public Object call(Interpreter interpreter, final Object[] args) throws Exception {
-        Object obj = getObject(args, 0);
-        if( obj == null ) {
-            return null;
-            //throw new ExpressionRuntimeException("Null is not valid in "+FUNCTION_GETITEM+" function.");
-        }
-        
-        if( !(obj instanceof JsonObject )) {
-            obj = JsonUtils.toJsonObject(obj);
-        }
-        
-        JsonObject jsonobj = ((JsonObject)obj);
-        ChartPanel graph = createGraph(jsonobj);
-        return graph;
-        
-    }
-    
+	public CreateChartPanelFunction() {
+		super(
+			Function.GROUP_PROGRAMMING,
+			"CREATE_CHARTPANEL",
+			Range.between(1, 2),
+			"",
+			"CREATE_CHARTPANEL" + "({{string}})",
+			null,
+			"ChartPanel",
+			false
+		);
+	}
 
-    public ChartPanel createGraph(JsonObject json) {
-        String chartType = json.getString("chartType").toLowerCase();
-        switch(chartType){
-            case "barchart": {
-                JsonArray datasetJson = json.getJsonArray("dataset");
-                DefaultCategoryDataset ds = new DefaultCategoryDataset();
-                for (JsonValue jsonValue : datasetJson) {
-                    JsonArray element = (JsonArray) jsonValue;
-                    double value = element.getJsonNumber(0).doubleValue();
-                    String field1 = element.getString(1, null);
-                    String field2 = element.getString(2, null);
-                    ds.addValue(value, field1, field2);
-                }
-                JFreeChart barChart = ChartFactory.createBarChart(
-                            json.getString("title", ""),
-                            json.getString("categoryAxisLabel",""),
-                            json.getString("valueAxisLabel",""),
-                            ds,
-                            StringUtils.equalsIgnoreCase(json.getString("orientation",null),"HORIZONTAL")? PlotOrientation.HORIZONTAL:PlotOrientation.VERTICAL,
-                            json.getBoolean("legend", true),
-                            false,
-                            false);
-		
-                ChartPanel chartPanel = new ChartPanel(barChart);
-                chartPanel.setPreferredSize(new Dimension( json.getInt("witdh",360) , json.getInt("height",200) ));
-                return chartPanel;
-            }
-            default: 
-                throw new IllegalArgumentException("Unknown chart type:" + chartType);
-                
-                
-        }
-    }
+	@Override
+	public boolean allowConstantFolding() {
+		return false;
+	}
 
+	@Override
+	public Object call(Interpreter interpreter, final Object[] args) throws Exception {
+		Object obj = getObject(args, 0);
+		if (obj == null) {
+			return null;
+			//throw new ExpressionRuntimeException("Null is not valid in "+FUNCTION_GETITEM+" function.");
+		}
+
+		if (!(obj instanceof JsonObject)) {
+			obj = JsonUtils.toJsonObject(obj);
+		}
+		Object obj1 = getObject(args, 1);
+		GetItemWithSize dataset = null;
+		if ((obj1 instanceof GetItemWithSize)) {
+			dataset = ((GetItemWithSize) obj1);
+		}
+
+		JsonObject jsonobj = ((JsonObject) obj);
+		ChartPanel graph = createGraph(jsonobj, dataset);
+		return graph;
+
+	}
+
+	public ChartPanel createGraph(JsonObject json, GetItemWithSize<GetItemByKey> dataset) {
+		String chartType = json.getString("chartType").toLowerCase();
+		switch (chartType) {
+			case "boxandwhisker": {
+				String valueField = json.getString("valueField");
+				HashMap<String, HashMap<String, List<Number>>> tree = new HashMap<String, HashMap<String, List<Number>>>();
+				String field1 = json.getString("field1");
+				String field2 = json.getString("field2");
+				for (int i = 0; i < dataset.size(); i++) {
+					GetItemByKey element = dataset.get(i);
+					String rowValue = Objects.toString(element.get(field1), null);
+					String colValue = Objects.toString(element.get(field2), null);
+					double elementValue = NumberUtils.toDouble(Objects.toString(element.get(valueField), null));
+
+					if (tree.containsKey(rowValue)) {
+						HashMap<String, List<Number>> colTree = tree.get(rowValue);
+						if (colTree.containsKey(colValue)) {
+							colTree.get(colValue).add(elementValue);
+						} else {
+							ArrayList<Number> list = new ArrayList<Number>();
+							list.add(elementValue);
+							colTree.put(colValue, list);
+						}
+					} else {
+						HashMap<String, List<Number>> e = new HashMap<String, List<Number>>();
+						ArrayList<Number> list = new ArrayList<Number>();
+						list.add(elementValue);
+						e.put(colValue, list);
+						tree.put(rowValue, e);
+					}
+				}
+				DefaultBoxAndWhiskerCategoryDataset ds = new DefaultBoxAndWhiskerCategoryDataset();
+				for (String rowKey : tree.keySet()) {
+					for (String colKey : tree.get(rowKey).keySet()) {
+						ds.add(tree.get(rowKey).get(colKey), rowKey, colKey);
+					}
+				}
+				try {
+
+				} catch (Exception ex) {
+					throw new RuntimeException("Not able to retrieve values", ex);
+				}
+
+				JFreeChart boxAndWhiskerChart = ChartFactory.createBoxAndWhiskerChart(
+					json.getString("title", ""),
+					json.getString("categoryAxisLabel", ""),
+					json.getString("valueAxisLabel", ""),
+					ds,
+					json.getBoolean("legend", true));
+
+				ChartPanel chartPanel = new ChartPanel(boxAndWhiskerChart);
+				chartPanel.setPreferredSize(new Dimension(json.getInt("witdh", 360), json.getInt("height", 200)));
+				return chartPanel;
+			}
+
+			case "barchart": {
+				DefaultCategoryDataset ds = new DefaultCategoryDataset();
+				if (dataset == null) {
+					JsonArray datasetJson = json.getJsonArray("dataset");
+					for (JsonValue jsonValue : datasetJson) {
+						JsonArray element = (JsonArray) jsonValue;
+						double value = element.getJsonNumber(0).doubleValue();
+						String field1 = element.getString(1, null);
+						String field2 = element.getString(2, null);
+						ds.addValue(value, field1, field2);
+					}
+				} else {
+					String valueField = json.getString("valueField");
+					String field1 = json.getString("field1");
+					String field2 = json.getString("field2");
+					for (int i = 0; i < dataset.size(); i++) {
+						GetItemByKey element = dataset.get(i);
+
+						try {
+							ds.addValue(NumberUtils.toDouble(Objects.toString(element.get(valueField), null), Double.NaN),
+								Objects.toString(element.get(field1), null),
+								Objects.toString(element.get(field2), null));
+						} catch (Exception ex) {
+							throw new RuntimeException("Not able to retrieve values", ex);
+						}
+					}
+
+				}
+				JFreeChart barChart = ChartFactory.createBarChart(
+					json.getString("title", ""),
+					json.getString("categoryAxisLabel", ""),
+					json.getString("valueAxisLabel", ""),
+					ds,
+					StringUtils.equalsIgnoreCase(json.getString("orientation", null), "HORIZONTAL") ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL,
+					json.getBoolean("legend", true),
+					false,
+					false);
+
+				ChartPanel chartPanel = new ChartPanel(barChart);
+				chartPanel.setPreferredSize(new Dimension(json.getInt("witdh", 360), json.getInt("height", 200)));
+				return chartPanel;
+			}
+			default:
+				throw new IllegalArgumentException("Unknown chart type:" + chartType);
+
+		}
+	}
+
 }
