graph.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <div id="graph_echart" ref="echart"></div>
  3. <!-- <div>
  4. <div class="echars">
  5. <div ref="first_pie" id="first_pie"></div>
  6. </div>
  7. </div> -->
  8. </template>
  9. <script>
  10. let option = {
  11. backgroundColor: 'rgba(4, 28, 50, 0.5)',
  12. tooltip: {},
  13. toolbox: {
  14. feature: {
  15. saveAsImage: {},
  16. },
  17. },
  18. legend: {
  19. type: "scroll",
  20. orient: "vertical",
  21. right: 20,
  22. bottom: 20,
  23. data: [],
  24. icon: "circle", // 这个字段控制形状 类型包括 circle,rect ,roundRect,triangle,diamond,pin,arrow,none
  25. itemWidth: 12, // 设置宽度
  26. itemHeight: 12, // 设置高度
  27. itemGap: 10, // 设置间距
  28. textStyle: {
  29. //图例文字的样式
  30. color: "#fff",
  31. fontSize: 14,
  32. },
  33. // textStyle: {
  34. // rich: {
  35. // name: {
  36. // color: "#fff", //#BCD3E5
  37. // fontSize: 14,
  38. // // width: 60,
  39. // },
  40. // value: {
  41. // color: "#64DAFF",
  42. // fontSize: 14,
  43. // padding: [5, 0],
  44. // align: "center",
  45. // // width: 40,
  46. // },
  47. // unit: {
  48. // color: "#fff", //#BCD3E5
  49. // fontSize: 14,
  50. // // width: 5,
  51. // },
  52. // },
  53. // },
  54. },
  55. series: [
  56. {
  57. type: "graph",
  58. layout: "force",
  59. force: {
  60. repulsion: 1500,
  61. edgeLength: 120,
  62. layoutAnimation: true,
  63. },
  64. symbolSize: 70,
  65. nodeScaleRatio: 1, //图标大小是否随鼠标滚动而变
  66. roam: true, //缩放
  67. draggable: true, //节点是否可以拖拽
  68. edgeSymbol: ["none", "arrow"], //线2头标记
  69. label: {
  70. normal: {
  71. show: true,
  72. position: "inside",
  73. color: "#FFF",
  74. },
  75. },
  76. edgeLabel: {
  77. normal: {
  78. show: true,
  79. textStyle: {
  80. fontSize: 12,
  81. color: "#FFF",
  82. },
  83. formatter: "{c}",
  84. },
  85. },
  86. symbolKeepAspect: false,
  87. focusNodeAdjacency: false, // 指定的节点以及其所有邻接节点高亮
  88. itemStyle: {
  89. normal: {
  90. borderColor: "#29ACFC",
  91. borderWidth: 2,
  92. shadowColor: "#29ACFC",
  93. color: "#29ACFC",
  94. curveness: 0.08,
  95. },
  96. },
  97. lineStyle: {
  98. normal: {
  99. opacity: 0.9,
  100. width: 2,
  101. curveness: 0.15,
  102. color: {
  103. type: "linear",
  104. x: 0,
  105. y: 0,
  106. x2: 0,
  107. y2: 1,
  108. colorStops: [
  109. {
  110. offset: 0,
  111. color: "#FFF", // 0% 处的颜色
  112. },
  113. {
  114. offset: 1,
  115. color: "#FFF", // 100% 处的颜色
  116. },
  117. ],
  118. globalCoord: false,
  119. },
  120. },
  121. },
  122. data: [],
  123. links: [],
  124. },
  125. ],
  126. };
  127. export default {
  128. components: {},
  129. data() {
  130. return {
  131. myChart: null,
  132. timer: null
  133. };
  134. },
  135. computed: {},
  136. mounted() {
  137. // this.setOptions(data, links);
  138. },
  139. methods: {
  140. setOptions(data, links, categors = []) {
  141. let _this = this;
  142. if (!this.myChart) {
  143. this.myChart = echarts.init(this.$refs.echart);
  144. }
  145. option.legend.data = categors.map((a) => { return a.name; })
  146. option.series[0].data = data
  147. option.series[0].links = links
  148. option.series[0].categories = categors
  149. console.log(option.legend);
  150. this.myChart.setOption(option);
  151. this.myChart.on("click", (params) => {
  152. this.$emit("click", params)
  153. });
  154. // 监听右键点击事件
  155. this.myChart.on('contextmenu', function (params) {
  156. let event = params.event
  157. // 阻止默认的右键菜单出现 even.preventDefault();
  158. event.stop()
  159. var menu = document.getElementById('myContext');
  160. if (!menu) return
  161. menu.style.left = event.offsetX + 'px';
  162. menu.style.top = event.offsetY + 'px';
  163. menu.style.display = 'block';
  164. // 关闭弹窗
  165. document.addEventListener('click', function closeMenu() {
  166. menu.style.display = 'none';
  167. document.removeEventListener('click', closeMenu);
  168. });
  169. });
  170. this.myChart.on('mouseout', (params) => {
  171. if (this.timer) clearTimeout(this.timer);
  172. this.timer = setTimeout(() => {
  173. var menu = document.getElementById('myContext');
  174. if (menu) menu.style.display = 'none';
  175. }, 3000);
  176. console.log('out');
  177. });
  178. // 自定义弹窗HTML
  179. // var contextMenuHtml = `
  180. // <div id="myContextMenu" style="display:none; position:absolute; z-index:100; background-color:#fff; border:1px solid #ccc;">
  181. // <ul>
  182. // <li>操作一</li>
  183. // <li>操作二</li>
  184. // <li>操作三</li>
  185. // </ul>
  186. // </div>`
  187. // document.body.appendChild(contextMenuHtml);
  188. // 图表点击事件监听
  189. // this.myChart.on("click", function (params) {
  190. // // 刷新数据的逻辑,例如从服务器获取新数据
  191. // fetchNewData()
  192. // .then(function (newData) {
  193. // let that = this
  194. // // 更新图表的数据
  195. // that.myChart.setOption({
  196. // series: [
  197. // {
  198. // // 假设您要更新的是 series 下的某个数据系列
  199. // data: newData[0], // 使用新数据更新 series.data
  200. // links: newData[1],
  201. // },
  202. // ],
  203. // });
  204. // })
  205. // .catch(function (error) {
  206. // console.error("Error fetching new data:", error);
  207. // });
  208. // // 这里可以添加其他需要在点击后执行的代码
  209. // });
  210. // 模拟的数据获取函数,实际应用中应该从服务器获取
  211. function fetchNewData() {
  212. // 返回一个新数据的 Promise
  213. return new Promise((resolve, reject) => {
  214. setTimeout(() => {
  215. // 模拟获取到的新数据
  216. const newData = [
  217. /* 新数据数组 */
  218. data2,
  219. links2,
  220. ];
  221. resolve(newData);
  222. }, 1000); // 延迟 1 秒来模拟异步数据加载
  223. });
  224. }
  225. },
  226. test() {
  227. this.initTwo(data2, links2);
  228. },
  229. },
  230. watch: {},
  231. beforeDestroy() { },
  232. };
  233. </script>
  234. <style lang="less" scoped>
  235. #graph_echart {
  236. width: 100%;
  237. height: 100%;
  238. }
  239. </style>