forked from peterhadlaw/personal-website-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviz.js
More file actions
66 lines (57 loc) · 1.5 KB
/
viz.js
File metadata and controls
66 lines (57 loc) · 1.5 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
// Create an array of each country's numbers
let australia = Object.values(data.australia);
let brazil = Object.values(data.brazil);
let uk = Object.values(data.uk);
let mexico = Object.values(data.mexico);
let singapore = Object.values(data.singapore);
let southAfrica = Object.values(data.southAfrica);
// Create an array of category labels
let labels = Object.keys(data.australia);
// Display the default plot
function init() {
let data = [{
values: australia,
labels: labels,
type: "pie"
}];
let layout = {
height: 600,
width: 800
};
Plotly.newPlot("pie", data, layout);
}
// On change to the DOM, call getData()
d3.selectAll("#selDataset").on("change", getData);
// Function called by DOM changes
function getData() {
let dropdownMenu = d3.select("#selDataset");
// Assign the value of the dropdown menu option to a letiable
let dataset = dropdownMenu.property("value");
// Initialize an empty array for the country's data
let data = [];
if (dataset == 'australia') {
data = australia;
}
else if (dataset == 'brazil') {
data = brazil;
}
else if (dataset == 'uk') {
data = uk;
}
else if (dataset == 'mexico') {
data = mexico;
}
else if (dataset == 'singapore') {
data = singapore;
}
else if (dataset == 'southAfrica') {
data = southAfrica;
}
// Call function to update the chart
updatePlotly(data);
}
// Update the restyled plot's values
function updatePlotly(newdata) {
Plotly.restyle("pie", "values", [newdata]);
}
init();