将外部JSON加载到ChartJs中

时间:2015-10-06 18:39:51

标签: javascript jquery json angularjs chart.js

我是一个新手,当使用库在JavaScript中绘制图表/我刚刚开始尝试使用Chartjs时,我一直无法使用getJson或其他任何东西来加载我的json对象并替换标签和数据。我之前使用过HighCharts,与此相比它非常简单。另外,我如何将它放入Angular中的指令并显示它。

https://jsfiddle.net/0u9Lpttx/1/

的index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div style="width: 100%; height: 100%;">
    <canvas id="myChart" style="width: 100%; height: auto;"></canvas>
</div>
<div id="js-legend" class="chart-legend"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.1/Chart.min.js"></script>
<script src="js/chartJsControl.js"></script>
</body>
</html>

data.json

[
  {
    "timestamp": "Monday",
    "original_tweet": "756",
    "retweets": "345",
    "shared": "34",
    "quoted": "14"
  },
  {
    "timestamp": "Tuesday",
    "original_tweet": "756",
    "retweets": "345",
    "shared": "34",
    "quoted": "14"
  },
  {
    "timestamp": "Wednesday",
    "original_tweet": "756",
    "retweets": "345",
    "shared": "34",
    "quoted": "14"
  }
]

chartJsControl.js

var test = [];
$.getJSON("data.json", function (json) {
    test.push(json[i].timestamp);

});
var data = {
    labels: test,
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.5)",
            strokeColor: "rgba(220,220,220,0.8)",
            highlightFill: "rgba(220,220,220,0.75)",
            highlightStroke: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        },
        {
            label: "My Second dataset",
            fillColor: "rgba(151,187,205,0.5)",
            strokeColor: "rgba(151,187,205,0.8)",
            highlightFill: "rgba(151,187,205,0.75)",
            highlightStroke: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};


var ctx = document.getElementById("myChart").getContext("2d");
ctx.canvas.width = 1000;
ctx.canvas.height = 800;

var myChart = new Chart(ctx).Bar(data);

1 个答案:

答案 0 :(得分:5)

如果你想使用data.json中返回的JSON,你需要在回调函数中这样做:

$.getJSON("data.json", function (json) {
  // will generate array with ['Monday', 'Tuesday', 'Wednesday']
  var labels = json.map(function(item) {
    return item.timestamp;
  });

  var data = {
    labels: labels,
    datasets: [
    {
      label: "My First dataset",
      fillColor: "rgba(220,220,220,0.5)",
      strokeColor: "rgba(220,220,220,0.8)",
      highlightFill: "rgba(220,220,220,0.75)",
      highlightStroke: "rgba(220,220,220,1)",
      data: [65, 59, 80, 81, 56, 55, 40]
    },
    {
      label: "My Second dataset",
      fillColor: "rgba(151,187,205,0.5)",
      strokeColor: "rgba(151,187,205,0.8)",
      highlightFill: "rgba(151,187,205,0.75)",
      highlightStroke: "rgba(151,187,205,1)",
      data: [28, 48, 40, 19, 86, 27, 90]
    }
    ]
  };

  var ctx = document.getElementById("myChart").getContext("2d");
  ctx.canvas.width = 1000;
  ctx.canvas.height = 800;

  var myChart = new Chart(ctx).Bar(data);
});

如果您使用Angular我会建议使用angular chart.js版本,请参阅:http://jtblin.github.io/angular-chart.js/

然后你已经有一个角度指令,你可以使用它!