Click event on stacked bar chart - ChartJs(堆积条形图上的单击事件 - ChartJs)
问题描述
我正在尝试在堆积条形图上实现点击事件.
I am trying to implement click event on Stacked Bar chart.
数据如下:
var chartData = {
type: 'horizontalBar',
data: {
labels: ['A0224', 'A3681', 'A3984', 'A4101', 'A4150', 'B0682', 'Others'],
datasets: [
{
label: "P1",
backgroundColor: '#cc3399',
data: [6, 7, 6, 8, 6, 10, 3]
},
{
label: "P2",
backgroundColor: '#0099ff',
data: [8, 9, 5, 8, 6, 10, 3]
},
{
label: "P3",
backgroundColor: '#0022ff',
data: [6, 7, 6, 8, 6, 10, 3]
}
]
},
options: {
legend: { display: false },
scales: {
yAxes: [{
stacked: true
}],
xAxes: [{
stacked: true
}]
},
}
}
点击事件和图表创建方法如下:
var myChart = new
Chart(document.getElementById("createCurrYearHccGapChart"),
chartData);
var canvas = document.getElementById('createCurrYearHccGapChart');
canvas.onclick = function (evt) {
var activePoints = myChart.getElementsAtEvent(evt);
var cdata = activePoints[0]['_chart'].config.data;
var idx = activePoints[0]['_index'];
var label = cdata.datasets[idx].label;
var value = cdata.datasets[0].data[idx];
};
图表如下所示:
以下是我点击单个栏时得到的值:
栏 1 - 标签 = P1,值 = 6
栏 2 - 标签 = P2,值 = 7
栏 3 - 标签 = P3,值 = 6 等等...
Below are the values i am getting wherever i click on single bar:
Bar 1 - label = P1, value = 6
Bar 2 - label = P2, value = 7
Bar 3 - label = P3, value = 6 and etc...
问题:
因此,无论我单击第一个柱,标签都是 P1,因为我将整个柱的索引值设为 0,柱 2 的索引值为 1,依此类推.
Problem:
So wherever i click on the 1st bar the label is P1 its because i am getting the index value as 0 for the entire bar and 1 for bar 2 and so on.
问题:
有什么方法可以识别x坐标值吗?这样我就可以确定点击来自哪个堆栈.
Question:
Is there any way to identify the x coordinate value? so that i can identify the click is from which stack.
推荐答案
您可以使用以下图表的 on-click 事件处理程序来完成此操作...
You could accomplish this using the following on-click event handler for your chart ...
canvas.onclick = function(evt) {
var activePoint = myChart.getElementAtEvent(evt)[0];
var data = activePoint._chart.data;
var datasetIndex = activePoint._datasetIndex;
var label = data.datasets[datasetIndex].label;
var value = data.datasets[datasetIndex].data[activePoint._index];
};
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩
var chartData = {
type: 'horizontalBar',
data: {
labels: ['A0224', 'A3681', 'A3984', 'A4101', 'A4150', 'B0682', 'Others'],
datasets: [{
label: "P1",
backgroundColor: '#cc3399',
data: [6, 7, 6, 8, 6, 10, 3]
}, {
label: "P2",
backgroundColor: '#0099ff',
data: [8, 9, 5, 8, 6, 10, 3]
}, {
label: "P3",
backgroundColor: '#0022ff',
data: [6, 7, 6, 8, 6, 10, 3]
}]
},
options: {
responsive: false,
legend: {
display: false
},
scales: {
yAxes: [{
stacked: true
}],
xAxes: [{
stacked: true
}]
},
}
}
var canvas = document.getElementById('createCurrYearHccGapChart');
var myChart = new Chart(canvas, chartData);
canvas.onclick = function(evt) {
var activePoint = myChart.getElementAtEvent(evt)[0];
var data = activePoint._chart.data;
var datasetIndex = activePoint._datasetIndex;
var label = data.datasets[datasetIndex].label;
var value = data.datasets[datasetIndex].data[activePoint._index];
console.log(label, value);
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="createCurrYearHccGapChart" height="170"></canvas>
这篇关于堆积条形图上的单击事件 - ChartJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:堆积条形图上的单击事件 - ChartJs


基础教程推荐
- 在for循环中使用setTimeout 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 响应更改 div 大小保持纵横比 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01