我尝试使用从 dart 应用程序调用的 js lib amCharts 显示一个简单的序列图。
在下面的代码中,第一次调用 main() 中的 display() 确实按预期显示了图表。
但是当我评论第一个 display() 并单击按钮从回调中调用 display() 时,图表不会出现。
import 'dart:js';
import 'dart:html';
main(){
ButtonElement button = new Element.tag("button");
button.type = "submit";
querySelector("#button").append(button);
button.onClick.listen((event) => someCallback());
display(); // this call does display the chart
}
someCallback() {
display(); // this call does not display the chart
}
display() {
var chartData = [
{
"date": "2012-03-01",
"price": 20
},
{
"date": "2012-03-02",
"price": 75
},
{
"date": "2012-03-03",
"price": 10
}
];
context['AmCharts'].callMethod('ready', [(){
var chart2 = new JsObject(context['AmCharts']['AmSerialChart']);
chart2['dataProvider'] = new JsObject.jsify(chartData);
chart2['categoryField'] = "date";
chart2['dataDateFormat'] = "YYYY-MM-DD";
var categoryAxis = chart2['categoryAxis'];
categoryAxis['parseDates'] = true;
categoryAxis['minPeriod'] = "DD";
categoryAxis['dashLength'] = 1;
var valueAxis = new JsObject(context['AmCharts']['ValueAxis']);
valueAxis['axisColor'] = "#DADADA";
valueAxis['dashLength'] = 1;
chart2.callMethod('addValueAxis', [valueAxis]);
var graph = new JsObject(context['AmCharts']['AmGraph']);
graph['title'] = "Price";
graph['valueField'] = "price";
chart2.callMethod('addGraph', [graph]);
chart2.callMethod('write', ["chart"]);
}]);
}