vue 中引用gojs绘制E-R图的方法示例

首先,在vue项目中安装gojs的依赖包,并在项目中引入。

创建tablePreview.vue

<style>
 #sample{
  position: relative;
  margin: 20px;
 }
 #myOverviewDiv {
  position: absolute;
  width:225px;
  height:100px;
  top: 10px;
  left: 10px;
  background-color: aliceblue;
  z-index: 300; /* make sure its in front */
  border: solid 1px blue;
 }
 #mySearch{
  width: 60%;
  float: right;
  margin-right: 20px;
 }
 .input_button{
  margin: 20px;
 }
 #entityRelation{
  width: 100%;
  height: 700px;
  border:1px solid #cccccc;
 }
 .returnShang{
  color: #fff;
  text-underline: none;
 }
 .returnShang:hover{
  color: #fff;
  text-underline: none;
 }
</style>
<template>
 <div>
  <div class="input_button">
   <Button type="success"><router-link to="/dataSourceAdmin" class="returnShang">返回上一级</router-link></Button>
   <Button type="primary" @click="searchDiagram()" style="float: right">Search</Button>
   <Input placeholder="请输入要查询的表名" id="mySearch" v-model="searchText" @keyup.enter.native="searchDiagram()"></Input>

  </div>
  <div id="sample">
   <div id="entityRelation"></div>
   <div id="myOverviewDiv"></div>
  </div>
 </div>
</template>
<script src="./tablePreview.js"></script>

在js文件中绘制E-R图,注意:初始化方面必须放在mounted中调用。

tablePreview.js如下

