/* 
 * Isomorphic SmartGWT web presentation layer 
 * Copyright 2000 and beyond Isomorphic Software, Inc. 
 * 
 * OWNERSHIP NOTICE 
 * Isomorphic Software owns and reserves all rights not expressly granted in this source code, 
 * including all intellectual property rights to the structure, sequence, and format of this code 
 * and to all designs, interfaces, algorithms, schema, protocols, and inventions expressed herein. 
 * 
 *  If you have any questions, please email <sourcecode@isomorphic.com>. 
 * 
 *  This entire comment must accompany any portion of Isomorphic Software source code that is 
 *  copied or moved from this file. 
 */

import com.smartgwt.client.types.ChartType;
import com.smartgwt.client.types.DataLineType;
import com.smartgwt.client.widgets.chart.FacetChart;
import com.smartgwt.client.widgets.cube.Facet;
import com.smartgwt.client.widgets.cube.FacetValue;
import com.smartgwt.client.widgets.drawing.DrawLine;
import com.smartgwt.client.widgets.drawing.DrawRect;
import com.smartgwt.client.widgets.drawing.Shadow;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class ScatterPlotChart {
	private VLayout layout = null;

	ScatterPlotChart() {
		final FacetChart chart = new FacetChart();
		chart.setData(ScatterPlotChartData.getData());
		chart.setID("multiScatterChart");
		chart.setWidth(300);
		Facet metric = new Facet();
		metric.setValues(new FacetValue("value"), new FacetValue("Time"));
		metric.setInlinedValues(true);
		metric.setId("metric");
		Facet animal = new Facet();
		animal.setId("animal");

		chart.setFacets(metric, animal);
		chart.setValueProperty("value");
		chart.setChartType(ChartType.SCATTER);
		chart.setTitle("Multi Scatter");
		chart.setShowScatterLines(true);
		chart.setDataLineType(DataLineType.SMOOTH);

		DynamicForm typeForm = new DynamicForm();
		typeForm.setTitleWidth(120);
		typeForm.setWidth(220);
		final SelectItem linesTypeItem = new SelectItem("linesType",
				"Show Lines");
		linesTypeItem.setDefaultValue("Smooth");
		linesTypeItem.setValueMap("None", "Straight", "Smooth");
		linesTypeItem.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				if ("None".equals(linesTypeItem.getValue())) {
					chart.setShowScatterLines(false);
				} else {
					if ("Smooth".equals(linesTypeItem.getValue())) {
						chart.setDataLineType(DataLineType.SMOOTH);
					} else {
						chart.setDataLineType(DataLineType.STRAIGHT);
					}
					chart.setShowScatterLines(true);
				}
			}
		});
		typeForm.setItems(linesTypeItem);

		updateChart(chart);
		layout = new VLayout(15);
		layout.setMembers(typeForm, chart);

		layout.draw();
	}

	public VLayout getChartLayout() {
		return layout;
	}

	private void updateChart(FacetChart chart) {
		DrawRect rect = new DrawRect();
		rect.setLineColor("#ffffff");
		chart.setLegendSwatchProperties(rect);

		DrawLine line = chart.getDataLineProperties();
		line.setShadow(new Shadow());
		chart.setDataLineProperties(line);

		chart.setData(ScatterPlotChartData.getData()); //We would like to change our data later.
	}
}
