After dynamically adding a lineseries, I would like to read the underlying data for all the rendered lines.
In the following code, the top buttons add and remove lines. When a new line is added or removed, I call getdata() which posts the available data at the bottom of the Panel. Immediately after a line is added and displayed, the displayName is available, but not the LineSeries data. Yet, as click on the [Read lines] button calls the same function and pulls the data! How can I get the latest lineseries data from the LineChart? Perhaps an invalidator or update? I need to read from the LineChart. Thanks. [CODE] <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.collections.ArrayCollection; import mx.charts.series.renderData.LineSeriesRenderData; import mx.graphics.Stroke; import mx.charts.series.LineSeries; [Bindable] private var expensesAC:ArrayCollection = new ArrayCollection( [ { Month: "Jan", Profit: 2000, Expenses: 1500, Amount: 450 }, { Month: "Feb", Profit: 1000, Expenses: 200, Amount: 600 }, { Month: "Mar", Profit: 1500, Expenses: 500, Amount: 300 }, { Month: "Apr", Profit: 1800, Expenses: 1200, Amount: 900 }, { Month: "May", Profit: 2400, Expenses: 575, Amount: 500 } ]); private function updateChart(evt:Event):void { if ( evt.currentTarget.selected == true ) { addToChart(evt.currentTarget.label); } else { removeFromChart(evt.currentTarget.label); } getdata(); } private function addToChart(item:String):void { var newLS:LineSeries = new LineSeries(); newLS.yField = item; newLS.displayName = item; newLS.setStyle('form','curve'); newLS.dataProvider = expensesAC; //newLS.setStyle('lineStroke',newStroke); var tmp:Array = linechart.series; tmp.push(newLS); linechart.series = tmp; linechart.invalidateSeriesStyles(); } private function removeFromChart(item:String):void { var objToRemoveIndex:int; for ( var i:int = 0; i < linechart.series.length; i++ ) { if ( linechart.series[i].yField == item ) { objToRemoveIndex = i; } } var tmp:Array = linechart.series; tmp.splice(objToRemoveIndex,1); linechart.series = tmp; linechart.invalidateSeriesStyles(); } private function getdata():void { txt.text = linechart.series.length + " lines in chart \n"; for ( var i:int = 0; i < linechart.series.length; i++ ) { txt.text += linechart.series[i].displayName + " "; txt.text += linechart.series[i].items[0].yValue + " " + linechart.series[i].items[1].yValue + "\n"; } } private function init():void { //addToChart('Amount'); //Amount.selected = true; } ]]> </mx:Script> <mx:Panel title="Where is the data?" height="500" width="100%" layout="vertical"> <mx:HBox id="choices" horizontalGap="0"> <mx:Button id="Profit" label="Profit" toggle="true" click="updateChart(event)"/> <mx:Button id="Expenses" label="Expenses" toggle="true" click="updateChart(event)"/> <mx:Spacer height="0"/> <mx:Button id="Amount" label="Amount" toggle="true" click="updateChart(event)"/> </mx:HBox> <mx:LineChart id="linechart" height="80%" width="100%" showDataTips="true" dataProvider="{expensesAC}" creationComplete="init()"> <mx:horizontalAxis> <mx:CategoryAxis categoryField="Month"/> </mx:horizontalAxis> </mx:LineChart> <mx:HBox width="100%" height="69"> <mx:Legend dataProvider="{linechart}" labelPlacement="right" verticalGap="2" direction="vertical"/> <mx:Button label="Read lines" click="getdata()"/> <mx:Text id="txt" width="426" height="70"/> </mx:HBox> </mx:Panel> </mx:Application> [/CODE]