package com.curiam.client.componentes;


import com.curiam.client.datos.ResultBeanTPB;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.event.ChartEvent;
import com.extjs.gxt.charts.client.event.ChartListener;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.charts.PieChart;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;

//gráfica de tarta de la libreria GXT
public class BasicChartExample extends LayoutContainer {
	
	ResultBeanTPB rbi = new ResultBeanTPB();
	
	public BasicChartExample(ResultBeanTPB rb){
		
		for(int i=0;i<rb.getProbabilities().size();i++){
			if(rb.getLegends().get(i).equals("nulo")) break;
			rbi.setProbabilities(rb.getProbabilities().get(i));
			rbi.setLabels(rb.getLabels().get(i));
			rbi.setLegends(rb.getLegends().get(i));
		}	
	}

  private ChartListener listener = new ChartListener() {
    
    public void chartClick(ChartEvent ce) {
      Info.display("Chart Clicked", "You selected {0}.", "" + ce.getValue());
    }
  };
  
  public void onRender(Element parent, int index) {
    super.onRender(parent, index);
    setLayout(new FlowLayout(5)); 
    ContentPanel cp = new ContentPanel();
    cp.setHeading("Probabilidad");
    cp.setFrame(false);
    cp.setSize(300, 220);
    cp.setLayout(new FitLayout());
  
    
  
    String url = "gxt/chart/open-flash-chart.swf";
    
    final Chart chart = new Chart(url);
    chart.setBorders(true);
    chart.setChartModel(getPieChartData());
  
    
    cp.add(chart);    

    add(cp);
   
  }

  public ChartModel getPieChartData() {
    ChartModel cm = new ChartModel("",
        "font-size: 14px; font-family: Verdana; text-align: center;");
    cm.setBackgroundColour("#fffff5");
  
    
    PieChart pie = new PieChart();
    pie.setAlpha(0.5f);
    pie.setNoLabels(false);
    pie.setTooltip("#label# <br> #percent#");
    pie.setColours("#006600", "#0033cc","#ff0000","#ffcc00");
   
    for(int i=0;i<rbi.getProbabilities().size();i++)
    	pie.addSlices(new PieChart.Slice(rbi.getProbabilities().get(i), rbi.getLegends().get(i) + '(' + NumberFormat.getFormat("00.0").format(rbi.getProbabilities().get(i)*100) + ')' , rbi.getLegends().get(i)));

    pie.addChartListener(listener);

    cm.addChartConfig(pie);
    return cm;
  }
  
  
  
  
  void main(){
	  System.out.println("Basic char");
  }
}
 