chart.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <script lang="ts" setup>
  2. import util from "@tools/util";
  3. import { ref, toRaw, computed, onMounted, watch } from 'vue';
  4. import * as echarts from 'echarts'
  5. const chartId = 'chart-' + util.newGUID(); //chartId
  6. const chartIns = ref(null) //chart 实例
  7. const props = defineProps({
  8. xAxis: {
  9. type: Object,
  10. required: true,
  11. default: () => ({})
  12. },
  13. series: {
  14. type: Array,
  15. required: true
  16. },
  17. height: {
  18. type: String,
  19. required: false,
  20. default: '500px'
  21. },
  22. width: {
  23. type: String,
  24. required: false,
  25. default: '500px'
  26. },
  27. title: {
  28. type: String,
  29. required: false
  30. },
  31. subtext: {
  32. type: String,
  33. required: false
  34. }
  35. })
  36. /**定义option */
  37. const option = computed({
  38. get() {
  39. return {
  40. title: {
  41. text: props.title || '',
  42. subtext: props.subtext || '',
  43. textStyle: {
  44. fontSize: util.vh(16),
  45. color: "#000",
  46. },
  47. top: 10,
  48. left: 30
  49. },
  50. angleAxis: props.xAxis || {},
  51. radiusAxis: {},
  52. polar: {
  53. radius: '70%',
  54. center: ['60%','50%']
  55. },
  56. tooltip: {},
  57. series: props.series || [],
  58. legend: {
  59. show: true,
  60. orient: 'vertical',
  61. left: 30,
  62. itemWidth: 16,
  63. itemHeight: 10,
  64. textStyle: {
  65. fontSize: util.vh(10)
  66. },
  67. top: 'middle',
  68. data: ['0-2.5','2.5-5','5-7.5','7.5-10','10-12.5','12.5-15','15-17.5','17.5-20','20-22.5','22.5-25','25-inf']
  69. }
  70. }
  71. },
  72. set(val) { }
  73. })
  74. watch(() => option, (newVal, oldVal) => {
  75. if (chartIns.value) {
  76. console.log(newVal)
  77. const echartIns = toRaw(chartIns.value)
  78. echartIns.setOption(newVal)
  79. }
  80. }, { deep: true })
  81. onMounted(() => {
  82. const echartIns = echarts.init(document.getElementById(chartId))
  83. chartIns.value = echartIns
  84. console.log(JSON.stringify(option.value))
  85. echartIns.setOption(option.value)
  86. console.log(echartIns)
  87. })
  88. </script>
  89. <template>
  90. <div :id="chartId" :style="{ height: props.height, width: props.width }"></div>
  91. </template>