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

后端 未结 21 1732
不思量自难忘°
不思量自难忘° 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条回答
  •  抹茶落季
    2020-11-28 20:58

    for those who like me use a function to create several graphics and want to update them a block too, only the function .destroy() worked for me, I would have liked to make an .update(), which seems cleaner but ... here is a code snippet that may help.

    var SNS_Chart = {};
    
    // IF LABELS IS EMPTY (after update my datas)
    if( labels.length != 0 ){
    
          if( Object.entries(SNS_Chart).length != 0 ){
    
                array_items_datas.forEach(function(for_item, k_arr){
                    SNS_Chart[''+for_item+''].destroy();
                });
    
           }
    
           // LOOP OVER ARRAY_ITEMS
           array_items_datas.forEach(function(for_item, k_arr){
    
                 // chart
                 OPTIONS.title.text = array_str[k_arr];
                 var elem = document.getElementById(for_item);
                 SNS_Chart[''+for_item+''] = new Chart(elem, {
                     type: 'doughnut',
                     data: {
                         labels: labels[''+for_item+''],
                         datasets: [{
                            // label: '',
                            backgroundColor: [
                                '#5b9aa0',
                                '#c6bcb6',
                                '#eeac99',
                                '#a79e84',
                                '#dbceb0',
                                '#8ca3a3',
                                '#82b74b',
                                '#454140',
                                '#c1502e',
                                '#bd5734'
                            ],
                            borderColor: '#757575',
                            borderWidth : 2,
                            // hoverBackgroundColor : '#616161',
                            data: datas[''+for_item+''],
                         }]
                     },
                     options: OPTIONS
    
                 });
                 // chart
           });
           // END LOOP ARRAY_ITEMS
    
      }
     // END IF LABELS IS EMPTY ...
    

提交回复
热议问题