我正在尝试创建莫里斯 donut chart . 我修改了它以从本地 json 文件获取数据,但由于某种原因它没有加载图表。控制台中也没有错误。
这是html文件
<meta charset=utf-8 />
<title>Morris.js Donut Chart Example</title>
</head>
<body onLoad="drawChart()">
<div id="donut-example"></div>
</body>
<script>
function drawChart() {
var jsonData = $.getJSON("data.json", function(json) {
console.log(json); // show the info in console
});
Morris.Donut({
element: 'donut-example',
data: jsonData
});
}
</script>
</html>
这是我的 data.json 文件
[ {label: "Download Sales", value: 12}, {label: "In-Store Sales", value: 30}, {label: "In-Store Sales", value: 25}, {label: "Mail-Order Sales", value: 20} ]
最佳答案
这是您需要了解并进行更改的内容,
function drawChart() {
$.getJSON("data.json", function (json) { // callback function which gets called when your request completes.
Morris.Donut({
element: 'donut-example',
data: json // use returned data to plot the graph
});
});
}
jquery.getJSON
关于javascript - 从本地 JSON 文件中获取数据的 Morris 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21797450/