import java.util.Date;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.ChartType;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.chart.FacetChart;
import com.smartgwt.client.widgets.chart.ValueFormatter;
import com.smartgwt.client.widgets.cube.Facet;
import com.smartgwt.client.widgets.cube.FacetValue;

public class AxisFormatDemo extends Window {

	public long minimumTimestamp = Long.MAX_VALUE;
	public long maximumTimestamp = Long.MIN_VALUE;
	
	public AxisFormatDemo() {
		setTitle("Axis Format Test");
		setWidth(500);
		setHeight(400);
		
		Facet metric = new Facet();
		metric.setValues(new FacetValue("value"), new FacetValue("time"));
		metric.setInlinedValues(true);
		metric.setId("metric");
		
		final FacetChart cpuChart = new FacetChart();  
		cpuChart.setWidth100();
		cpuChart.setHeight100();
		cpuChart.setFacets(metric);  
		cpuChart.setValueProperty("value");
		cpuChart.setXAxisMetric("time");
		cpuChart.setChartType(ChartType.SCATTER);  
		cpuChart.setTitle("CPU Utilization");
		cpuChart.setShowValueAxisLabel(false);
		cpuChart.setShowDataAxisLabel(false);
		cpuChart.setShowLegend(false);
		cpuChart.setShowDataPoints(false); 
		cpuChart.setShowScatterLines(true);
		
		// Y-Axis Formatter.
		final NumberFormat numberFormatter = NumberFormat.getFormat("0");
		cpuChart.setAxisValueFormatter(new ValueFormatter() {
			@Override
			public String format(Object value) {
				// n.b. value may be an Integer, Double, Long, or a Float. So use the .toString() to parse it.
				double n = Double.parseDouble(value.toString());
				// Format the percentage.
				return numberFormatter.format(n * 100) + "%";
			}
			
		});
		// X-Axis Formatter. Set format to 23:59 time format.
		final DateTimeFormat dateFormatter = DateTimeFormat.getFormat("HH:mm");
		cpuChart.setXAxisValueFormatter(new ValueFormatter() {
			@Override
			public String format(Object value) {
				double n = Double.parseDouble(value.toString());
				// Calculate and format the time.
				return dateFormatter.format(new Date((long) 
						(n * (maximumTimestamp - minimumTimestamp))
						+ minimumTimestamp)
				);
			}
		});
		// Create test data.
		ChartData chartData[] = new ChartData[] {
			new ChartData(1372894200000l, 0.34f), 
			new ChartData(1372894500000l, 0.32f), 
			new ChartData(1372894800000l, 0.32f), 
			new ChartData(1372895100000l, 0.36f), 
			new ChartData(1372895400000l, 0.39f), 
			new ChartData(1372895700000l, 0.42f), 
			new ChartData(1372896000000l, 0.43f), 
			new ChartData(1372896300000l, 0.40f), 
			new ChartData(1372896600000l, 0.42f), 
			new ChartData(1372896900000l, 0.41f), 
			new ChartData(1372897200000l, 0.39f), 
			new ChartData(1372897500000l, 0.35f)
		};
		// We need to get the records for the chart data.
		Record records[] = new Record[chartData.length];
		for(int i=0; i<chartData.length; i++) {
			records[i] = chartData[i].getRecord();
		}
		// Set the chart data.
		cpuChart.setData(records);
		
		addItem(cpuChart);
	}
	
	private class ChartData extends Record {
		long timestamp;
		float value;
		private ChartData(long timestamp, float value) {
			// Find minimum and maximum timestamp's;
			if (minimumTimestamp > timestamp) {
				minimumTimestamp = timestamp;
			}
			if (maximumTimestamp < timestamp) {
				maximumTimestamp = timestamp;
			}
			this.timestamp = timestamp;
			this.value = value;
		}
		// Create a record.
		private Record getRecord() {
			Record record = new Record();
			// Set time to be a value from 0.0 to 1.0.
			record.setAttribute("time",
					(double) (timestamp - minimumTimestamp) / 
					(maximumTimestamp - minimumTimestamp));
			record.setAttribute("value", value);
			return record;
		}
	}
	
}
