ChartJS 3.8.0堆积条形图组合标签

ygya80vv  于 2022-11-07  发布在  Chart.js
关注(0)|答案(1)|浏览(191)

在chart.js 3.8.0中,是否可以合并堆叠条形图上的标签?我找到的最接近的解决方案是为工具提示页脚回调添加,并在工具提示中手动添加额外的文本,但它不包括与该数据集关联的图例/颜色图标。
示例HTML:

  1. <html>
  2. <head></head>
  3. <body>
  4. <h1>
  5. Stacked Bar Chart (chart.js)
  6. </h1>
  7. <canvas id="chart_display"></canvas>
  8. </body>
  9. </html>

示例Javascript:

  1. const data = {
  2. labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
  3. datasets: [
  4. {
  5. label: 'Retained',
  6. data: [62,68,74,80,66,84],
  7. backgroundColor: 'rgb(0, 178, 169)',
  8. order: 3,
  9. stack: 'stack1'
  10. },
  11. {
  12. label: 'Expired',
  13. data: [5,3,7,9,2,4],
  14. backgroundColor: 'rgb(207, 16, 45)',
  15. order: 3,
  16. stack: 'stack1'
  17. }
  18. ]
  19. }
  20. const config = {
  21. type: 'bar',
  22. data: data,
  23. options: {
  24. scales: {
  25. x: {
  26. stacked: true
  27. },
  28. y: {
  29. stacked: true
  30. }
  31. }
  32. },
  33. };
  34. const reportOutput = new Chart(
  35. document.getElementById('chart_display'),
  36. config
  37. );

Example in JSFiddle
在旧版本中可能会出现这种情况,如下所示:https://www.chartjs.org/docs/3.5.0/samples/bar/stacked.html
希望我刚才忽略了一个简单的配置选项。

ki1q1bka

ki1q1bka1#

您需要将mode: 'index'添加到工具提示配置中以实现该行为:
第一个

相关问题