动态地填充ArrayCollection



我试图用json data填充ArrayCollection,以便将其用作LineChartdataprovider。我尝试使用下面的代码,但它不会工作。

jsonData= (com.adobe.serialization.json.JSON.decode(dataLoader.data));
for (var i:int=0; i<10; i++){
        frame.addItem({ date_time:((jsonData[i].date_time).toString()) , glucose:((jsonData[i].content).glucose)}); }
有谁能帮帮我吗?谢谢。

这个测试应用程序可以工作。它使用json数据的简化版本。但是处理过程是一样的。我还使用了一个不同的JSON解码器。

我唯一能想到的是你的createDate函数是错误的。我使用了一个简单的版本来演示一切都应该像预期的那样工作。

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="creationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.charts.DateTimeAxis;
            import mx.charts.series.LineSeries;
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;
            [Bindable]
            private var frame:ArrayCollection;
            protected function creationCompleteHandler(event:FlexEvent):void {
                frame = new ArrayCollection();
                var data:String = '[{ "content": { "glucose": 135 }, "date_time": "2016,09,15"},' +
                    '{ "content": { "glucose": 55 }, "date_time": "2016,09,16"},' +
                    '{ "content": { "glucose": 13 }, "date_time": "2016,09,17"},' +
                    '{ "content": { "glucose": 70 }, "date_time": "2016,09,18"}]';
                var jsonData:Object = JSON.parse(data);
                for each (var item:Object in jsonData) {
                    frame.addItem({ date_time:((item.date_time).toString()) , glucose:(item.content.glucose)});
                }
                var localSeries:LineSeries = new LineSeries(); 
                localSeries.dataProvider = frame; 
                localSeries.yField = "glucose"; 
                localSeries.xField = "date_time"; 
                localSeries.displayName = "Glucose Level"; 
                var currentSeries:Array = myChart.series; 
                currentSeries.push(localSeries); 
                myChart.series = currentSeries; 
                var hAxis:DateTimeAxis = new DateTimeAxis(); 
                hAxis.parseFunction = createDate; 
                myChart.horizontalAxis = hAxis;
            }
            public function createDate(s:String):Date {
                // Get an array of Strings from the 
                // comma-separated String passed in.
                var a:Array = s.split(",");
                // Trace out year, month, and day values.
                trace("y:" + a[0]);
                trace("m:" + a[1]);
                trace("d:" + a[2]);
                // To create a Date object, you pass "YYYY,MM,DD", 
                // where MM is zero-based, to the Date() constructor.
                var newDate:Date = new Date(a[0],a[1]-1,a[2]);
                return newDate;
            }
        ]]>
    </fx:Script>
    <mx:LineChart id="myChart" />
</s:WindowedApplication>

相关内容

  • 没有找到相关文章

最新更新