apache-flex 动态填充数组集合Flex

nwlls2ji  于 2022-11-01  发布在  Apache
关注(0)|答案(1)|浏览(190)

我尝试用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)}); }

有人能帮帮我吗?谢谢。

vsikbqxv

vsikbqxv1#

这个测试应用程序可以工作。它使用了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>

相关问题