【问题标题】:ng2-chart tooltip position change option?ng2-chart 工具提示位置更改选项?
【发布时间】:2019-12-09 01:48:57
【问题描述】:

我正在尝试使用 ng2-chart Angular2 指令为 Chart.js 构建一个圆环图。这个chart.js 有工具提示定位的默认选项,我想让工具提示显示在图表之外。有没有办法让它成为可能?

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

我希望工具提示之类的内容显示在图表之外,而不是显示在图表内部;顶部、底部和侧面。

【问题讨论】:

    标签: angular chart.js ng2-charts


    【解决方案1】:

    您可以像这样在tooltip 上使用custom 属性。

    tooltips:{
      custom:function(tooltipModel){
    
       //Top-Left
        tooltipModel.x=10;
        tooltipModel.y=0;
    
      }.bind(this)
    

    实时示例链接是:https://stackblitz.com/edit/ng2-charts-doughnut-template-6wef9r

    【讨论】:

    • 感谢您的回答。它显示在图表之外,但仅显示在一个点上。我正在寻找显示在每个数据之外的工具提示,即图表周围。
    • @PassionateLeaner 如果你想让它出现在右下角或其他地方,你应该编写自己的算法。
    【解决方案2】:

    您可以通过自定义工具提示来实现此目的。 https://www.chartjs.org/docs/latest/configuration/tooltip.html#position-modes

    以下是我遇到同样问题时实现的源代码。

    • 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>

    输出

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      相关资源
      最近更新 更多