Radar.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <div ref="chartRef" :style="{ height, width }"></div>
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, PropType, ref, Ref, reactive, watchEffect } from 'vue';
  6. import { useECharts } from '/@/hooks/web/useECharts';
  7. export default defineComponent({
  8. name: 'Radar',
  9. props: {
  10. chartData: {
  11. type: Array,
  12. default: () => [],
  13. },
  14. option: {
  15. type: Object,
  16. default: () => ({}),
  17. },
  18. width: {
  19. type: String as PropType<string>,
  20. default: '100%',
  21. },
  22. height: {
  23. type: String as PropType<string>,
  24. default: 'calc(100vh - 78px)',
  25. },
  26. },
  27. setup(props) {
  28. const chartRef = ref<HTMLDivElement | null>(null);
  29. const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
  30. const option = reactive({
  31. title: {
  32. text: '基础雷达图',
  33. },
  34. legend: {
  35. data: ['文综', '理综'],
  36. },
  37. radar: {
  38. indicator: [
  39. { name: '历史', max: 100 },
  40. { name: '地理', max: 110 },
  41. { name: '生物', max: 120 },
  42. { name: '化学', max: 130 },
  43. { name: '物理', max: 140 },
  44. { name: '政治', max: 150 },
  45. ],
  46. },
  47. series: [
  48. {
  49. type: 'radar',
  50. data: [
  51. {
  52. value: [82, 70, 60, 55, 90, 66],
  53. name: '文综',
  54. },
  55. ],
  56. },
  57. ],
  58. });
  59. watchEffect(() => {
  60. props.chartData && initCharts();
  61. });
  62. function initCharts() {
  63. if (props.option) {
  64. Object.assign(option, props.option);
  65. }
  66. //图例类型
  67. let typeArr = Array.from(new Set(props.chartData.map((item) => item.type)));
  68. //雷达数据
  69. let indicator = Array.from(
  70. new Set(
  71. props.chartData.map((item) => {
  72. let { name, max } = item;
  73. return { name, max };
  74. })
  75. )
  76. );
  77. let data = [];
  78. typeArr.forEach((type) => {
  79. let obj = { name: type };
  80. let chartArr = props.chartData.filter((item) => type === item.type);
  81. obj['value'] = chartArr.map((item) => item.value);
  82. //data数据
  83. data.push(obj);
  84. });
  85. option.radar.indicator = indicator;
  86. option.series[0]['data'] = data;
  87. setOptions(option);
  88. }
  89. return { chartRef };
  90. },
  91. });
  92. </script>