export default{
 data() {
  return {
   myDiagram: '',
   searchText: '',
   tabViewList: '',
   tabRelView: ''
  }
 },
 mounted() {
  var dataSoureId = JSON.parse(sessionStorage.getItem("previewInfo")).datasourceId
  let _this = this
  _this.$ajax.post(_this.cfg.api.dataPoolAdmin.tableRelView +'?datasourceId=' + dataSoureId)
   .then(function (res) {
    if(res.data.result) {
     _this.tabViewList = res.data.data.tabViewList
     _this.tabRelView = res.data.data.tabRelViewList
     _this.init()
    }
   })

 },
 methods: {
  init() {
   var go = this.go
   if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this
   var a = go.GraphObject.make; // 定义模板

   this.myDiagram =
    a(go.Diagram, 'entityRelation', // 必须命名或引用div html元素
     {
      initialContentAlignment: go.Spot.Center,
      allowDelete: false,
      allowCopy: false,
      layout: a(go.ForceDirectedLayout),
      "undoManager.isEnabled": true
     });

   // define several shared Brushes
   var bluegrad = a(go.Brush, "Linear", { 0: "rgb(150, 150, 250)", 0.5: "rgb(86, 86, 186)", 1: "rgb(86, 86, 186)" });
   var greengrad = a(go.Brush, "Linear", { 0: "rgb(158, 209, 159)", 1: "rgb(67, 101, 56)" });
   var redgrad = a(go.Brush, "Linear", { 0: "rgb(206, 106, 100)", 1: "rgb(180, 56, 50)" });
   var yellowgrad = a(go.Brush, "Linear", { 0: "rgb(254, 221, 50)", 1: "rgb(254, 182, 50)" });
   var lightgrad = a(go.Brush, "Linear", { 1: "#E6E6FA", 0: "#FFFAF0" });

   // the template for each attribute in a node's array of item data
   var itemTempl =
    a(go.Panel, "Horizontal",
     a(go.Shape,
      { desiredSize: new go.Size(10, 10) },
      new go.Binding("figure", "figure"),
      new go.Binding("fill", "color")),
     a(go.TextBlock,//items样式
      { stroke: "#333333",
       row: 0, alignment: go.Spot.Center,
       margin: new go.Margin(0, 14, 0, 2),
       font: "bold 14px sans-serif" },
      new go.Binding("text", "name"))
    );

   // define the Node template, representing an entity
   this.myDiagram.nodeTemplate =
    a(go.Node, "Auto", // the whole node panel
     { selectionAdorned: true,
      resizable: true,
      layoutConditions: go.Part.LayoutStandard & ~go.Part.LayoutNodeSized,
      fromSpot: go.Spot.AllSides,
      toSpot: go.Spot.AllSides,
      isShadowed: true,
      shadowColor: "#CCAA" },
     new go.Binding("location", "location").makeTwoWay(),
     // whenever the PanelExpanderButton changes the visible property of the "LIST" panel,
     // clear out any desiredSize set by the ResizingTool.
     new go.Binding("desiredSize", "visible", function(v) { return new go.Size(NaN, NaN); }).ofObject("LIST"),
     // define the node's outer shape, which will surround the Table
     a(go.Shape, "Rectangle",
      /*{ fill: lightgrad, stroke: "#756875", strokeWidth: 3 }),*/
      new go.Binding("fill", "isHighlighted", function(h) { return h ? "#F44336" : "#A7E7FC"; }).ofObject()),
     a(go.Panel, "Table",
      { margin: 15, stretch: go.GraphObject.Fill },
      a(go.RowColumnDefinition, { row: 0, sizing: go.RowColumnDefinition.None }),
      // the table header
      a(go.TextBlock,
       {
        row: 0, alignment: go.Spot.Center,
        margin: new go.Margin(0, 14, 0, 2), // leave room for Button
        font: "bold 16px sans-serif"
       },
       new go.Binding("text", "key")),
      // 折叠/展开按钮
      /*a("PanelExpanderButton", "LIST", // the name of the element whose visibility this button toggles
       { row: 0, alignment: go.Spot.TopRight }),*/
      // the list of Panels, each showing an attribute
      a(go.Panel, "Vertical",
       {
        name: "LIST",
        row: 1,
        padding: 0,//表名到下边框的距离
        alignment: go.Spot.TopLeft,
        defaultAlignment: go.Spot.Left,
        stretch: go.GraphObject.Horizontal,
        itemTemplate: itemTempl
       },
       new go.Binding("itemArray", "items"))
     ) // end Table Panel
    ); // end Node

   // define the Link template, representing a relationship
   this.myDiagram.linkTemplate =
    a(go.Link, // the whole link panel
     {
      selectionAdorned: true,
      layerName: "Foreground",
      reshapable: true,
      routing: go.Link.AvoidsNodes,
      corner: 5,
      curve: go.Link.JumpOver,
      toEndSegmentLength: 100
     },
     a(go.Shape, // the link shape
      { stroke: "#303B45", strokeWidth: 2.5 }),
     a(go.TextBlock, // the "from" label
      {
       textAlign: "center",
       font: "bold 14px sans-serif",
       stroke: "#1967B3",
       segmentIndex: 0,
       segmentOffset: new go.Point(NaN, NaN),
       segmentOrientation: go.Link.OrientUpright
      },
      new go.Binding("text", "text")),
     a(go.TextBlock, // the "to" label
      {
       textAlign: "center",
       font: "bold 14px sans-serif",
       stroke: "#1967B3",
       segmentIndex: -1,
       segmentOffset: new go.Point(NaN, NaN),
       segmentOrientation: go.Link.OrientUpright
      },
      new go.Binding("text", "toText"))
    );

   // create the model for the E-R diagram
   var nodeDataArray = []
   for(var i =0; i< this.tabViewList.length; i++){
    nodeDataArray.push(JSON.parse(JSON.stringify(
     { key: this.tabViewList[i].tableName,
      name: this.tabViewList[i].tableCnName,
      items: [{name: this.tabViewList[i].tableCnName, isKey: false, figure: 'Decision', color: "#2d8cf0"}]
     }
     )))
   }
   var linkDataArray = []
   for(var j =0; j< this.tabRelView.length; j++){
    linkDataArray.push(JSON.parse(JSON.stringify(
     { from: this.tabRelView[j].fromTableName, to: this.tabRelView[j].toTableName,
      text: this.tabRelView[j].fromText, toText: this.tabRelView[j].toText
     })
    ))
   }
   /*var nodeDataArray = [
    { key: this.tabViewList[].tableName,
     items: [ { name: "角色标识:BIGINT", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "模块标识:BIGINT", iskey: false, figure: "Cube", color: bluegrad }] },
    { key: "角色信息表",
     items: [ { name: "标识:BIGINT", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "名称:VARCHAR(100)", iskey: false, figure: "Cube1", color: bluegrad },
      { name: "描述:VARCHAR(100)", iskey: false, figure: "Cube1", color: bluegrad },
      { name: "排序:VARCHAR(100)", iskey: false, figure: "Cube1", color: bluegrad } ] },
    { key: "用户权限表",
     items: [ { name: "CategoryID", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "CategoryName", iskey: false, figure: "Cube", color: bluegrad },
      { name: "Description", iskey: false, figure: "Cube", color: bluegrad },
      { name: "Picture", iskey: false, figure: "TriangleUp", color: redgrad } ] },
    { key: "用户信息表",
     items: [ { name: "OrderID", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "ProductID", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "UnitPrice", iskey: false, figure: "MagneticData", color: greengrad },
      { name: "Quantity", iskey: false, figure: "MagneticData", color: greengrad },
      { name: "Discount", iskey: false, figure: "MagneticData", color: greengrad } ] },
    { key: "表1",
     items: [ { name: "OrderID", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "ProductID", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "UnitPrice", iskey: false, figure: "MagneticData", color: greengrad },
      { name: "Quantity", iskey: false, figure: "MagneticData", color: greengrad },
      { name: "Discount", iskey: false, figure: "MagneticData", color: greengrad } ] },
    { key: "表2",
     items: [ { name: "OrderID", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "ProductID", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "UnitPrice", iskey: false, figure: "MagneticData", color: greengrad },
      { name: "Quantity", iskey: false, figure: "MagneticData", color: greengrad },
      { name: "Discount", iskey: false, figure: "MagneticData", color: greengrad } ] },
    { key: "表3" },
    { key: "表4",
     items: [ { name: "OrderID", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "ProductID", iskey: true, figure: "Decision", color: yellowgrad },
      { name: "UnitPrice", iskey: false, figure: "MagneticData", color: greengrad },
      { name: "Quantity", iskey: false, figure: "MagneticData", color: greengrad },
      { name: "Discount", iskey: false, figure: "MagneticData", color: greengrad } ] },
   ];*/
   /*var linkDataArray = [
    { from: "角色权限表", to: "角色信息表", text: "N", toText: "1" },
    { from: "角色权限表", to: "用户权限表", text: "N", toText: "1" },
    { from: "用户信息表", to: "角色权限表", text: "N", toText: "1" },
    { from: "表1", to: "角色权限表", text: "N", toText: "1" },
    { from: "角色权限表", to: "表1", text: "N", toText: "1" },
    { from: "表2", to: "用户信息表", text: "N", toText: "1" },
    { from: "表3", to: "用户信息表", text: "N", toText: "1" },
    { from: "表4", to: "角色权限表", text: "N", toText: "1" }
   ];*/
   this.myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
   // Overview
   var myOverview =
    a(go.Overview, "myOverviewDiv", // the HTML DIV element for the Overview
     { observed: this.myDiagram, contentAlignment: go.Spot.Center });
  },
  searchDiagram() { // called by button
   var input = document.getElementById("mySearch");
   if (!input) return;
   input.focus();
   this.myDiagram.startTransaction("highlight search");
   if (this.searchText) {
    // search four different data properties for the string, any of which may match for success
    // create a case insensitive RegExp from what the user typed
    var regex = new RegExp(this.searchText, "i");
    var results = this.myDiagram.findNodesByExample({ key: regex },{name: regex});
    this.myDiagram.highlightCollection(results);
    // try to center the diagram at the first node that was found
    if (results.count > 0) this.myDiagram.centerRect(results.first().actualBounds);
   } else { // empty string only clears highlighteds collection
    this.myDiagram.clearHighlighteds();
   }

   this.myDiagram.commitTransaction("highlight search");
  }
 }
}

