-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrome-firefox-versions.html
147 lines (130 loc) · 3.61 KB
/
chrome-firefox-versions.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
<!DOCTYPE html>
<html>
<head>
<title>Chrome vs Firefox versions</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.17/c3.min.css">
</head>
<body>
<h3>chartjs</h3>
<canvas id="myChart" width="400" height="200"></canvas>
<h3>c3js</h3>
<div id="chart"></div>
<script src="https://unpkg.com/moment/min/moment.min.js"></script>
<script src="https://unpkg.com/chart.js/dist/Chart.min.js"></script>
<script src="https://unpkg.com/d3/dist/d3.min.js"></script>
<script src="https://unpkg.com/c3/c3.min.js"></script>
<script>
load()
.then(({chromeData, ffData}) => {
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Chrome versions',
borderColor: '#00f',
data: chromeData,
borderWidth: 1,
fill: false,
}, {
label: 'Firefox versions',
borderColor: '#f00',
data: ffData,
borderWidth: 1,
fill: false,
}]
},
options: {
responsive: true,
title:{
display:true,
text:'Chrome Vs Firefox versions'
},
tooltips: {
mode: 'x',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
type: "time",
display: true,
time: {format: 'YYYY-MM-DD'},
scaleLabel: {
display: true,
labelString: 'Date'
}
}],
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
var chart = c3.generate({
data: {
xs: {
'chrome': 'x1',
'firefox': 'x2',
},
columns: [
['x1'].concat(chromeData.map(o => o.x)),
['x2'].concat(ffData.map(o => o.x)),
['chrome'].concat(chromeData.map(o => o.y)),
['firefox'].concat(ffData.map(o => o.y))
]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}
});
});
function load() {
if (localStorage.chromeVsFirefoxVersions) {
var cache = JSON.parse(localStorage.chromeVsFirefoxVersions);
if (Date.now() < cache.time + 7*86.4e6) {
return Promise.resolve(cache.data);
}
}
return Promise.all([
fetch('https://cors-anywhere.herokuapp.com/https://en.wikipedia.org/wiki/Google_Chrome_release_history').then(r => r.text()),
fetch('https://cors-anywhere.herokuapp.com/https://en.wikipedia.org/wiki/Firefox_release_history').then(r => r.text())
])
.then(r => {
const [chromeDoc, ffDoc] = r.map(x => new DOMParser().parseFromString(x, "text/html"));
const chromeT = chromeDoc.querySelectorAll('table.wikitable')[1];
const chromeM = Array.from(chromeT.rows).slice(1, -1).map(tr => [tr.cells[0].textContent, tr.cells[1].textContent]);
const chromeD = transpose(chromeM);
const chromeData = chromeD[1].map((d,i) => ({
x: (d.match(/\d{4}-\d{2}-\d{2}/)||[])[0],
y: +chromeD[0][i].match(/\d+\.\d+/)
}));
const ffTs = Array.from(ffDoc.querySelectorAll('table.wikitable.mw-collapsible'));
const ffM =ffTs.flatMap(t => Array.from(t.rows).slice(1).filter(tr=>tr.cells.length>=2 && tr.cells[0].nodeName==='TD').map(tr => [tr.cells[0].textContent.trim(), tr.cells[1].textContent.trim()]).filter(([v]) => /^\d+\.\d+/));
const ffD=transpose(ffM)
const ffData = ffD[1].map((d, i) => ({
x: new Date(d.split('(')[0]).toJSON().slice(0, 10),
y: +ffD[0][i].match(/\d+\.\d+/)
}));
localStorage.chromeVsFirefoxVersions = JSON.stringify({time: Date.now(), data: {chromeData, ffData}});
return {chromeData, ffData};
});
}
function transpose(a) {
return Object.keys(a[0]).map(i=> a.map(r => r[i]));
}
</script>
</body>
</html>