[flex] Flex 遇到一个数据加载的问题
sekiyika
2009-11-17
小弟初学Flex。。。 <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:flash="flash.filters.*" backgroundColor="white" backgroundGradientAlphas="[0.0, 0.0]" fontGridFitType="none" color="#FFFFFF" creationComplete="initApp()" initialize="loadData()"> <mx:Script> <![CDATA[ import mx.rpc.http.HTTPService; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.controls.*; import mx.core.*; import mx.effects.*; import mx.events.DragEvent; [Bindable] private var stockName: String; [Bindable] private static var items : ArrayCollection; [Bindable] private var partialItems: ArrayCollection = new ArrayCollection(); var dataLoader: DataLoader; private function loadData(): void { var stockCode = Application.application.parameters.stockCode; stockName = Application.application.parameters.stockName; dataLoader = new DataLoader(stockCode); } private function initApp() : void { items = dataLoader.getDataItem(); for(var i = 0; i < 50 && i < items.length; i++) { partialItems.addItemAt(items.getItemAt(items.length - 1 - i, 0), 0); } } private function resize(event: MouseEvent): void { if(event.delta > 0){ if(partialItems.length > 4){ for(var i = 0; i < 3; i++){ partialItems.removeItemAt(i); } } } else { for(var i = 0; i < 3; i++){ if(partialItems.length < items.length){ partialItems.addItemAt(items.getItemAt(items.getItemIndex(partialItems.getItemAt(0, 0)) - 1, 0), 0); } } } } var tag = 0; var j: int = 0; private function onMouseDown(event: MouseEvent): void { tag = event.stageX; linechart.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } private function onMouseMove(event: MouseEvent): void { var distance: int = event.stageX - tag; j = distance / 50; if(j < 0){ for(var i = j; i < 0; i++){ if(items.getItemIndex(partialItems.getItemAt(partialItems.length - 1,0 )) != items.length - 1){ partialItems.addItem(items.getItemAt(items.getItemIndex(partialItems.getItemAt(partialItems.length - 1, 0)) + 1)); partialItems.removeItemAt(0); } } } else if(j > 0){ for(var i = 0; i < j; i++){ if(items.getItemIndex(partialItems.getItemAt(0,0)) > 0){ partialItems.addItemAt(items.getItemAt(items.getItemIndex(partialItems.getItemAt(0,0)) - 1), 0); partialItems.removeItemAt(partialItems.length - 1); } } } } private function onMouseUp(event: MouseEvent) : void { linechart.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } private function onMouseOut(event: MouseEvent): void { linechart.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); } private function getLable(item: Object, field: String, index: Number): String { return ""; } ]]> </mx:Script> <mx:SolidColor id="sc1"> <mx:alpha>.1</mx:alpha> <mx:color>blue</mx:color> </mx:SolidColor> <mx:AreaChart id="linechart" left="0" right="0" top="0" bottom="0" dataProvider="{partialItems}" showDataTips="true" fontSize="11" fontWeight="normal" color="#020202" mouseWheel="resize(event)" mouseDown="onMouseDown(event)" mouseUp="onMouseUp(event)" rollOut="onMouseOut(event)"> <mx:backgroundElements> <mx:GridLines> <mx:horizontalStroke> <mx:Stroke> <mx:alpha>0.1</mx:alpha> <mx:color>blue</mx:color> </mx:Stroke> </mx:horizontalStroke> </mx:GridLines> </mx:backgroundElements> <mx:horizontalAxis> <mx:CategoryAxis id="ca" categoryField="date" /> </mx:horizontalAxis> <mx:horizontalAxisRenderer> <mx:AxisRenderer axis="{ca}" canDropLabels="true" /> </mx:horizontalAxisRenderer> <mx:verticalAxis> <mx:LinearAxis baseAtZero="false" autoAdjust="true" /> </mx:verticalAxis> <mx:secondVerticalAxis> <mx:LinearAxis baseAtZero="false" autoAdjust="true" labelFunction="getLable" /> </mx:secondVerticalAxis> <mx:series> <mx:AreaSeries displayName="{stockName}" yField="close" areaFill="{sc1}"> <mx:areaStroke> <mx:Stroke> <mx:color>blue</mx:color> <mx:weight>1</mx:weight> <mx:alpha>.4</mx:alpha> </mx:Stroke> </mx:areaStroke> </mx:AreaSeries> <mx:CandlestickSeries displayName="{stockName}" openField="open" highField="high" lowField="low" closeField="close"> <mx:fill> <mx:SolidColor color="red" /> </mx:fill> <mx:declineFill> <mx:SolidColor color="green" /> </mx:declineFill> </mx:CandlestickSeries> </mx:series> </mx:AreaChart> </mx:Application> 干这事的就是上面的loadData方法和initApp方法,从服务器上加载数据的代码如下: package { import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.http.HTTPService; public class DataLoader { private var service: HTTPService = new HTTPService(); private var dataArray: ArrayCollection; public function DataLoader(stockCode: String) { this.getData(stockCode); } private function getData(stockCode: String): void { service.url = "DataService/getData?stockCode=" + stockCode; service.method = "POST"; service.addEventListener("result", httpResult); service.addEventListener("fault", httpFault) service.send(); } public function httpResult(event:ResultEvent): void { dataArray = service.lastResult.root.item; } public function httpFault(event:FaultEvent):void { var faultstring:String = event.fault.faultString; Alert.show("Fault: " + faultstring); } public function getDataItem(): ArrayCollection { return dataArray; } } } 情况很紧急啊,希望各位大牛们给点提示!谢谢了!
|
|
kevinyao
2009-11-17
initApp方法中dataLoader.getDataItem();使用有问题.你怎么知道这个时候就能够取得服务器端的数据?
修改方法很简单. 1.先创建一个事件.表示数据已经收到了. 假设事件类叫 DataReachedEvent. 2.修改DataLoader. public class DataLoader extends DispatchEvent { private var service: HTTPService = new HTTPService(); public function DataLoader():void{}; public function getData(stockCode: String): void { service.url = "DataService/getData?stockCode=" + stockCode; service.method = "POST"; service.addEventListener("result", httpResult); service.addEventListener("fault", httpFault) service.send(); } public function httpResult(event:ResultEvent): void { var dataEvent: DataReachedEvent = new DataReachedEvent(); dataEvent.dataArr = service.lastResult.root.item; this.dispatchEvent(dataEvent); } } 3. loadData()方法修改 private function loadData(): void { var stockCode = Application.application.parameters.stockCode; stockName = Application.application.parameters.stockName; dataLoader = new DataLoader(); dataLoader.addEventListener(DataReachedEvent, initApp); dataLoader.getData(stockCode); } 4. creationComplete="initApp()" initialize="loadData()"修改成 creationComplete="loadData()" 5. initApp() 修改成 initApp(e: DataReachedEvent) {.......} 大致思路是这样. 其实没有必要专门搞一个DataLoader,除非该项目统一了编码规范. 自己调试一下 |
|
sekiyika
2009-11-24
谢谢楼上的回答,呵呵,确实用不着新建一个DataLoader类,现在感觉有杀鸡焉用牛刀的感觉,问题解决了
|