如何在不中断其正常工作的情况下触发ChartJS图例onClick

e0bqpujr  于 2023-11-18  发布在  Chart.js
关注(0)|答案(2)|浏览(184)

当图例在chart.js饼(v2.5.0)图表中单击时,我想记录单击的图例。为此,我向图例添加了onClick函数,并在其中调用默认函数Chart.defaults.global.legend.onClick。但它变得错误,图表没有更新。
这是我用过的配置

  1. "legend": {
  2. "position": "top",
  3. "onClick": function(e,l){
  4. console.log("l.text");
  5. Chart.defaults.global.legend.onClick.call(this, e, l);
  6. }
  7. }

字符串
在获取此错误时单击图例
未捕获的TypeError:无法读取未定义的属性“_Meta”
我也试过了,也不管用。

  1. var original = Chart.defaults.global.legend.onClick;
  2. Chart.defaults.global.legend.onClick = function(e, legendItem) {
  3. /* do custom stuff here */
  4. original.call(this, e, legendItem);
  5. };

b1zrtrql

b1zrtrql1#

它几乎看起来像是文档的例子是错误的,似乎他们没有给予图表,这就是为什么它失败,如果你自己提供图表,并编写完整的onclick它确实工作
范例:

  1. const defaultLegendClickHandler = Chart.defaults.global.legend.onClick;
  2. var newLegendClickHandler = function(e, legendItem) {
  3. alert(legendItem.text)
  4. const index = legendItem.index;
  5. const {
  6. type
  7. } = chart.config;
  8. if (type === 'pie' || type === 'doughnut') {
  9. // Pie and doughnut charts only have a single dataset and visibility is per item
  10. for (i = 0, ilen = (chart.data.datasets || []).length; i < ilen; ++i) {
  11. meta = chart.getDatasetMeta(i);
  12. // toggle visibility of index if exists
  13. if (meta.data[index]) {
  14. meta.data[index].hidden = !meta.data[index].hidden;
  15. }
  16. }
  17. } else {
  18. const index = legendItem.datasetIndex;
  19. const ci = this.chart;
  20. const meta = ci.getDatasetMeta(index);
  21. // See controller.isDatasetVisible comment
  22. meta.hidden = meta.hidden === null ? !ci.data.datasets[index].hidden : null;
  23. // We hid a dataset ... rerender the chart
  24. ci.update();
  25. }
  26. chart.update();
  27. };
  28. const options = {
  29. type: 'pie',
  30. data: {
  31. labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
  32. datasets: [{
  33. label: '# of Votes',
  34. data: [12, 19, 3, 5, 2, 3],
  35. borderWidth: 1
  36. }, {
  37. label: '# of Votes2',
  38. data: [12, 19, 3, 5, 2, 3],
  39. borderWidth: 1
  40. }, {
  41. label: '# of Votes3',
  42. data: [12, 19, 3, 5, 2, 3],
  43. borderWidth: 1
  44. }, {
  45. label: '# of Votes4',
  46. data: [12, 19, 3, 5, 2, 3],
  47. borderWidth: 1
  48. }]
  49. },
  50. options: {
  51. legend: {
  52. onClick: newLegendClickHandler
  53. }
  54. }
  55. }
  56. const ctx = document.getElementById('chartJSContainer').getContext('2d');
  57. const chart = new Chart(ctx, options);

个字符

展开查看全部
qojgxg4l

qojgxg4l2#

您可以通过使用以下行更新图例来更新图例onClick。

  1. @ViewChild('barChart') barChart: BaseChartDirective;
  2. legend: {
  3. labels: {
  4. onClick: (event, barLegendItem, legend) => {
  5. const datasetIndex = barLegendItem.datasetIndex;
  6. const scatterDataset = this.barChartData.datasets[datasetIndex - this.noOfInnings];
  7. const barDataset = this.barChartData.datasets[datasetIndex];
  8. scatterDataset.hidden = !scatterDataset.hidden;
  9. barDataset.hidden = !barDataset.hidden;
  10. legend.chart.update(this.barChart);
  11. }
  12. }
  13. HTML :
  14. canvas id="chart1" class="w-100 h-100" baseChart
  15. #barChart
  16. [data]="barChartData"
  17. [options]="barChartOptions"
  18. [plugins]="barChartPlugins"
  19. [legend]="barChartLegend">
  20. </canvas>

字符串

展开查看全部

相关问题