-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (55 loc) · 1.89 KB
/
script.js
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
var parentDiv = document.getElementById('graph-wrapper'),
svg = d3.select('svg'),
svgWidth = parentDiv.offsetWidth,
svgHeight = parentDiv.offsetHeight,
margin = { top: 20, right: 30, bottom: 30, left: 60 },
innerWidth = svgWidth - margin.left - margin.right,
innerHeight = svgHeight - margin.top - margin.bottom,
colors = ['#D65076', '#45B8AC', '#EFC050', '#5B5EA6', '#9B2335'],
aspect = svgWidth / svgHeight;
function renderData(data) {
var xScale = d3
.scaleLinear()
.domain([0, d3.max(data, (d) => d.amount)])
.range([0, innerWidth]),
yScale = d3
.scaleBand()
.domain(data.map((d) => d.item))
.range([0, innerHeight])
.padding(0.04);
var g = svg
.append('g')
.attr('transform', `translate( ${margin.left}, ${margin.top} )`);
// format x axis. Replace default G w/ B for billion
var formattedVal = (number) => d3.format('.3s')(number).replace('G', 'B');
var yAxis = d3.axisLeft(yScale);
var xAxis = d3.axisBottom(xScale).tickFormat(formattedVal);
yAxis(g.append('g'));
xAxis(g.append('g').attr('transform', 'translate(0, ' + innerHeight + ')'));
g.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('transform', 'translate(4, 0)')
.attr('y', (d) => yScale(d.item))
.attr('fill', (d, i) => {
return colors[i];
})
.attr('width', (d) => xScale(d.amount))
.attr('height', yScale.bandwidth()); // computed width of a single bar
}
function resizeGraph() {
var targetWidth = parentDiv.clientWidth;
svg.attr('width', targetWidth);
svg.attr('height', Math.round(targetWidth / aspect));
}
svg
.attr('width', svgWidth)
.attr('height', svgHeight)
.attr('viewBox', '0 0 ' + svgWidth + ' ' + svgHeight)
.attr('perserveAspectRatio', 'xMinYMid')
.call(resizeGraph);
d3.json('/data.json').then(function (data) {
renderData(data);
});
window.addEventListener('resize', resizeGraph);