generated from usf-cs360-spring2020/template-bulma
-
Notifications
You must be signed in to change notification settings - Fork 1
/
enrique.js
308 lines (250 loc) · 8.84 KB
/
enrique.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
function drawBarchart(data){
// configuration of svg/plot area
let config = {
'svg': {},
'margin': {},
'plot': {}
};
config.margin.top = 120;
config.margin.right = 25;
config.margin.bottom = 0;
config.margin.left = 30;
config.svg.height = 700 - config.margin.top - config.margin.bottom;
config.svg.width = 970 - config.margin.left - config.margin.right;
let lowerpad = 0;//for moving the chart up and down.
// setup svg
let svg = d3.select("#barChart");
svg.attr('width', config.svg.width + config.margin.left +config.margin.right);
svg.attr('height', config.svg.height + 2*config.margin.top + config.margin.bottom);
svg.attr("transform", translate(config.margin.left, config.margin.top))
//svg.style("background-color", "pink");
//x-axis scaleband (for months) to the middle of the svg for the left bar-chart
let x = d3.scaleBand()
.range([0, config.svg.width/2])// to the middle
.domain(data.map(d => d.Month))
.paddingInner(0.2)
.paddingOuter(0.1);
//x-axis scaleband (for months) from the middle of the svg for the right bar-chart
let x2 = d3.scaleBand()
.range([config.svg.width/2, config.svg.width]) // from the middle
.domain(data.map(d => d.Month))
.paddingInner(0.2)
.paddingOuter(0.1);
//y-axis scale linear by minutes
let y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.NonLifeThreatening)])
.range([config.svg.height, 0])
.nice();
//drawing the x axis left
xaxis = d3.axisBottom(x)
.tickSize(5)
.tickPadding(5)
.tickSizeOuter(0);
//drawing the x axis right
xaxis2 = d3.axisBottom(x2)
.tickSize(5)
.tickPadding(5)
.tickSizeOuter(0);
yaxis = d3.axisLeft(y)
.tickValues(d3.range(0, 11, 1))
.ticks(10)
.tickPadding(1);
//left axis
svg.append("g")
.attr("id", "x-axis")
.attr("transform", `translate(${config.margin.left}, ${config.svg.height + config.margin.top - lowerpad})`)
.call(xaxis)
.selectAll("text") //from here adding the months texts and rotating them
.attr("y", 6) //to align the rotation to the tick mark
.attr("x", 9)
.attr("dy", ".35em")
.attr("transform", "rotate(45)")
.style("text-anchor", "start");
//left axis
svg.append("g")
.attr("id", "x-axis2")
.attr("transform", `translate(${config.margin.left}, ${config.svg.height + config.margin.top - lowerpad})`)
.call(xaxis2)
.selectAll("text") //from here adding the months texts and rotating them
.attr("y", 6) //to align the rotation to the tick mark
.attr("x", 9)
.attr("dy", ".35em")
.attr("transform", "rotate(45)")
.style("text-anchor", "start");
svg.append("g")
.attr("id", "y-axis")
.attr("transform", `translate(${config.margin.left}, ${config.margin.top - lowerpad})`)
.call(yaxis);
// add the Y gridlines
svg.append("g")
.attr("class", "grid")
.attr("transform", `translate(${config.margin.left}, ${config.margin.top - lowerpad})`)
.call(yaxis
.tickSize(-config.svg.width)
.tickFormat("")
);
//minute label for y-axis
svg
.append("text")
.attr("class", "legend-text")
.attr("x", config.margin.left - 20)
.attr("y", config.margin.top -18)
.text("Minutes")
.attr("alignment-baseline", "middle")
.style("font-size","12px")
.style('fill', 'black');
//label for non life threatening calls
// svg
// .append("text")
// .attr("class", "legend-text")
// .attr("x", config.margin.left + 30)
// .attr("y", config.margin.top -40)
// .text("Non Life Threatening Medical Calls")
// .attr("alignment-baseline", "middle")
// .style("font-size","20px")
// .style('fill', 'black');
//label for non life threatening calls
// svg
// .append("text")
// .attr("class", "legend-text")
// .attr("x", config.margin.left + 475)
// .attr("y", config.margin.top -40)
// .text("Potentially Life Threatening Medical Calls")
// .attr("alignment-baseline", "middle")
// .style("font-size","20px")
// .style('fill', 'black');
//drawbars left
let bars = svg.append("g")
.attr("id", "bars")
.attr("transform", `translate(${config.margin.left}, ${config.margin.top})`)
.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", d => x(d.Month))
.attr("y", d => y(d.NonLifeThreatening))
.attr("width", x.bandwidth())
.attr("height", d => config.svg.height- lowerpad - y(d.NonLifeThreatening))
.style("fill", "#3a9dc7");
//drawbars right
let bars2 = svg.append("g")
.attr("id", "bars2")
.attr("transform", `translate(${config.margin.left}, ${config.margin.top})`)
.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", d => x2(d.Month))
.attr("y", d => y(d.PotentiallyLifeThreatening))
.attr("width", x2.bandwidth())
.attr("height", d => config.svg.height- lowerpad - y(d.PotentiallyLifeThreatening))
.style("fill", "#7FFFD4");
//Mouseover start
bars.on("mouseover", function(d) {
bars.filter(e => (d.Month !== e.Month)).transition().style("fill", "lightgrey").attr("opacity", "0.7");
bars2.filter(e => (d.Month !== e.Month)).transition().style("fill", "lightgrey").attr("opacity", "0.7");
});
bars2.on("mouseover", function(d) {
bars2.filter(e => (d.Month !== e.Month)).transition().style("fill", "lightgrey").attr("opacity", "0.7");
bars.filter(e => (d.Month !== e.Month)).transition().style("fill", "lightgrey").attr("opacity", "0.7");
});
bars.on("mouseout", function(d) {
bars.transition().style("fill", "#3a9dc7").attr("opacity", "1");
bars2.transition().style("fill", "#7FFFD4").attr("opacity", "1");
});
bars2.on("mouseout", function(d) {
bars2.transition().style("fill", "#7FFFD4").attr("opacity", "1");
bars.transition().style("fill", "#3a9dc7").attr("opacity", "1");
});
//mouseover end
//tooltip
bars.on("mouseover.hover2", function(d) {
let me = d3.select(this);
let div = d3.select("body").append("div");
div.attr("id", "details");
div.attr("class", "tooltip");
let rows = div.append("table")
.selectAll("tr")
.data(Object.keys(d))
.enter()
.append("tr");
console.log(rows);
rows.append("th").text(key => key);
rows.append("td").text(key => d[key]);
});
bars.on("mousemove.hover2", function(d) {
let div = d3.select("div#details");
// get height of tooltip
let bbox = div.node().getBoundingClientRect();
div.style("left", d3.event.pageX + "px")
div.style("top", (d3.event.pageY - bbox.height) + "px");
});
bars.on("mouseout.hover2", function(d) {
d3.selectAll("div#details").remove();
});
//bars2
var highlight = document.createElement("tr");
highlight.className = ("is-selected");
bars2.on("mouseover.hover2", function(d) {
let me = d3.select(this);
let div = d3.select("body").append("div");
div.attr("id", "details");
div.attr("class", "tooltip");
let rows = div.append("table")
.selectAll("tr")
.data(Object.keys(d))
.enter()
.append("tr");
rows.append("th").text(key => key);
rows.append("td").text(key => d[key]);
console.log(rows);
});
bars2.on("mousemove.hover2", function(d) {
let div = d3.select("div#details");
// get height of tooltip
let bbox = div.node().getBoundingClientRect();
div.style("left", d3.event.pageX + "px")
div.style("top", (d3.event.pageY - bbox.height) + "px");
});
bars2.on("mouseout.hover2", function(d) {
d3.selectAll("div#details").remove();
});
svg
.append("text")
.attr("id", "charttitle")
.attr("x", 55)
.attr("y", 20)
.style("text-anchor", "left")
.style("font-weight", 600)
.style("font-size", "22px")
.text("Average Response time of life threatening vs non life threatening calls");
//legend
let ordinal = d3.scaleOrdinal()
.domain(["Potentially Life Threatening Medical Calls", "Non life threatening medical calls"])
.range(["#3a9dc7", "#7FFFD4"]);
svg.append("g")
.attr("class", "legendOrdinal")
.attr("transform", "translate(640,70)");
let legendOrdinal = d3.legendColor()
.shape("path", d3.symbol().type(d3.symbolCircle).size(150)())
.scale(ordinal)
.on("cellover", function(d) {
console.log(d);
if (d == "Potentially Life Threatening Medical Calls"){
bars2.filter(e => (d.Month !== e.Month)).transition().style("fill", "lightgrey").attr("opacity", "0.7");
}
else{
bars.filter(e => (d.Month !== e.Month))
.transition().style("fill", "lightgrey").attr("opacity", "0.7");
}
})
.on("cellout", function(d) {
bars2.transition().style("fill", "#7FFFD4").attr("opacity", "1");
bars.transition().style("fill", "#3a9dc7").attr("opacity", "1");
});
svg.select(".legendOrdinal")
.call(legendOrdinal);
function translate(x, y) {
return 'translate(' + x + ',' + y + ')';
}
}