Chart.js 混合图表的工具提示悬停自定义

2022-01-22 00:00:00 charts tooltip javascript chart.js

我在尝试使用 chart.js 为混合图表自定义悬停工具提示时遇到了一些问题.

I was having some problem when trying to customize the hover tooltip for mixed chart using chart.js.

我有一个显示特定产品总利润的条形图和一个显示该特定产品总数量的折线图.当我将鼠标悬停在条形图上时,我希望工具提示显示如下内容:

I have a bar chart which show the total profit for certain product and a line chart to show the total quantity of that certain product. When I hover over the bar chart, I wanted the tooltip to show something like:

Total profit: $ 1399.30

价格应采用两位小数格式,附加在总利润:$"后面.当我将鼠标悬停在折线图上时,我想显示如下内容:

The price should be in two decimal format appended to the back of 'Total profit: $'. When I hover over the line chart, I wanted to show something like:

Quantity sold: 40 unit(s)

这是我填充相关数组的代码:

Here is my code to populate related array:

for(var i = 0 ; i < topProductList.length; i++){
    labelData.push(topProductList[i].itemName);
    amountData.push((topProductList[i].price * topProductList[i].quantity).toFixed(2));
    quantityData.push(topProductList[i].quantity);
}

我尝试按照@GRUNT 的建议合并回 x 轴标签的回调:

The callback where I tried to merge back the x-axis label as suggested by @GRUNT:

tooltips: {
     callbacks: {
        title: function(t, d) {
             return d.labels[t[0].index].replace(/
/, ' ');
        }
     }
}

我的图表选项:

var opt = {
    type: "bar",
    data: { 
        labels: labelData, 
        datasets: [{ 
                type: "bar",
                label: 'Total Profit ',
                data: amountData, 
                },{
                type: "line",
                label: 'Quantity Sold ',
                data: quantityData, 
                }] 
        }, 
        options: options
};

现在,当我将鼠标悬停在条形图上时,我得到 Total profit: 1399.3 而对于折线图,Quantity sold: 40 这不是我想要的输出以上.

For now, when I hover my bar chart, I am getting Total profit: 1399.3 and for line chart, Quantity sold: 40 which is not my desired output as above.

任何想法如何覆盖工具提示悬停自定义?

Any ideas how to override the tooltip hover customization?

谢谢!

推荐答案

您可以使用以下tooltip的 label 回调函数,在不同的图表上悬停时显示不同的tooltip标签:

You can use the following tooltip­'s label callback function, for showing different tooltip labels when hovered on different graphs :

tooltips: {
   callbacks: {
      label: function(t, d) {
         var xLabel = d.datasets[t.datasetIndex].label;
         var yLabel = t.yLabel;
         // if line chart
         if (t.datasetIndex === 0) return xLabel + ': ' + yLabel + ' unit(s)';
         // if bar chart
         else if (t.datasetIndex === 1) return xLabel + ': $' + yLabel.toFixed(2);
      }
   }
}

另外,您的第一个数据集应该是 line 图表,然后是 bar ,如下所示:

also, your first dataset should be for line chart, followed by bar , like so :

datasets: [{
   type: "line",
   label: 'Quantity Sold ',
   data: quantityData
}, {
   label: 'Total Profit ',
   data: amountData
}]

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩

var chart = new Chart(ctx, {
   type: 'bar',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
      datasets: [{
         type: "line",
         label: 'Quantity Sold',
         data: [40, 50, 60, 70, 80],
         borderColor: 'orange',
         fill: false
      }, {
         label: 'Total Profit',
         data: [1399.3, 356.62, 1249, 465.23, 1480.4],
         backgroundColor: 'rgba(0, 119, 220, 0.4)',
      }]
   },
   options: {
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true
            }
         }]
      },
      tooltips: {
         callbacks: {
            label: function(t, d) {
               var xLabel = d.datasets[t.datasetIndex].label;
               var yLabel = t.yLabel;
               // if line chart
               if (t.datasetIndex === 0) return xLabel + ': ' + yLabel + ' unit(s)';
               // if bar chart
               else if (t.datasetIndex === 1) return xLabel + ': $' + yLabel.toFixed(2);
            }
         }
      }
   }
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>

相关文章