获取yLabel值onclick图表js [英] get yLabel value onclick chart js
本文介绍了获取yLabel值onclick图表js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我单击栏时,我想要y标签的值.
I want to have the y label value, when I click on the bar.
就像上面的示例一样,当我单击2月的蓝色条时,我想要的标签值为40
Like the above example, when I click on feb blue bar I want to have label value, which is 40
我在stackOverFlow和其他站点上查找了示例,但它们仅具有显示图例的标签示例.
I looked for examples on stackOverFlow and other sites but they only have example of label for the legend show up.
我尝试过的一些代码
onClick: function(evt, element) {
var activePoints = bar_chart.getElementAtEvent(evt);
console.log(activePoints[0]._model.datasetLabel);
}
推荐答案
单击此演示时,此演示会在条形图中提供值.我不是chart.js专家,所以可能会有更好的解决方案.
This demo gives the value in the bar chart when you click on it. I'm not a chart.js expert so there may be better solutions.
https://codepen.io/newschapmj1/pen/PerOzM
/* from https://github.com/chartjs/Chart.js/issues/2292 */
document.getElementById("myChart").onclick = function (evt) {
var activePoints = myChart.getElementsAtEventForMode(evt, 'point', myChart.options);
var firstPoint = activePoints[0];
var label = myChart.data.labels[firstPoint._index];
var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
alert(label + ": " + value);
};
这篇关于获取yLabel值onclick图表js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文