SyndromeChart.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <script setup lang="ts">
  2. import { provide, ref } from 'vue';
  3. import VChart, { type EChartsOption, theme } from './echart';
  4. const { dataset = [] } = defineProps<{ dataset?: { label: string; score: number; }[] }>();
  5. const snapshot = defineModel('snapshot', { default: '' });
  6. provide(...theme);
  7. const option = ref<EChartsOption>({
  8. backgroundColor: 'transparent',
  9. color: [ '#38ff6e' ],
  10. textStyle: {
  11. fontStyle: 24,
  12. },
  13. radar: {
  14. indicator: [],
  15. axisLine: { lineStyle: { color: 'rgba(56,255,110, 0.2)', }, },
  16. splitLine: { lineStyle: { color: 'transparent', }, },
  17. },
  18. series: {
  19. type: 'radar',
  20. label: { show: false },
  21. data: [
  22. {
  23. value: [ 60, 73, 85, 40 ],
  24. name: '中医证素',
  25. },
  26. ],
  27. },
  28. });
  29. watchEffect(() => {
  30. const indicator: { name: string; max: number }[] = [];
  31. const value: number[] = [];
  32. for ( const item of dataset ) {
  33. const { label: name, score } = item;
  34. const max = 110;
  35. indicator.push({ name, max });
  36. value.push(score);
  37. }
  38. option.value.radar = {
  39. ...option.value.radar,
  40. indicator,
  41. };
  42. option.value.series = {
  43. ...option.value.series,
  44. data: [ { name: '中医证素', value } ],
  45. };
  46. });
  47. const chart = useTemplateRef<InstanceType<typeof VChart>>('chart');
  48. let loaded = false;
  49. function onFinished() {
  50. if (loaded || !snapshot.value.startsWith(`data:image/`)) return;
  51. snapshot.value = chart.value?.getDataURL() ?? '';
  52. loaded = true;
  53. }
  54. </script>
  55. <template>
  56. <div class="mx-auto" style="width: 50%;">
  57. <div class="chart-container">
  58. <v-chart ref="chart" class="chart" :option="option" @finished="onFinished" />
  59. </div>
  60. </div>
  61. </template>
  62. <style scoped lang="scss">
  63. .chart-container {
  64. position: relative;
  65. padding-bottom: 100%;
  66. > .chart {
  67. position: absolute;
  68. top: 0;
  69. left: 0;
  70. width: 100%;
  71. height: 100%;
  72. }
  73. }
  74. </style>