-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceableSankey.html
272 lines (233 loc) · 6.88 KB
/
traceableSankey.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!-- Styles -->
<style>
body {
font-family: Helvetica, Arial, sans-serif, "Apple Color Emoji",
"Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv {
width: 100%;
height: 90%;
padding: 20 auto;
}
.amcharts-highlightMe > g > g {
fill-opacity: 1;
}
</style>
<!-- Resources -->
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<!-- Chart code -->
<script>
//<---------Labelling and highlighting code--------------->
getMatchers = matchers => {
const originalMatchers = [...matchers];
console.log(input);
// filter input to get all the children of the targets
const children = input.filter(connection =>
matchers.includes(connection.to)
);
children.forEach(child => {
if (!matchers.includes(child.from)) {
matchers.push(child.from);
}
});
if (originalMatchers.length === matchers.length) {
return children;
} else {
return getMatchers(matchers);
}
};
stripIds = (input, target) => {
input.map(row => {
delete row.id;
});
return input;
};
addIdHighlight = (input, target) => {
input.map(row => {
target.map(t => {
if (t.from === row.from && t.to === row.to) {
row.id = "highlightMe";
}
});
});
return input;
};
function dataReload(target, input) {
const x = [target.from];
const dataToHightlight = getMatchers(x);
const strippedInput = stripIds(input, dataToHightlight);
const output = addIdHighlight(input, dataToHightlight);
return output;
}
function dataDehighlight(target, input) {
const x = [target.from];
const dataToHightlight = getMatchers(x);
const strippedInput = stripIds(input, dataToHightlight);
document.getElementById("chartdiv").style.display = "none";
document.getElementById("chartdiv").style.display = "block";
return strippedInput;
}
//<------Data Munging work------------------>
function getDataStructure(input) {
//get the deduplicated list (ie. no double values in a list)
const uniqueFrom = [...new Set(input.map(a => a.from))];
const uniqueTo = [...new Set(input.map(a => a.to))];
const unique = [...new Set([...uniqueFrom, ...uniqueTo])];
// console.log(uniqueFrom)
// console.log(uniqueTo)
// console.log(unique)
//Task one - get the unique lists totals (ie. the number of times)
//Stage one - look at all the froms and count the ones going
let fromRegister = {};
for (const key of unique) {
fromRegister[key] = 0;
}
input.map(obj => {
for (i = 0; i < unique.length; i++) {
if (unique[i] === obj.to) {
fromRegister[obj.to]++;
}
}
});
//Stage two - look at all the tos and count them
let toRegister = {};
for (const key of unique) {
toRegister[key] = 0;
}
input.map(obj => {
for (i = 0; i < unique.length; i++) {
if (unique[i] === obj.from) {
toRegister[obj.from]++;
}
}
});
//Task Two - the algo
//Stage one - for each item that doesn't appear in the TO column gets the value of 1
const groundMetrics = unique.filter(val => !uniqueTo.includes(val));
input.map(row => {
if (groundMetrics.includes(row.from)) {
row.value = 1;
}
});
//Stage two - for each non-ground metric, find the value going into it and store that
input.map(row => {
let valueIntoNode = row.value || 0;
//get just the records that are not groundMetrics and hence appear in the TO column
if (uniqueTo.includes(row.from)) {
//filter the rows that are specific to the row we are looking at
const incomingFilteredRows = input.filter(row2 => row2.to === row.from); // for a node X, [{from: A, to: X, value: 1}, {from: B, to: X, value: 1}]
incomingFilteredRows.forEach(obj => {
valueIntoNode += obj.value;
});
const outGoingFilteredRows = input.filter(
row2 => row2.from === row.from
);
let valueOutOfNode = row.value || outGoingFilteredRows.length || 1;
// console.log(valueIntoNode)
// console.log(valueOutOfNode)
// row.value = valueOutOfNode / valueIntoNode || Math.max(valueIntoNode, valueOutOfNode);
row.value = valueIntoNode / valueOutOfNode;
}
});
//Stage three - add extra fields
input.map(row => {
row.linkColor = "#551a8b";
row.nodeColor = "#06D6A0";
});
return input
}
//<------Input Declatation------------------>
let raw = [
{
from: "A",
to: "E"
},
{
from: "B",
to: "E"
},
{
from: "B",
to: "F"
},
{
from: "C",
to: "F"
},
{
from: "F",
to: "P"
},
{
from: "D",
to: "E"
},
{
from: "E",
to: "I"
},
{
from: "P",
to: "J"
},
{
from: "I",
to: "J"
},
{
from: "J",
to: "Q"
},
{ from: "F", to: "Q" }
];
let input = getDataStructure(raw);
let target = { from: "", to: "" };
//<------AmCharts Code------------------>
am4core.ready(function() {
var chart = am4core.create("chartdiv", am4charts.SankeyDiagram);
chart.hiddenState.properties.opacity = 0; // this creates initial fade-in
chart.data = dataReload(target, input);
chart.dataFields.fromName = "from";
chart.dataFields.toName = "to";
chart.dataFields.value = "value";
//use defined colour
chart.dataFields.color = "nodeColor";
chart.links.template.propertyFields.fill = "linkColor";
//highlight on hover
let hoverState = chart.links.template.states.create("hover");
hoverState.properties.fillOpacity = 0.6;
//
chart.links.template.propertyFields.id = "id";
chart.links.template.fillOpacity = 0.2;
// for right-most label to fit
chart.paddingRight = 30;
// make nodes draggable
var nodeTemplate = chart.nodes.template;
nodeTemplate.inert = true;
nodeTemplate.readerTitle = "Drag me!";
nodeTemplate.showSystemTooltip = true;
nodeTemplate.width = 20;
nodeTemplate.clickable = false;
//Css class names set to true
am4core.options.autoSetClassName = true;
// Add events on links
chart.links.template.cursorOverStyle = am4core.MouseCursorStyle.pointer;
chart.links.template.tooltipText = "{from} to {to}";
//on click highlight below
chart.links.template.events.on("hit", function(ev) {
chart.colors.reset();
var linkData = ev.target.dataItem.dataContext;
if (linkData === target) {
target = "";
chart.data = dataDehighlight(target, input);
} else {
target = linkData;
chart.data = dataReload(target, input);
}
});
});
</script>
<!-- HTML -->
<div id="chartdiv"></div>