简体   繁体   English

使用 chart.js 在工具提示中显示两个值

[英]Displaying two values in tooltip using chart.js

I want to display two values(Amount, paid status) in the tooltip when the user hovers display Bar Dataset and Line Dataset based on the month.当用户悬停显示基于月份的条形数据集和线形数据集时,我想在工具提示中显示两个值(金额,支付状态)。 currently, it's displaying a BarDataset value目前,它正在显示 BarDataset 值

let amount= [500, 2000, 1400, 900];
let paidStatus = ["Unpaid", "Paid", "Unpaid","Paid"];
let Months = ['January', 'February', 'March', 'April'];
let myChart = document.getElementById('myChart').getContext('2d');

let massPopChart = new Chart(myChart, {
 type: 'bar',
data: {
    datasets: [{
        label: 'Bar Dataset',
        data: amount
    }, {
        label: 'Line Dataset',
        data: paidStatus,

        // Changes this dataset to become a line
        type: 'line'
    }],
    labels: Months
},
options: {
  tooltips: {
  mode: 'index'
}
}
});`

You might want to try representing the "paid status" as a numeric (0-unpaid, 1-paid) then use a callback to customize the tooltip:您可能想尝试将“付费状态”表示为数字(0-未付费,1-已付费),然后使用回调来自定义工具提示:

function(tooltipItems, data) {
   var y = tooltipItems.yLabel;
    if(tooltipItems.datasetIndex === 1) {
      tooltipItems.yLabel === 0 ? y = 'unpaid' : y = 'paid'
    }
    return y
  }
}

jsfiddle jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM