CanvasJS没有加载外部json数据的柱形图

时间:2016-02-28 02:50:26

标签: canvasjs

我尝试从文件

加载带有Json格式的外部数据的柱形图

到目前为止,我有一个jsfiddle。 谢谢你的帮助。

http://jsfiddle.net/t9n4d8z4/1/

        $(document).ready(function() {
          var dataPoints = [];
          $.getJSON("https://api.myjson.com/bins/1kfs1", function(result) {
            for (var i = 0; i <= result.length - 1; i++) {
              dataPoints.push({
                label: result[i].label,
                y: parseInt(result[i].y)
              });
            }
            var chart = new CanvasJS.Chart("chartContainer", {
              data: [{
                type: "column",
                dataPoints: result
              }]
            });

            chart.render();
          });
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 个答案:

答案 0 :(得分:0)

json格式处理不当。

这是工作小提琴:http://jsfiddle.net/canvasjs/t9n4d8z4/3/

&#13;
&#13;
        $(document).ready(function() {
          var dataPoints = [];
          $.getJSON("https://api.myjson.com/bins/1kfs1", function(result) {
            for (var i = 0; i <= result.dataPoints.length - 1; i++) {
              dataPoints.push({
                label: result.dataPoints[i].label,
                y: parseInt(result.dataPoints[i].y)
              });
            }

            var chart = new CanvasJS.Chart("chartContainer", {
              data: [{
                type: "column",
                dataPoints: dataPoints
              }]
            });

            chart.render();
          });
        });
&#13;
<script src="http://canvasjs.com/assets/script/canvasjs.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
&#13;
&#13;
&#13;