-
Notifications
You must be signed in to change notification settings - Fork 24
/
bubble_chart.js
460 lines (433 loc) · 12.8 KB
/
bubble_chart.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* Purpose:
* Generate a reusable bubble chart
*
* Instantiate the settings before rendering the bubble chart
* Generate a reusable bubble chart using d3.v4.js on a dataset loaded through D3.
*
* Original Author: Deborah Mesquita
* Source:
*
* {@link https://bl.ocks.org/dmesquita/37d8efdb3d854db8469af4679b8f984a Deborah Mesquita's block}
*
* {@link https://medium.freecodecamp.org/a-gentle-introduction-to-d3-how-to-build-a-reusable-bubble-chart-9106dc4f6c46 Tutorial and explanation}
*
* {@link https://taylorchasewhite.github.io/babyNames/ Live demo}
* @author Deborah Mesquita
* @author Taylor White <[email protected]>
* @since 07.04.17
* @summary Generate a reusable bubble chart
* @requires d3.v4.js
* @class
*
* @example
* var chart = bubbleChart(); // instantiate the chart
*
* // update settings
* chart.width(850).height(850).minRadius(7).maxRadius(55).forceApart(-170);
*
* // example of chaining
* chart.columnForColors("Sex").columnForRadius("BirthCount");
* chart.customColors(["M","F"],["#70b7f0","#e76486"]).showTitleOnCircle(true);
* chart.title('Most popular baby names in 2016').columnForTitle("Name");
* chart.unitName("babies");
*
* // load the data and render the chart
* d3.select("#divBubbleChart")
* .data(newData)
* .call(chart);
*
* @returns Chart function so that you can render the chart when ready
*/
function bubbleChart() {
var width = 960,
height = 960,
marginTop = 96,
minRadius = 6,
maxRadius = 20,
columnForColors = "category",
columnForTitle = "title",
columnForRadius = "views",
forceApart = -50,
unitName="hearts",
customColors=false,
customRange,
customDomain,
chartSelection,
chartSVG,
showTitleOnCircle=false;
/**
* The command to actually render the chart after setting the settings.
* @public
* @param {string} selection - The div ID that you want to render in
*/
function chart(selection) {
var data = selection.datum();
chartSelection=selection;
var div = selection,
svg = div.selectAll('svg');
svg.attr('width', width).attr('height', height);
chartSVG=svg;
var tooltip = selection
.append("div")
.style("position", "absolute")
.style("visibility", "hidden")
.style("color", "white")
.style("padding", "8px")
.style("background-color", "#626D71")
.style("border-radius", "6px")
.style("text-align", "center")
.style("font-family", "monospace")
.style("width", "400px")
.text("");
var simulation = d3.forceSimulation(data)
.force("charge", d3.forceManyBody().strength([forceApart]))
.force("x", d3.forceX())
.force("y", d3.forceY())
.on("tick", ticked);
function ticked(e) {
node.attr("transform",function(d) {
return "translate(" + [d.x+(width / 2), d.y+((height+marginTop) / 2)] +")";
});
}
var colorCircles;
if (!customColors) {
colorCircles = d3.scaleOrdinal(d3.schemeCategory10);
}
else {
colorCircles = d3.scaleOrdinal()
.domain(customDomain)
.range(customRange);
}
var minRadiusDomain = d3.min(data, function(d) {
return +d[columnForRadius];
});
var maxRadiusDomain = d3.max(data, function(d) {
return +d[columnForRadius];
});
var scaleRadius = d3.scaleLinear()
.domain([minRadiusDomain, maxRadiusDomain])
.range([minRadius, maxRadius])
var node = svg.selectAll("circle")
.data(data)
.enter()
.append("g")
.attr('transform', 'translate(' + [width / 2, height / 2] + ')')
.style('opacity',1);
node.append("circle")
.attr("id",function(d,i) {
return i;
})
.attr('r', function(d) {
return scaleRadius(d[columnForRadius]);
})
.style("fill", function(d) {
return colorCircles(d[columnForColors]);
})
.on("mouseover", function(d) {
tooltip.html(d[columnForTitle] + "<br/>" + d[columnForColors] + "<br/>" + d[columnForRadius] + " "+ unitName);
return tooltip.style("visibility", "visible");
})
.on("mousemove", function() {
return tooltip.style("top", (d3.event.pageY - 10) + "px").style("left", (d3.event.pageX + 10) + "px");
})
.on("mouseout", function() {
return tooltip.style("visibility", "hidden");
});
node.append("clipPath")
.attr("id",function(d,i) {
return "clip-"+i;
})
.append("use")
.attr("xlink:href",function(d,i) {
return "#" + i;
});
if (showTitleOnCircle) {
node.append("text")
.attr("clip-path",function(d,i) {
return "url(#clip-" + i + ")"
})
.attr("text-anchor", "middle")
.append("tspan")
.attr("x",function(d) {
return 0;//-1*scaleRadius(d[columnForRadius])/3;
})
.attr("y",function(d) {
return ".3em";//scaleRadius(d[columnForRadius])/4;
})
.text(function(d) {
return d[columnForTitle];
})
.on("mouseover", function(d) {
tooltip.html(d[columnForTitle] + "<br/>" + d[columnForColors] + "<br/>" + d[columnForRadius] + " "+ unitName);
return tooltip.style("visibility", "visible");
})
.on("mousemove", function() {
return tooltip.style("top", (d3.event.pageY - 10) + "px").style("left", (d3.event.pageX + 10) + "px");
})
.on("mouseout", function() {
return tooltip.style("visibility", "hidden");
});
}
svg.append('text')
.attr('x',width/2).attr('y',marginTop)
.attr("text-anchor", "middle")
.attr("font-size","1.8em")
.text(title);
}
chart.width = chartWidth;
chart.height = chartHeight;
chart.columnForColors = chartColForColors;
chart.columnForRadius = chartColForRadius;
chart.columnForTitle = chartColForTitle;
chart.minRadius = chartMinRadius;
chart.maxRadius = chartMaxRadius;
chart.forceApart = chartForceApart;
chart.unitName = chartUnitName;
chart.customColors = chartCustomColors;
chart.showTitleOnCircle = chartShowTitleOnCircle;
chart.title=chartTitle;
chart.remove = chartRemove;
/**
* Get/set the height of the chart
* Use 'chart.width' to get or set.
* @example
* chart.columnForColors(960); // Sets the width of the SVG to 960
* chart.columnForColors(); // returns 960
*
* @public
* @param {number} [value] - The width of the chart
* @returns function - Chart, allowing chaining of commands
*/
function chartWidth(value) {
if (!arguments.length) {
return width;
}
width = value;
return chart;
};
/**
* Get/set the height of the chart.
* Use 'chart.height' to get or set.
* @example
* chart.height(960); // Sets the height of the SVG to 960
* chart.height(); // returns 960
*
* @public
* @param {number} [value] - The height of the chart
* @returns function - Chart, allowing chaining of commands
*/
function chartHeight(value) {
if (!arguments.length) {
return height;
}
height = value;
marginTop=0.05*height;
return chart;
};
/**
* Get/set the property used to determine the colors of the bubbles.
* Use 'chart.columnForColors' to get or set.
* @example
* chart.columnForColors("Sex"); // Sets the column to birthCount
* chart.columnForColors(); // returns "Sex"
* @public
* @param {string} [value] - Property name to bind the bubble color to.
* @returns function - Chart, allowing chaining of commands
*/
function chartColForColors(value) {
if (!arguments.length) {
return columnForColors;
}
columnForColors = value;
return chart;
};
/**
* Get/set the property to determine the titles of the bubbles.
* Use 'chart.columnForTitle' to get or set.
* @example
* chart.columnForTitle("Name"); // Sets the column to birthCount
* chart.columnForTitle(); // returns "Name"
*
* @param {string} [value] - Property name to bind the bubble title to.
* @returns function - Chart, allowing chaining of commands
*/
function chartColForTitle(value) {
if (!arguments.length) {
return columnForTitle;
}
columnForTitle = value;
return chart;
};
/**
* Get/set the property to determine the radii of the bubbles.
* Use 'chart.columnForRadius' to get or set.
*
* @example
* chart.columnForRadius("birthCount"); // Sets the column to birthCount
* chart.columnForRadius(); // returns "birthCount"
* @public
* @param {string} [value] - Property name to bind the bubble radius to. Requires a numerical property.
* @returns function - Chart, allowing chaining of commands
*/
function chartColForRadius (value) {
if (!arguments.length) {
return columnForRadius;
}
columnForRadius = value;
return chart;
};
/**
* Get/set the minimum radius of the bubbles.
* Use 'chart.minRadius' to get or set.
* @example
* chart.columnForColors(3); // Sets the column to birthCount
* chart.columnForColors(); // returns 3
*
* @param {number} [value] - The minimum radius for the width of the bubbles
* @returns function - Chart, allowing chaining of commands
*/
function chartMinRadius(value) {
if (!arguments.length) {
return minRadius;
}
minRadius = value;
return chart;
};
/**
* Get/set the maximum radius of the bubbles.
* Use 'chart.maxRadius' to get or set.
*
* @public
* @param {number} [value] - The maximum radius of the bubbles for the largest value in the dataset
* @returns function - Chart, allowing chaining of commands
*/
function chartMaxRadius(value) {
if (!arguments.length) {
return maxRadius;
}
maxRadius = value;
return chart;
};
/**
* Get/set the unit name for the property the is represented by the radius of the bubbles.
* Used in the tooltip of the bubbles.
* Use 'chart.unitName' to get or set.
* @example
* chart.unitName(" babies"); // Sets the column to birthCount
* chart.unitName(); // returns " babies"
*
* @public
* @param {any} [value] - The unit name to display on the tooltip when hovering over a bubble
* @returns function - Chart, allowing chaining of commands
*/
function chartUnitName(value) {
if (!arguments.length) {
return unitName;
}
unitName = value;
return chart;
};
/**
* Get/set the force the separates and pushes together the bubbles on loading of the chart
* Use 'chart.forceApart' to get or set.
* @example
* chart.forceApart(150); // Sets the column to birthCount
* chart.forceApart(); // returns 150
*
* @public
* @param {any} [value] - Determines the force to separate the bubbles from each other when loading the chart
* @returns function - Chart, allowing chaining of commands
*/
function chartForceApart(value) {
if (!arguments.length) {
return forceApart;
}
forceApart = value;
return chart;
};
/**
* Get/set the property that determines if we show or hide the title of the data on the bubbles.
* Use 'chart.showTitleOnCircle' to get or set.
* @example
* chart.showTitleOnCircle(true);
* chart.forceApart(); // returns true
*
* @public
* @param {boolean} [value] - Determines whether to show or hide the title of the data on the bubbles
* @returns function - Chart, allowing chaining of commands
*/
function chartShowTitleOnCircle(value) {
if (!arguments.length) {
return showTitleOnCircle;
}
showTitleOnCircle = value;
return chart;
};
/**
* Set the domain and range of the colors used for the bubbles. This is only needed if you want to use custom colors in the chart.
* Use 'chart.customColors' to set.
* @example
* chart.customColors(["M","F"], ["#70b7f0","#e76486"]); // Sets the custom colors domain and range
*
* @param {any[]} domain - The domain. This is the set of categories used for binding the colors.
* @param {string[]} range - The range. This is an array of color codes that you want to represent each category in the domain.
* Note: The length of the array must perfectly match the length of the domain array.
* @returns function - Chart, allowing chaining of commands
*/
function chartCustomColors(domain,range) {
customColors=true;
customDomain=domain;
customRange=range;
return chart;
};
/**
* Get/set the property that determines the title of the chart.
* Use 'chart.title' to get or set.
* @example
* chart.title("Birth Count in the U.S. in 2016"); // Sets the chart title
* chart.title(); // returns "Birth Count in the U.S. in 2016"
* @public
* @param {string} value - The title of the chart
* @returns function - Chart, allowing chaining of commands
*/
function chartTitle(value) {
if (!arguments.length) {
return title;
}
title = value;
return chart;
}
/**
* Animate the removal of data from the chart (and the title)
* @public
* @param {function} [callback] - At the end of each node animation call this function for each node
* @returns function - Chart, allowing chaining of commands
*/
function chartRemove(callback) {
chartSVG.selectAll("text")
.style("opacity",1)
.transition()
.duration(500)
.style("opacity", "0")
.remove();
if (!arguments.length) {
chartSVG.selectAll("g")
.style("opacity",1)
.transition()
.duration(500)
.style("opacity", "0")
.remove();
}
else {
chartSVG.selectAll("g")
.style("opacity",1)
.duration(500)
.style("opacity", "0")
.remove()
.on("end", callback);
}
return chart;
}
return chart;
}