Chart.js -如何使用间隔更新图表数据

ezykj2lf  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(299)

我有一个从API获取数据的圆环图。图表显示正常,但我不能让图表数据更新,每2秒更新一次。我尝试并搜索了许多主题,但大多数都是过时的chart.js版本,我的javascript技能很差。

Chart.js v3.9.1

  1. const totalRunTime = current_array.MTConnectStreams.Streams.DeviceStream.ComponentStream[12].Samples.DynamicBasicData[1];
  2. const totalTimeRemaining = current_array.MTConnectStreams.Streams.DeviceStream.ComponentStream[12].Samples.DynamicBasicData[13];
  3. const timeRemaining = ((totalTimeRemaining / totalRunTime) * 100)
  4. // Setup
  5. const datapoints = [(100 - timeRemaining), timeRemaining];
  6. const data = {
  7. datasets: [{
  8. data: datapoints,
  9. backgroundColor: ['rgba(20, 121, 255, 0.7)', 'rgba(208, 208, 208, 0.5)'],
  10. borderWidth: 1,
  11. cutout: '70%'
  12. }]
  13. };
  14. // Counter
  15. const counter = {
  16. id: 'counter',
  17. beforeDraw( chart, args, options ) {
  18. const { ctx, chartArea: { top, right, bottom, left, width, height } } = chart;
  19. ctx.save();
  20. ctx.font = options.fontSize + 'px ' + options.fontFamily;
  21. ctx.textAlign = 'center';
  22. ctx.fillStyle = options.fontColor;
  23. ctx.fillText(datapoints[0].toFixed(3) + '%', width / 2, (height / 2) + (options.fontSize * 0.34));
  24. }
  25. };
  26. // Config
  27. const config = {
  28. type: 'doughnut',
  29. data,
  30. options: {
  31. plugins: {
  32. legend: {
  33. display: false
  34. },
  35. tooltip: {
  36. enabled: false
  37. },
  38. counter: {
  39. fontColor: '#193b68',
  40. fontSize: '16',
  41. fontFamily: 'Sofia Pro Medium'
  42. }
  43. }
  44. },
  45. plugins: [counter]
  46. };
  47. // Render init
  48. const doughnutChart = new Chart(
  49. document.getElementById('doughnutChart'),
  50. config
  51. );
  52. setInterval(function addData(chart, data) {
  53. chart.data.datasets.forEach((dataset) => {
  54. dataset.data = data;
  55. });
  56. chart.update('none');
  57. }, 2000
  58. );

HTML格式

  1. <canvas id="doughnutChart"></canvas>
0kjbasz6

0kjbasz61#

它看起来好像没有更新,因为您调用了数据,并将其存储在一个变量中,从应用程序的范围来看,该变量尚未更改。
如果您将数据请求移动到一个函数,然后从setInterval内的初始化代码 * 和 * 中调用该函数,您应该会看到每次调用函数getData时,数据都会更新。
下面是一个符合我意思的代码版本,其中使用了getData()而不是datapoints

  1. const getData = () => {
  2. console.log('Requesting data')
  3. const totalRunTime = current_array.MTConnectStreams.Streams.DeviceStream.ComponentStream[12].Samples.DynamicBasicData[1];
  4. const totalTimeRemaining = current_array.MTConnectStreams.Streams.DeviceStream.ComponentStream[12].Samples.DynamicBasicData[13];
  5. const timeRemaining = ((totalTimeRemaining / totalRunTime) * 100)
  6. return [(100 - timeRemaining), timeRemaining]
  7. }
  8. const data = {
  9. datasets: [{
  10. data: getData(),
  11. backgroundColor: ['rgba(20, 121, 255, 0.7)', 'rgba(208, 208, 208, 0.5)'],
  12. borderWidth: 1,
  13. cutout: '70%'
  14. }]
  15. };
  16. const counter = {
  17. id: 'counter',
  18. beforeDraw( chart, args, options ) {
  19. const { ctx, chartArea: { top, right, bottom, left, width, height } } = chart;
  20. ctx.save();
  21. ctx.font = options.fontSize + 'px ' + options.fontFamily;
  22. ctx.textAlign = 'center';
  23. ctx.fillStyle = options.fontColor;
  24. ctx.fillText(getData()[0].toFixed(3) + '%', width / 2, (height / 2) + (options.fontSize * 0.34));
  25. }
  26. };
  27. const config = {
  28. type: 'doughnut',
  29. data,
  30. options: {
  31. plugins: {
  32. legend: {
  33. display: false
  34. },
  35. tooltip: {
  36. enabled: false
  37. },
  38. counter: {
  39. fontColor: '#193b68',
  40. fontSize: '16',
  41. fontFamily: 'Sofia Pro Medium'
  42. }
  43. }
  44. },
  45. plugins: [counter]
  46. };
  47. const doughnutChart = new Chart(
  48. document.getElementById('chart'),
  49. config
  50. );
  51. setInterval(function addData(chart, data) {
  52. console.log('updating', Date.now())
  53. doughnutChart.data.datasets.forEach((dataset) => {
  54. dataset.data = getData();
  55. });
  56. doughnutChart.update('none');
  57. }, 2000
  58. );

这是一个codepen of an updating version,但使用了随机值而不是数据请求。
这可能就是问题所在,我希望这对你有帮助。
一个技巧是使用console.log来查看调用了什么,将代码放在函数中也可以通过使用console.logdebugger查看调用代码的位置和时间,从而使调试变得更容易。

展开查看全部

相关问题