-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·206 lines (182 loc) · 6.63 KB
/
script.js
File metadata and controls
executable file
·206 lines (182 loc) · 6.63 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
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
//Million format fonction
var millionsFormat = d3.format("$,");
//Special Format for the data supply
function supplyFormat(x) {
var s = d3.format(".3s")(x);
switch (s[s.length - 1]) {
case "G": return s.slice(0, -1) + "B";
}
return s;
}
//A dict to change the last label of the bubble
var dict = {
market_cap: "market_capClean",
volume_24h: "volume_24hClean",
price: "priceClean",
circulating_supply: "circulating_supplyClean"
};
//Setting width and height
var width = window.innerWidth, height = 500;
//Svg creation
var svg = d3.select("#graph").append("svg").attr("width", width).attr("height", height).attr("overflow","hidden")
// .call(d3.zoom().on("zoom", function () {
// svg.attr("transform", d3.event.transform)
// }));
//creating a pack
var pack = d3.pack()
.size([width, height])
.padding(1.5);
// Importing data and processing it
d3.csv("CMC Crypto Portfolio Tracker - CleanSummary 04 06 2021.csv", function(d) {
return {
symbol: d.symbol,
name : d.name,
cmc_rank : d.cmc_rank,
market_cap: +d.market_cap.replace(/\,/g, '').replace(/\./g, '').slice(0,-2),
market_capClean: millionsFormat(+d.market_cap.replace(/\,/g, '').replace(/\./g, '').slice(0,-2)),
volume_24h: +d.volume_24h.replace(/\,/g, '').replace(/\./g, '').slice(0,-2),
volume_24hClean: millionsFormat(+d.volume_24h.replace(/\,/g, '').replace(/\./g, '').slice(0,-2)),
price: +d.price.replace(/\$/g, '').replace(/\,/g, '').replace(/\./g, '').slice(0,-2),
priceClean: millionsFormat(+d.price.replace(/\,/g, '')),
circulating_supply: +d.circulating_supply.replace(/\,/g, '').replace(/\./g, '').slice(0,-2),
circulating_supplyClean: supplyFormat(+d.circulating_supply.replace(/\,/g, '').replace(/\./g, '').slice(0,-2))
};
}).then(function(data) {
//Creating a hierarchy
var root = d3.hierarchy({children: data})
.sum(function(d) { return d.market_cap; })
.sort(function(a, b) { return b.market_cap - a.market_cap; })
//Appending the tooltips
var tooltip = d3.select('#graph')
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "black")
.style("border-radius", "5px")
.style("padding", "10px")
.style("color", "white")
.style("position", "absolute")
;
//1st function about the tooltip
let showTooltip = function(d) {
tooltip
.transition()
.duration(200)
tooltip
.style("opacity", 1)
.html("Currency: " + d.data.symbol + "<br> Market Capitalization: " + d.data.market_capClean)
.style("left", (d.x + (d3.mouse(this)[0] + 30)) + "px")
.style("top", (d.y + (d3.mouse(this)[1] + 30)) + "px");
}
//2nd function about the tooltip
let moveTooltip = function(d) {
tooltip
.style("left", (d.x + (d3.mouse(this)[0] + 30)) + "px")
.style("top", (d.y + (d3.mouse(this)[1] + 30)) + "px");
}
//3rd function about the tooltip
let hideTooltip = function(d) {
tooltip
.transition()
.duration(200)
.style("opacity", 0);
}
//Creating nodes from the hierarchy and placing the nodes in the svg
var node = svg.selectAll(".node")
.data(pack(root).leaves())
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("mouseover", showTooltip)
.on("mousemove", moveTooltip)
.on("mouseleave", hideTooltip);
//Appending the circles
node.append("circle")
.attr("id", function(d) { return d.id; })
.attr("r", function(d) { return d.r; })
.attr("fill", "black")
//Appending the main labels
node.append("text")
.attr("class","labels")
.attr("dy", ".2em")
.text(function(d) {
return d.data.symbol ;
})
.attr("font-family", "BloombergBold")
.attr("font-size", function(d){
return d.r/5;
})
.attr("fill", "white")
.style("text-anchor", "middle")
//Appending the second labels
node.append("text")
.attr("class","ranks")
.attr("dy", "1.8em")
.text(function(d) {
return "CMC Rank" + " " + d.data.cmc_rank ;
})
.attr("font-family", "BloombergBold")
.attr("font-size", function(d){
return d.r/7;
})
.attr("fill", "white")
.style("text-anchor", "middle")
});
//Creating the function allowing the update of the data in the bubbles
function updateData(variable) {
d3.csv("CMC Crypto Portfolio Tracker - CleanSummary 04 06 2021.csv", function(d) {
return {
symbol: d.symbol,
name : d.name,
cmc_rank : d.cmc_rank,
market_cap: +d.market_cap.replace(/\,/g, '').replace(/\./g, '').slice(0,-2),
market_capClean: millionsFormat(+d.market_cap.replace(/\,/g, '').replace(/\./g, '').slice(0,-2)),
volume_24h: +d.volume_24h.replace(/\,/g, '').replace(/\./g, '').slice(0,-2),
volume_24hClean: millionsFormat(+d.volume_24h.replace(/\,/g, '').replace(/\./g, '').slice(0,-2)),
price: +d.price.replace(/\$/g, '').replace(/\,/g, '').replace(/\./g, '').slice(0,-2),
priceClean: millionsFormat(+d.price.replace(/\,/g, '')),
circulating_supply: +d.circulating_supply.replace(/\,/g, '').replace(/\./g, '').slice(0,-2),
circulating_supplyClean: supplyFormat(+d.circulating_supply.replace(/\,/g, '').replace(/\./g, '').slice(0,-2))
};
}).then(function(data) {
var root = d3.hierarchy({children: data})
.sum(function(d) { return d[variable]; })
.sort(function(a, b) { return b[variable] - a[variable]; })
var node = svg.selectAll(".node")
.data(pack(root).leaves())
.transition()
.duration(2000)
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.select("circle")
.attr("id", function(d) { return d.id; })
.attr("r", function(d) { return d.r; })
.attr("fill", "black")
svg.selectAll(".node").select(".labels")
.transition()
.duration(2000)
.attr("dy", ".2em")
.style("text-anchor", "middle")
.text(function(d) {
return d.data.symbol;
})
.attr("font-family", "BloombergBold")
.attr("font-size", function(d){
return d.r/5;
})
.attr("fill", "white");
svg.selectAll(".node").select(".ranks")
.transition()
.delay(500)
.duration(1000)
.attr("dy", "1.8em")
.text(function(d) {
return d.data[dict[variable]] ;
})
.attr("font-family", "BloombergBold")
.attr("font-size", function(d){
return d.r/7;
})
.attr("fill", "white")
.style("text-anchor", "middle");
});
};