效果

image.png

代码

  1. import { useRef, useEffect } from 'react';
  2. import * as echarts from 'echarts';
  3. import _ from 'lodash';
  4. const data = [
  5. {
  6. text: '第一项',
  7. num: 100,
  8. },
  9. {
  10. text: '第二项',
  11. num: 55,
  12. },
  13. {
  14. text: '第三项',
  15. num: 88,
  16. },
  17. {
  18. text: '第四项',
  19. num: 66,
  20. },
  21. {
  22. text: '第五项',
  23. num: 46,
  24. },
  25. ];
  26. const max = _.maxBy(data, (item) => item.num)?.num;
  27. const option: echarts.EChartsOption = {
  28. normal: {
  29. backgroundColor: '',
  30. },
  31. tooltip: {
  32. show: true,
  33. trigger: 'item',
  34. axisPointer: {
  35. type: 'shadow',
  36. },
  37. backgroundColor: 'rgba(0, 0, 0, 0.6)',
  38. borderWidth: 1,
  39. borderColor: '#798CB2',
  40. textStyle: {
  41. fontSize: 24,
  42. color: '#fff',
  43. },
  44. },
  45. radar: {
  46. // shape: 'circle',
  47. radius: "60%", // 大小
  48. center: ["50%", "50%"], // 图的位置
  49. nameGap: 10, // 图中工艺等字距离图的距离
  50. name: { // 配置名称
  51. rich: { // 配置名称样式
  52. a: {
  53. color: '#fff',
  54. fontSize: 14,
  55. },
  56. b: {
  57. color: 'rgba(255, 255, 255, 0.6)',
  58. fontSize: 14,
  59. },
  60. },
  61. formatter: (value: string, indicator: any) => {
  62. return `{a|${value}}\n{b|(${indicator.count})}`;
  63. },
  64. },
  65. splitArea: {
  66. areaStyle: {
  67. color: ['transparent'],
  68. },
  69. },
  70. indicator: data.map((item) => ({
  71. text: item.text,
  72. max,
  73. num: item.num,
  74. })),
  75. },
  76. series: [
  77. {
  78. name: '标题名称',
  79. symbol: 'none',
  80. areaStyle: {
  81. color: 'rgba(109, 200, 236, 0.4)',
  82. },
  83. itemStyle: {
  84. color: 'rgba(109, 200, 236, 1)',
  85. },
  86. lineStyle: {
  87. color: 'rgba(109, 200, 236, 1)',
  88. width: 2,
  89. },
  90. type: 'radar',
  91. data: [data.map((item) => item.num)],
  92. },
  93. ],
  94. };
  95. export default () => {
  96. const chartRef = useRef<HTMLDivElement>(null);
  97. const myChart = useRef<echarts.ECharts>();
  98. const initChart = () => {
  99. if (!myChart.current && chartRef?.current) {
  100. myChart.current = echarts.init(chartRef.current);
  101. myChart.current.setOption(option);
  102. }
  103. };
  104. useEffect(() => {
  105. initChart();
  106. }, []);
  107. return (
  108. <div style={{ width: 300, height: 300 }} ref={chartRef} />
  109. );
  110. };

注意

雷达图的 grid 不生效,可以使用 radar: { radius: "60%", center: ["50%", "50%"]} 调整大小和位置。