-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (70 loc) · 2.35 KB
/
index.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
// import our components
import { Boxplot } from "./Boxplot.js";
import { Explainer } from "./Explainer.js";
import { Beeswarm } from "./Beeswarm.js";
import { Table } from "./Table.js";
// let table, boxplot, beeswarm;
let boxplot, beeswarm, table, explainer;
// global state
let state = {
data: [],
selectedConstraint: null,
long_data: [],
filtered_long_data: [],
clickedData: null,
clickedHash: null,
};
d3.csv("./data/stsen_05_data_sampled_small.csv", d3.autoType).then(data => {
state.data = data;
const long_data = [];
data.forEach( function(row) {
Object.keys(row).forEach(function(colname) {
if(colname == "District" || colname == "Value" ||
colname == "election" || colname == "eg" || colname == "id" ||
colname == "D_seats" || colname == "hash" || colname == "total_districts" ||
colname == "epsilon") {
return
}
long_data.push({"District": colname,
"Value": +row[colname],
"election": row["election"],
"eg": row["eg"],
"id": row["id"],
"D_seats": row["D_seats"],
"hash": row["hash"],
"total_districts": row["total_districts"]});
});
});
state.long_data = long_data;
init();
})
function init() {
const initial_value = "PRES16";
state.selectedConstraint = initial_value;
state.filtered_long_data = state.long_data.filter(d => d.election === initial_value);
const selectElement = d3.select('#dropdown').on("change", function(){
let tempFilterLong = [];
setGlobalState({selectedConstraint: this.value})
tempFilterLong = state.long_data.filter(d => d.election === state.selectedConstraint)
setGlobalState({
filtered_long_data: tempFilterLong
});
});
boxplot = new Boxplot(state, setGlobalState);
beeswarm = new Beeswarm(state, setGlobalState);
table = new Table(state, setGlobalState);
explainer = new Explainer();
draw();
}
function draw() {
boxplot.draw(state, setGlobalState);
beeswarm.draw(state, setGlobalState);
table.draw(state);
}
// 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();
}