简体   繁体   English

从Ajax请求中使用Json_encoded数组重绘Chart.js图表

[英]Redraw Chart.js Chart with Json_encoded array from ajax request

I have a chart that I'm drawing using the chart.js library. 我有一个使用chart.js库绘制的图表。 The first draw goes perfectly, but I'm attempting to redraw the doughnut chart with NEW data from an ajax call. 第一次绘制非常顺利,但是我尝试使用ajax调用中的NEW数据重绘甜甜圈图。

I'm using PHP and Codeigniter to grab all the data I need and return a json_encoded array to redraw the graph. 我正在使用PHP和Codeigniter捕获我需要的所有数据,并返回json_encoded数组以重绘该图。 It is not working with my current code, HOWEVER, if i copy the returned code from the console, and paste it in it works FINE! 但是,如果我从控制台复制返回的代码并将其粘贴到它中,它就不能与我当前的代码一起使用,很好! Anyone know why this is? 有人知道为什么吗? I've tried JSON_FORCE_OBJECT too and it doesn't work... 我也尝试过JSON_FORCE_OBJECT并且它不起作用...

Here is my controller code... 这是我的控制器代码...

$data = array();
  foreach($g_data as $gd) {
    $temp = array(
        'value' => $gd['count'],
        'color' => $this->App_model->adjustBrightness($base_color),
        'label' => $gd['label']
    );
    array_push($data, $temp);
  }

  echo json_encode($data);

Here is my JS... 这是我的JS ...

$.ajax({
     type: "POST",
     url: siteUrl + "app/load_graph",
     data: {
      question_id: question_id
     },
     success: function(data) {

        //Get context with jQuery - using jQuery's .get() method.
        var ctx = $("#analytics-graph").get(0).getContext("2d");
        //This will get the first returned node in the jQuery collection.
        var myNewChart = new Chart(ctx);

        new Chart(ctx).Doughnut(data);

    } 
  });

Here is what is output to the console from my php request: 这是我的php请求输出到控制台的内容:

[{"value":3,"color":"#64ff9d","label":"less than 1"},{"value":0,"color":"#63ff9c","label":"2"},{"value":0,"color":"#71ffaa","label":"3"},{"value":0,"color":"#74ffad","label":"4"},{"value":2,"color":"#57ff90","label":"5"},{"value":0,"color":"#7cffb5","label":"6+"}]

And here is what is baffling me, if I copy the above returned object and use it as a variable in my code, it works perfectly! 而这就是让我感到困惑的事情,如果我复制上面返回的对象并将其用作代码中的变量,则效果很好! no idea why... 不知道为什么...

$.ajax({
     type: "POST",
     url: siteUrl + "app/load_graph",
     data: {
      question_id: question_id
     },
     success: function(data) {

        //Get context with jQuery - using jQuery's .get() method.
        var ctx = $("#analytics-graph").get(0).getContext("2d");
        //This will get the first returned node in the jQuery collection.
        var myNewChart = new Chart(ctx);

        var data = [{"value":3,"color":"#64ff9d","label":"less than 1"},{"value":0,"color":"#63ff9c","label":"2"},{"value":0,"color":"#71ffaa","label":"3"},{"value":0,"color":"#74ffad","label":"4"},{"value":2,"color":"#57ff90","label":"5"},{"value":0,"color":"#7cffb5","label":"6+"}]

        new Chart(ctx).Doughnut(data);

    } 
  });

I'm an idiot, just needed the "json" type in the jquery ajax function, this question is probably pretty useful to someone else so here is what fixed it: dataType: "json", 我是个白痴,只需要在jquery ajax函数中使用“ json”类型,这个问题可能对其他人很有用,所以这是它的解决方法:dataType:“ json”,

$.ajax({
     type: "POST",
     dataType: "json",
     url: siteUrl + "app/load_graph",
     data: {
      question_id: question_id
     },
     success: function(data) {

        //Get context with jQuery - using jQuery's .get() method.
        var ctx = $("#analytics-graph").get(0).getContext("2d");
        //This will get the first returned node in the jQuery collection.
        var myNewChart = new Chart(ctx);

        new Chart(ctx).Doughnut(data);

    } 
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM