簡體   English   中英

使用angular-chart.js時如何顯示所有工具提示Chart.js 2

[英]How to show all tooltips when using angular-chart.js Chart.js 2

我想在angular-chart.js圖表​​中打開所有工具提示,然后使用html2canvas獲取更新的圖像。

現在,showTooltip方法不屬於Chart.js。

這是行不通的。

$scope.$on('chart-create', function (event, chart) {
    $scope.bondsChart = chart;
    console.log(chart);
});

$scope.downloadChartFn = function () {
  // *** No showTooltip function on Chart.js 2.3.
  $scope.bondsChart.showTooltip($scope.bondsChart.segments, true);

  // Yes http://stackoverflow.com/questions/36760712/html2canvas-conflict-in-mozila-firefox
  if (document.getElementById('green-bonds-bar-chart-stacked-canvas') !== null) {
      html2canvas(document.getElementById('green-bonds-bar-chart-stacked-canvas'), {
        onrendered: function (canvas) {
          var a = document.createElement("a");
          a.download = "chart.png";
          a.href = canvas.toDataURL("image/png");
          document.body.appendChild(a);
          a.click();
      }
   });
}

圖表插件可以正常工作。 取自此jsfiddle

只需使用選項調用插件

options: {
  showAllTooltips: true
}

我沒有最終使用它,因為沒有算法可以防止工具提示重疊。

在角度圖中,我將其放在app.config中。

app.config(['ChartJsProvider', function (ChartJsProvider) {
}

插件

Chart.pluginService.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) {
        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;
    }
  }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM