-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarchart.js
More file actions
158 lines (132 loc) · 3.59 KB
/
Copy pathbarchart.js
File metadata and controls
158 lines (132 loc) · 3.59 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
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
/*****************************************/
/* DRAW BAR CHART - ALREADY COMPLETE */
/*****************************************/
// CHART AREA
let margin = { top: 40, right: 20, bottom: 40, left: 90 },
width =
document.querySelector("#chart-area").clientWidth -
margin.left -
margin.right,
height = 400 - margin.top - margin.bottom;
width = width > 600 ? 600 : width;
let svg = d3
.select("#chart-area")
.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 + ")");
// AXIS
let x = d3
.scaleBand()
.range([0, width])
.paddingInner(0.1);
let y = d3.scaleLinear().range([height, 0]);
let xAxis = d3
.axisBottom()
.scale(x)
.tickFormat(function(d) {
return shortenString(d, 20);
});
let yAxis = d3.axisLeft().scale(y);
let xAxisGroup = svg.append("g").attr("class", "x-axis axis");
let yAxisGroup = svg.append("g").attr("class", "y-axis axis");
function renderBarChart(data) {
// Check array length (top 5 attractions)
if (data.length > 5) {
errorMessage("Max 5 rows");
return;
}
// Check object properties
if (
!data[0].hasOwnProperty("Visitors") ||
!data[0].hasOwnProperty("Location") ||
!data[0].hasOwnProperty("Category")
) {
errorMessage(
"The Object properties are not correct! An attraction should include at least: 'Visitors', 'Location', 'Category'"
);
return;
}
x.domain(
data.map(function(d) {
return d.Location;
})
);
y.domain([
0,
d3.max(data, function(d) {
return d.Visitors;
})
]);
// ---- DRAW BARS ----
let bars = svg
.selectAll(".bar")
.remove()
.exit()
.data(data);
bars
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", function(d) {
return x(d.Location);
})
.attr("y", function(d) {
return y(d.Visitors);
})
.attr("height", function(d) {
return height - y(d.Visitors);
})
.attr("width", x.bandwidth())
.on("mouseover", function(event, d) {
//Get this bar's x/y values, then augment for the tooltip
let xPosition =
margin.left +
width / 2 +
parseFloat(d3.select(this).attr("x")) +
x.bandwidth() / 2;
let yPosition =
margin.top + parseFloat(d3.select(this).attr("y")) / 2 + height;
//Update the tooltip position and value
d3.select("#tooltip")
.style("left", xPosition + "px")
.style("top", yPosition + "px")
.select("#value")
.text(d.Visitors);
//Show the tooltip
d3.select("#tooltip").classed("hidden", false);
})
.on("mouseout", function(d) {
//Hide the tooltip
d3.select("#tooltip").classed("hidden", true);
});
// ---- DRAW AXIS ----
xAxisGroup = svg
.select(".x-axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
yAxisGroup = svg.select(".y-axis").call(yAxis);
svg.select("text.axis-title").remove();
svg
.append("text")
.attr("class", "axis-title")
.attr("x", -5)
.attr("y", -15)
.attr("dy", ".1em")
.style("text-anchor", "end")
.text("Annual Visitors");
}
function errorMessage(message) {
console.log(message);
}
function shortenString(content, maxLength) {
// Trim the string to the maximum length
let trimmedString = content.substr(0, maxLength);
// Re-trim if we are in the middle of a word
trimmedString = trimmedString.substr(
0,
Math.min(trimmedString.length, trimmedString.lastIndexOf(" "))
);
return trimmedString;
}