基于Vue2和jsmind.js实现思维导图

使用Vue2和jsmind.js实现思维导图,包括放大、缩小、添加、修改和删除等基本功能,以及使用jsmind.menu实现右键点击显示菜单。

1 参考文档

注意:vue-jsmind是在jsmind.js基础上进行了封装,本文用的是jsmind不是vue-jsmind;jsmind.menu.js不是jsmind.js自带的,是其他大神开发的。

1.1 参考网址

实现基本功能使用jsmind.js即可,不需要jsmind.menu.js

开源项目地址: http://hizzgdev.github.io/jsmind/developer.html

gitee上的地址:https://gitee.com/mirrors/jsmind

github上的地址:https://github.com/hizzgdev/jsmind

实现右键菜单的js文件,实现此功能需要将jsmind.menu.js文件下载下来,再动态导入到工程中。

https://github.com/allensunjian/jsmind.menu.js

1.2 安装jsmind

我使用的版本是:"jsmind": "^0.4.6";

cnpm install jsmind --save

2 截图

 

3 源代码

<template>
  <div>
    <div>
      <input type="button" value="放大" v-on:click="zoomIn" />
      <input type="button" value="缩小" v-on:click="zoomOut" />
    </div>
    <div id="jsmind_container"></div>
  </div>
</template>

<script>
import "jsmind/style/jsmind.css";
import jsMind from "jsmind/js/jsmind.js";
window.jsMind = jsMind;

require("jsmind/js/jsmind.draggable.js");
require("jsmind/js/jsmind.screenshot.js");
require("@/assets/js/jsmind.menu.js");

export default {
  name: "MyJsmind",

  data: function () {
    return {
      jm: null,
    };
  },

  mounted: function () {
    this.init_data();
  },

  methods: {
    init_data: function () {
      var mind = {
        /* 元数据,定义思维导图的名称、作者、版本等信息 */
        meta: {
          name: "jsMind-demo-tree",
          author: "hizzgdev@163.com",
          version: "0.2",
        },
        /* 数据格式声明 */
        format: "node_tree",
        /* 数据内容 */
        data: {
          id: "root",
          topic: "jsMind",
          children: [
            {
              id: "easy",
              topic: "Easy",
              direction: "left",
              expanded: false,
              children: [
                { id: "easy1", topic: "Easy to show" },
                { id: "easy2", topic: "Easy to edit" },
                { id: "easy3", topic: "Easy to store" },
                { id: "easy4", topic: "Easy to embed" },
              ],
            },
            {
              id: "open",
              topic: "Open Source",
              direction: "right",
              expanded: true,
              children: [
                { id: "open1", topic: "on GitHub" },
                { id: "open2", topic: "BSD License" },
              ],
            },
            {
              id: "powerful",
              topic: "Powerful",
              direction: "right",
              children: [
                { id: "powerful1", topic: "Base on Javascript" },
                { id: "powerful2", topic: "Base on HTML5" },
                { id: "powerful3", topic: "Depends on you" },
              ],
            },
            {
              id: "other",
              topic: "test node",
              direction: "left",
              children: [
                { id: "other1", topic: "I'm from local variable" },
                { id: "other2", topic: "I can do everything" },
              ],
            },
          ],
        },
      };

      var options = {
        container: "jsmind_container",
        editable: true,
        theme: "primary",

        menuOpts: {
          showMenu: true,
          injectionList: [
            {
              target: "edit",
              text: "编辑节点",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "addChild",
              text: "添加子节点",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "addBrother",
              text: "添加兄弟节点",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "delete",
              text: "删除节点",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "screenshot",
              text: "下载导图",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "showAll",
              text: "展开全部节点",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "hideAll",
              text: "收起全部节点",
              callback: function (node) {
                console.log(node);
              },
            },
          ],
        },
      };

      this.jm = new jsMind(options);
      this.jm.show(mind);
    },

    zoomIn: function () {
      this.jm.view.zoomIn();
    },

    zoomOut: function () {
      this.jm.view.zoomOut();
    },
  },
};
</script>

<style scoped>
#jsmind_container {
  width: 100%;
  height: 800px;
}
</style>

  • 5
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用Vue 3和AntV来实现思维导图,可以按照以下步骤进行操作: 1. 安装Vue 3和AntV:在项目目录下运行命令`npm install vue@next @antv/g6`来安装Vue 3和AntV G6图表库。 2. 创建Vue组件:在Vue项目中创建一个新的组件,命名为"MindMap"。在组件中导入Vue和AntV G6。 3. 初始化G6图表:在组件的生命周期钩子函数`mounted`中,使用AntV G6的`Graph`类初始化一个图表实例,并将其绑定到Vue组件实例的数据属性中。 4. 定义思维导图数据:在Vue组件中定义一个数据属性,用于存储思维导图的节点和边的数据。 5. 绘制思维导图:在Vue组件的`mounted`生命周期钩子函数中,使用G6图表实例的`data`方法将思维导图的数据绑定到图表上,并使用G6图表实例的`render`方法绘制图表。 6. 添加交互能力:通过G6图表实例的事件监听和交互能力,为思维导图添加节点的拖拽、缩放、连线等交互操作。 7. 样式定制和主题设置:通过G6图表实例的配置项,可以定制思维导图的样式和主题,如节点的颜色、形状、连线的样式等。 8. 完善其他功能:根据实际需求,可以实现添加节点、删除节点、编辑节点内容、保存导图等功能,通过Vue的数据双向绑定,更新思维导图的数据并重新渲染图表。 以上是用Vue 3和AntV实现思维导图的基本步骤,具体的实现方式可以根据项目需求进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值