score:1

Accepted answer

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

score:-1

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

enter image description here


Related Query

More Query from same tag