我正在尝试使用ArrayCollection
填充json data
,以便将其用作dataprovider
的{{1}}。
我尝试使用以下代码,但它不起作用。
LineChart
任何人都可以帮助我吗? 谢谢。
答案 0 :(得分:0)
此测试应用程序有效。它使用了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>