-
Notifications
You must be signed in to change notification settings - Fork 65
/
chart.html
85 lines (75 loc) · 1.97 KB
/
chart.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>chart-csv</title>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.20/c3.css"
rel="stylesheet"
type="text/css"
/>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"
charset="utf-8"
></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.20/c3.min.js"></script>
</head>
<body onload="run()">
<div id="graph"></div>
<script>
function parseCSV(csv) {
return csv
.split('\n')
.filter(function (row) {
return !!row.trim()
})
.map(function (row) {
return row.split(',')
})
}
function haveHead(csv) {
return !/^\d+$/.test(csv[0][0])
}
function chart(id, csv) {
csv = parseCSV(csv)
var columns = []
var showLegend = haveHead(csv)
if (showLegend) {
csv.shift().forEach(function (head) {
columns.push([head])
})
} else {
for (var n = 0; n < csv[0].length; n++) {
columns.push([n])
}
}
csv.forEach(function (r) {
r.forEach(function (c, i) {
columns[i].push(parseFloat(c, 10))
})
})
var opts = {
bindto: '#' + id,
data: {
// use first column as x value
x: columns[0][0],
columns: columns,
type: 'line',
},
point: { show: false },
axis: { x: { show: true } },
legend: { show: showLegend },
}
c3.generate(opts)
}
function run() {
chart('graph', csv)
}
var csv = `
n, node,lavamoat-node,bify,lavamoat-bify,endo-unsafe
0,0.053574319,0.452297975,0.029908432,0.082897103,0.483492059
5000000000,3.916342085,4.173224536,3.789678462,3.889670235,4.137583762
`
</script>
</body>
</html>