forked from NorthwoodsSoftware/GoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparklineGraphs.html
178 lines (154 loc) · 6.85 KB
/
sparklineGraphs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<!DOCTYPE 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-2019 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>