scatterSingleChart.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <script setup name="scatterSingleChart">
  2. import util from "@tools/util";
  3. import chartTheme from './../chartTheme.json'
  4. import { ref, toRaw, computed, onMounted, watch, nextTick } from 'vue';
  5. import * as echarts from 'echarts'
  6. const chartId = 'chart-' + util.newGUID(); //chartId
  7. const chartIns = ref(null) //chart 实例
  8. const props = defineProps({
  9. xAxis: {
  10. type: Array,
  11. required: true,
  12. default: () => ([])
  13. },
  14. yAxis: {
  15. type: Array,
  16. required: true,
  17. default: () => ([])
  18. },
  19. series: {
  20. type: Array,
  21. required: false,
  22. default: () => ([])
  23. },
  24. height: {
  25. type: String,
  26. required: false,
  27. default: '500px'
  28. },
  29. title: {
  30. type: String,
  31. required: false,
  32. default: ''
  33. },
  34. subtext: {
  35. type: String,
  36. required: false,
  37. default: ''
  38. },
  39. width: {
  40. type: String,
  41. required: false,
  42. default: '500px'
  43. },
  44. })
  45. /**定义option */
  46. const option = computed({
  47. get() {
  48. return {
  49. tooltip: {
  50. position: 'top',
  51. formatter: function (params) {
  52. return (
  53. params.value[2] +
  54. ' commits in ' +
  55. params.value[0] +
  56. ' of ' +
  57. params.value[1]
  58. );
  59. }
  60. },
  61. title: {
  62. text: props.title || '',
  63. subtext: props.subtext || '',
  64. top: 6,
  65. left: '5%',
  66. },
  67. grid: {
  68. top: 40,
  69. left: 2,
  70. bottom: 10,
  71. right: 20,
  72. containLabel: true
  73. },
  74. xAxis: props.xAxis || [],
  75. // {
  76. // type: 'category',
  77. // data: props.xAxis || [],
  78. // boundaryGap: false,
  79. // splitLine: {
  80. // show: true
  81. // },
  82. // axisLine: {
  83. // show: false
  84. // }
  85. // },
  86. yAxis: props.yAxis || [],
  87. // {
  88. // type: 'category',
  89. // data: props.yAxis,
  90. // axisLine: {
  91. // show: false
  92. // }
  93. // },
  94. series: props.series || []
  95. // [
  96. // {
  97. // name: 'Punch Card',
  98. // type: 'scatter',
  99. // symbolSize: function (val) {
  100. // return val[2] * 2;
  101. // },
  102. // data: props.data,
  103. // animationDelay: function (idx) {
  104. // return idx * 5;
  105. // }
  106. // }
  107. // ]
  108. }
  109. },
  110. set(val) { }
  111. })
  112. watch(() => option, (newVal, oldVal) => {
  113. if (chartIns.value) {
  114. console.log(newVal)
  115. const echartIns = toRaw(chartIns.value)
  116. echartIns.setOption(toRaw(newVal.value))
  117. }
  118. }, { deep: true })
  119. onMounted(() => {
  120. nextTick(() => {
  121. echarts.registerTheme('chartTheme', chartTheme)
  122. const echartIns = echarts.init(document.getElementById(chartId), 'chartTheme')
  123. chartIns.value = echartIns
  124. echartIns.setOption(option.value)
  125. window.addEventListener('resize', () => {
  126. echartIns.resize()
  127. })
  128. })
  129. })
  130. </script>
  131. <template>
  132. <div :id="chartId" :style="{ height: props.height, width: props.width }"></div>
  133. </template>