graph.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div ref="chart" class="chart" :style="{ width: width + 'px', height: height + 'px' }"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. export default {
  7. name: 'Graph',
  8. props: {
  9. // 图表宽度
  10. width: {
  11. type: Number,
  12. default: 500
  13. },
  14. // 图表高度
  15. height: {
  16. type: Number,
  17. default: 200
  18. },
  19. // 关系图数据
  20. graphData: {
  21. type: Object,
  22. required: true
  23. }
  24. },
  25. data() {
  26. return {
  27. chartInstance: null
  28. }
  29. },
  30. watch: {
  31. graphData: {
  32. handler(newVal) {
  33. if (this.chartInstance) {
  34. this.setOptions()
  35. }
  36. },
  37. deep: true
  38. }
  39. },
  40. mounted() {
  41. this.initChart()
  42. },
  43. beforeDestroy() {
  44. if (this.chartInstance) {
  45. this.chartInstance.dispose()
  46. }
  47. },
  48. methods: {
  49. initChart() {
  50. this.chartInstance = echarts.init(this.$refs.chart)
  51. this.setOptions()
  52. },
  53. setOptions() {
  54. const options = {
  55. title: {},
  56. tooltip: {},
  57. legend: {},
  58. animationDurationUpdate: 1500,
  59. animationEasingUpdate: 'quinticInOut',
  60. series: [
  61. {
  62. type: 'graph',
  63. layout: 'force',
  64. // 设置节点符号为矩形
  65. // symbol: 'rect',
  66. // symbolSize: (value) => {
  67. // if (!value || !value.name) {
  68. // return [80, 30] // 返回默认大小
  69. // }
  70. // // 根据节点名称长度或其他属性来确定大小
  71. // const textSize = value.name.length * 10 // 假设每增加一个字符宽度增加10px
  72. // return [textSize, 30]
  73. // },
  74. symbolSize: 50,
  75. roam: true,
  76. draggable: true,
  77. label: {
  78. show: true
  79. },
  80. edgeSymbol: ['circle', 'arrow'],
  81. edgeSymbolSize: [4, 10],
  82. edgeLabel: {
  83. show: true,
  84. formatter: '{c}',
  85. position: 'middle',
  86. textStyle: {
  87. fontSize: 14,
  88. color: '#333'
  89. }
  90. },
  91. // edgeLabel: {
  92. // fontSize: 20
  93. // },
  94. data: this.graphData.data,
  95. links: this.graphData.links,
  96. categories: this.graphData.categories,
  97. force: {
  98. repulsion: 1500
  99. }
  100. }
  101. ]
  102. }
  103. this.chartInstance.setOption(options)
  104. }
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. .chart {
  110. background-color: aliceblue;
  111. border-radius: 10px;
  112. }
  113. </style>