Pre*_*thy 18 javascript jquery chart.js
我想禁用chart.js蜘蛛图表图例点击,因为当我点击图例时,数据系列隐藏了相关的值集,如下图所示.
我的要求是我不想禁用数据集.我试过了preventDefault(); 在图表上单击但它不起作用.
我的代码示例附在下面.请检查..
<!doctype html>
<html>
<head>
<title>Radar Chart</title>
<script src="../dist/Chart.bundle.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<div style="width:75%">
<canvas id="canvas"></canvas>
</div>
<script>
var randomScalingFactor = function() {
return Math.round(Math.random() * 100);
};
var randomColorFactor = function() {
return Math.round(Math.random() * 255);
};
var randomColor = function(opacity) {
return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
};
var config = {
type: 'radar',
data: {
labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
datasets: [{
label: "My First dataset",
backgroundColor: "rgba(0,0,0,0.5)",
pointBackgroundColor: "rgba(220,220,220,1)",
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
}, {
label: "My Second dataset",
backgroundColor: "rgba(0,120,0,0.5)",
pointBackgroundColor: "rgba(151,187,205,1)",
hoverPointBackgroundColor: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
},]
},
options: {
legend: {
position: 'top',
onClick: (e) => e.stopPropagation()
},
title: {
display: true,
text: ''
},
scale: {
reverse: false,
gridLines: {
color: ['black']
},
ticks: {
beginAtZero: true
}
}
}
};
window.onload = function() {
window.myRadar = new Chart(document.getElementById("canvas"), config);
};
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
ste*_*425 44
根据文档,有一个onClick展示事件对象的图例的处理程序.如果你stopPropagation停止数据系列隐藏:
let chart = new Chart(elem.find('canvas')[0], {
type: 'line',
data: {
labels: [],
datasets: []
},
options: {
responsive: true,
maintainAspectRatio: false,
legend: {
onClick: (e) => e.stopPropagation()
}
}
});
Run Code Online (Sandbox Code Playgroud)
以上是ES6,如果你不使用下面支持的浏览器是较旧的ES5等效.
legend: {
onClick: function (e) {
e.stopPropagation();
}
}
Run Code Online (Sandbox Code Playgroud)
Chartjs必须在legend.onClick这之后注册自己的点击事件,这就是为什么这会阻止它执行.
Q--*_*ten 19
在撰写本文时(Chart.js v3.5.1),正确答案是
options: {
plugins: {
legend: {
onClick: null
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据 Natan Almeida de Lima 的上述评论。将其添加为答案,因为我没有将其视为单行评论,而是在自己弄清楚之后才发现的。
小智 8
另外,您可以使用null或评估false为禁用所有图例上的单击事件的任何值。
options: {
legend: {
onClick: null
}
}
Run Code Online (Sandbox Code Playgroud)
注意:通过onClick在Chart.js中的以下代码上忽略点击事件(https://github.com/chartjs/Chart.js/blob/6bea15e7cf89003e3a5945a20cf1d2cc5096728e/src/plugins/plugin.legend.js#L481)
要覆盖单击图例项的默认行为,即在图表中显示/隐藏关联数据集,您可以使用以下选项(options为清晰起见,在内部显示):
options: {
legend: {
onClick: function(event, legendItem) {}
}
}
Run Code Online (Sandbox Code Playgroud)
这是覆盖默认行为的方法,即通过提供具有相同参数的函数.根据您的要求,此函数应该有一个空体(因此,立即返回),因为单击图例项时绝对不会发生任何事情.legend.onClick在文档中查找.虽然它目前只出现在两种图表类型下,但此选项应适用于所有图表类型.
| 归档时间: |
|
| 查看次数: |
16182 次 |
| 最近记录: |