-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
196 lines (174 loc) · 5.22 KB
/
script.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
const svgPadding = { top: 0, right: 0, bottom: 0, left: 0 };
const dimensions = { x: 800, y: 650 };
const tilePadding = 3;
const charWidth = 5;
const titleRowDistance = 10;
const titleXPos = 3;
const containerDom = document.getElementById('container');
const tooltip = d3.select('#container').append('div').attr('id', 'tooltip');
const row1 = d3.select('#tooltip').append('p').html('row1');
const row2 = d3.select('#tooltip').append('p').html('row2');
const row3 = d3.select('#tooltip').append('p').html('row3');
d3.select('#container')
.append('h1')
.attr('id', 'title')
.html('Video Game Sales');
d3.select('#container')
.append('h2')
.attr('id', 'description')
.html('Top 100 Most Sold Video Games Grouped by Platform');
const svg = d3
.select('#container')
.append('svg')
.attr('width', dimensions.x)
.attr('height', dimensions.y);
const treemapWrapper = svg
.append('g')
.attr(
'transform',
'translate(' + svgPadding.left + ', ' + svgPadding.top + ')'
);
d3.json(
'https://cdn.freecodecamp.org/testable-projects-fcc/data/tree_map/video-game-sales-data.json'
).then((data) => {
let root = d3
.hierarchy(data)
.sum((d) => d.value)
.sort(function (a, b) {
return -a.value - -b.value;
});
d3
.treemap()
.tile(d3.treemapSquarify)
.size([
dimensions.x - svgPadding.left - svgPadding.right,
dimensions.y - svgPadding.top - svgPadding.bottom,
])
.paddingInner(tilePadding)
.paddingOuter(1)(root);
const categoriesArray = shuffle(data.children.map((d) => d.name));
const colorScale = d3
.scaleLinear()
.domain([0, categoriesArray.length - 1])
.range([0, 1]);
treemapWrapper
.selectAll('g')
.data(root.leaves())
.enter()
.append('g')
.attr('transform', (d) => 'translate(' + d.x0 + ', ' + d.y0 + ')')
.attr('data-width', (d) => d.x1 - d.x0)
.attr('data-title', (d) => d.data.name)
.append('rect')
.attr('class', 'tile')
.attr('data-name', (d) => d.data.name)
.attr('data-category', (d) => d.data.category)
.attr('data-value', (d) => d.data.value)
.attr('width', (d) => d.x1 - d.x0)
.attr('height', (d) => d.y1 - d.y0)
.style('fill', (d) => color(d.data.category, categoriesArray, colorScale))
.on('mousemove', (d) => {
row1.html('Name: ' + d.data.name);
row2.html('Category: ' + d.data.category);
row3.html('Value: ' + d.data.value);
tooltip
.attr('class', 'visible')
.attr('data-value', d.data.value)
.style('left', d3.mouse(containerDom)[0] + 10 + 'px')
.style(
'top',
d3.mouse(containerDom)[1] -
tooltip.node().getBoundingClientRect().height / 2 +
'px'
);
})
.on('mouseleave', (d) => {
tooltip.attr('class', '');
});
treemapWrapper
.selectAll('g')
.append('text')
.attr('class', 'tile-text')
.each(function () {
fitTitle(
d3.select(this),
d3.select(this.parentNode).attr('data-title'),
d3.select(this.parentNode).attr('data-width'),
titleRowDistance
);
});
const legend = d3
.select('#container')
.append('svg')
.attr('width', dimensions.x)
.attr('height', 100);
const legendScale = d3
.scaleBand()
.domain(categoriesArray)
.range([0, dimensions.x]);
const legendAxis = d3.axisBottom(legendScale);
legend
.append('g')
.attr('id', 'legend')
.attr('transform', 'translate(0, 40)')
.call(legendAxis);
d3.select('#legend')
.selectAll('rect')
.data(categoriesArray)
.enter()
.append('rect')
.attr('class', 'legend-item')
.attr('width', dimensions.x / categoriesArray.length - 20)
.attr('height', dimensions.x / categoriesArray.length - 20)
.attr('x', (d, i) => i * (dimensions.x / categoriesArray.length) + 10)
.attr('y', (dimensions.x / categoriesArray.length) * -1 + 22)
.attr('fill', (d) => color(d, categoriesArray, colorScale));
});
function fitTitle(obj, title, width, yPos) {
let copiedTitle = title;
remainingWords = [];
const tspan = obj
.append('tspan')
.text(copiedTitle)
.attr('y', yPos)
.attr('x', titleXPos);
while (
tspan.node().getBoundingClientRect().width > width - titleXPos - 10 &&
copiedTitle.split(' ').length > 1
) {
tspan.text(
copiedTitle
.split(' ')
.slice(0, copiedTitle.split(' ').length - 1)
.join(' ')
);
remainingWords.unshift(
copiedTitle.split(' ')[copiedTitle.split(' ').length - 1]
);
copiedTitle = copiedTitle
.split(' ')
.slice(0, copiedTitle.split(' ').length - 1)
.join(' ');
}
if (remainingWords.length > 0) {
fitTitle(obj, remainingWords.join(' '), width, yPos + titleRowDistance);
}
return;
}
function shuffle(array) {
let copied = [...array];
let shuffled = [];
while (copied.length > 0) {
let randomNr = Math.floor(Math.random() * copied.length);
shuffled.push(copied[randomNr]);
copied.splice(randomNr, 1);
}
return shuffled;
}
function color(string, categories, scale) {
if (categories.indexOf(string) % 2 === 0) {
return d3.interpolateRainbow(scale(categories.indexOf(string)));
} else {
return d3.interpolateSinebow(scale(categories.indexOf(string)));
}
}