2016-12-20 19 views
1

私はChart.js 2.4.0で折れ線グラフを使用しています。データポイントを1つホバーするときだけでなく、常にツールチップを表示できるプラグインを登録しました。Chart.jsのプラグインを登録解除する

enableChartToAlwaysShowTooltips(): void { 
    Chart.plugins.register({ 
     beforeRender: function (chart) { 
      if (chart.config.options.showAllTooltips) { 
       // create an array of tooltips 
       // we can't use the chart tooltip because there is only one tooltip per chart 
       chart.pluginTooltips = []; 
       chart.config.data.datasets.forEach(function (dataset, i) { 
        if (dataset.data.length === 1) { 
         chart.getDatasetMeta(i).data.forEach(function (sector, j) { 
          chart.pluginTooltips.push(new Chart.Tooltip({ 
           _chart: chart.chart, 
           _chartInstance: chart, 
           _data: chart.data, 
           _options: chart.options.tooltips, 
           _active: [sector] 
          }, chart)); 
         }); 
        } 
       }); 

       // turn off normal tooltips 
       chart.options.tooltips.enabled = false; 
      } 
     }, 
     afterDraw: function (chart, easing) { 
      if (chart.config.options.showAllTooltips) { 
       // we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once 
       if (!chart.allTooltipsOnce) { 
        if (easing !== 1) 
         return; 
        chart.allTooltipsOnce = true; 
       } 

       // turn on tooltips 
       chart.options.tooltips.enabled = true; 
       Chart.helpers.each(chart.pluginTooltips, function (tooltip) { 
        tooltip.initialize(); 
        tooltip.update(); 
        // we don't actually need this since we are not animating tooltips 
        tooltip.pivot(); 
        tooltip.transition(easing).draw(); 
       }); 
       chart.options.tooltips.enabled = false; 
      } 
     } 
    }); 
} 

あなたはしかし、私はつまり、このプラグインによって提供される機能は発生しません、プログラムによって特定の状況でこのプラグインを「登録解除」にしたい、私は新しいプラグインを登録していることがわかります。

APIのドキュメントを検索していましたが、「削除する」または「登録を解除する」などの方法がありましたが、役に立たなかった。

アドバイスはありますか?

答えて

0

あなたが登録したプラグインはchart.config.options.showAllTooltipsfalseであれば絶対に何もしません、どちらも二つの方法、beforeRenderafterDrawをオーバーライドしています。したがって、プラグインの機能を無効にするには、手動でチャートオブジェクトを操作し、chart.config.options.showAllTooltipsfalseに設定します。

注意:変更を適用するには、後でチャートをupdateにする必要があります。

警告2:現在のコードがchart.options.tooltips.enabledであるため、プラグインのコードを少し変更する必要があります。 ifにはelseの部分が含まれており、chart.options.tooltips.enabledという値が復元されます。または、プラグインコードをそのままにして、希望の値chart.options.tooltips.enabledと、値chart.config.options.showAllTooltipsを手動で設定することもできます。

関連する問題