jquery 如何禁用chartjs dclick

bweufnob  于 2024-01-07  发布在  jQuery
关注(0)|答案(6)|浏览(198)

我想禁用chart.js蜘蛛图图例点击,因为当我点击图例时,数据序列隐藏了相关的值集,如下图所示。
x1c 0d1x的数据



我的要求是我不想禁用数据集。我已经尝试了preventDefault();在图表上单击,但它不起作用。
我的代码示例附在下面。请检查。

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Radar Chart</title>
  5. <script src="../dist/Chart.bundle.js"></script>
  6. <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. <div style="width:75%">
  10. <canvas id="canvas"></canvas>
  11. </div>
  12. <script>
  13. var randomScalingFactor = function() {
  14. return Math.round(Math.random() * 100);
  15. };
  16. var randomColorFactor = function() {
  17. return Math.round(Math.random() * 255);
  18. };
  19. var randomColor = function(opacity) {
  20. return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
  21. };
  22. var config = {
  23. type: 'radar',
  24. data: {
  25. labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
  26. datasets: [{
  27. label: "My First dataset",
  28. backgroundColor: "rgba(0,0,0,0.5)",
  29. pointBackgroundColor: "rgba(220,220,220,1)",
  30. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
  31. }, {
  32. label: "My Second dataset",
  33. backgroundColor: "rgba(0,120,0,0.5)",
  34. pointBackgroundColor: "rgba(151,187,205,1)",
  35. hoverPointBackgroundColor: "#fff",
  36. pointHighlightStroke: "rgba(151,187,205,1)",
  37. data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
  38. },]
  39. },
  40. options: {
  41. legend: {
  42. position: 'top',
  43. onClick: (e) => e.stopPropagation()
  44. },
  45. title: {
  46. display: true,
  47. text: ''
  48. },
  49. scale: {
  50. reverse: false,
  51. gridLines: {
  52. color: ['black']
  53. },
  54. ticks: {
  55. beginAtZero: true
  56. }
  57. }
  58. }
  59. };
  60. window.onload = function() {
  61. window.myRadar = new Chart(document.getElementById("canvas"), config);
  62. };
  63. </script>
  64. </body>
  65. </html>

字符串

bfnvny8b

bfnvny8b1#

根据文档,有一个onClick处理器用于暴露事件对象的图例。如果你stopPropagation,它会停止隐藏数据序列:

  1. let chart = new Chart(elem.find('canvas')[0], {
  2. type: 'line',
  3. data: {
  4. labels: [],
  5. datasets: []
  6. },
  7. options: {
  8. responsive: true,
  9. maintainAspectRatio: false,
  10. legend: {
  11. onClick: (e) => e.stopPropagation()
  12. }
  13. }
  14. });

字符串
以上是ES6,如果您不使用下面支持的浏览器,则是较旧的ES5等效。

  1. legend: {
  2. onClick: function (e) {
  3. e.stopPropagation();
  4. }
  5. }


Chartjs必须在legend.onClick之后注册自己的click事件,这就是为什么这会停止它的执行。
docs

展开查看全部
z4bn682m

z4bn682m2#

此外,您可以使用null或任何评估false的值来禁用所有图例上的单击事件。

  1. options: {
  2. legend: {
  3. onClick: null
  4. }
  5. }

字符串
注意:在Chart.js(https://github.com/chartjs/Chart.js/blob/6bea15e7cf89003e3a5945a20cf1d2cc5096728e/src/plugins/plugin.legend.js#L481)中的以下代码中忽略onClick的点击事件

vyu0f0g1

vyu0f0g13#

在撰写本文时(Chart.js v3.5.1),正确答案是

  1. options: {
  2. plugins: {
  3. legend: {
  4. onClick: null
  5. }
  6. }
  7. }

字符串
根据上面Natan Almeida de Lima的评论,把它作为一个答案,因为我没有把它看作是一个单行评论,我只是在自己弄清楚之后才发现的。

4dc9hkyq

4dc9hkyq4#

要覆盖单击图例项的默认行为,即在图表中显示/隐藏关联的数据集,您可以使用以下选项(为清晰起见,显示在options内):

  1. options: {
  2. legend: {
  3. onClick: function(event, legendItem) {}
  4. }
  5. }

字符串
这是覆盖默认行为的方法,即提供一个具有相同参数的函数。(因此,立即返回),因为点击图例项时绝对不会发生任何事情。在docs中查找legend.onClick。虽然它目前只出现在两种图表类型下,此选项应该适用于所有图表类型。

l2osamch

l2osamch5#

对于Chart.js版本4.4.0,您可以使用以下解决方案:

  1. options: {
  2. plugins: {
  3. legend: {
  4. onClick: (e) => e.native.stopPropagation()
  5. }
  6. }
  7. }

字符串
附加信息:由onClick触发的事件(在示例中:e)具有以下类型:ChartEvent

btqmn9zl

btqmn9zl6#

您需要添加属性 legendItemClick 来覆盖默认操作。这对饼图很有用

  1. legendItemClick: function(e){
  2. console.log("Clicked an item with text: " + e.text);
  3. e.preventDefault();
  4. }

字符串
https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/chart/events/legenditemclick

相关问题