删除chart.js中的“标签”

gudnpqoy  于 2022-11-06  发布在  Chart.js
关注(0)|答案(3)|浏览(298)

我正在使用Chart.js v2.7.2,想删除“label”字段。关闭它会返回“undefined”,我尝试过的各种选项都没有任何效果。有人对此有新的见解吗?图例、标题等都无法删除它。

  1. let thisChart = new Chart(gov_chart, {
  2. type: 'horizontalBar',
  3. data: {
  4. label: 'I want to remove this',
  5. labels: [data1, data2],
  6. datasets: [{
  7. backgroundColor: ['rgb(240,61,74)', 'rgb(0, 156, 255)'],
  8. data: [data1.count, data2.count],
  9. }]
  10. },
  11. options: {
  12. scales: {
  13. xAxes: [{
  14. ticks: {
  15. beginAtZero: true
  16. }
  17. }]
  18. }
  19. },
  20. legend: {
  21. display: false
  22. },
  23. title: {
  24. display: false
  25. },
  26. tooltips: {
  27. callbacks: {
  28. label: function(tooltipItem) {
  29. return tooltipItem.yLabel;
  30. }
  31. }
  32. }
  33. });
g2ieeal7

g2ieeal71#

请注意,对于3.x版,接受的答案已过时。要删除图例,您现在必须指定插件。https://www.chartjs.org/docs/latest/configuration/legend.html
例如

  1. var chart = new Chart(ctx, {
  2. type: 'bar',
  3. data: data,
  4. options: {
  5. plugins: {
  6. legend: {
  7. display: false
  8. }
  9. }
  10. }
  11. });
55ooxyrt

55ooxyrt2#

label应该在datasets的内部,例如

  1. type: 'horizontalBar',
  2. data: {
  3. labels: [data1, data2],
  4. datasets: [{
  5. label: 'put it here', // => here
  6. backgroundColor: ['rgb(240,61,74)', 'rgb(0, 156, 255)'],
  7. data: [data1.count, data2.count],
  8. }]
  9. },

这样你就不会

**已更新:**如果您不想看到它,请将legend配置放在options内。显然,我看到您的legend位于options对象之外。

  1. options: {
  2. legend: {
  3. display: false
  4. }
  5. }
展开查看全部
5kgi1eie

5kgi1eie3#

您可以使用过滤器来删除您不想显示的标签

  1. options: {
  2. plugins: {
  3. legend: {
  4. labels: {
  5. filter: (l) => (l.text !== 'Label you want to remove')
  6. }
  7. }
  8. }
  9. }

相关问题