Last active
April 3, 2017 14:27
-
-
Save RadLikeWhoa/118b123959ef414a04fcae71911043fd to your computer and use it in GitHub Desktop.
Emergencies Makeover
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Makeover</title> | |
<link rel="stylesheet" href="style.css" /> | |
</head> | |
<body> | |
<script src="//d3js.org/d3.v4.min.js"></script> | |
<script src="main.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const columns = [ 'Activity' ] | |
let data = [ | |
[ 'Gleitschirm / Delta', [ 157, 143, 173 ] ], | |
[ 'Andere Bergsportarten', [ 196, 194, 205 ] ], | |
[ 'Mountainbike', [ 155, 176, 162 ] ], | |
[ 'Klettern (Fels)', [ 117, 113, 122 ] ], | |
[ 'Variantenabfahrten', [ 222, 161, 172 ] ], | |
[ 'Skitouren', [ 348, 314, 321 ] ], | |
[ 'Hochtouren', [ 362, 348, 402 ] ], | |
[ 'Bergwandern', [ 996, 1007, 1193 ] ] | |
] | |
data.forEach((d, i) => { | |
data[i][2] = Math.round(d[1].reduce((a, b) => a + b) / d[1].length) | |
}) | |
data = data.sort((a, b) => a[2] - b[2]).reverse() | |
const maxCases = Math.max.apply(null, data.map(d => d[2])) | |
const lines = (test, el) => { | |
const width = 140 | |
const height = 20 | |
const min = Math.min.apply(null, test) | |
const max = Math.max.apply(null, test) | |
let data = [] | |
for (let i = 0; i < test.length; i++) { | |
data[i] = { | |
'x': i, | |
'y': +test[i] | |
} | |
} | |
const x = d3.scaleLinear() | |
.range([ 0, width ]) | |
.domain([ 0, 2 ]) | |
const y = d3.scaleLinear() | |
.range([ height, 0 ]) | |
.domain([ min - 50, max + 50 ]) | |
const line = d3.line() | |
.x(d => x(d.x)) | |
.y(d => y(d.y)) | |
d3.select(el).append('svg') | |
.attr('width', width) | |
.attr('height', height) | |
.append('path') | |
.attr('class', 'line') | |
.datum(data) | |
.attr('d', line) | |
} | |
const bar = (val, el) => { | |
const width = 300 | |
const height = 20 | |
const x = d3.scaleLinear() | |
.range([ 0, width - 50 ]) | |
.domain([ 0, maxCases ]) | |
const svg = d3.select(el).append('svg') | |
.attr('width', width) | |
.attr('height', height) | |
svg.append('rect') | |
.attr('class', 'bar') | |
.datum(data) | |
.attr('height', height) | |
.attr('width', x(val)) | |
svg.append('text') | |
.text(val) | |
.attr('x', x(val) + 10) | |
.attr('y', 15) | |
.attr('fill', '#000') | |
} | |
const table = d3.select('body').append('table') | |
const thead = table.append('thead').append('tr') | |
const tbody = table.append('tbody') | |
thead.selectAll('th') | |
.data(columns) | |
.enter() | |
.append('th') | |
.text(d => d) | |
const trows = tbody.selectAll('tr') | |
.data(data) | |
.enter() | |
.append('tr') | |
const tcells = trows.selectAll('td.text') | |
.data(d => [ d[0] ]) | |
.enter() | |
.append('td') | |
.attr('class', 'text') | |
.text(d => d) | |
thead.append('th').text('Trend') | |
trows.selectAll('td.sparkline') | |
.data(d => [ d[1] ]) | |
.enter() | |
.append('td') | |
.attr('class', 'sparkline') | |
.each((d, i, nodes) => lines(d, nodes[i])) | |
thead.append('th').text('Average # of Emergencies') | |
trows.selectAll('td.bar') | |
.data(d => [ d[2] ]) | |
.enter() | |
.append('td') | |
.attr('class', 'bar') | |
.each((d, i, nodes) => bar(d, nodes[i])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
font: 100%/1.5 sans-serif; | |
margin: 1.5em; | |
} | |
path { | |
fill: none; | |
stroke: #000; | |
} | |
table { | |
margin: 0 auto; | |
border-collapse: collapse; | |
} | |
td, | |
th { | |
padding: 0.5em; | |
border-bottom: 1px solid #dadada; | |
} | |
th { | |
text-align: left; | |
} | |
svg { | |
display: block; | |
} | |
path { | |
stroke-width: 2px; | |
} | |
tbody tr:first-child { | |
color: #dd6a4c; | |
} | |
tbody tr:first-child rect, | |
tbody tr:first-child text { | |
fill: #dd6a4c; | |
} | |
tbody tr:first-child path { | |
stroke: #dd6a4c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment