如何在Chart.JS 2.0上使用tooltipTemplate

How to use the tooltipTemplate on Chart.JS 2.0

本文关键字:tooltipTemplate Chart JS      更新时间:2023-09-26

我正在尝试将甜甜圈图与多个数据集一起使用,并使用tooltipTemplate功能自定义工具提示中的文本,但没有任何效果。这在以前的Chart-js版本中有效,但不支持多个数据集。有人能帮忙吗?以下是我的代码:

options: {
    tooltips: {
        tooltipTemplate: "<%if (label){%><%=value%><%} else {%> No data <%}%>",
    },
}

正如potatopeelings在评论中提到的,您必须为工具提示设置回调。

这里有一个例子:

options: {
  tooltips: {
    callbacks: {
      label: function(tooltipItem, data) {
        var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || 'Other';
        var label = data.labels[tooltipItem.index];
        return datasetLabel + ': ' + label;
      }
    }
  }
}

现场演示

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'doughnut',
    data: {
        labels: ["Men", "Women", "Unknown"],
        datasets: [{
            label: 'Sweden',
            data: [60, 40, 20],
            backgroundColor: ['rgba(158, 216, 202, 0.75)', 'rgba(255, 150, 162, 0.75)', 'rgba(160, 160, 160, 0.75)']
        }, {
            label: 'Netherlands',
            data: [40, 70, 10],
            backgroundColor: ['rgba(158, 216, 202, 0.5)', 'rgba(255, 150, 162, 0.5)', 'rgba(160, 160, 160, 0.5)']
        }, {
            data: [33, 33, 34],
            backgroundColor: ['rgba(158, 216, 202, 0.25)', 'rgba(255, 150, 162, 0.25)', 'rgba(160, 160, 160, 0.25)']
        }]
    },
    options: {
        tooltips: {
            callbacks: {
                label: function(tooltipItem, data) {
                    var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || 'Other';
                    var label = data.labels[tooltipItem.index];
                    return datasetLabel + ': ' + label;
                }
            }
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.2/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>

Chart.js 1.x工具提示模板等效于Chart.js 2.x:中的options.tooltips.callbacks.title

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'doughnut',
  data: {
    labels: [
      "Men",
      "Women",
      "Unknown"
    ],
    datasets: [{
      label: 'Sweden',
      data: [60, 40, 20],
      backgroundColor: [
        'rgba(158, 216, 202, 0.75)',
        'rgba(255, 150, 162, 0.75)',
        'rgba(160, 160, 160, 0.75)'
      ]
    }, {
      label: 'Netherlands',
      data: [40, 70, 10],
      backgroundColor: [
        'rgba(158, 216, 202, 0.5)',
        'rgba(255, 150, 162, 0.5)',
        'rgba(160, 160, 160, 0.5)'
      ]
    }, {
      data: [33, 33, 34],
      backgroundColor: [
        'rgba(158, 216, 202, 0.25)',
        'rgba(255, 150, 162, 0.25)',
        'rgba(160, 160, 160, 0.25)'
      ]
    }]
  },
  options: {
    tooltips: {
      callbacks: {
        label: function(tooltipItem, data) {
                return 'This value ' + tooltipItem.yLabel;
        },
        title: function(tooltipItem, data) {
           return 'The tooltip title ' + tooltipItem[0].xLabel;
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.2/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>

必须将工具提示modeoptions设置为显示多个工具提示的label

options: {
    tooltips: {
        mode : 'label'
    }
}

如果你想隐藏标签,你可以简单地尝试这个

options = 
  {                       
    tooltips :{          
      titleFontSize : 0,
      titleMarginBottom:-0.5
    }
  }

工具提示参考https://www.chartjs.org/docs/latest/configuration/tooltip.html