-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.js
More file actions
75 lines (72 loc) · 2.21 KB
/
Copy pathplot.js
File metadata and controls
75 lines (72 loc) · 2.21 KB
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
document.addEventListener("DOMContentLoaded", async function () {
var ctx = document.getElementById('plot').getContext('2d');
var time = [0];
var aliveData = [];
var lastValueAlive;
var deadData = [];
var lastValueDead;
var generation = 1;
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: time,
datasets: [{
label: 'Cells appeared',
data: aliveData,
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
fill: false
}, {
label: 'Cells died',
data: deadData,
borderColor: 'rgba(255, 99, 132, 1)',
backgroundColor: 'rgba(255, 99, 132, 0.2)',
fill: false
}]
},
options: {
responsive: true,
scales: {
x: {
title: {
display: true,
text: 'Generation'
}
},
y: {
title: {
display: true,
text: 'Number of Cells'
}
}
}
}
});
function updatePlot() {
var aliveCells = parseInt(document.getElementById('aliveCells').innerText.split(" ")[1]);
var deadCells = parseInt(document.getElementById('deadCells').innerText.split(" ")[1]);
if (generation == 1) {
lastValueAlive = aliveCells;
lastValueDead = deadCells;
}
else {
aliveData.push(aliveCells - lastValueAlive);
deadData.push(deadCells - lastValueDead);
lastValueAlive = aliveCells;
lastValueDead = deadCells;
chart.update();
}
time.push(generation);
generation += 1;
if (time.length > 1000) {
time.shift();
aliveData.shift();
deadData.shift();
}
}
while (true) {
updatePlot()
if (document.getElementById('generationNum').innerHTML.includes("ended")) break;
await delay(100);
}
});