How to clear a chart from a canvas so that hover events cannot be triggered?

后端 未结 21 1817
不思量自难忘°
不思量自难忘° 2020-11-28 20:07

I\'m using Chartjs to display a Line Chart and this works fine:

// get line chart canvas
var targetCanvas = document.getElementById(\'chartCanvas\').getConte         


        
21条回答
  •  旧时难觅i
    2020-11-28 20:55

    You should save the chart as a variable. On global scope, if its pure javascript, or as a class property, if its Angular.

    Then you'll be able to use this reference to call destroy().

    Pure Javascript:

    var chart;
    
    function startChart() {
        // Code for chart initialization
        chart = new Chart(...); // Replace ... with your chart parameters
    }
    
    function destroyChart() {
        chart.destroy();
    }
    

    Angular:

    export class MyComponent {
        chart;
    
        constructor() {
            // Your constructor code goes here
        }
    
        ngOnInit() {
            // Probably you'll start your chart here
    
            // Code for chart initialization
            this.chart = new Chart(...); // Replace ... with your chart parameters
        }
    
        destroyChart() {
            this.chart.destroy();
        }
    }
    

提交回复
热议问题