这里在gojs的EntityRelationship实例的基础上,加了遮罩图与搜索后高亮显示功能。

遮罩层的作用是当有很多数据时,可以通过拖动遮罩层来查找。

这个是遮罩层的div

这里是把之前定义好的Diagram放进遮罩层。

搜索框的作用即是更快的找到相应的数据,下图代码是设置搜索后显示高亮的数据。

效果图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • GOJS+VUE实现流程图效果

    前言及展示 在项目中需要根据传过来的数据画出流程图,采用了GOJS插件,功能很全面,可以根据自己的需要调整,不过建议简单的流程图还是自己手写一个组件,更加便于维护和变换.有一点需要注意的是,GOJS是需要收费的,有水印,虽然可以手动去掉,但是公司用的话还是需要买.GOJS的官网上有关于在VUE中应用GOJS的小例子:Minimal GoJS Sample in Vue.js.推荐看一下,可以解决大部分简单需求,这个例子可以满足你并行步骤数比较固定的二叉树画法的流程图. 这是官网的例子,其中模块,

  • vue中使用gojs/jointjs的示例代码

    因为公司项目需求,要画出相关业务的流程图,以便客户了解自己身处何处 搜索框输入 "前端流程图插件",查了很多资料,总结一下有以下几种 flow-chart 代码写法繁琐,不是json就可以解决,效果也比较丑,PASS darge-d3 github :https://github.com/dagrejs/dagre-d3 效果图 下载里面的demo,改一下json就可以了 // States var states = [ "NEW", "SUBMITTED

  • vue 中引用gojs绘制E-R图的方法示例

    首先,在vue项目中安装gojs的依赖包,并在项目中引入. 创建tablePreview.vue <style> #sample{ position: relative; margin: 20px; } #myOverviewDiv { position: absolute; width:225px; height:100px; top: 10px; left: 10px; background-color: aliceblue; z-index: 300; /* make sure its

  • Vue中ref和$refs的介绍以及使用方法示例

    前言 在JavaScript中需要通过document.querySelector("#demo")来获取dom节点,然后再获取这个节点的值.在Vue中,我们不用获取dom节点,元素绑定ref之后,直接通过this.$refs即可调用,这样可以减少获取dom节点的消耗. ref介绍 ref被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs对象上.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素:如果用在子组件上,引用就指向该子组件实例 通俗的讲,re

  • vue中get请求如何传递数组参数的方法示例

    前言: vue中在与后端进行数据交互时,使用axios发送请求,不做配置直接使用get请求传递数组类型参数的时候,后端是无法接收数据的,需要对axios一些简单的配置才能让后端完美的接收数组 1.问题 示例代码 let params = { statusList: ['OVERDUE', 'DELAY'] } this.$http.get('/list', params) .then(res => {}) .catch(e => {}) 上述代码在不做配置的时候请求信息为:/list?stat

  • vue中引用阿里字体图标的方法

    想在vue中引用阿里的iconfont,却出现报错 ,原因是没有对应的loader处理字体文件. 解决办法 1.引入css文件 import 'font-awesome/css/font-awesome.min.css' 2.在webpack.config中配置 { test: /\.(eot|svg|ttf|woff|woff2)$/, loader: 'file-loader' } 总结 以上所述是小编给大家介绍的vue中引用阿里字体图标的方法,希望对大家有所帮助,如果大家有任何疑问请给我留

  • vue中引用swiper轮播插件的教程详解

    有时候我们需要在vue中使用轮播组件,如果是在vue组件中引入第三方组件的话,最好通过npm安装,从而进行统一安装包管理. 申明:本文所使用的是vue.2x版本. 通过npm安装插件:  npm install swiper --save-dev 在需要使用swiper的组件里引入swiper,swiper的初始化放在mounted里 Slider.vue源码: <template> <div class="swiper-container"> <div

  • Vue中使用better-scroll实现轮播图组件

    better-scroll 是什么 better-scroll 是一款重点解决移动端(已支持 PC)各种滚动场景需求的插件.它的核心是借鉴的 iscroll 的实现,它的 API 设计基本兼容 iscroll,在 iscroll 的基础上又扩展了一些 feature 以及做了一些性能优化. better-scroll 是基于原生 JS 实现的,不依赖任何框架.它编译后的代码大小是 63kb,压缩后是 35kb,gzip 后仅有 9kb,是一款非常轻量的 JS lib. 今天我们利用它实现一个横向

  • Python几种绘制时间线图的方法

    目录 Matplotlib 制作 Plotly 绘制 Excel 绘制 Matplotlib 制作 Matplotlib 作为 Python 家族最为重要的可视化工具,其基本的 API 以及绘制流程还是需要掌握的.尤其是该库的灵活程度以及作为众多工具的基础,重要性不言而喻 下面我们来看下该如何绘制一个时间线图表 导入库以及设置 XY 轴数据 import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] p

  • EasyX绘制透明背景图的方法详解

    目录 三元光栅操作 优化方案 三元光栅操作 根据在网上的搜索总结得到两种方案,最常见的绘制带有透明背景的图像的方案都是采用如下的源图像和掩码图像叠加来消去边缘部分: IMAGE img[2]; loadimage(&img[0], "sun1.png", 100, 100); // 掩码图像 loadimage(&img[1], "sun0.png", 100, 100); // 源图像 putimage(0, 0, &img[0], NOT

  • Vue 中使用vue2-highcharts实现曲线数据展示的方法

    1.要实现的效果如下图: 2.vue前端页面如下: <template> <div> <div> <div> <img src="../assets/index/back.png" class="rank-head-back" @click="routerBack"/> <span >历史曲线</span> </div> </div> &l

  • extjs4图表绘制之折线图实现方法分析

    本文实例讲述了extjs4图表绘制之折线图实现方法.分享给大家供大家参考,具体如下: 本篇文章将介绍extjs中自带的图表 在本次案例中,提供一下功能: 1.从后端请求数据并运用到图表中,形成动态数据. 2.查询出每年各个月中人数. 请看下面代码: Ext.define('ChartLineTest', { extend: 'Ext.panel.Panel', autoScroll : true, selectYear:null,//定义年份 initComponent: function ()

随机推荐