Radar.vue 2.4 KB

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