Skip to content

Instantly share code, notes, and snippets.

@simonsarris
Created May 2, 2014 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonsarris/4cb5a870ad8733f92fa6 to your computer and use it in GitHub Desktop.
Save simonsarris/4cb5a870ad8733f92fa6 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>Minimal GoJS Sample</title>
<!-- Copyright 1998-2014 by Northwoods Software Corporation. -->
<script src="http://gojs.net/latest/release/go.js"></script>
<script id="code">
function init() {
var $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram = $(go.Diagram, "myDiagram", // create a Diagram for the DIV HTML element
{
initialContentAlignment: go.Spot.Center, // center the content
"undoManager.isEnabled": true // enable undo & redo
});
// define a simple Node template
myDiagram.nodeTemplate =
$(go.Node, "Auto", // the Shape will go around the TextBlock
$(go.Shape, "RoundedRectangle",
// Shape.fill is bound to Node.data.color
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 3 }, // some room around the text
// TextBlock.text is bound to Node.data.key
new go.Binding("text", "key"))
);
// but use the default Link template, by not setting Diagram.linkTemplate
// create the model data that will be represented by Nodes and Links
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" },
{ key: "Gamma", color: "lightgreen" },
{ key: "Delta", color: "pink" }
],
[
{ from: "Alpha", to: "Beta" },
{ from: "Alpha", to: "Gamma" },
{ from: "Beta", to: "Beta" },
{ from: "Gamma", to: "Delta" },
{ from: "Delta", to: "Alpha" }
]);
save();
}
// Show the diagram's model in JSON format that the user may edit
function save() {
document.getElementById("mySavedModel").value = myDiagram.model.toJson();
myDiagram.isModified = false;
}
function load() {
myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value);
}
function addarbitrary() {
var n = myDiagram.findNodeForKey('Alpha');
myDiagram.model.setDataProperty(n.data, 'someArbitraryStuff', 'some arbitrary string');
}
</script>
</head>
<body onload="init()">
<div id="sample">
<p>Minimal <b>GoJS</b> Sample</p>
<!-- The DIV for the Diagram needs an explicit size or else we won't see anything.
Also add a border to help see the edges. -->
<div id="myDiagram" style="border: solid 1px black; width:400px; height:400px"></div>
<button id="SaveButton" onclick="save()">Save</button>
<button onclick="load()">Load</button>
<button onclick="addarbitrary()">Add arbitrary data to Alpha</button>
<textarea id="mySavedModel" style="width:100%;height:300px">
</textarea>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment