This repository has been archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualization.html
570 lines (535 loc) · 23.9 KB
/
Visualization.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html" charset="UTF-8">
<title>Stellaris Galaxy Visualization</title>
<script type="text/javascript" src="./lib/d3%20v3.5.5/d3.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<style type="text/css">
.yearSliderContainer {
width: 95%;
border-bottom: gray solid 1px;
margin-bottom: 5px;
}
.nodeRadioContainer label{
vertical-align: top;
}
.nodeRadioContainer input[type="radio"]{
margin-top: 1px;
vertical-align: top;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 25px;
background: #d3d3d3;
outline: none;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
background: darkblue;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 25px;
height: 25px;
background: darkblue;
cursor: pointer;
}
body {
background: black;
font-family: "Roboto", sans-serif;
font-size: 12px;
color: white;
}
.nodeshape {
stroke: white;
stroke-width: 1px;
fill: #FFFFFF;
}
.nodeshape:hover {
stroke: orangered;
}
.nodetext {
fill: white;
border: 1px solid white;
z-index: 99;
}
.nodeinfobox {
fill: darkblue;
}
.link {
stroke: #555555;
}
.SystemInfoBox{
width: 50%;
border-left: darkblue solid 5px;
border-top: gray solid 1px;
padding-left: 5px;
margin-top: 15px;
padding-top: 5px;
}
.SystemComparisonBox{
width: 50%;
border-left: darkgreen solid 5px;
border-top: gray solid 1px;
padding-left: 5px;
margin-top: 15px;
padding-top: 5px;
}
</style>
</head>
<body>
<!-- SVG for the Star Chart -->
<svg id="Chart" width="100%"></svg>
<!-- DIV for the Year Slider -->
<div class="yearSliderContainer">
<input type="range" min="2100" max="2200" value="2100" class="slider" id="slider">
<p>Year: <span id="sliderValue"></span></p>
</div>
<!-- TODO: DIV for the Node Modification Radio Buttons -->
<div class="nodeRadioContainer">
Node Color Modifications:
<form>
<input type="radio" name="nodeColMod" value="none" checked><label>None</label>
<input type="radio" name="nodeColMod" value="countries"><label>Countries</label>
<input type="radio" name="nodeColMod" value="starbases"><label>Starbases</label>
</form>
</div>
<!-->
<!-- DIV for the System Info Box -->
<div style="justify-content: space-around; display: flex">
<div class="SystemInfoBox">
<div id="InfoBox_Name"></div>
<div id="InfoBox_Starbase"></div>
<div id="InfoBox_Bypasses"></div>
<div id="InfoBox_Planets"></div>
</div>
<div class="SystemComparisonBox">
<div id="ComparisonBox_Name"></div>
<div id="ComparisonBox_Starbase"></div>
<div id="ComparisonBox_Bypasses"></div>
<div id="ComparisonBox_Planets"></div>
</div>
</div>
<script type="module">
// Shift detection for comparison
let isShiftpressed = false;
window.onkeydown = function(e){ if(e.shiftKey) isShiftpressed = true; };
window.onkeyup = function(e){ isShiftpressed = false; };
/** import JSON data **/
d3.json("./JSON%20Data/superData.json", function (data) {
console.log(data);
/** setup svg **/
// get width to squarify svg
let svgSize = document.getElementById("Chart").getBoundingClientRect().width;
const svg = d3.select("svg#Chart")
.attr("width", svgSize)
.attr("height", svgSize);
// initialize variables for Chart
const margin = 10;
const chartSize = svgSize - 2 * margin;
/** define scale functions **
* Scale to map System coordinates [-500, 500] inside chart height & width
**/
const xScale = d3.scale.linear()
.range([margin, margin + chartSize - 1])
.domain([500, -500]);
const yScale = d3.scale.linear()
.range([margin, margin + chartSize - 1])
.domain([-500, 500]);
console.log("xScale, yScale: ", xScale, yScale);
/** define a color scale functions **
* ColorCategory:
* lookup for different country colors (taken from https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/)
* getCatColor:
* function returns color for index, returns white for all but the top 15
* getSBLevelColor:
* function returns color for Starbase level, returns white for others (taken from http://colorbrewer2.org/#type=sequential&scheme=Reds&n=6)
**/
const ColorCategory = [
"#e6194b", // Red
"#3cb44b", // Green
"#ffe119", // Yellow
"#0082c8", // Blue
"#f58231", // Orange
"#911eb4", // Purple
"#46f0f0", // Cyan
"#f032e6", // Magenta
"#d2f53c", // Lime
"#fabebe", // Pink
"#008080", // Teal
"#aa6e28", // Brown
"#800000", // Maroon
"#aaffc3", // Mint
"#808000", // Olive
"#ffffff" // White for all others (minor factions etc)
];
// function to get color for indices 0-14, else returns white
function getCatColor(index) {
if (index >= 0 && index < 15) return ColorCategory[index];
else return ColorCategory[15];
}
// function to get color for starbase level
function getSBLevelColor(level) {
switch (level){
case "outpost":
return "#fcbba1";
case "starport":
return "#fc9272";
case "starhold":
return "#fb6a4a";
case "starfortress":
return "#de2d26";
case "citadel":
return "#a50f15";
default:
return "#ffffff";
}
}
/** create mapping to find year index by date value **
* yeariMap.indexOf(<date>) returns index of year in data
**/
const yeariMap = data.map(function (d) {
return parseInt(d.date);
});
/** Draw Function for Chart **/
function drawStarChart(year) {
// deleting existing Graph before redraw
let nodeToClear = document.getElementById("Chart");
while (nodeToClear.firstChild){
nodeToClear.removeChild(nodeToClear.firstChild);
}
// draw new Chart
let yindex = yeariMap.indexOf(year);
console.log("drawing year: " + year + "(" + yindex + ")");
/** create Link Array for Layout **
* Format:
* [
* {
* "source": <system index>,
* "target": <system.hyperlane[j] value>
* }
* ...
* ]
**/
const links = [];
data[yindex].system.forEach(function (e, i) { // loop through system elements
e.hyperlanes.forEach(function (f, j) { // loop through hyperlane elements of each system
links.push({ // for each hyperlane element add new element to Links list
source: i,
target: parseInt(f)
});
});
});
/** create Layout **/
let force = d3.layout.force()
.nodes(data[yindex].system)
.links(links)
.size([chartSize, chartSize])
.start();
/** draw edges (hyperlanes) **/
let link = svg.selectAll(".link")
.data(links)
.enter()
.append("line")
.attr("class", "link")
.attr("x1", function (d) {
return xScale(d.source.coordinate.x);
})
.attr("y1", function (d) {
return yScale(d.source.coordinate.y);
})
.attr("x2", function (d) {
return xScale(d.target.coordinate.x);
})
.attr("y2", function (d) {
return yScale(d.target.coordinate.y);
});
/** draw graph nodes (systems) **/
let node = svg.selectAll("g")
.data(data[yindex].system)
.enter()
.append("g")
.attr("class", "node")
// add id to nodes to find them by name
.attr("id", function (d) {
return "System_" + d.name;
})
.attr("transform", function (d) {
return "translate(" + xScale(d.coordinate.x) + ", " + yScale(d.coordinate.y) + ")";
});
// add tooltip w/ system name
node.append("title")
.text(function (d) {
return d.name;
});
// draw node circle
node.append("circle")
.attr("class", "nodeshape")
.style("fill", function (d) {
// get starbase in system and find owner
let starbaseIndex = parseInt(d.starbase);
// switch case for the Node Color Modifications (Radio Buttons)
switch (settingNodeCol){
case "countries":
if (starbaseIndex) {
let starbaseOwner = parseInt(data[yindex].starbase[starbaseIndex].owner);
return getCatColor(starbaseOwner);
}
else return "#000000"; // if no starbase in system, no owner
case "starbases":
if (starbaseIndex){
let starbaseLevel = data[yindex].starbase[starbaseIndex].level;
return getSBLevelColor(starbaseLevel);
}
else break; // break to default to white (nodeshape CSS class)
default:
break; // break to default to white (nodeshape CSS class)
}
})
.attr("r", 5)
.on("click", function () {
selectSystem(this.parentNode, isShiftpressed);
});
// add label (Name)
node.append("text")
.attr("class", "nodetext")
.text(function (d) {
return d.name;
})
.text(function (d) {
d.textwidth = this.getComputedTextLength(); // compute Text length for Textbox
});
// add frame for label (readability)
node.append("rect")
.attr("class", "nodeinfobox")
.attr("width", function (d) {
return d.textwidth + 10;
})
.attr("height", 20)
.attr("rx", 5)
.attr("ry", 5)
.style("visibility", "hidden");
// re-append Text to move to top
node.append("text")
.attr("class", "nodetext")
.text(function (d) {
return d.name;
})
.attr("dy", 14)
.attr("dx", 5)
.style("visibility", "hidden");
}
/** Select function to mark selected Node and show name **/
let selectedNode;
let comparisonNode;
function selectSystem(selNode, cmp) {
if (selNode == null){
selectedNode = null;
comparisonNode = null;
}
// If shift is pressed select as comparison Node
if (cmp){
// unselect old Node if it exists
if (comparisonNode) {
d3.select(comparisonNode.childNodes[1]).style("stroke", "white");
d3.select(comparisonNode.childNodes[3]).style("visibility", "hidden")
.style("fill", "darkblue");
d3.select(comparisonNode.childNodes[4]).style("visibility", "hidden");
}
// update selected Node
if (selNode === comparisonNode) {
// breakout if same node is clicked again (deselect)
comparisonNode = null;
return;
}
comparisonNode = selNode;
console.log("selected: ", comparisonNode);
// highlight new Node
d3.select(comparisonNode.childNodes[1]).style("stroke", "green");
d3.select(comparisonNode.childNodes[3]).style("visibility", "visible")
.style("fill", "darkgreen");
d3.select(comparisonNode.childNodes[4]).style("visibility", "visible");
// re-append selected Node to move it in front
comparisonNode.parentNode.appendChild(comparisonNode);
}
else {
// unselect old Node if it exists
if (selectedNode) {
d3.select(selectedNode.childNodes[1]).style("stroke", "white");
d3.select(selectedNode.childNodes[3]).style("visibility", "hidden");
d3.select(selectedNode.childNodes[4]).style("visibility", "hidden");
}
// update selected Node
if (selNode === selectedNode) {
// breakout if same node is clicked again (deselect)
selectedNode = null;
return;
}
selectedNode = selNode;
console.log("selected: ", selectedNode);
// highlight new Node
d3.select(selectedNode.childNodes[1]).style("stroke", "red");
d3.select(selectedNode.childNodes[3]).style("visibility", "visible");
d3.select(selectedNode.childNodes[4]).style("visibility", "visible");
// re-append selected Node to move it in front
selectedNode.parentNode.appendChild(selectedNode);
}
// update System Info Box
updateInfobox();
}
/** setup slider for year **/
let currentYear;
let sliderMin = parseInt(data[0].date);
let sliderMax = parseInt(data[data.length-1].date);
d3.select("#slider")
.attr("min", sliderMin)
.attr("max", sliderMax);
const slider = document.getElementById("slider");
const sliderDisplay = document.getElementById("sliderValue");
/** slider update function **/
slider.oninput = function () {
sliderDisplay.innerHTML = slider.value;
currentYear = parseInt(slider.value);
// save selected System by id if any selected
let previousSysID = null;
let previousCmpID = null;
if (selectedNode) previousSysID = selectedNode.id;
if (comparisonNode) previousCmpID = comparisonNode.id;
// deselect System
selectSystem(null, isShiftpressed);
// redraw with new year
drawStarChart(currentYear);
// select previously selected system if any selected
if (previousSysID) selectSystem(document.getElementById(previousSysID), false);
if (previousCmpID) selectSystem(document.getElementById(previousCmpID), true);
// update Infobox after new selection
updateInfobox();
};
//** TODO: setup radio buttons **/
let settingNodeCol = "none";
const radiosNodeCol = document.getElementsByName("nodeColMod");
radiosNodeCol.forEach(function (e) {
e.onchange = function () {
settingNodeCol = this.value;
// save selected System by id if any selected
let previousSysID = null;
let previousCmpID = null;
if (selectedNode) previousSysID = selectedNode.id;
if (comparisonNode) previousCmpID = comparisonNode.id;
// deselect System
selectSystem(null, isShiftpressed);
// redraw with new year
drawStarChart(currentYear);
// select previously selected system if any selected
if (previousSysID) selectSystem(document.getElementById(previousSysID), false);
if (previousCmpID) selectSystem(document.getElementById(previousCmpID), true);
// update Infobox after new selection
updateInfobox();
}
});
/** function to update System Info Box **/
function updateInfobox() {
if (selectedNode == null) return;
// update System name
d3.select("#InfoBox_Name").text(
"System: " + selectedNode.__data__.name
);
// update Starbase
d3.select("#InfoBox_Starbase").text(function () {
let starbaseIndex = selectedNode.__data__.starbase;
let starbaseLevel = "None";
// return "None" if no starbase present
if (!starbaseIndex) return "Starbase: " + starbaseLevel;
else {
// shorten reference to starbase element in starbase array in current year
let starbaseData = data[yeariMap.indexOf(currentYear)].starbase[starbaseIndex];
// gather starbase level
starbaseLevel = starbaseData.level;
starbaseLevel = starbaseLevel[0].toUpperCase() + starbaseLevel.substring(1);
// TODO: gather owner name from country list (not yet parsed)
return "Starbase:\t\t" + starbaseLevel;
}
});
// update Planets
let planets = d3.select("#InfoBox_Planets")
.text("Planets:")
.selectAll("div")
.data(selectedNode.__data__.planets)
.enter()
.append("div")
.style("margin-left", "25px")
.html(function (d) {
let planet = data[yeariMap.indexOf(currentYear)].planet[parseInt(d)];
if (planet.colonization_date){
return "<b>"+planet.name+"</b><br/>" +
"Size: "+planet.size+"("+planet.tile_count+" Tiles)<br/>" +
"Colonized " +
planet.colonization_date.split(".").reverse().join(".") +
" (Population: "+planet.pop_count+" with "+planet.army_count+" armies)";
}
else {
return "<b>"+planet.name+"</b><br/>" +
"Size: "+planet.size+"("+planet.tile_count+" Tiles)";
}
});
// update Bypasses
d3.select("#InfoBox_Bypasses").text(function () {
// TODO: Bypass Info (not completely parsed)
});
// COMPARE BOX
if (comparisonNode == null) return;
// update System name
d3.select("#ComparisonBox_Name").text(
"System: " + comparisonNode.__data__.name
);
// update Starbase
d3.select("#ComparisonBox_Starbase").text(function () {
let starbaseIndex = comparisonNode.__data__.starbase;
let starbaseLevel = "None";
// return "None" if no starbase present
if (!starbaseIndex) return "Starbase: " + starbaseLevel;
else {
// shorten reference to starbase element in starbase array in current year
let starbaseData = data[yeariMap.indexOf(currentYear)].starbase[starbaseIndex];
// gather starbase level
starbaseLevel = starbaseData.level;
starbaseLevel = starbaseLevel[0].toUpperCase() + starbaseLevel.substring(1);
// TODO: gather owner name from country list (not yet parsed)
return "Starbase:\t\t" + starbaseLevel;
}
});
// update Planets
let cplanets = d3.select("#ComparisonBox_Planets")
.text("Planets:")
.selectAll("div")
.data(comparisonNode.__data__.planets)
.enter()
.append("div")
.style("margin-left", "25px")
.html(function (d) {
let planet = data[yeariMap.indexOf(currentYear)].planet[parseInt(d)];
if (planet.colonization_date){
return "<b>"+planet.name+"</b><br/>" +
"Size: "+planet.size+"("+planet.tile_count+" Tiles)<br/>" +
"Colonized " +
planet.colonization_date.split(".").reverse().join(".") +
" (Population: "+planet.pop_count+" with "+planet.army_count+" armies)";
}
else {
return "<b>"+planet.name+"</b><br/>" +
"Size: "+planet.size+"("+planet.tile_count+" Tiles)";
}
});
// update Bypasses
d3.select("#ComparisonBox_Bypasses").text(function () {
// TODO: Bypass Info (not completely parsed)
});
}
});
</script>
</body>