/*
 * 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.
 */
package com.smartgwt.sample.showcase.client.chart.zoom;


import com.google.gwt.user.client.Random;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.ChartType;
import com.smartgwt.client.types.LabelCollapseMode;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.chart.ChartPointClickEvent;
import com.smartgwt.client.widgets.chart.ChartPointClickHandler;
import com.smartgwt.client.widgets.chart.ChartPointHoverCustomizer;
import com.smartgwt.client.widgets.chart.FacetChart;
import com.smartgwt.client.widgets.cube.Facet;
import com.smartgwt.sample.showcase.client.ShowcasePanel;
import com.smartgwt.sample.showcase.client.chart.ChartSamplePanelFactory;

public class TimeDataZoom  extends ShowcasePanel {
    private static final String DESCRIPTION = "<p>This example shows about samples points on the y, a time on the x</p>";
    

    public static class Factory extends ChartSamplePanelFactory {

        public String getDescription() {
            return DESCRIPTION;
        }

        @Override
        public String getNodeID() {
            return "TimeDataZoom";
        }

        @Override
        public ShowcasePanel createShowcasePanel() {
            return new TimeDataZoom();
        }
    }


    @Override
    public Canvas getViewPanel() {
        FacetChart chart = new FacetChart();
        chart.setShowTitle(false);
        chart.setWidth100();
        chart.setHeight100();
        chart.setMargin(5);

        chart.setChartType(ChartType.LINE);
        chart.setAutoRotateLabels(false);
        chart.setMinLabelGap(5);
        chart.setLabelCollapseMode(LabelCollapseMode.TIME);
	    chart.setValueTitle("km/h");  
	    
	    
        chart.setData(getData());
	    chart.setFacets(new Facet("time", "TS"), new Facet("region", "region"));
	    chart.setValueProperty("value"); 
	    
        chart.setShowDataPoints(Boolean.TRUE);
        chart.setPointHoverCustomizer(new ChartPointHoverCustomizer() {
            public String hoverHTML(Float value, Record record) {
            	if (record==null) {
                    return ("<b>record is null, but should not</b>");
            	}//end if
            	else {
            		return ("<b>" + record.toString() +"</b>");
            	}
            }
        });

        // Setting chart point click handler  
        chart.setPointClickHandler(new ChartPointClickHandler() {  
            public void onPointClick(ChartPointClickEvent event) {
            	
                // Saving reference to record we are editing, for ease of updating  
                Record rec=event.getRecord();
            	if (rec==null) {
                    SC.say("<b>record is null, but should not</b>");
            	}//end if
            	else {
            		SC.say("<b>" + rec.toString() +"</b>");
            	}                
            }  
        });       
        
        chart.setDecimalPrecision(6);
        chart.setCanZoom(true);
        chart.setZoomLogScale(false);
        chart.setZoomShowSelection(false);
        FacetChart zcp = new FacetChart();
        zcp.setShowInlineLabels(false);
        chart.setZoomChartProperties(zcp);

        return chart;
    }

    @Override
    public String getIntro() {
        return DESCRIPTION;
    }
    
    private Record[] getData() {
        int len = 100;
        Record[] res = new Record[len * 2];
        long startts=System.currentTimeMillis();
        for (int i = 0; i < len; i++) {
            res[i * 2] = new Record();
            int speed= Math.abs(Random.nextInt(200));
            res[i * 2].setAttribute("time", new java.util.Date(startts));
            res[i * 2].setAttribute("value", speed);
            res[i * 2].setAttribute("region", "speed1");

            int speed2= Math.abs(Random.nextInt(200));
            res[i * 2 + 1] = new Record();
            res[i * 2 + 1].setAttribute("time", new java.util.Date(startts));
            res[i * 2 + 1].setAttribute("value", speed2);
            res[i * 2 + 1].setAttribute("region", "speed2");
            
            startts=startts+60*1000;//add a second
        }
        return res;
    }
    
    

}