-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.html
125 lines (91 loc) · 3.27 KB
/
Index.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
<!DOCTYPE html>
<meta charset="utf-8">
<!--Hello! this is our first draft visualization attempt
This file includes an interactive bar chart that changes as you press the buttons
we will use this for interactive reference
code is taken from https://d3-graph-gallery.com/graph/barplot_button_data_simple.html -->
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<!-- Add 4 buttons -->
<button onclick="update(data1)">2017-2018</button>
<button onclick="update(data2)">2018-2019</button>
<button onclick="update(data3)">2019-2020</button>
<button onclick="update(data4)">2020-2021</button>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<script>
// create 2 data_set
var data1 = [
{group: "Marriage", value: -0.65},
{group: "Death", value: 16},
{group: "Birth", value: 8},
{group: "Unemployment", value: 20},
{group: "Crime", value: 10}
];
var data2 = [
{group: "Marriage", value: 7},
{group: "Death", value: 1},
{group: "Birth", value: 20},
{group: "Unemployment", value: 16},
{group: "Crime", value: 6}
];
var data3 = [
{group: "Marriage", value: 6},
{group: "Death", value: 10},
{group: "Birth", value: 2},
{group: "Unemployment", value: 20},
{group: "Crime", value: 20}
];
var data4 = [
{group: "Marriage", value: 7},
{group: "Death", value: 1},
{group: "Birth", value: 20},
{group: "Unemployment", value: 16},
{group: "Crime", value: 0}
];
// set the dimensions and margins of the graph
var margin = {top: 30, right: 30, bottom: 60, left: 60},
width = 460 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// X axis
var x = d3.scaleBand()
.range([ 0, width ]) // where the x axis starts on the left side
.domain(data1.map(function(d) { return d.group; }))
.padding(0.6);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
// Add Y axis
var y = d3.scaleLinear()
.domain([0, 20])
.range([ height, 1]);
svg.append("g")
.attr("class", "myYaxis")
.call(d3.axisLeft(y));
// A function that create / update the plot for a given variable:
function update(data) {
var u = svg.selectAll("rect")
.data(data)
u
.enter()
.append("rect")
.merge(u)
.transition()
.duration(1000)
.attr("x", function(d) { return x(d.group); })
.attr("y", function(d) { return y(d.value); })
.attr("width", x.bandwidth())
.attr("height", function(d) { return height - y(d.value); })
.attr("fill", "#69b3a2")
}
// Initialize the plot with the first dataset
update(data1)
</script>