ChartJS为堆叠条形图添加自定义工具提示

问题描述 投票:0回答:1

我有以下不同月份的数据,如下表所示。

var barChartData = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
        label: 'Task 1',
        backgroundColor: "rgba(220,220,220,0.5)",
        data: [50, 40, 23, 45, 67, 78, 23]
    }, {
        label: 'Task 2',
        backgroundColor: "rgba(151,187,205,0.5)",
        data: [50, 40, 78, 23, 23, 45, 67]
    }, {
        label: 'Task 3',
        backgroundColor: "rgba(82,154,190,0.5)",
        data: [50, 67, 78, 23, 40, 23, 0]
    }]
};  

我想生成一个像这样的工具提示。

预期图表

我试过以下代码

var ctx = document.getElementById("canvas").getContext("2d");
var myBar = new Chart(ctx, {
    type: 'bar',
    data: barChartData,
    options: {
        title: {
            display: true,
            text: "Chart.js Bar Chart - Stacked"
        },
        tooltips: {
            mode: 'label',
            callbacks: {
                label: function(tooltipItem, data) {
                  
                   var tasks = data.datasets[tooltipItem.datasetIndex].label;
                   var valor = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
                   var count =  data.datasets.length;
                   var total = 0;
                   var label = '';
                   
                   for (var i = 0; i < data.datasets.length; i++) {
                       total += data.datasets[i].data[tooltipItem.index];                       
                   }                   
                   label += "Total : " + total + "(";
                   
                    for (var i = 0; i < data.datasets.length; i++) {
                       label += data.datasets[tooltipItem.datasetIndex].label + " - $" + tooltipItem.yLabel.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ",";
                    }
                    var lastChar = label.slice(-1);
                    if (lastChar === ',') {
                        label = label.slice(0, -1);
                    }
                    
                    label += ")";
                   return [ label, "Count : " + count ];
                }
            }
        },
        responsive: true,
        scales: {
            xAxes: [{
                stacked: true,
            }],
            yAxes: [{
                stacked: true
            }]
        }
    }
});

我需要每月显示一个工具提示,所以我将模式切换为 "标签",它就会创建三个独立的工具提示。我怎样才能达到预期的输出?

谢谢,谢谢

javascript chart.js
1个回答
0
投票

你已经差不多了......诀窍是回调函数必须收集并返回一个 "任务1 "和 "任务2 "的数据集。array 的标签,但不是每一个 dataset.

callbacks: {
  label: (tooltipItem, data) => {
    if (tooltipItem.datasetIndex > 0) {
      return null;
    }

在组成主标签时,还存在访问错误的objectsproperties的问题。

请看下面你修改后的代码。

new Chart("canvas", {
  type: 'bar',
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: 'Task 1',
      backgroundColor: "rgba(220,220,220,0.5)",
      data: [50, 40, 23, 45, 67, 78, 23]
    }, {
      label: 'Task 2',
      backgroundColor: "rgba(151,187,205,0.5)",
      data: [50, 40, 78, 23, 23, 45, 67]
    }, {
      label: 'Task 3',
      backgroundColor: "rgba(82,154,190,0.5)",
      data: [50, 67, 78, 23, 40, 23, 0]
    }]
  },
  options: {
    title: {
      display: true,
      text: "Chart.js Bar Chart - Stacked"
    },
    tooltips: {
      mode: 'label',
      callbacks: {
        label: (tooltipItem, data) => {
          if (tooltipItem.datasetIndex > 0) {
            return null;
          }
          var tasks = data.datasets[tooltipItem.datasetIndex].label;
          var valor = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
          var count = data.datasets.length;
          var total = 0;
          var label = '';
          for (var i = 0; i < data.datasets.length; i++) {
            total += data.datasets[i].data[tooltipItem.index];
          }
          label += "Total : " + total + " (";
          for (var i = 0; i < data.datasets.length; i++) {
            if (label.endsWith(",")) {
              label += " ";
            }
            label += data.datasets[i].label + " - $" + data.datasets[i].data[tooltipItem.index].toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ",";
          }
          label = label.slice(0, -1) + ")";
          return [label, "Count : " + count];
        }
      }
    },
    responsive: true,
    scales: {
      xAxes: [{
        stacked: true,
      }],
      yAxes: [{
        stacked: true
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="canvas" height="100"></canvas>
© www.soinside.com 2019 - 2024. All rights reserved.