Morris图表从本地JSON文件中获取数据

Gov*_*las 1 html javascript jquery json morris.js

我正在尝试创建一个莫里斯甜甜圈图表.我已经修改它以从本地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>
Run Code Online (Sandbox Code Playgroud)

这是我的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} ]
Run Code Online (Sandbox Code Playgroud)

Dip*_*ole 5

以下是您需要了解和进行更改的内容,

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
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

jquery.getJSON