我正在使用Chart JS,尝试在条形图标签中创建单击事件,单击事件在x轴下方不起作用,我尝试在画布中的单击事件中使用其他方法实现此功能,但不起作用
HTML程式码:
<div style="width:700px; height: 250px;" >
<canvas id="myChart" style="width:400px; height: 200px;" #mychart
(chartClick)="chartClicked($event)">
</canvas>
</div>
Chart.ts:
this.canvas = this.mychart.nativeElement;
this.ctx = this.canvas.getContext('2d');
let subPerf = new Chart(this.ctx, {
type: 'line',
data: {
labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
datasets: [{
label: 'My Dataset',
backgroundColor: "rgba(255, 99, 132,0.4)",
borderColor: "rgb(255, 99, 132)",
fill: true,
data: [10, 12, 6, 5, 11, 7, 9]
}]
},
options: {
responsive: true,
onClick: event => {
console.log(event)
const canvasPosition = getRelativePosition(event, subPerf);
// Substitute the appropriate scale IDs
const dataX = subPerf.scales.x.getValueForPixel(canvasPosition.x);
console.log(dataX)
const dataY = subPerf.scales.y.getValueForPixel(canvasPosition.y);
// let point = Chart.helpers.getRelativePosition(event, subPerf.chart);
// let xIndex = subPerf.scales['x-axis-0'].getValueForPixel(point.x);
// let label = subPerf.data.labels[xIndex];
// console.log(label + ' at index ' + xIndex);
},
scales: {
xAxis:{
grid: {
display: false,
drawBorder: false
},
title: {
display: true,
text: "ss",
},
},
yAxis:{
grid: {
display: false,
drawBorder: false,
},
title: {
display: true,
text: "jj",
},
},
},
}
});
插入图像,绿色区域无法点击
版本Angular -14 ng-charts-4.0.1图表-3.9.0节点-16.15.1提前感谢
1条答案
按热度按时间ndasle7k1#
chart.js选项的
onClick
不起作用,这些事件似乎只在图表区域内起作用。如果你查看文档,它没有明确说明这一点,但如果你阅读你看到的所有模式,它们都适合获取相对于数据点(link to documentation)的位置/项目/...。唯一方法是通过一个eventlistener。类似于 (出于调试原因,我会让它保持简单):
但我不太了解Angular