-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbonus.js
219 lines (176 loc) · 5.52 KB
/
bonus.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var svgWidth = 960;
var svgHeight = 500;
var margin = {
top: 20,
right: 40,
bottom: 80,
left: 100
};
var width = svgWidth - margin.left - margin.right;
var height = svgHeight - margin.top - margin.bottom;
// Create an SVG wrapper, append an SVG group that will hold our chart,
// and shift the latter by left and top margins.
var svg = d3
.select(".chart")
.append("svg")
.attr("width", svgWidth)
.attr("height", svgHeight);
// Append an SVG group
var chartGroup = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
// Initial Params
var chosenXAxis = "obesity";
// function used for updating x-scale var upon click on axis label
function xScale(healthData, chosenXAxis) {
// create scales
var xLinearScale = d3.scaleLinear()
.domain([d3.min(healthData, d => d[chosenXAxis]) * 0.8,
d3.max(healthData, d => d[chosenXAxis]) * 1.2
])
.range([0, width]);
return xLinearScale;
}
// function used for updating xAxis var upon click on axis label
function renderAxes(newXScale, xAxis) {
var bottomAxis = d3.axisBottom(newXScale);
xAxis.transition()
.duration(1000)
.call(bottomAxis);
return xAxis;
}
// function used for updating circles group with a transition to
// new circles
function renderCircles(circlesGroup, newXScale, chosenXAxis) {
circlesGroup.transition()
.duration(1000)
.attr("cx", d => newXScale(d[chosenXAxis]));
return circlesGroup;
}
// function used for updating circles group with new tooltip
function updateToolTip(chosenXAxis, circlesGroup) {
var label;
if (chosenXAxis === "obesity") {
label = "Obesity:";
}
else {
label = "Smokes:";
}
var toolTip = d3.tip()
.attr("class", "tooltip")
.offset([80, -60])
.html(function(d) {
return (`${d.abbr}<br>${label} ${d[chosenXAxis]}`);
});
circlesGroup.call(toolTip);
circlesGroup.on("mouseover", function(data) {
toolTip.show(data);
})
// onmouseout event
.on("mouseout", function(data, index) {
toolTip.hide(data);
});
return circlesGroup;
}
// Retrieve data from the CSV file and execute everything below
d3.csv("data.csv").then(function(healthData, err) {
if (err) throw err;
// parse data
healthData.forEach(function(data) {
data.state = +data.state;
data.obesity = +data.obesity;
data.smokes = +data.smokes;
data.income = +data.income;
});
// xLinearScale function above csv import
var xLinearScale = xScale(healthData, chosenXAxis);
// Create y scale function
var yLinearScale = d3.scaleLinear()
.domain([0, d3.max(healthData, d => d.income)])
.range([height, 0]);
// Create initial axis functions
var bottomAxis = d3.axisBottom(xLinearScale);
var leftAxis = d3.axisLeft(yLinearScale);
// append x axis
var xAxis = chartGroup.append("g")
.classed("x-axis", true)
.attr("transform", `translate(0, ${height})`)
.call(bottomAxis);
// append y axis
chartGroup.append("g")
.call(leftAxis);
// append initial circles
var circlesGroup = chartGroup.selectAll("circle")
.data(healthData)
.enter()
.append("circle")
.attr("cx", d => xLinearScale(d[chosenXAxis]))
.attr("cy", d => yLinearScale(d.income))
.attr("r", 20)
.attr("fill", "pink")
.attr("opacity", ".5");
// Create group for two x-axis labels
var labelsGroup = chartGroup.append("g")
.attr("transform", `translate(${width / 2}, ${height + 20})`);
var obesityLabel = labelsGroup.append("text")
.attr("x", 0)
.attr("y", 20)
.attr("value", "obesity") // value to grab for event listener
.classed("active", true)
.text("Obesity Level");
var smokeLabel = labelsGroup.append("text")
.attr("x", 0)
.attr("y", 40)
.attr("value", "smokes") // value to grab for event listener
.classed("inactive", true)
.text("Smoke Level");
// append y axis
chartGroup.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.classed("axis-text", true)
.text("Income Level");
// updateToolTip function above csv import
var circlesGroup = updateToolTip(chosenXAxis, circlesGroup);
// x axis labels event listener
labelsGroup.selectAll("text")
.on("click", function() {
// get value of selection
var value = d3.select(this).attr("value");
if (value !== chosenXAxis) {
// replaces chosenXAxis with value
chosenXAxis = value;
console.log(chosenXAxis)
// functions here found above csv import
// updates x scale for new data
xLinearScale = xScale(healthData, chosenXAxis);
// updates x axis with transition
xAxis = renderAxes(xLinearScale, xAxis);
// updates circles with new x values
circlesGroup = renderCircles(circlesGroup, xLinearScale, chosenXAxis);
// updates tooltips with new info
circlesGroup = updateToolTip(chosenXAxis, circlesGroup);
// changes classes to change bold text
if (chosenXAxis === "smokes") {
obesityLabel
.classed("active", true)
.classed("inactive", false);
smokesLabel
.classed("active", false)
.classed("inactive", true);
}
else {
obesityLabel
.classed("active", false)
.classed("inactive", true);
smokesLabel
.classed("active", true)
.classed("inactive", false);
}
}
});
}).catch(function(error) {
console.log(error);
});
console.log(chosenXAxis);