gojs
Version:
Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams
364 lines (338 loc) • 17.7 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover"/>
<meta name="description" content="Interactive entity-relationship diagram or data model diagram implemented by GoJS in JavaScript for HTML."/>
<link rel="stylesheet" href="../assets/css/style.css"/>
<!-- Copyright 1998-2023 by Northwoods Software Corporation. -->
<title>Entity Relationship</title>
</head>
<body>
<!-- This top nav is not part of the sample code -->
<nav id="navTop" class="w-full z-30 top-0 text-white bg-nwoods-primary">
<div class="w-full container max-w-screen-lg mx-auto flex flex-wrap sm:flex-nowrap items-center justify-between mt-0 py-2">
<div class="md:pl-4">
<a class="text-white hover:text-white no-underline hover:no-underline
font-bold text-2xl lg:text-4xl rounded-lg hover:bg-nwoods-secondary " href="../">
<h1 class="my-0 p-1 ">GoJS</h1>
</a>
</div>
<button id="topnavButton" class="rounded-lg sm:hidden focus:outline-none focus:ring" aria-label="Navigation">
<svg fill="currentColor" viewBox="0 0 20 20" class="w-6 h-6">
<path id="topnavOpen" fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM9 15a1 1 0 011-1h6a1 1 0 110 2h-6a1 1 0 01-1-1z" clip-rule="evenodd"></path>
<path id="topnavClosed" class="hidden" fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
</svg>
</button>
<div id="topnavList" class="hidden sm:block items-center w-auto mt-0 text-white p-0 z-20">
<ul class="list-reset list-none font-semibold flex justify-end flex-wrap sm:flex-nowrap items-center px-0 pb-0">
<li class="p-1 sm:p-0"><a class="topnav-link" href="../learn/">Learn</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="../samples/">Samples</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="../intro/">Intro</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="../api/">API</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/products/register.html">Register</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="../download.html">Download</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="https://forum.nwoods.com/c/gojs/11">Forum</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/contact.html"
target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/contact.html', 'contact');">Contact</a></li>
<li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/sales/index.html"
target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/sales/index.html', 'buy');">Buy</a></li>
</ul>
</div>
</div>
<hr class="border-b border-gray-600 opacity-50 my-0 py-0" />
</nav>
<div class="md:flex flex-col md:flex-row md:min-h-screen w-full max-w-screen-xl mx-auto">
<div id="navSide" class="flex flex-col w-full md:w-48 text-gray-700 bg-white flex-shrink-0"></div>
<!-- * * * * * * * * * * * * * -->
<!-- Start of GoJS sample code -->
<script src="../release/go.js"></script>
<div id="allSampleContent" class="p-4 w-full">
<script src="../extensions/Figures.js"></script>
<script id="code">
function init() {
// Since 2.2 you can also author concise templates with method chaining instead of GraphObject.make
// For details, see https://gojs.net/latest/intro/buildingObjects.html
const $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram =
new go.Diagram("myDiagramDiv", // must name or refer to the DIV HTML element
{
allowDelete: false,
allowCopy: false,
"undoManager.isEnabled": true
});
function colorSwitch(n){
var isDark = myDiagram.model.modelData.darkMode;
if(n === "green"){
return ((isDark) ? "#429E6F" :"#62bd8e");
}
else if(n === "blue"){
return ((isDark) ? "#3f9fc6" :"#3999bf");
}
else if(n === "purple"){
return ((isDark) ? "#9951c9" :"#7f36b0");
}
else if(n === "red"){
return ((isDark) ? "#ff4d3d" :"#c41000");
}
}
// the template for each attribute in a node's array of item data
var itemTempl =
$(go.Panel, "Horizontal",
$(go.Shape,
{ desiredSize: new go.Size(15, 15), strokeJoin: "round", strokeWidth: 3, stroke: "#eeeeee", margin: 2 },
new go.Binding("figure", "figure"),
new go.Binding("fill", "color", n => colorSwitch(n)),
new go.Binding("stroke", "color", n => colorSwitch(n))
),
$(go.TextBlock,
{
font: " 14px sans-serif",
stroke: "black"
},
new go.Binding("text", "name"), new go.Binding("stroke", "", n => (myDiagram.model.modelData.darkMode) ? "#f5f5f5" :"#000000")),
);
// define the Node template, representing an entity
myDiagram.nodeTemplate =
$(go.Node, "Auto", // the whole node panel
{
selectionAdorned: true,
resizable: true,
layoutConditions: go.Part.LayoutStandard & ~go.Part.LayoutNodeSized,
fromSpot: go.Spot.LeftRightSides,
toSpot: go.Spot.LeftRightSides,
isShadowed: true,
shadowOffset: new go.Point(4, 4),
shadowColor: "#919cab"
},
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", v => new go.Size(NaN, NaN)).ofObject("LIST"),
// define the node's outer shape, which will surround the Table
$(go.Shape, "RoundedRectangle",
{ stroke: "#e8f1ff", strokeWidth: 3 },
new go.Binding("fill", "", n => (myDiagram.model.modelData.darkMode) ? "#4a4a4a" :"#f7f9fc")
),
$(go.Panel, "Table",
{ margin: 8,
stretch: go.GraphObject.Fill,
width: 160
},
$(go.RowColumnDefinition, { row: 0, sizing: go.RowColumnDefinition.None }),
// the table header
$(go.TextBlock,
{
row: 0, alignment: go.Spot.Center,
margin: new go.Margin(0, 24, 0, 2), // leave room for Button
font: "bold 16px sans-serif"
},
new go.Binding("text", "key"),
new go.Binding("stroke", "", n => (myDiagram.model.modelData.darkMode) ? "#d6d6d6" :"#000000")
),
// the collapse/expand button
$("PanelExpanderButton", "LIST", // the name of the element whose visibility this button toggles
{ row: 0, alignment: go.Spot.TopRight },
new go.Binding("ButtonIcon.stroke", "", n => (myDiagram.model.modelData.darkMode) ? "#d6d6d6" :"#000000")
),
// the list of Panels, each showing an attribute
$(go.Panel, "Table",
{
name: "LIST",
row: 1,
padding: 3,
alignment: go.Spot.TopLeft,
defaultAlignment: go.Spot.Left,
stretch: go.GraphObject.Horizontal,
itemTemplate: itemTempl,
}),
$(go.TextBlock,
{
font: "bold 15px sans-serif",
text: "Attributes",
row: 1,
alignment: go.Spot.TopLeft,
margin: new go.Margin(8, 0, 0, 0),
},
new go.Binding("stroke", "", n => (myDiagram.model.modelData.darkMode) ? "#d6d6d6" :"#000000")
),
$("PanelExpanderButton", "NonInherited", // the name of the element whose visibility this button toggles
{ row: 1,
column: 1
},
new go.Binding("ButtonIcon.stroke", "", n => (myDiagram.model.modelData.darkMode) ? "#d6d6d6" :"#000000")
),
$(go.Panel, "Vertical",
{
name: "NonInherited",
alignment: go.Spot.TopLeft,
defaultAlignment: go.Spot.Left,
itemTemplate: itemTempl,
row: 2
},
new go.Binding("itemArray", "items")
),
$(go.TextBlock,
{
font: "bold 15px sans-serif",
text: "Inherited Attributes",
row: 3,
alignment: go.Spot.TopLeft,
margin: new go.Margin(8, 0, 0, 0),
},
new go.Binding("visible", "visibility", Boolean),
new go.Binding("stroke", "", n => (myDiagram.model.modelData.darkMode) ? "#d6d6d6" :"#000000")
),
$("PanelExpanderButton", "Inherited" , // the name of the element whose visibility this button toggles
{
row: 3,
column: 1,
},
new go.Binding("visible", "visibility", Boolean),
new go.Binding("ButtonIcon.stroke", "", n => (myDiagram.model.modelData.darkMode) ? "#d6d6d6" :"#000000")
),
$(go.Panel, "Vertical",
{
name: "Inherited",
alignment: go.Spot.TopLeft,
defaultAlignment: go.Spot.Left,
itemTemplate: itemTempl,
row: 4
},
new go.Binding("itemArray", "inheriteditems")
)
)
// end Table Panel
); // end Node
// define the Link template, representing a relationship
myDiagram.linkTemplate =
$(go.Link, // the whole link panel
{
selectionAdorned: true,
layerName: "Background",
reshapable: true,
routing: go.Link.AvoidsNodes,
corner: 5,
curve: go.Link.JumpOver,
isShadowed: true,
shadowOffset: new go.Point(2, 2),
shadowColor: "#919cab"
},
$(go.Shape, // the link shape
{ stroke: "#f7f9fc", strokeWidth: 4 }),
$(go.Panel, "Auto", {segmentIndex: 0 , segmentOffset: new go.Point(22,0)},
$(go.Shape, "RoundedRectangle", {fill: "#f7f9fc"}, {stroke: "#eeeeee"}),
$(go.TextBlock, // the "from" label
{
textAlign: "center",
font: "bold 14px sans-serif",
stroke: "black",
background: "#f7f9fc",
segmentOffset: new go.Point(NaN, NaN),
segmentOrientation: go.Link.OrientUpright
},
new go.Binding("text", "text"))),
$(go.Panel, "Auto",
{
segmentIndex: -1,
segmentOffset: new go.Point(-13,0)
},
$(go.Shape, "RoundedRectangle", {fill: "#edf6fc"}, {stroke: "#eeeeee"}),
$(go.TextBlock, // the "to" label
{
textAlign: "center",
font: "bold 14px sans-serif",
stroke: "black",
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 = [
{
key: "Products", visibility: true, location: new go.Point(250,250) ,
items: [{ name: "ProductID", iskey: true, figure: "Decision", color: "purple" },
{ name: "ProductName", iskey: false, figure: "Hexagon", color: "blue" },
{ name: "ItemDescription", iskey: false, figure: "Hexagon", color: "blue" },
{ name: "WholesalePrice", iskey: false, figure: "Circle", color: "green", },
{ name: "ProductPhoto", iskey: false, figure: "TriangleUp", color: "red"}],
inheriteditems: [{ name: "SupplierID", iskey: false, figure: "Decision", color: "purple" },
{ name: "CategoryID", iskey: false, figure: "Decision", color: "purple" }]
},
{
key: "Suppliers", visibility: false,location: new go.Point(500,0) ,
items: [{ name: "SupplierID", iskey: true, figure: "Decision", color: "purple" },
{ name: "CompanyName", iskey: false, figure: "Hexagon", color: "blue" },
{ name: "ContactName", iskey: false, figure: "Hexagon", color: "blue" },
{ name: "Address", iskey: false, figure: "Hexagon", color: "blue" },
{ name: "ShippingDistance", iskey: false, figure: "Circle", color: "green", },
{ name: "Logo", iskey: false, figure: "TriangleUp", color: "red"}],
inheriteditems: []
},
{
key: "Categories", visibility: true,location: new go.Point(0,30) ,
items: [{ name: "CategoryID", iskey: true, figure: "Decision", color: "purple" },
{ name: "CategoryName", iskey: false, figure: "Hexagon", color: "blue" },
{ name: "Description", iskey: false, figure: "Hexagon", color: "blue" },
{ name: "Icon", iskey: false, figure: "TriangleUp", color: "red"}],
inheriteditems: [{ name: "SupplierID", iskey: false, figure: "Decision", color: "purple" }]
},
{
key: "Order Details", visibility: true,location: new go.Point(600,350) ,
items: [{ name: "OrderID", iskey: true, figure: "Decision", color: "purple" },
{ name: "UnitPrice", iskey: false, figure: "Circle", color: "green", },
{ name: "Quantity", iskey: false, figure: "Circle", color: "green", },
{ name: "Discount", iskey: false, figure: "Circle", color: "green" }],
inheriteditems: [{ name: "ProductID", iskey: true, figure: "Decision", color: "purple" }]
},
];
var linkDataArray = [
{ from: "Products", to: "Suppliers", text: "0..N", toText: "1" },
{ from: "Products", to: "Categories", text: "0..N", toText: "1" },
{ from: "Order Details", to: "Products", text: "0..N", toText: "1" },
{ from: "Categories", to: "Suppliers", text: "0..N", toText: "1" }
];
myDiagram.model = new go.GraphLinksModel(
{
copiesArrays: true,
copiesArrayObjects: true,
nodeDataArray: nodeDataArray,
linkDataArray: linkDataArray
});
myDiagram.model.modelData.darkMode = false;
}
function buttonClick()
{
var myDiagramDiv = document.getElementById("myDiagramDiv");
var button = document.getElementById("darkMode");
myDiagramDiv.style["background-color"] = (button.innerHTML === "Dark Mode") ? "#242424" : "#ffffff";
button.innerHTML = (button.innerHTML === "Dark Mode") ? "Light Mode" : "Dark Mode";
colorSwitch();
}
function colorSwitch()
{
myDiagram.model.commit(m => {
m.set(m.modelData, "darkMode", !m.modelData.darkMode);
}, null);
}
window.addEventListener('DOMContentLoaded', init);
</script>
<div id="sample">
<div id="myDiagramDiv" style="background-color: white; border: solid 1px black; width: 100%; height: 700px"></div>
<button id="darkMode" onclick="buttonClick()">Dark Mode</button>
<p>Sample for representing the relationship between various entities. Try dragging the nodes -- their links will avoid other nodes, by virtue of the <a>Link,AvoidsNodes</a> property assigned to the
custom link template's <a>Link.routing</a>. Also note the use of <a href="../intro/buttons.html" target="_blank">Panel Expander Buttons</a> to allow for expandable/collapsible node data.
</p>
<p>Buttons are defined in <a href="../extensions/Buttons.js">Buttons.js</a>.</p>
<p>The dark and light modes are controlled by using a boolean model property representing the current mode of the diagram. See the API page on <a href="../api/symbols/Model.html">Models</a> for more information of model data.</p>
</div>
</div>
<!-- * * * * * * * * * * * * * -->
<!-- End of GoJS sample code -->
</div>
</body>
<!-- This script is part of the gojs.net website, and is not needed to run the sample -->
<script src="../assets/js/goSamples.js"></script>
</html>