gojs
Version:
Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams
138 lines (131 loc) • 5.84 kB
HTML
<html>
<head>
<meta charset="UTF-8">
<title>Kitten Monitor</title>
<meta name="description" content="A visual monitor of the position of kittens in a house." />
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Copyright 1998-2020 by Northwoods Software Corporation. -->
<script src="../release/go.js"></script>
<script src="../assets/js/goSamples.js"></script> <!-- this is only for the GoJS Samples framework -->
<script id="code">
function init() {
if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this
var $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram =
$(go.Diagram, "myDiagramDiv",
{
initialContentAlignment: go.Spot.TopLeft,
isReadOnly: true, // allow selection but not moving or copying or deleting
scaleComputation: function(d, newsc) {
// only allow scales that are a multiple of 0.1
var oldsc = Math.round(d.scale * 10);
var sc = oldsc + ((newsc * 10 > oldsc) ? 1 : -1);
if (sc < 1) sc = 1; // but disallow zero or negative!
return sc / 10;
},
"toolManager.hoverDelay": 100 // how quickly tooltips are shown
});
// the background image, a floor plan
myDiagram.add(
$(go.Part, // this Part is not bound to any model data
{
width: 840, height: 570,
layerName: "Background", position: new go.Point(0, 0),
selectable: false, pickable: false
},
$(go.Picture, "https://upload.wikimedia.org/wikipedia/commons/9/9a/Sample_Floorplan.jpg")
));
// the template for each kitten, for now just a colored circle
myDiagram.nodeTemplate =
$(go.Node,
new go.Binding("location", "loc"), // specified by data
new go.AnimationTrigger("position", { duration: 2000 }),
{ locationSpot: go.Spot.Center }, // at center of node
$(go.Shape, "Circle",
{ width: 15, height: 15, strokeWidth: 3 },
new go.Binding("fill", "color", makeFill),
new go.Binding("stroke", "color", makeStroke)
), // also specified by data
{ // this tooltip shows the name and picture of the kitten
toolTip:
$("ToolTip",
$(go.Panel, "Vertical",
$(go.Picture, { margin: 3 },
new go.Binding("source", "src", function(s) { return "images/" + s + ".png"; })),
$(go.TextBlock, { margin: 3 },
new go.Binding("text", "key"))
)
) // end Adornment
}
);
// pretend there are four kittens
myDiagram.model.nodeDataArray = [
{ key: "Alonzo", src: "50x40", loc: new go.Point(220, 130), color: 2 },
{ key: "Coricopat", src: "55x55", loc: new go.Point(420, 250), color: 4 },
{ key: "Garfield", src: "60x90", loc: new go.Point(640, 450), color: 6 },
{ key: "Demeter", src: "80x50", loc: new go.Point(140, 350), color: 8 }
];
// simulate some real-time position monitoring, once every 2 seconds
function randomMovement() {
var model = myDiagram.model;
model.startTransaction("update locations");
var arr = model.nodeDataArray;
var picture = myDiagram.parts.first();
for (var i = 0; i < arr.length; i++) {
var data = arr[i];
var pt = data.loc;
var x = pt.x + 25 * Math.random() - 10;
var y = pt.y + 25 * Math.random() - 10;
// make sure the kittens stay inside the house
var b = picture.actualBounds;
if (x < b.x || x > b.right) x = pt.x;
if (y < b.y || y > b.bottom) y = pt.y;
model.setDataProperty(data, "loc", new go.Point(x, y));
}
model.commitTransaction("update locations");
}
function loop() {
setTimeout(function() { randomMovement(); loop(); }, 2000);
}
loop(); // start the simulation
// generate some colors based on hue value
function makeFill(number) {
return HSVtoRGB(0.1 * number, 0.5, 0.7);
}
function makeStroke(number) {
return HSVtoRGB(0.1 * number, 0.5, 0.5); // same color but darker (less V in HSV)
}
function HSVtoRGB(h, s, v) {
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return 'rgb(' +
Math.floor(r * 255) + ',' +
Math.floor(g * 255) + ',' +
Math.floor(b * 255) + ')';
}
}
</script>
</head>
<body onload="init()">
<div id="sample">
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
<p>This diagram displays a monitored floor plan with several nodes (representing kittens) to view in real-time.</p>
<p>Every two seconds the kitten positions are updated</p>
<p>The <a href="../intro/toolTips.html">Tooltip</a> for each kitten shows its name and photo.</p>
<p>There is a custom <a>Diagram.scaleComputation</a> that limits the <a>Diagram.scale</a> values to multiples of 0.1.</p>
</div>
</body>
</html>