vue 中引用gojs繪制E-R圖的方法示例
首先,在vue項(xiàng)目中安裝gojs的依賴包,并在項(xiàng)目中引入。
創(chuàng)建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">返回上一級(jí)</router-link></Button> <Button type="primary" @click="searchDiagram()" style="float: right">Search</Button> <Input placeholder="請(qǐng)輸入要查詢的表名" 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中調(diào)用。
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")), // 折疊/展開(kāi)按鈕 /*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: "角色標(biāo)識(shí):BIGINT", iskey: true, figure: "Decision", color: yellowgrad }, { name: "模塊標(biāo)識(shí):BIGINT", iskey: false, figure: "Cube", color: bluegrad }] }, { key: "角色信息表", items: [ { name: "標(biāo)識(shí):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: "用戶權(quán)限表", 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: "角色權(quán)限表", to: "角色信息表", text: "N", toText: "1" }, { from: "角色權(quán)限表", to: "用戶權(quán)限表", text: "N", toText: "1" }, { from: "用戶信息表", to: "角色權(quán)限表", text: "N", toText: "1" }, { from: "表1", to: "角色權(quán)限表", text: "N", toText: "1" }, { from: "角色權(quán)限表", to: "表1", text: "N", toText: "1" }, { from: "表2", to: "用戶信息表", text: "N", toText: "1" }, { from: "表3", to: "用戶信息表", text: "N", toText: "1" }, { from: "表4", to: "角色權(quán)限表", 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實(shí)例的基礎(chǔ)上,加了遮罩圖與搜索后高亮顯示功能。
遮罩層的作用是當(dāng)有很多數(shù)據(jù)時(shí),可以通過(guò)拖動(dòng)遮罩層來(lái)查找。
這個(gè)是遮罩層的div
這里是把之前定義好的Diagram放進(jìn)遮罩層。
搜索框的作用即是更快的找到相應(yīng)的數(shù)據(jù),下圖代碼是設(shè)置搜索后顯示高亮的數(shù)據(jù)。
效果圖如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中v-model雙向綁定input輸入框問(wèn)題
這篇文章主要介紹了vue中v-model雙向綁定input輸入框問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue使用axios跨域請(qǐng)求數(shù)據(jù)問(wèn)題詳解
這篇文章主要為大家詳細(xì)介紹了vue使用axios跨域請(qǐng)求數(shù)據(jù)的問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Vue3?Composition?API優(yōu)雅封裝第三方組件實(shí)例
這篇文章主要為大家介紹了Vue3?Composition?API優(yōu)雅封裝第三方組件實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07基于vue 動(dòng)態(tài)菜單 刷新空白問(wèn)題的解決
這篇文章主要介紹了基于vue 動(dòng)態(tài)菜單 刷新空白問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue點(diǎn)擊右鍵出現(xiàn)自定義操作菜單實(shí)現(xiàn)代碼
這篇文章主要給大家介紹了關(guān)于vue點(diǎn)擊右鍵出現(xiàn)自定義操作菜單實(shí)現(xiàn)的相關(guān)資料,在網(wǎng)頁(yè)中我們也希望可以像桌面軟件一樣,點(diǎn)擊右鍵后出現(xiàn)操作菜單,對(duì)選中的數(shù)據(jù)項(xiàng)進(jìn)行相應(yīng)的操作,需要的朋友可以參考下2023-08-08Vue如何基于vue-i18n實(shí)現(xiàn)多國(guó)語(yǔ)言兼容
這篇文章主要介紹了Vue如何基于vue-i18n實(shí)現(xiàn)多國(guó)語(yǔ)言兼容,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Vue 過(guò)渡(動(dòng)畫(huà))transition組件案例詳解
這篇文章主要介紹了Vue 過(guò)渡(動(dòng)畫(huà))transition組件案例詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01