/* * 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
. * * 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 java.util.Arrays; import java.util.Comparator; import com.smartgwt.client.data.Criteria; import com.smartgwt.client.data.DSCallback; import com.smartgwt.client.data.DSRequest; import com.smartgwt.client.data.DSResponse; import com.smartgwt.client.data.DataSource; import com.smartgwt.client.data.Record; import com.smartgwt.client.types.Alignment; import com.smartgwt.client.types.ChartType; import com.smartgwt.client.types.LabelCollapseMode; import com.smartgwt.client.types.LabelRotationMode; import com.smartgwt.client.types.VerticalAlignment; import com.smartgwt.client.widgets.Canvas; import com.smartgwt.client.widgets.Label; import com.smartgwt.client.widgets.chart.FacetChart; import com.smartgwt.client.widgets.cube.Facet; import com.smartgwt.client.widgets.events.MovedEvent; import com.smartgwt.client.widgets.events.MovedHandler; import com.smartgwt.client.widgets.events.ParentMovedHandler; import com.smartgwt.client.widgets.events.ParentMovedEvent; import com.smartgwt.client.widgets.events.ResizedEvent; import com.smartgwt.client.widgets.events.ResizedHandler; import com.smartgwt.client.widgets.form.DynamicForm; import com.smartgwt.client.widgets.form.fields.FormItem; 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.form.fields.events.FormItemInitHandler; import com.smartgwt.client.widgets.grid.ListGridField; import com.smartgwt.client.widgets.layout.VLayout; import com.google.gwt.core.client.EntryPoint; public class StockPricesZoom implements EntryPoint { private static final String LOADING_MESSAGE = "Loading data ..."; private static final String ERROR_MESSAGE = "This test uses sample data provided by Yahoo™ Finance, " + "but Yahoo data is not currently available. Refresh the sample to " + "try again. You may need to wait a while for the Yahoo service to " + "become available again."; private FacetChart stockChart; private SelectItem symbolItem; private StockPricesDataSource stockDs; private Label label; @Override public void onModuleLoad() { DataSource nasdaqSymbols = new StockSymbolDataSource(); stockDs = new StockPricesDataSource(); stockChart = new FacetChart(); stockChart.setID("stockChart"); stockChart.setAutoDraw(false); stockChart.setShowTitle(false); stockChart.setShowDataAxisLabel(false); stockChart.setCanZoom(true); stockChart.setChartType(ChartType.AREA); stockChart.setFacets(new Facet("date", "Day")); stockChart.setValueProperty("close"); stockChart.setLabelCollapseMode(LabelCollapseMode.TIME); stockChart.setMinLabelGap(4); stockChart.setRotateLabels(LabelRotationMode.NEVER); // Add a resized handler to keep the label, if any, in the center // of the chart. stockChart.addResizedHandler(new ResizedHandler() { public void onResized(ResizedEvent event) { StockPricesZoom.this.centerLabel(); } }); stockChart.addMovedHandler(new MovedHandler() { public void onMoved(MovedEvent event) { StockPricesZoom.this.centerLabel(); } }); stockChart.addParentMovedHandler(new ParentMovedHandler() { public void onParentMoved(ParentMovedEvent event) { StockPricesZoom.this.centerLabel(); } }); DynamicForm symbolForm = new DynamicForm(); symbolForm.setNumCols(8); symbolItem = new SelectItem("symbol", "Stock Symbol"); symbolItem.setAutoFetchData(true); symbolItem.setOptionDataSource(nasdaqSymbols); symbolItem.setDefaultToFirstOption(true); symbolItem.setDefaultValue("AAPL"); symbolItem.setPickListWidth(450); symbolItem.setPickListFields(new ListGridField("symbol"), new ListGridField("name")); symbolItem.setDisplayField("name"); symbolItem.setValueField("symbol"); symbolItem.setSortField("symbol"); symbolItem.setInitHandler(new FormItemInitHandler() { @Override public void onInit(FormItem item) { StockPricesZoom.this.updateData(); } }); symbolItem.addChangedHandler(new ChangedHandler() { @Override public void onChanged(ChangedEvent event) { StockPricesZoom.this.updateData(); } }); symbolForm.setItems(symbolItem); VLayout layout = new VLayout(20); layout.setAutoDraw(false); layout.setWidth100(); layout.setHeight100(); layout.setMargin(5); layout.setMembers(symbolForm, stockChart); layout.draw(); } private void centerLabel() { if (label != null) { String widthStr = stockChart.getWidthAsString(); String heightStr = stockChart.getHeightAsString(); float widthRatio = 0.6f, heightRatio = 0.6f; try { int width = Integer.parseInt(widthStr, 10), height = Integer.parseInt(heightStr, 10); label.setTop(Math.round((1 - heightRatio) / 2 * height)); label.setLeft(Math.round((1 - widthRatio) / 2 * width)); label.setHeight(Math.round(heightRatio * height)); label.setWidth(Math.round(widthRatio * width)); } catch (NumberFormatException e) {} } } private void showMessage(String message, boolean alignCenter) { Label label = this.label; if (label == null) { label = this.label = new Label(); label.setAutoDraw(true); label.setContents(""); label.setValign(VerticalAlignment.CENTER); label.setWrap(true); label.setShowEdges(false); stockChart.addChild(label); } label.setContents(message); Alignment alignment = (alignCenter ? Alignment.CENTER : Alignment.LEFT); label.setAlign(alignment); centerLabel(); label.show(); } private void hideMessage() { if (this.label != null) { this.label.hide(); } } protected void updateData() { stockChart.destroyItems(); this.showMessage(LOADING_MESSAGE, true); String symbol = symbolItem.getValueAsString(); stockDs.fetchData(new Criteria("symbol", symbol), new DSCallback() { public void execute(DSResponse response, Object rawData, DSRequest request) { Record[] data = response.getData(); if (data != null && data.length > 0) { StockPricesZoom.this.hideMessage(); stockChart.setTitle(symbolItem.getDisplayValue()); Arrays.sort(data, new Comparator
() { public int compare(Record lhs, Record rhs) { return lhs.getAttributeAsDate("date").compareTo(rhs.getAttributeAsDate("date")); } }); stockChart.setData(data); } else { StockPricesZoom.this.showMessage(ERROR_MESSAGE, false); stockChart.setData(new Record[0]); } } }); } }