javascript - ChartJS 将工具提示添加到分组条形图

标签 javascript charts chart.js

我正在尝试使用 ChartJS 将欧元符号添加到我的分组条形图的工具提示中。剪断:

tooltips: {
   mode: 'label',
   callbacks: {
      label: function(tooltipItem, data) {
         return data['datasets'][0]['data'][tooltipItem['index']] + '€';
      }
   }
}

此代码适用于我的折线图,但不适用于我的分组条形图。我希望我的条形图在悬停时如下所示:

enter image description here

但是我的图表中没有欧元符号,它只是显示它的值。我做错了什么?

谢谢。

** 编辑

所以我的完整选项如下所示:

options: {
            title: {
                display: true,
                text: 'Title',
            },
            scales: {
                yAxes: [{
                    scaleLabel: {
                        display: true,
                        labelString: 'Wert in €'
                    }
                }],
                xAxes: [{
                    scaleLabel: {
                        display: true,
                        labelString: 'Zeitintervall'
                    }
                }]
            },
            tooltips: {
                mode: 'label',
                    callbacks: {
                        label: function(tooltipItem, data) {
                            return data['datasets'][0]['data'][tooltipItem['index']] + '€';
                        }
                    }
            }
        }

一旦我移除了刻度,它就显示出欧元符号。 所以我的选项现在如下所示:

options: {
            title: {
                display: true,
                text: 'Title'
            },
            tooltips: {
                mode: 'label',
                callbacks: {
                    label: function(tooltipItem, data) {
                        return data['datasets'][0]['data'][tooltipItem['index']] + ' €';
                    }
                }
            }
        }

但现在我遇到了另一个问题,它显示了两个不同柱的相同值: enter image description here

你可以清楚地看到值是不一样的。这里有什么问题?

最佳答案

这可以使用以下工具提示标签回调函数来实现:

tooltips: {
   mode: 'label',
   callbacks: {
      label: function(t, d) {
         var dstLabel = d.datasets[t.datasetIndex].label;
         var yLabel = t.yLabel;
         return dstLabel + ': ' + yLabel + ' €';
      }
   }
}

仅供引用:这与比例无关。它与 scales

一起工作得很好

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

var myChart = new Chart(ctx, {
   type: 'bar',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
      datasets: [{
         label: 'DST1',
         backgroundColor: '#3e95cd',
         data: [3, 2, 4, 5, 1]
      }, {
         label: 'DST2',
         backgroundColor: '#8e5ea2',
         data: [2, 4, 1, 2, 5]
      }]
   },
   options: {
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true,
               stepSize: 1
            }
         }]
      },
      title: {
         display: true,
         text: 'Title'
      },
      tooltips: {
         mode: 'label',
         callbacks: {
            label: function(t, d) {
               var dstLabel = d.datasets[t.datasetIndex].label;
               var yLabel = t.yLabel;
               return dstLabel + ': ' + yLabel + ' €';
            }
         }
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.js"></script>
<canvas id="ctx"></canvas>

关于javascript - ChartJS 将工具提示添加到分组条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47914729/

相关文章:

javascript - 你如何在 angular-chart.js 中设置饼图颜色

javascript - 在 JavaScript 中获取 json-object 的键

javascript - 包含 '%d' 的字符串正在转换为 'NaN'

javascript - 在 javascript 原型(prototype)中使用 valueOf 函数

javascript - 我可以调整或使用给定的 (JavaScript) 图表来创建不同的/新的图表吗?

javascript - Chart.JS 工具提示回调标签和标题 (v3.5)

javascript - Jquery 评分星级无法正常工作

android - 有没有办法在 flutter charts_flutter : ^0. 8.1 中垂直放置标签文本

javascript - 将Chart.JS的DataSource设置为Array

javascript - angular chart js设置条形图的填充颜色