ng2-chart tooltip position change option?

假装没事ソ 提交于 2021-01-28 06:40:23

问题


I am trying to build a Doughnut Chart using ng2-chart Angular2 directives for Chart.js. This chart.js has default option for tooltip positioning, I want to make tooltip to show outside the chart. Is there a way to make it possible?

public doughnutChartOptions = {
 responsive: true,
  tooltips:{
   mode: 'nearest',
  }
}

I expect something like tooltip to be displayed outside the chart, Instead of showing inside the chart; top, bottom and to sides.


回答1:


You can use custom property on tooltip like this.

tooltips:{
  custom:function(tooltipModel){

   //Top-Left
    tooltipModel.x=10;
    tooltipModel.y=0;

  }.bind(this)

And live example link is: https://stackblitz.com/edit/ng2-charts-doughnut-template-6wef9r




回答2:


You can achieve this through the custom tooltip. https://www.chartjs.org/docs/latest/configuration/tooltip.html#position-modes

Below is the source code that I had implemented when I was facing the same issue.

  • xyz.component.ts

// import the required classes and modules.

@ViewChild("piechart", { static: true }) private piechartRef;

// put the below code into the method based on your requirement

// pie chart start
this.piechart = new Chart(this.piechartRef.nativeElement, {
      type: 'pie',
      data: {
        labels: ["Normal", "Human Impersonation", "Bot"],
        datasets: [
          {
            fill: true,
            backgroundColor: ['#53AF50', '#F8DBD8', 'rgb(255, 129, 0)'],
            data: [73, 42, 19]
          }
        ]
      },
      options: {
        title: {
          display: false,
        },
        legend: {
          display: false,
        },
        tooltips: {
          // Disable the on-canvas tooltip
          enabled: false,
          custom: function (tooltipModel) {
            // Tooltip Element
            var tooltipEl = document.getElementById('chartjs-tooltip');

            // Create element on first render
            if (!tooltipEl) {
              tooltipEl = document.createElement('div');
              tooltipEl.id = 'chartjs-tooltip';
              tooltipEl.innerHTML = '<table></table>';
              tooltipEl.style.backgroundColor = "#FFFFFF";
              tooltipEl.style.borderColor = "#B2BABB";
              tooltipEl.style.borderWidth = "thin";
              tooltipEl.style.borderStyle = "solid";
              document.body.appendChild(tooltipEl);
            }

            // Hide if no tooltip
            if (tooltipModel.opacity === 0) {
              tooltipEl.style.opacity = "0";
              return;
            } else {
              tooltipEl.style.opacity = "1";
            }

            // Set caret Position
            tooltipEl.classList.remove('above', 'below', 'no-transform');
            if (tooltipModel.yAlign) {
              tooltipEl.classList.add(tooltipModel.yAlign);
            } else {
              tooltipEl.classList.add('no-transform');
            }

            function getBody(bodyItem) {
              return bodyItem.lines;
            }

            // Set Text
            if (tooltipModel.body) {
              var titleLines = tooltipModel.title || [];
              var bodyLines = tooltipModel.body.map(getBody);

              var innerHtml = '<thead>';

              titleLines.forEach(function (title) {
                innerHtml += '<tr><th>' + title + '</th></tr>';
              });
              innerHtml += '</thead><tbody>';

              bodyLines.forEach(function (body, i) {
                let name = body[0].split(":")[0];
                let icon = null;
                if(name == "Human Impersonation"){
                  icon = '<div style="margin-top:2px;margin-right:2px;float: left;height: 15px;width: 15px;border: 1px solid black;clear: both;background-color: #F8DBD8;"></div>';
                  name = icon + name;
                }else if(name == "Bot"){
                  icon = '<div style="margin-top:2px;margin-right:2px;float: left;height: 15px;width: 15px;border: 1px solid black;clear: both;background-color: rgb(255, 129, 0);"></div>';
                  name = icon + name;
                }else if(name == "Normal"){
                  icon = '<div style="margin-top:2px;margin-right:2px;float: left;height: 15px;width: 15px;border: 1px solid black;clear: both;background-color: #53AF50;"></div>';
                  name = icon + name;
                }
                let value = body[0].split(":")[1];
                innerHtml += '<tr><td style="padding: 0px"><b>' + name + ':</b> ' + value + '</td></tr>';
              });
              innerHtml += '</tbody>';

              var tableRoot = tooltipEl.querySelector('table');
              tableRoot.innerHTML = innerHtml;
            }

            // `this` will be the overall tooltip
            var position = this._chart.canvas.getBoundingClientRect();

            // Display, position, and set styles for font
            tooltipEl.style.position = 'absolute';
            tooltipEl.style.left = position.left + window.pageXOffset + tooltipModel.caretX + 'px';
            tooltipEl.style.top = position.top + window.pageYOffset + tooltipModel.caretY + 'px';
            tooltipEl.style.fontFamily = tooltipModel._bodyFontFamily;
            tooltipEl.style.fontSize = tooltipModel.bodyFontSize + 'px';
            tooltipEl.style.fontStyle = tooltipModel._bodyFontStyle;
            tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px';
            tooltipEl.style.pointerEvents = 'none';
          }
        }
      }
    });
    this.piechart.update();
    // pie chart end
<canvas #piechart style="height: 60px !important">
    {{piechart}}
</canvas>

output



来源:https://stackoverflow.com/questions/57292496/ng2-chart-tooltip-position-change-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!