package com.chart.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.LegendPosition;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.visualizations.AnnotatedTimeLine;
import com.google.gwt.visualization.client.visualizations.Gauge;
import com.google.gwt.visualization.client.visualizations.IntensityMap;
import com.google.gwt.visualization.client.visualizations.MotionChart;
import com.google.gwt.visualization.client.visualizations.OrgChart;
import com.google.gwt.visualization.client.visualizations.Table;
import com.google.gwt.visualization.client.visualizations.Toolbar;
import com.google.gwt.visualization.client.visualizations.Toolbar.Component;
import com.google.gwt.visualization.client.visualizations.Toolbar.Type;
import com.google.gwt.visualization.client.visualizations.corechart.CoreChart;
import com.google.gwt.visualization.client.visualizations.corechart.LineChart;
import com.google.gwt.visualization.client.visualizations.corechart.PieChart;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Chart implements EntryPoint {
	  static DataTable getDailyActivities() {
		    DataTable data = DataTable.create();
		    data.addColumn(ColumnType.STRING, "Task");
		    data.addColumn(ColumnType.NUMBER, "Hours per Day");
		    data.addRows(5);
		    data.setValue(0, 0, "Work");
		    data.setValue(0, 1, 11);
		    data.setValue(1, 0, "Eat");
		    data.setValue(1, 1, 2);
		    data.setValue(2, 0, "Commute");
		    data.setValue(2, 1, 2);
		    data.setValue(3, 0, "Watch TV");
		    data.setValue(3, 1, 2);
		    data.setValue(4, 0, "Sleep");
		    data.setValue(4, 1, 7);
		    return data;
		  }
	  
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
      final VLayout vp = new VLayout();
      vp.setHeight100();
      vp.setWidth(500);
		
      final HLayout toolBarLayout = new HLayout();
      toolBarLayout.setWidth100();
      toolBarLayout.setHeight(50);
      toolBarLayout.addMember(new Label("Dummy Label"));

      vp.addMember(toolBarLayout);
      
		VisualizationUtils.loadVisualizationApi(new Runnable() {
        public void run() {

          
          vp.addMember(getChartWidget());
          
          toolBarLayout.addMember(getToolbar("dataSource"));
          
          vp.draw();
         
       }
      }, AnnotatedTimeLine.PACKAGE, CoreChart.PACKAGE,
      Gauge.PACKAGE,  IntensityMap.PACKAGE,
      MotionChart.PACKAGE, OrgChart.PACKAGE,
      Table.PACKAGE
     );
		
	}

	  public Widget getChartWidget() {
		    /* create a datatable */
		    DataTable data = Chart.getDailyActivities();

		    /* create pie chart */
		    PieChart.PieOptions options = PieChart.createPieOptions();
		    options.setWidth(400);
		    options.setHeight(240);
		    options.set3D(true);
		    options.setTitle("My Daily Activities");
		    options.setLegend(LegendPosition.LEFT);

		    LineChart result = new LineChart(data, options);
		    
		    
		    VLayout layout = new VLayout(10);
		    layout.addMember(result);
		    
			String dataSource = "http://";


			layout.addMember(getToolbar(dataSource));
			
		    return layout;
		  }
	  
	private Toolbar getToolbar(String dataSource){
		
		Toolbar toolbar = new Toolbar();
		toolbar.setWidth("220px");
		toolbar.setHeight("20px");
		String GADGET = "http://www.google.com/ig/modules/line-chart.xml";

		Component component = Component.create();
		component.setType(Type.CSV);
		component.setDataSource(dataSource);
		toolbar.addComponent(component);

		component = Component.create();
		component.setType(Type.HTML);
		component.setDataSource(dataSource);
		toolbar.addComponent(component);

		component = Component.create();
		component.setType(Type.IGOOGLE);
		component.setGadget(GADGET);
		component.setDataSource(dataSource);
		toolbar.addComponent(component);  
		
		
		return toolbar;
		
	}	
	}
