How to display data visualization relationship diagrams (such as relationship diagrams) in Vue

How to display data visualization relationship diagrams (such as relationship diagrams) in Vue

With the development of data analysis and visualization technology, more and more applications begin to use relational graphs to show the relationship between data. In Vue, we can use the third-party library Vis.js to realize the display of the relationship graph, and perform data binding and interaction through Vue components. This article will introduce how to use Vis.js in Vue to create a relationship graph and implement data binding and interaction functions.

insert image description here

Introduction to Vis.js

Vis.js is a JavaScript library for creating interactive data visualizations. It provides a series of tools and APIs that enable developers to easily create various types of diagrams, including relational graphs, timelines, maps, etc. Vis.js also provides a graphical interface, allowing developers to build diagrams intuitively.

Using Vis.js in Vue, we can create relationship graphs through Vue components and embed them into Vue applications.

Install Vis.js

To use Vis.js in Vue, we first need to install the Vis.js library. It can be installed with the following command:

npm install vis --save

Create Vue components

Here is a simple Vue component for creating a relationship graph:

<template>
  <div ref="container"></div>
</template>

<script>
import {
      
       DataSet, Network } from 'vis'

export default {
      
      
  props: {
      
      
    nodes: {
      
      
      type: Array,
      required: true,
    },
    edges: {
      
      
      type: Array,
      required: true,
    },
  },
  data() {
      
      
    return {
      
      
      network: null,
    }
  },
  mounted() {
      
      
    this.init()
  },
  methods: {
      
      
    init() {
      
      
      // 创建数据集
      const nodesDataSet = new DataSet(this.nodes)
      const edgesDataSet = new DataSet(this.edges)

      // 创建关系图谱
      const container = this.$refs.container
      const data = {
      
       nodes: nodesDataSet, edges: edgesDataSet }
      const options = {
      
      }
      this.network = new Network(container, data, options)
    },
  },
  watch: {
      
      
    nodes() {
      
      
      this.network.setData({
      
       nodes: this.nodes, edges: this.edges })
    },
    edges() {
      
      
      this.network.setData({
      
       nodes: this.nodes, edges: this.edges })
    },
  },
}
</script>

In the above code, we first introduced the Vis.js library and defined a Vue component. In props, we define two array parameters: nodes and edges, which are used to receive the node and edge data of the relationship graph. In the mounted hook function, we call the init method to create the relationship graph. In the init method, we create the dataset and then use the Network class to create the relationship graph. Finally, we use watch to monitor the changes of nodes and edges parameters in order to realize data binding.

Use the relationship graph

The following is a simple example to show how to use the above Vue components to display the relationship graph:

<template>
  <div>
    <div>
      <label>节点数:</label>
      <input v-model="nodeCount" type="number" min="1" max="100" step="1" @change="generateData" />
    </div>
    <div>
      <label>边数:</label>
      <input v-model="edgeCount" type="number" min="1" max="100" step="1" @change="generateData" />
    </div>
    <div>
      <relation-graph :nodes="nodes" :edges="edges" />
    </div>
  </div>
</template>

<script>
import RelationGraph from '@/components/RelationGraph.vue'

export default {
      
      
  components: {
      
      
    RelationGraph,
  },
  data() {
      
      
    return {
      
      
      nodeCount: 10,
      edgeCount: 15,
      nodes: [],
      edges: [],
    }
  },
  mounted() {
      
      
    this.generateData()
  },
  methods: {
      
      
    generateData() {
      
      
      // 生成节点数据
      this.nodes = []
      for (let i = 1; i <= this.nodeCount; i++) {
      
      
        this.nodes.push({
      
       id: i, label: `Node ${ 
        i}` })
      }

      // 生成边数据
      const maxEdgeCount = (this.nodeCount * (this.nodeCount - 1)) / 2
      const edgeCount = Math.min(this.edgeCount, maxEdgeCount)
      const edges = new Set()
      while (edges.size < edgeCount) {
      
      
        const source = Math.floor(Math.random() * this.nodeCount) + 1
        const target = Math.floor(Math.random() * this.nodeCount) + 1
        if (source !== target) {
      
      
          edges.add(`${ 
        Math.min(source, target)}-${ 
        Math.max(source, target)}`)
        }
      }
      this.edges = Array.from(edges).map((edge) => {
      
      
        const [source, target] = edge.split('-')
        return {
      
       from: source, to: target }
      })
    },
  },
}
</script>

In the above code, we first introduced the previously defined relational graph component RelationGraph, and then defined an input box in the template to control the number of generated nodes and edges. Finally, we use the RelationGraph component to display the relationship graph and pass node and edge data through props.

In the mounted hook function, we call the generateData method to generate node and edge data. In the generateData method, we use the Math.random function to generate random nodes and edges, and then assign them to the nodes and edges parameters.

Implement interactive functions

In Vis.js, we can implement interactive functions through events. For example, we can listen to the click event to realize the click operation of the node. The following is an example to implement the function of popping up node information when a node is clicked:

<template>
  <div>
    <div>
      <label>节点数:</label>
      <input v-model="nodeCount" type="number" min="1" max="100" step="1" @change="generateData" />
    </div>
    <div>
      <label>边数:</label>
      <input v-model="edgeCount" type="number" min="1" max="100" step="1" @change="generateData" />
    </div>
    <div>
      <relation-graph :nodes="nodes" :edges="edges" @clickNode="handleClickNode" />
    </div>
  </div>
</template>

<script>
import RelationGraph from '@/components/RelationGraph.vue'

export default {
      
      
  components: {
      
      
    RelationGraph,
  },
  data() {
      
      
    return {
      
      
      nodeCount: 10,
      edgeCount: 15,
      nodes: [],
      edges: [],
    }
  },
  mounted() {
      
      
    this.generateData()
  },
  methods: {
      
      
    generateData() {
      
      
      // 生成节点数据
      this.nodes = []
      for (let i = 1; i <= this.nodeCount; i++) {
      
      
        this.nodes.push({
      
       id: i, label: `Node ${ 
        i}` })
      }

      // 生成边数据
      const maxEdgeCount = (this.nodeCount * (this.nodeCount - 1)) / 2
      const edgeCount = Math.min(this.edgeCount, maxEdgeCount)
      const edges = new Set()
      while (edges.size < edgeCount) {
      
      
        const source = Math.floor(Math.random() * this.nodeCount) + 1
        const target = Math.floor(Math.random() * this.nodeCount) + 1
        if (source !== target) {
      
      
          edges.add(`${ 
        Math.min(source, target)}-${ 
        Math.max(source, target)}`)
        }
      }
      this.edges = Array.from(edges).map((edge) => {
      
      
        const [source, target] = edge.split('-')
        return {
      
       from: source, to: target }
      })
    },
    handleClickNode(event) {
      
      
      alert(`你点击了节点:${ 
        event.nodes[0]}`)
    },
  },
}
</script>

In the above code, we added the @clickNode event listener on the RelationGraph component and defined the handleClickNode method. In the handleClickNode method, we use the alert function to pop up node information.

Summarize

This article introduces how to use Vis.js in Vue to create a relationship graph and implement data binding and interaction functions. Through the introduction of this article, you can learn how to use the Vis.js library in the Vue project to display the relationship graph and implement data binding and interaction functions. Hope this article can help you.

Guess you like

Origin blog.csdn.net/it_xushixiong/article/details/131241605