Chart.JS使用JSON定义

时间:2017-05-07 13:05:44

标签: json chart.js

你好我们有问题,我不能显示我的图表它会变成这样 enter image description here

正如你可以看到我还打印控制台日志。没有发生任何错误。 下面是我的index.php

<!DOCTYPE html>
<html>
<head>
    <title>Line</title>
</head>
<body>
<div class="chart-container">
<canvas id="line-chartcanvas"></canvas>
</div>

<script src="js/Chart.bundle.min.js"></script>
<script src="js/jquery.js"></script>
<script src="js/chartmain.js"></script>
</body>
</html>

我的chartmain.js

$(function(){
  $.ajax({
    url:"http://localhost/Media/chart.php",
    type: "GET",
    success : function (data) {

      var count = [];
      var displayLink = [];

      for(var i in data){
        count.push ("Player" + data[i].count);
        displayLink.push(data[i].displayLink);

      }

      var chartdata = {
        labels: displayLink,
        datasets : [
          {
            data: displayLink,
            label : 'Score',
            backgroundColor: 
            "#F1c40F"
          }
                ]

    };
     var ctx =$("#line-chartcanvas");
     var barGraph = new Chart(ctx,{
      type: 'bar',
      data: chartdata

    });
    console.log(data);
    },

    error : function (data) {
      console.log(data);
    },
  });
});

我的Json数组

[
  {
    "count": "3",
    "displayLink": "www.bharian.com.my"
  },
  {
    "count": "1",
    "displayLink": "www.hmetro.com.my"
  },
  {
    "count": "6",
    "displayLink": "www.nst.com.my"
  },
  {
    "count": "1",
    "displayLink": "www.sinarharian.com.my"
  },
  {
    "count": "1",
    "displayLink": "www.utusan.com.my"
  }
]

我想也许它无法从JSON读取我的数据,但在控制台日志中读取

1 个答案:

答案 0 :(得分:0)

感谢您帮助我,已经找到答案,我无法从JSON获取数据,但我可以从MySQL获取数据