/
bubble chart.html
285 lines (228 loc) · 6.4 KB
/
bubble chart.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
---
layout: default
---
<style>
.enter {
fill: green;
transition: fill 750ms ease-out;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
<button id="upbutton" onclick="firstbutton()">update</button>
<button onclick="world()">world</button>
<script>
// Create some data
// Create a bubble with that data
// Update that data smoothely
d3.json("/js/usaco2.json", function(error, usdata) {
d3.json("/js/worldco2.json", function(error, worlddata) {
//Everything launches from here?
// ud(usdata, worlddata)
ud(dataProcess(usdata,worlddata))
});
});
var statesPlusTest = []
// Process the data into awesomesauce stuff ;)
function dataProcess(usdata,worlddata) {
var stData = usdata.states;
var cyData = worlddata.countries;
for (i in stData) {
statesPlusTest.push(stData[i])
};
// console.log(statesPlusTest[2])
// [i].id = ID
// [i].name = Name
// [i].data.gross.pctchng = gross % change data
// [i].data.gross.y00 = gross y00 data
// [i].data.gross.y13 = gross y13 data
// HERE WILL GO, PUSH IN THE ITEM FROM THE NEW COUNTRY.
return statesPlusTest
};
var states = [
[10, 20,"California"], [10, 90,"Abington"], [10, 50,"French"], [100, 33,"Carb"], [330, 95,"Lleywn"],
[410, 12,"Twank"], [475, 44,"Apolonia"], [25, 67,"Facker"], [85, 21,"Podle"], [220, 88,"Bubledal"],[220, 88,"Bubledal"],[650,150,"ChickenSqua"]
];
var countries = [
[50, 200,"Dalton"], [1480, 90,"Calib"], [50, 50,"Chalk"], [100, 33,"Vernal"], [330, 95,"Flipmite"],
[410, 12,"Alkala"], [475, 44,"Carapd"], [25, 67,"asd"], [85, 21,"asdf"], [220, 88,"wer"],[650,150,"astas"]
];
//indexes, Width and height, Padding, parameters
var j = 1;
var add = 0;
var w = 1000;
var h = 500;
var AxisPaddingTop = 20;
var AxisPaddingLeft = 150;
var StandardPadding = 20;
var BubblePadding = 5;
var r = 10;
var AddCountries = 4;
var dlengthMin = states.length
var dlengthMax = dlengthMin + AddCountries;
var statesPlus = states
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.append("g")
.attr("class", "axis") //Assign "axis" class
.attr("transform", "translate(0," + (AxisPaddingTop) + ")")
function update(statesPlus,j) {
// Add in new data.
// check max and min
//if change, define the new range and axis
//
sorter(statesPlus,j)
var xScale1 = d3.scale.linear()
.domain([0, d3.max(statesPlus, function(d) {return d[j]; })])
.range([AxisPaddingLeft, w-(StandardPadding*2)]);
var xAxis1 = d3.svg.axis()
.scale(xScale1)
.orient("top")
.ticks(10);
svg.selectAll("g.axis")
.transition() //maybe remove
.duration(2000)
.call(xAxis1);
svg.selectAll("circle")
.data(statesPlus)
.enter()
.append("circle")
svg.selectAll("text.bubbles")
.data(statesPlus)
.enter()
.append("text")
.attr("class","bubbles")
svg.selectAll("text.names")
.data(statesPlus)
.enter()
.append("text")
.attr("class","names")
svg.selectAll("circle")
.transition()
.duration(2000)
.attr("cx", function(d) {
return xScale1(d[j])
})
.attr("cy",function(d,i){
// console.log(i + ": " + d[2] )
return (AxisPaddingTop + 20 + ((i)*2*r) + (i*BubblePadding))
})
.attr("r", "10px")
svg.selectAll("text.bubbles")
.transition()
.duration(2000)
.text(function(d) {
return d[j];
})
.attr("x", function(d) {
return xScale1(d[j])
})
.attr("y",function(d, i){
return (AxisPaddingTop + 20 + ((i)*2*r) + (i*BubblePadding))
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "red");
svg.selectAll("text.names")
.transition()
.duration(2000)
.text(function(d) {
return d[2];
})
.attr("x", function(d) {
return StandardPadding
})
.attr("y",function(d, i){
return (AxisPaddingTop + 20 + ((i)*2*r) + (i*BubblePadding))
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px");
// statesPlus = sorter(statesPlus,j)
// var bubble2 = svg.selectAll("circle")
// bubble2.transition()
// .duration(2000)
// .attr("cy",function(d,i){
// console.log(i + ": " + d[2] )
// return (AxisPaddingTop + 20 + ((i)*2*r) + (i*BubblePadding))
// })
// .attr("r", "10px")
// .attr("cx", function(d) {
// return xScale1(d[j])
// })
// ADD THE EXIT TOO!!!
// Remove old elements as needed.
// text.exit().remove();
}
function ud(x) {
// console.log(x)
// dataProcess()
//iterate between the options
if (j==0) {j+=1}
// states.push(countries[1])}
else{
j-=1
// console.log(countries[1])
};
update(states,j);
}
function firstbutton(){
ud()
}
function sorter(statesPlus,j){
var raw = []
raw = statesPlus
for (var i = 0; i < raw.length; i++) {
// set an index for each datapoint
raw[i][3] = i;
raw[i][4] = 0;
};
var rawSORT = raw;
// Sort Everything based on the order of importance!!!
rawSORT = rawSORT.sort(function(a,b){
if (a[j] > b[j]) {
return -1;
} else if (a[j] < b[j]) {
return 1;
}
});
console.log('awfa')
for (var i = 0; i < rawSORT.length; i++) {
rawSORT[i][4] = i;
};
for (var i = 0; i < raw.length; i++) {
// console.log(rawSORT[i])
rawSORT[i]
if (raw[i][3] === rawSORT[i][3]) {
// console.log('hello')
raw[i][4] = rawSORT[i][4]
};
};
for (var i = 0; i < raw.length; i++) {
raw(i)
rawSORT(i)
};
// return raw
// What if instead of sorting the json, we add in data to the json about the rank, every time....the nodes stay in the same index but we hang them based on the rank "r" instead of index "i"
// We do this each time we run, because it matters based on the type (j) that we're
// 1) erase previous rank "r"
// 2) sort
// 3) push new "r"
}
// Figure out flow of
// data
// functions (ud()? button enters to switch the j but not to reprocess the data?)
//Sort! get the sort to work.
// switch over functions to be from world data/state data
// add in bar graph
</script>