图表Piechart在悬停时只显示百分比,但它显示数据和百分比标签,我只想显示百分比

cgh8pdjw  于 2023-02-04  发布在  Chart.js
关注(0)|答案(1)|浏览(187)
  1. import React from "react";
  2. import { Pie } from "react-chartjs-2";
  3. function Test() {
  4. const data2 = [20, 10, 8, 10, 30, 3, 1, 12, 5, 2, 1, 2];
  5. //method 2
  6. const names = [
  7. "Burn/ Charity",
  8. "for sale",
  9. "Liquidity",
  10. "Team",
  11. "Locked",
  12. "Released/mo",
  13. "charity",
  14. "Marketing/Sales",
  15. "Tax on Sales",
  16. "Charity",
  17. "Liquidity",
  18. "to Tokes Holder",
  19. ];
  20. let total = data2.reduce(
  21. (accumulator, currentValue) => accumulator + currentValue
  22. );
  23. console.log(total);
  24. let labelss = data2.map(
  25. (value, index) => Math.round((value / total) * 100) + "%"
  26. );
  27. const data4 = data2.map((item, index) => {
  28. return names[index] + " " + labelss[index];
  29. });
  30. const data = {
  31. labels: data4,
  32. datasets: [
  33. {
  34. data: data2,
  35. backgroundColor: [
  36. "red",
  37. "yellow",
  38. "blue",
  39. "black",
  40. "orange",
  41. "grey",
  42. "green",
  43. "salmon",
  44. "grey",
  45. "lightgreen",
  46. "pink",
  47. "yellow",
  48. ],
  49. },
  50. ],
  51. };
  52. return <Pie data={data} />;
  53. }
  54. export default Test;
rjjhvcjd

rjjhvcjd1#

您可以使用工具提示label回调,如下所示:

  1. function Test() {
  2. const data2 = [20, 10, 8, 10, 30, 3, 1, 12, 5, 2, 1, 2];
  3. //method 2
  4. const names = [
  5. "Burn/ Charity",
  6. "for sale",
  7. "Liquidity",
  8. "Team",
  9. "Locked",
  10. "Released/mo",
  11. "charity",
  12. "Marketing/Sales",
  13. "Tax on Sales",
  14. "Charity",
  15. "Liquidity",
  16. "to Tokes Holder",
  17. ];
  18. let total = data2.reduce(
  19. (accumulator, currentValue) => accumulator + currentValue
  20. );
  21. console.log(total);
  22. let labelss = data2.map(
  23. (value, index) => Math.round((value / total) * 100) + "%"
  24. );
  25. const data4 = data2.map((item, index) => {
  26. return names[index] + " " + labelss[index];
  27. });
  28. const data = {
  29. labels: data4,
  30. datasets: [{
  31. data: data2,
  32. backgroundColor: [
  33. "red",
  34. "yellow",
  35. "blue",
  36. "black",
  37. "orange",
  38. "grey",
  39. "green",
  40. "salmon",
  41. "grey",
  42. "lightgreen",
  43. "pink",
  44. "yellow",
  45. ],
  46. }, ],
  47. };
  48. const ctx = document.getElementById('chartJSContainer').getContext('2d');
  49. new Chart(ctx, {
  50. type: 'doughnut',
  51. data: data,
  52. options: {
  53. plugins: {
  54. tooltip: {
  55. callbacks: {
  56. label: (ttItem) => (ttItem.label)
  57. }
  58. }
  59. }
  60. }
  61. });
  62. }
  63. Test();
  1. <body>
  2. <canvas id="chartJSContainer" width="600" height="400"></canvas>
  3. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.js"></script>
  4. </body>

如果我没记错的话,对于react包,你需要在prop变量中提供选项,就像你对data所做的那样,所以你会得到这样的结果:

  1. const opts = {
  2. plugins: {
  3. tooltip: {
  4. callbacks: {
  5. label: (ttItem) => (ttItem.label)
  6. }
  7. }
  8. }
  9. };
  10. return <Pie data={data} options={opts}/>;
展开查看全部

相关问题