-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
46 lines (40 loc) · 1.08 KB
/
main.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
// import our components
import { Table } from "./components/Table.js";
import { Barchart } from "./components/Barchart.js";
import { Count } from "./components/Count.js";
let table, barchart, count;
// global state
let state = {
data: [],
domain: [],
selectedState: null,
selectedMetric: null,
};
d3.csv("./statePopulations.csv", d3.autoType).then(data => {
console.log("data", data);
state.data = data;
state.domain = [
0,
d3.max(data
.map(d => [d["Age < 20"], d["Age 20-65"], d["Age 65+"]])
.flat()
)]
init();
});
function init() {
table = new Table(state, setGlobalState);
barchart = new Barchart(state, setGlobalState);
count = new Count(state, setGlobalState);
draw();
}
function draw() {
table.draw(state);
barchart.draw(state, setGlobalState);
count.draw(state, setGlobalState);
}
// UTILITY FUNCTION: state updating function that we pass to our components so that they are able to update our global state object
function setGlobalState(nextState) {
state = { ...state, ...nextState };
console.log("new state:", state);
draw();
}