-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarchart04_scale.html
More file actions
120 lines (97 loc) · 3.21 KB
/
barchart04_scale.html
File metadata and controls
120 lines (97 loc) · 3.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>
<script src="../web_modules/d3/build/d3.js"></script>
<style type="text/css">
body {
width: 800px;
margin: 25px auto;
font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
}
rect {
fill: steelblue;
fill-opacity: 0.8;
}
rect:hover {
fill-opacity: 1;
}
.axis {
font-size: smaller;
}
</style>
</head>
<body>
<h1>Student's First BarChart</h1>
<script type="text/javascript">
const margin = {top: 40, bottom: 10, left: 120, right: 20};
const width = 800 - margin.left - margin.right;
const height = 600 - margin.top - margin.bottom;
// Creates sources <svg> element
const svg = d3.select('body').append('svg')
.attr('width', width+margin.left+margin.right)
.attr('height', height+margin.top+margin.bottom);
// Group used to enforce margin
const g = svg.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
// Global variable for all data
var data;
const bar_height = 50;
/////////////////////////
// TODO create an x and y scale and axis
// x a lienar scale with range: [0, width] and domain from 0 max temperature
// y a band ordinal scale range: [0, height] and domain the different cities
d3.json('weather.json', function(json) {
data = json;
update(data);
});
function update(new_data) {
const scales = setScales(new_data);
// Render the chart with new data
// DATA JOIN
const rect = g.selectAll('rect').data(new_data);
// ENTER
// new elements
const rect_enter = rect.enter().append('rect')
.attr('x', 0)
rect_enter.append('title');
// ENTER + UPDATE
// both old and new elements
rect.merge(rect_enter)
.attr('height', bar_height)
.attr('width', (d) => scales.scaleX(d.temperature))
.attr('y', (d, i) => scales.scaleY(i * (bar_height + 5)));
rect.merge(rect_enter).select('title').text((d) => d.location.city);
// EXIT
// elements that aren't associated with data
rect.exit().remove();
}
function setScales(new_data){
// X SCALE
// get setup data
const maxTemp = Math.max(...new_data.map(d=>d.temperature));
// set d3 elements
const scaleX = d3.scaleLinear().domain([0, maxTemp]).range([0, width]);
const axisX = d3.axisTop().scale(scaleX);
const axis_containerX = d3.select('svg').append('g')
.attr('class', 'axis')
.attr('transform', `translate(${margin.left},${margin.top})`);
// Y SCALE
// get setup data
const cities = new_data.map(v => v.location.city);
const rangesY = new_data.map((v,i) => (bar_height + 5) * i);
// set d3 elements
const scaleY = d3.scaleOrdinal().domain(cities).range(rangesY);
const axisY = d3.axisLeft().scale(scaleY);
const axis_containerY = d3.select('svg').append('g')
.attr('class', 'axis')
.attr('transform', `translate(${margin.left},${margin.top})`);
// scale axis containers
axis_containerX.call(axisX)
axis_containerY.call(axisY)
return {scaleX, scaleY}
}
</script>
</body>
</html>