-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathselection_plot.html
186 lines (153 loc) · 4.47 KB
/
selection_plot.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
}
</style>
<body>
X Selector <select id = "xAxis"></select>
Y Selector <select id = "yAxis"></select>
<div id = "chart"></div>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var xSelection = "group"
var ySelection = "group"
d3.csv("data/groupdata.csv", function(error, data) {
var list = ["group","value","density","gmp","popChange","gridorder"]
if (error) throw error;
draw(data,xSelection,ySelection)
xSelection = generateAxis("x",list,data)
ySelection = generateAxis("y",list,data)
});
function generateAxis(axis,list,data){
var xSelection = "group"
var ySelection = "group"
for(var i in list){
d3.select("#"+axis+"Axis").append("option").attr("value",list[i]).text(list[i])
}
d3.select("#xAxis").on("change",function(){
xSelection = d3.select(this).property('value')
update(data,xSelection,ySelection)
})
d3.select("#yAxis").on("change",function(){
ySelection = d3.select(this).property('value')
// draw(data,xSelection,ySelection)
update(data,xSelection,ySelection)
})
}
function update(data,xLabel,yLabel){
console.log([xLabel,yLabel])
data.forEach(function(d) {
d[xLabel] = +d[xLabel];
d[yLabel] = +d[yLabel];
});
x.domain(d3.extent(data, function(d) { return d[xLabel]; }));
y.domain(d3.extent(data, function(d) { return d[yLabel] ; }));
d3.selectAll(".axis").remove()
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text(xLabel);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text(yLabel)
d3.selectAll("circle")
.transition().duration(1000)
.attr("cx", function(d) { return x(d[xLabel]); })
.attr("cy", function(d) { return y(d[yLabel] ); })
}
function draw(data,xLabel,yLabel){
data.forEach(function(d) {
d[xLabel] = +d[xLabel];
d[yLabel] = +d[yLabel];
});
x.domain(d3.extent(data, function(d) { return d[xLabel]; }));
y.domain(d3.extent(data, function(d) { return d[yLabel] ; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text(xLabel);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text(yLabel)
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", xLabel+"_"+yLabel)
.attr("r", 10)
.attr("opacity",.1)
.attr("cx", function(d) { return x(d[xLabel]); })
.attr("cy", function(d) { return y(d[yLabel] ); })
.style("fill", function(d) { return "red"; return color(d.species); });
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
}
</script>