• vue2使用 vis-network 和 vue-vis-network 插件封装一个公用的关联关系图


    效果图:

    vis组件库:vis.js

    vis-network中文文档:vis-network

    安装组件库:

    1. npm install vis-network vue-vis-network
    2. yarn add vis-network vue-vis-network

    新建RelationGraph.vue文件:

    1. <script>
    2. import {
    3. Network
    4. } from 'vis-network/standalone/esm/vis-network.js';
    5. import VueVisNetwork from 'vue-vis-network';
    6. export default {
    7. name:"RelationGraph",
    8. props: {
    9. nodes: {
    10. type: Array,
    11. required: true,
    12. },
    13. edges: {
    14. type: Array,
    15. required: true,
    16. },
    17. options: {
    18. type: Object,
    19. default: () => ({}),
    20. },
    21. },
    22. components: {
    23. VueVisNetwork,
    24. },
    25. mounted() {
    26. // 创建关联关系图
    27. const container = this.$refs.networkContainer;
    28. const data = {
    29. nodes: this.nodes,
    30. edges: this.edges,
    31. };
    32. new Network(container, data, this.options);
    33. },
    34. };
    35. script>

    页面使用:

    1. <div>
    2. <relation-graph :nodes="nodes" :edges="edges" :options="graphOptions">relation-graph>
    3. div>
    1. import RelationGraph from './RelationGraph.vue';
    2. export default {
    3. components: {
    4. RelationGraph,
    5. },
    6. data() {
    7. return {
    8. nodes: [{
    9. id: 0,
    10. label: "大前端",
    11. color: {
    12. background: "#fd91b7"
    13. },
    14. },
    15. {
    16. id: 1,
    17. label: "HTML",
    18. color: {
    19. background: "#7ed6df"
    20. },
    21. },
    22. {
    23. id: 2,
    24. label: "JavaScript",
    25. color: {
    26. background: "#d294e2"
    27. },
    28. },
    29. {
    30. id: 3,
    31. label: "CSS",
    32. color: {
    33. background: "#ffb300"
    34. },
    35. }
    36. ],
    37. edges: [{
    38. id: "e1",
    39. from: 0,
    40. to: 1,
    41. label: "含"
    42. },
    43. {
    44. id: "e2",
    45. from: 1,
    46. to: 0,
    47. label: "嵌"
    48. },
    49. {
    50. id: "e3",
    51. from: 0,
    52. to: 2,
    53. label: "step1"
    54. },
    55. {
    56. id: "e4",
    57. from: 0,
    58. to: 3,
    59. label: "step1"
    60. },
    61. ],
    62. graphOptions: {
    63. autoResize: true, //网络将自动检测其容器的大小调整,并相应地重绘自身
    64. locale: "cn", //语言设置:工具栏显示中文
    65. //设置语言
    66. locales: {
    67. cn: {
    68. //工具栏中文翻译
    69. edit: "编辑",
    70. del: "删除当前节点或关系",
    71. back: "返回",
    72. addNode: "添加节点",
    73. addEdge: "添加连线",
    74. editNode: "编辑节点",
    75. editEdge: "编辑连线",
    76. addDescription: "点击空白处可添加节点",
    77. edgeDescription: "点击某个节点拖拽连线可连接另一个节点",
    78. editEdgeDescription: "可拖拽连线改变关系",
    79. createEdgeError: "无法将边连接到集群",
    80. deleteClusterError: "无法删除集群.",
    81. editClusterError: "无法编辑群集'",
    82. },
    83. },
    84. // 设置节点样式
    85. nodes: {
    86. shape: "dot", //节点的外观。为circle时label显示在节点内,为dot时label显示在节点下方
    87. size: 30, //节点的大小,
    88. shadow: false, //如果为true,则节点使用默认设置投射阴影。
    89. font: {
    90. //字体配置
    91. size: 18,
    92. color: "rgb(117, 218, 167)",
    93. align: "center",
    94. },
    95. color: {
    96. border: "transparent", //节点边框颜色
    97. background: "#ffc7c7", //节点背景颜色
    98. highlight: {
    99. //节点选中时状态颜色
    100. border: "rgb(255, 0, 0)",
    101. background: "rgb(255, 0, 0)",
    102. },
    103. hover: {
    104. //节点鼠标滑过时状态颜色
    105. border: "#dff9fb",
    106. background: "#88dab1",
    107. },
    108. },
    109. margin: 5, //当形状设置为box、circle、database、icon、text;label的边距
    110. widthConstraint: 100, //设置数字,将节点的最小和最大宽度设为该值,当值设为很小的时候,label会换行,节点会保持一个最小值,里边的内容会换行
    111. borderWidth: 1, //节点边框宽度,单位为px
    112. borderWidthSelected: 3, //节点被选中时边框的宽度,单位为px
    113. labelHighlightBold: false, //确定选择节点时标签是否变为粗体
    114. },
    115. // 边线配置
    116. edges: {
    117. width: 1,
    118. length: 200,
    119. color: {
    120. color: "#848499",
    121. highlight: "rgb(255, 85, 0)",
    122. hover: "#88dab1",
    123. inherit: "from",
    124. opacity: 1.0,
    125. },
    126. font: {
    127. color: "#343434",
    128. size: 18, // px
    129. face: "arial",
    130. background: "none",
    131. strokeWidth: 2, // px
    132. strokeColor: "#ffffff",
    133. align: "horizontal",
    134. multi: false,
    135. vadjust: 0,
    136. bold: {
    137. color: "#343434",
    138. size: 14, // px
    139. face: "arial",
    140. vadjust: 0,
    141. mod: "bold",
    142. },
    143. ital: {
    144. color: "#343434",
    145. size: 14, // px
    146. face: "arial",
    147. vadjust: 0,
    148. mod: "italic",
    149. },
    150. boldital: {
    151. color: "#343434",
    152. size: 14, // px
    153. face: "arial",
    154. vadjust: 0,
    155. mod: "bold italic",
    156. },
    157. mono: {
    158. color: "#343434",
    159. size: 15, // px
    160. face: "courier new",
    161. vadjust: 2,
    162. mod: "",
    163. },
    164. },
    165. shadow: false,
    166. smooth: {
    167. //设置两个节点之前的连线的状态
    168. enabled: true, //默认是true,设置为false之后,两个节点之前的连线始终为直线,不会出现贝塞尔曲线
    169. },
    170. arrows: {
    171. to: true, //默认是false
    172. }, //箭头指向to
    173. },
    174. // 布局
    175. layout: {
    176. randomSeed: 2, //配置每次生成的节点位置都一样,参数为数字1、2等
    177. },
    178. //计算节点之前斥力,进行自动排列的属性
    179. physics: {
    180. enabled: true, //默认是true,设置为false后,节点将不会自动改变,拖动谁谁动。不影响其他的节点
    181. barnesHut: {
    182. gravitationalConstant: -4000,
    183. centralGravity: 0.3,
    184. springLength: 120,
    185. springConstant: 0.04,
    186. damping: 0.09,
    187. avoidOverlap: 0,
    188. },
    189. },
    190. },
    191. }
    192. },
    193. }

  • 相关阅读:
    python神经网络编程 豆瓣,python神经网络图像分类
    jQuery UI简单的讲解
    算法刷题记录-图(LeetCode)
    Spring | 事件监听器应用与最佳实践
    Postgresql中的变长参数类型VARIADIC实例与限制
    【JVM】类的生命周期
    ADVANCED PROGRAMMING TECHNIQUES IN JAVA
    图像分割简述
    解锁AIGC的神秘咒语:AI对话的艺术
    mysql主从复制
  • 原文地址:https://blog.csdn.net/2201_75870706/article/details/132558127