-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscript.js
151 lines (112 loc) · 3.39 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
let w = 960;
let h = 640;
let xPadding = 70;
let yPadding = 50;
let viz = d3.select("#container")
.append("svg")
.attr("width", w)
.attr("height", h)
;
function gotData(incomingData){
console.log("incomingData", incomingData);
// restructure data to get min and max
let mergedData = d3.merge(incomingData);
console.log("mergedData", mergedData);
// x Scale & Axis
let maxX = d3.max(mergedData, function(d, i){
return d.x;
});
let xScale = d3.scaleLinear().domain([0, maxX]).range([xPadding, w-xPadding]);
let xAxisGroup = viz.append("g").attr("class", "xaxis");
let xAxis = d3.axisBottom(xScale);
xAxisGroup.call(xAxis);
xAxisGroup.attr("transform", "translate(0, "+(h-yPadding)+")");
// yScale & Axis
let maxY = d3.max(mergedData, function(d, i){
return d.y;
});
let yScale = d3.scaleLinear().domain([0, maxY]).range([ h-yPadding, yPadding ])
let yAxisGroup = viz.append("g").attr("class", "yaxis");
let yAxis = d3.axisLeft(yScale);
yAxisGroup.call(yAxis);
yAxisGroup.attr("transform", "translate("+xPadding+",0)");
// group for viz
let vizGroup = viz.append("g").attr("class", "vizGroup");
function getGroupLocation(d,i){
let x = xScale(d.x);
let y = yScale(d.y);
return "translate("+x+", "+y+")"
}
function getIncomingGroupLocation(d,i){
let x = xScale(d.x);
let y = -30;
return "translate("+x+", "+y+")"
}
function getExitingGroupLocation(d,i){
let x = xScale(d.x);
let y = h+30;
return "translate("+x+", "+y+")"
}
let dataIndex = 0;
function visualizeData(){
// get data
let dataToShow = incomingData[dataIndex];
console.log("dataToShow", dataToShow);
function assignKeys(d, i){
return d.name;
}
// viz
let datagroups = vizGroup.selectAll(".datagroup").data(dataToShow, assignKeys);
// ENTERING ELEMENTS
let enteringElements = datagroups.enter()
.append("g")
.attr("class", "datagroup")
;
enteringElements.append("circle")
.attr("r", 30)
.attr("fill", "red")
;
enteringElements.append("text")
.text(function(d, i){
return d.name;
})
.attr("x", -17)
.attr("y", 17)
.attr("font-family", "sans-serif")
.attr("font-size", "3em")
.attr("fill", "white")
;
enteringElements.attr("transform", getIncomingGroupLocation).transition().delay(500).attr("transform", getGroupLocation);
//EXITING ELEMENTS
let exitingElements = datagroups.exit();
exitingElements.transition().delay(500).attr("transform", getExitingGroupLocation).remove();
// UPDATING ELEMENTS
datagroups.select("text")
.text(function(d, i){
return d.name;
})
;
datagroups.transition().duration(500).attr("transform", getGroupLocation);
}
document.getElementById("step1").addEventListener("click", function(){
dataIndex = 0;
visualizeData();
});
document.getElementById("step2").addEventListener("click", function(){
dataIndex = 1;
visualizeData();
});
document.getElementById("step3").addEventListener("click", function(){
dataIndex = 2;
visualizeData();
});
document.getElementById("step4").addEventListener("click", function(){
dataIndex = 3;
visualizeData();
});
document.getElementById("step5").addEventListener("click", function(){
dataIndex = 4;
visualizeData();
});
}
d3.json("data.json").then(gotData);