cloud-g2.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <div id="cloudId" style="width: 100%;height: 100%;" v-loading="state.loading">
  3. </div>
  4. </template>
  5. <script setup lang="ts">
  6. import {getCurrentInstance, reactive, watch} from "vue";
  7. import {Chart} from "@antv/g2";
  8. const {proxy} = getCurrentInstance()
  9. const props = defineProps({
  10. data: {}
  11. })
  12. const state: any = reactive({
  13. chartChangeData: null,
  14. loading: false
  15. })
  16. const initChart = () => {
  17. const data = props.data.map(v => {
  18. v.value = v.count
  19. v.text = v.keyword
  20. return v
  21. })
  22. if (state.chartChangeData) {
  23. state.chartChangeData(data)
  24. state.loading = false
  25. } else {
  26. const chart = new Chart({
  27. container: 'cloudId',
  28. autoFit: true,
  29. })
  30. chart.wordCloud()
  31. .data(data)
  32. .layout({
  33. spiral: 'rectangular',
  34. fontSize: [10, 40],
  35. })
  36. .encode('color', 'text')
  37. .tooltip({
  38. items: [
  39. {name: '数量', field: 'count'},
  40. ]
  41. })
  42. .legend({
  43. color: {
  44. position: 'bottom',
  45. layout: {
  46. justifyContent: 'center',
  47. },
  48. }
  49. })
  50. chart.render();
  51. state.chartChangeData = (data) => chart.changeData(data)
  52. state.loading = false
  53. }
  54. }
  55. watch(() => props.data, () => {
  56. state.loading = true
  57. setTimeout(() => {
  58. initChart()
  59. }, 100)
  60. })
  61. </script>
  62. <style lang="scss" scoped>
  63. </style>