Chartjs - destroy()不执行任何操作

rggaifut  于 2022-11-07  发布在  Chart.js
关注(0)|答案(3)|浏览(225)

chartJS有问题。我正在调用一个函数来创建一个新的图表。创建工作正常,但是我想销毁所有以前的图表,以便在调用函数时得到一个新的图表。
这是我目前的代码:

  1. var chartID;
  2. function addData(chartType,data) {
  3. for (var i = 0; i < data.length; i++) {
  4. dataLabels.push(data[i][0]);
  5. dataPoints.push(data[i][1]);
  6. //console.log(data[i][1]);
  7. }
  8. if(chartID){
  9. console.log('destroy');
  10. chartID.destroy();
  11. }
  12. var ctx = document.getElementById('chart01').getContext('2d');
  13. chartID = new Chart(ctx, {
  14. type: chartType,
  15. data: {
  16. labels: dataLabels,
  17. datasets: [{
  18. label: 'Labels',
  19. data: dataPoints,
  20. backgroundColor: '#333'
  21. }]
  22. },
  23. options: {
  24. maintainAspectRatio: false,
  25. aspectRatio: 1
  26. }
  27. });
  28. }
4c8rllxm

4c8rllxm1#

即使是我之前也遇到了同样的问题。我只是简单地添加了一个条件来检查图表变量是否为空。

  1. if(chartID != null){
  2. chartID.destroy();
  3. }

在函数的顶部包含它。当你全局声明chartID时,它会很好地工作。这样你就不需要再次声明图表了。

hiz5n14c

hiz5n14c2#

试试看:

  1. const chartCanvas = document.getElementById('myChart');
  2. if( window.lineChart != undefined){
  3. window.lineChart.destroy();
  4. }
  5. window.lineChart = new Chart(chartCanvas,{
  6. type: 'line',
  7. data: {
  8. labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  9. datasets: [
  10. {
  11. label: 'Cases',
  12. data: [23,42,22,45,56,77,33,22,46,77,32,44],
  13. fill: false,
  14. // backgroundColor: 'red',
  15. borderColor: ['rgb(255, 255, 0)'],
  16. lineTension: 0.2,
  17. borderWidth: 1.5
  18. }
  19. ]
  20. },
  21. options:{
  22. title: {
  23. display: true,
  24. text: "Temperature variation",
  25. fontFamily: 'Arial',
  26. fontSize: 16
  27. },
  28. legend: {
  29. display: true,
  30. position: "right",
  31. labels: {
  32. boxWidth:10
  33. }
  34. },
  35. //stacked - start with 0 as minimum value for y-axis
  36. scales:{
  37. yAxes: [{
  38. stacked: true,
  39. gridLines:{
  40. display: true,
  41. color: '#FFFFFF',
  42. lineWidth: 1,
  43. zeroLineColor: 'blue',
  44. zeroLineWidth: 2,
  45. drawBorder: false // this to remove the ghost line that appears
  46. // when you add zeroLine x-axis
  47. }
  48. }],
  49. xAxes: [{
  50. gridLines:{
  51. display: true,
  52. zeroLineColor: 'blue',
  53. zeroLineWidth: 1,
  54. color: 'transparent'
  55. }
  56. }]
  57. }
  58. }
  59. });

---------------------上面添加了代码示例----------------
我对ChartJs有一些问题。不知何故,它创建了一个新的图表,以前的图表仍然在画布上,当你悬停时会显示出来。
这对我很有效:

  1. if( window.chartID!= undefined){
  2. window.chartID.destroy();
  3. }
  4. chartID = new Chart(ctx, {...});
展开查看全部
xfb7svmp

xfb7svmp3#

我正在创建多个图表点击。但在创建图表之前,我只是破坏任何以前的图表,所以,这就是它的外观

  1. var chartStatus
  2. // on one onclick
  3. if (chartStatus) { chartStatus.destroy(); }
  4. chartStatus = new Chart(document.getElementById("co2Chart"), co2Config);
  5. // on another onclick
  6. if (chartStatus) { chartStatus.destroy(); }
  7. chartStatus = new Chart(document.getElementById("tempChart"), tempConfig);

相关问题