gojs
Version:
Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams
178 lines (154 loc) • 7.2 kB
HTML
<html>
<head>
<meta charset="UTF-8">
<title>Sparkline Graphs GoJS Sample</title>
<meta name="description" content="Sparkline charts in a Diagram." />
<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", // create a Diagram for the DIV HTML element
{
layout: $(go.GridLayout, { wrappingColumn: 2, isOngoing: false }),
"undoManager.isEnabled": true // enable undo & redo
});
var SPARK_STROKEWIDTH = 1;
var SPARK_INTERVAL = 3;
var BASELINE_LENGTH = 75;
function makeStringFromValues(values) {
if (values.length < 1) return "M 0 " + values + " L " + BASELINE_LENGTH + " 0";
// if only one value, make a line BASELINE_LENGTHpx long at that value
var str = "M 0 " + Math.round(-values[0] * SPARK_INTERVAL);
if (values.length < 2) str += " L " + BASELINE_LENGTH * SPARK_INTERVAL + " " + Math.round(-values[0] * SPARK_INTERVAL);
for (var i = 0; i < values.length; i++) {
str += "L " + Math.round((i + 1) * SPARK_INTERVAL) + " " + Math.round(-values[i] * SPARK_INTERVAL);
}
return str;
};
// determine y offset
function makeAlignmentFromValues(values) {
var min = Infinity;
for (var i = 0; i < values.length; i++) {
min = Math.min(min, values[i]);
}
var y = (-min * SPARK_INTERVAL);
if (min > 0) { y = -SPARK_STROKEWIDTH; }; // add the strokeWidth
return new go.Spot(0, 1, 0, -y);
}
var sparkLine = $(go.Panel, "Horizontal",
{
alignment: go.Spot.Left,
alignmentFocusName: 'spark'
},
new go.Binding('alignmentFocus', 'values', makeAlignmentFromValues),
$(go.TextBlock,
{ visible: false, margin: 1, font: '11px sans-serif', background: 'white' },
new go.Binding('alignment', 'values', function(values) {
var min = Infinity;
var max = -Infinity;
for (var i = 0; i < values.length; i++) {
min = Math.min(min, values[i]);
max = Math.max(max, values[i]);
}
if (max - min === 0) return go.Spot.Center;
return new go.Spot(0, 1 - Math.abs((values[0] - min) / (max - min)), 0, 0);
}),
new go.Binding('stroke', 'color'),
new go.Binding('text', 'label'),
new go.Binding('visible', 'label', function(l) { return true; })
),
$(go.Shape, {
fill: null,
strokeWidth: SPARK_STROKEWIDTH,
stroke: 'gray',
name: 'spark'
},
new go.Binding('stroke', 'color'),
new go.Binding('geometryString', 'values', makeStringFromValues)
)
) // end sparkLine itemTemplate
myDiagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, { fill: 'rgba(200,200,255,.3)', strokeWidth: 0 }),
$(go.Panel, "Spot",
{ itemTemplate: sparkLine },
new go.Binding("itemArray", "items"),
$(go.Shape, { width: 1, height: 200, fill: 'red', stroke: null, strokeWidth: 0 })
)
);
function rand(min, max) {
return Math.random() * (max - min) + min;
}
// startValue optional
function makeRandomValues(numberOfValues, /* optional! */ startValue) {
var values = [];
var last = startValue || rand(-30, 30);
for (var i = 0; i < numberOfValues; i++) {
var newval = last + rand(-3, 3);
values.push(newval);
last = newval;
}
return values;
}
myDiagram.model = $(go.GraphLinksModel,
{
copiesArrays: true,
copiesArrayObjects: true,
nodeDataArray:
[
{
key: "Alpha", items: [
{ color: '#FF69B4', values: makeRandomValues(50, 20) },
{ color: '#FFB6C1', values: makeRandomValues(50) },
{ color: '#FF69B4', values: makeRandomValues(50) },
{ color: '#C71585', values: makeRandomValues(50, -20) },
{ color: 'gray', values: [0] }
]
},
{
key: "Beta", items: [
{ color: 'rgba(255,0,0,.6)', values: makeRandomValues(50), label: 'label A' },
{ color: 'rgba(0,0,255,.6)', values: makeRandomValues(50), label: 'long label B' },
{ color: 'darkgray', values: makeRandomValues(50), label: 'label C' },
{ color: 'lime', values: makeRandomValues(50), label: 'label D' },
{ color: 'gray', values: [0] }
]
},
{
key: "Gamma", items: [
{ color: 'rgba(255,0,0,.6)', values: makeRandomValues(50, -10), label: 'Important\nYear' },
{ color: 'gray', values: makeRandomValues(20, 30) },
{ color: 'gray', values: makeRandomValues(40, 30) },
{ color: 'gray', values: makeRandomValues(50, 30) },
{ color: 'gray', values: [0] }
]
}
],
});
}
</script>
</head>
<body onload="init()">
<div id="sample">
<!-- The DIV for the Diagram needs an explicit size or else we won't see anything.
This also adds a border to help see the edges of the viewport. -->
<div id="myDiagramDiv" style="border: solid 1px black; width:700px; height:500px"></div>
<p>
This sample shows how to create and align sparkline style charts in a Node, using Spot Panel and <code>Panel.alignmentFocusName</code>.
</p>
<p>
The Spot Panel's main element is a red vertical line, and its other elements are items in an item array. Instead of aligning to these item array items, which are Horizontal Panels, we want to align to the Shape that represents the sparkline, inside of the Panel.
To do this we set <code>Panel.alignmentFocusName</code> to <code>"spark"</code>, and then set the <code>alignmentFocus</code> in
<code>function makeAlignmentFromValues()</code> to the top left point, plus some offset to normalize the y-axis of the sparklines.
</p>
<p>
The Sparklines exist in a horizontal panel with optional labels at the front, and these sparkline labels also have their alignment computed within the horizontal panel, based on the fractional height of the starting value of the sparkline.
</p>
</div>
</body>
</html>