简体   繁体   English

chartjs如何从数据库动态更新数据(Chartjs无法获取数据)

[英]chartjs how to update dynamically data from database(Chartjs cant get the data)

I am so much new to chartjs and doing this current project with laravel.我对 chartjs 非常陌生,并且正在使用 laravel 做这个当前项目。 Now, I need to create a chart which needs to update automatically with no refresh and the data comes from mysql database.I found this kind of code online: https://codepen.io/jordanwillis/pen/bqaGRR .现在,我需要创建一个图表,该图表需要自动更新,无需刷新,数据来自mysql数据库。我在网上找到了这种代码: https ://codepen.io/jordanwillis/pen/bqaGRR。 I tried doing this code and the output always fails me.我尝试执行此代码,但输出总是让我失望。

   // used for example purposes
function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// create initial empty chart
var ctx_live = document.getElementById("mycanvas");
var myChart = new Chart(ctx_live, {
  type: 'line',
  data: {
    labels: readingID,
    datasets: [{
      data: moist,
      borderWidth: 1,
      borderColor:'#00c0ef',
      label: 'liveCount',
    }]
  },
  options: {
    responsive: true,
    title: {
      display: true,
      text: "Chart.js - Dynamically Update Chart Via Ajax Requests",
    },
    legend: {
      display: false
    },
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true,
        }
      }]
    }
  }
});

// this post id drives the example data
var readingID = [];
var moist = [];

// logic to get new data
var getData = function() {
  $.ajax({
    url: "/soildata",
    type:"GET",
    success: function(data) {
         console.log(data);
        data = JSON.parse(data);


      // process your data to pull out what you plan to use to update the chart
      // e.g. new label and a new data point

      // add new label and data point to chart's underlying data structures
      myChart.data.labels.push("ReadingID " + readingID++);
      myChart.data.datasets[0].data.push(getRandomIntInclusive(1, 25)); <<<<<-----------I DONT KNOW HOW TO PLAY ON THIS PART I GUESS THIS PART CAN MAKE MY DATABASE WORKING

      // re-render the chart
      myChart.update();
    }
  });
};

// get new data every 3 seconds
setInterval(getData, 3000);

I guess I need to make something on this function so that my data coming from my database will work properly: Chart.data.datasets[0].data.push(getRandomIntInclusive(1, 25));我想我需要在这个函数上做一些事情,这样我的数据库中的数据才能正常工作:Chart.data.datasets[0].data.push(getRandomIntInclusive(1, 25));

Anyone?..does anyone know about this?..I am a newbie on chartjs and truely I spent weeks in solving but I guess I cant.I hope someone out there could be a help, even a different code that will solve my problem is highly accepted.有人吗?..有人知道吗?..我是 chartjs 的新手,确实我花了几周的时间来解决问题,但我想我不能。我希望有人能提供帮助,即使是不同的代码也能解决我的问题被高度接受。 Thank you for any actions that will be taken..感谢您将采取的任何行动..

Assuming the data you get back from that ajax call is good, you should be fine with just replacing the假设您从该 ajax 调用返回的数据是好的,您只需更换

  myChart.data.datasets[0].data.push(getRandomIntInclusive(1, 25));

with

  myChart.data.datasets[0].data.push(data);

Of course this is dependent on the format of that data when it gets back to you.当然,这取决于数据返回给您时的格式。

I recommend console.log-ing that data once it gets back from the ajax to make sure it's in a format suitable for chartjs我建议在数据从 ajax 返回后使用 console.log-ing 来确保它的格式适合 chartjs

Let's say, moist = [50, 30] and the content of the JSON response is as follows:比方说, moist = [50, 30] JSON 响应的内容如下:

data{"id":"1" , "val":"20"}

To update moist from [50,30] to [1, 20] :moist[50,30]更新为[1, 20]

myChart.data.datasets[0].data[0] = data.id;
myChart.data.datasets[1].data[1] = data.val;
myChart.update();

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

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