效果

基础环形饼图.jpg

代码

  1. import React, { useRef, useEffect } from 'react';
  2. const echarts = require('echarts');
  3. const TableList: React.FC<{}> = () => {
  4. const chart: any = useRef(null);
  5. useEffect(() => {
  6. const myChart = echarts.init(chart.current);
  7. const data = [
  8. {
  9. name: '数据1',
  10. value: 36,
  11. },
  12. {
  13. name: '数据2',
  14. value: 20,
  15. },
  16. {
  17. name: '数据3',
  18. value: 16,
  19. },
  20. {
  21. name: '数据4',
  22. value: 10,
  23. },
  24. {
  25. name: '数据5',
  26. value: 9,
  27. },
  28. ];
  29. const option = {
  30. tooltip: {
  31. trigger: 'item',
  32. },
  33. legend: {
  34. // selectedMode: false, // 取消图例上的点击事件
  35. icon: 'circle',
  36. left: '50%',
  37. top: '30%',
  38. textStyle: {
  39. // color: '#000',
  40. rich: {
  41. value: {
  42. align: 'left',
  43. width: 50,
  44. },
  45. name: {
  46. align: 'left',
  47. width: 50,
  48. color: '#25b864',
  49. },
  50. count: {
  51. align: 'left',
  52. width: 50,
  53. color: '#1890ff',
  54. },
  55. },
  56. },
  57. // data: data.map((item) => item.value),
  58. // formatter: (name: string) => {
  59. // console.log(name, 'test name');
  60. // },
  61. formatter: (name: string) => {
  62. const { value } = data.find((item) => item.name === name) || {};
  63. return `{name| ${name}}{value| ${value}%}{count| ¥4544}`;
  64. },
  65. },
  66. series: [
  67. {
  68. // name: '数量',
  69. type: 'pie',
  70. radius: ['40%', '55%'],
  71. center: ['25%', '50%'],
  72. data,
  73. label: {
  74. normal: {
  75. show: false,
  76. position: 'center',
  77. formatter: '{text|{c}}\n{b}',
  78. rich: {
  79. text: {
  80. fontSize: 30,
  81. },
  82. value: {
  83. fontSize: 20,
  84. },
  85. },
  86. },
  87. emphasis: {
  88. show: true,
  89. // textStyle: {
  90. // fontSize: '12',
  91. // },
  92. },
  93. },
  94. },
  95. ],
  96. };
  97. myChart.setOption(option);
  98. }, []);
  99. return (
  100. <div>
  101. test
  102. <div style={{ width: 600, height: 400 }} ref={chart} />
  103. </div>
  104. );
  105. };
  106. export default TableList;