-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathReactBubbleChartD3.js
More file actions
409 lines (380 loc) · 15.1 KB
/
ReactBubbleChartD3.js
File metadata and controls
409 lines (380 loc) · 15.1 KB
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
//------------------------------------------------------------------------------
// Copyright Jonathan Kaufman 2015
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//------------------------------------------------------------------------------
import * as d3 from "d3";
/**
* Properties defined during construction:
* svg
* html
* legend
* bubble
* diameter
* colorRange
* colorLegend
* selectedColor
* legendSpacing
* smallDiameter
* textColorRange
* mediumDiameter
* configureLegend
* selectedTextColor
* fontSizeFactor
* duration
* delay
*/
export default class ReactBubbleChartD3 {
constructor(el, props = {}) {
this.legendSpacing = typeof props.legendSpacing === 'number' ? props.legendSpacing : 3;
this.selectedColor = props.selectedColor;
this.selectedTextColor = props.selectedTextColor;
this.smallDiameter = props.smallDiameter || 40;
this.mediumDiameter = props.mediumDiameter || 115;
this.fontSizeFactor = props.fontSizeFactor;
this.duration = props.duration === undefined ? 500 : props.duration;
this.delay = props.delay === undefined ? 7 : props.delay;
// create an <svg> and <html> element - store a reference to it for later
this.svg = d3.select(el).append('svg')
.attr('class', 'bubble-chart-d3')
.style('overflow', 'visible');
this.html = d3.select(el).append('div')
.attr('class', 'bubble-chart-text')
.style('position', 'absolute')
.style('left', 0) // center horizontally
.style('right', 0)
.style('margin-left', 'auto')
.style('margin-right', 'auto');
this.legend = d3.select(el).append('svg')
.attr('class', 'bubble-legend')
.style('overflow', 'visible')
.style('position', 'absolute');
this.tooltip = this.html.append('div')
.attr('class', 'tooltip')
.style('position', 'absolute')
.style('border-radius', '5px')
.style('border', '3px solid')
.style('padding', '5px')
.style('z-index', 500);
// create legend and update
this.adjustSize(el);
this.update(el, props);
}
/**
* Set this.diameter and this.bubble, also size this.svg and this.html
*/
adjustSize(el) {
// helper values for positioning
this.diameter = Math.min(el.offsetWidth, el.offsetHeight);
const top = Math.max((el.offsetHeight - this.diameter)/2, 0);
// center some stuff vertically
this.svg.attr('width', this.diameter)
.attr('height', this.diameter)
.style('position', 'relative')
.style('top', top + 'px'); // center vertically
this.html.style('width', this.diameter + 'px')
.style('height', this.diameter + 'px')
.style('top', top + 'px'); // center vertically;
// create the bubble layout that we will use to position our bubbles\
this.bubble = d3.pack()
.size([this.diameter, this.diameter])
.padding(3);
}
/**
* Create and configure the legend
*/
configureLegend(el, props) {
this.createLegend = props.legend;
// for each color in the legend, remove any existing, then
// create a g and set its transform
this.legend.selectAll('.legend-key').remove();
if (!this.createLegend) return;
const legendRectSize = Math.min(((el.offsetHeight-20) - (this.colorLegend.length-1)*this.legendSpacing)/this.colorLegend.length, 18);
const legendHeight = this.colorLegend.length * (legendRectSize + this.legendSpacing) - this.legendSpacing;
this.legend.style('height', legendHeight + 'px')
.style('width', legendRectSize + 'px')
.style('top', (el.offsetHeight - legendHeight)/2 + 'px')
.style('left', 60 + 'px');
const legendKeys = this.legend.selectAll('.legend-key')
.data(this.colorLegend)
.enter()
.append('g')
.attr('class', 'legend-key')
.attr('transform', (d, i) => {
var height = legendRectSize + this.legendSpacing;
var vert = i * height;
return 'translate(' + 0 + ',' + vert + ')';
});
// for each <g> create a rect and have its color... be the color
legendKeys.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill', c => c.color)
.style('stroke', c => c.color);
// add necessary labels to the legend
legendKeys.append('text')
.attr('x', legendRectSize + 2)
.attr('y', legendRectSize - 4)
.text(c => c.text);
}
/**
* Create and configure the tooltip
*/
configureTooltip(el, props) {
this.createTooltip = props.tooltip;
this.tooltipFunc = props.tooltipFunc;
// remove all existing divs from the tooltip
this.tooltip.selectAll('div').remove();
// intialize the styling
this.tooltip.style('display', 'none');
if (!this.createTooltip) return;
// normalize the prop formats
this.tooltipProps = (props.tooltipProps || []).map(tp =>
typeof tp === 'string' ? {css: tp, prop: tp, display: tp} : tp
);
// create a div for each of the tooltip props
for (var {css, prop} of this.tooltipProps) {
this.tooltip.append('div')
.attr('class', css);
}
}
/**
* This is where the magic happens.
* Update the tooltip and legend.
* Set up and execute transitions of existing bubbles to new size/location/color.
* Create and initialize new bubbles.
* Remove old bubbles.
* Maintain consistencies between this.svg and this.html
*/
update(el, props) {
this.adjustSize(el);
// initialize color legend values and color range values
// color range is just an array of the hex values
// color legend is an array of the color/text objects
const colorLegend = props.colorLegend || [];
this.colorRange = colorLegend.map(c =>
typeof c === 'string' ? c : c.color
);
this.colorLegend = colorLegend.slice(0).reverse().map(c =>
typeof c === 'string' ? {color: c} : c
);
this.textColorRange = colorLegend.map(c =>
typeof c === 'string' ? '#000000' : (c.textColor || '#000000')
);
this.configureLegend(el, props);
this.configureTooltip(el, props);
const data = props.data;
if (!data) return;
const fontFactor = this.fontSizeFactor;
const duration = this.duration;
const delay = this.delay;
// define a color scale for our colorValues
const color = d3.scaleQuantize()
.domain([
props.fixedDomain ? props.fixedDomain.min : d3.min(data, d => d.colorValue),
props.fixedDomain ? props.fixedDomain.max : d3.max(data, d => d.colorValue)
])
.range(this.colorRange);
// define a color scale for text town
const textColor = d3.scaleQuantize()
.domain([
props.fixedDomain ? props.fixedDomain.min : d3.min(data, d => d.colorValue),
props.fixedDomain ? props.fixedDomain.max : d3.max(data, d => d.colorValue)
])
.range(this.textColorRange)
// generate data with calculated layout values
const nodes = d3.hierarchy(data.length ? {children: data} : data).sum(d => d.value)
// assign new data to existing DOM for circles and labels
const circles = this.svg.selectAll('circle')
.data(this.bubble(nodes).descendants(), d => 'g' + d.data._id);
const labels = this.html.selectAll('.bubble-label')
.data(this.bubble(nodes).descendants(), d => 'g' + d.data._id);
// update - this is created before enter.append. it only applies to updating nodes.
// create the transition on the updating elements before the entering elements
// because enter.append merges entering elements into the update selection
// for circles we transition their transform, r, and fill
circles.transition()
.duration(duration)
.delay((d, i) => i * delay)
.attr('transform', d => 'translate(' + d.x + ',' + d.y + ')')
.attr('r', d => d.r)
.style('opacity', 1)
.style('fill', d => d.selected ? this.selectedColor : color(d.data.colorValue));
// for the labels we transition their height, width, left, top, and color
labels
.on('mouseover', this._tooltipMouseOver.bind(this, color, el))
.transition()
.duration(duration)
.delay((d, i) => i * delay)
.style('height', d => 2 * d.r + 'px')
.style('width', d => 2 * d.r + 'px')
.style('left', d => d.x - d.r + 'px')
.style('top', d => d.y - d.r + 'px')
.style('opacity', 1)
.style('color', d => d.selected ? this.selectedTextColor : textColor(d.data.colorValue))
.attr('class', d => {
var size;
if (2*d.r < this.smallDiameter) size = 'small';
else if (2*d.r < this.mediumDiameter) size = 'medium';
else size = 'large';
return 'bubble-label ' + size
})
// we can pass in a fontSizeFactor here to set the label font-size as a factor of its corresponding circle's radius; this overrides CSS font-size styles set with the small, medium and large classes
.style('font-size', d => fontFactor ? fontFactor * d.r + 'px' : null);
// enter - only applies to incoming elements (once emptying data)
if (this.bubble(nodes).descendants().length) {
// initialize new circles
circles.enter().filter(function(d){
return !d.children
}).append('circle')
.attr('transform', d => 'translate(' + d.x + ',' + d.y + ')')
.attr('r', 0)
.attr('class', d => d.children ? 'bubble' : 'bubble leaf')
.style('fill', d => d.selected ? this.selectedColor : color(d.data.colorValue))
.transition()
.duration(duration * 1.2)
.attr('transform', d => 'translate(' + d.x + ',' + d.y + ')')
.attr('r', d => d.r)
.style('opacity', 1);
// intialize new labels
labels.enter().append('div')
.attr('class', d => {
var size;
if (2*d.r < this.smallDiameter) size = 'small';
else if (2*d.r < this.mediumDiameter) size = 'medium';
else size = 'large';
return 'bubble-label ' + size
})
.text(d => d.data.displayText || d.data._id)
.on('click', (d, i) => {d3.event.stopPropagation(); props.onClick(d)})
.on('mouseover', this._tooltipMouseOver.bind(this, color, el))
.on('mouseout', this._tooltipMouseOut.bind(this))
.style('position', 'absolute')
.style('height', d => 2 * d.r + 'px')
.style('width', d => 2 * d.r + 'px')
.style('left', d => d.x - d.r + 'px')
.style('top', d => d.y - d.r + 'px')
.style('color', d => d.selected ? this.selectedTextColor : textColor(d.data.colorValue))
.style('opacity', 0)
.transition()
.duration(duration * 1.2)
.style('opacity', 1)
.style('font-size', d => fontFactor ? fontFactor * d.r + 'px' : null);
}
// exit - only applies to... exiting elements
// for circles have them shrink to 0 as they're flying all over
circles.exit()
.transition()
.duration(duration)
.attr('transform', d => {
const dy = d.y - this.diameter/2;
const dx = d.x - this.diameter/2;
const theta = Math.atan2(dy,dx);
const destX = this.diameter * (1 + Math.cos(theta) )/ 2;
const destY = this.diameter * (1 + Math.sin(theta) )/ 2;
return 'translate(' + destX + ',' + destY + ')'; })
.attr('r', 0)
.remove();
// for text have them fade out as they're flying all over
labels.exit()
.transition()
.duration(duration)
.style('top', d => {
const dy = d.y - this.diameter/2;
const dx = d.x - this.diameter/2;
const theta = Math.atan2(dy,dx);
const destY = this.diameter * (1 + Math.sin(theta) )/ 2;
return destY + 'px'; })
.style('left', d => {
const dy = d.y - this.diameter/2;
const dx = d.x - this.diameter/2;
const theta = Math.atan2(dy,dx);
const destX = this.diameter * (1 + Math.cos(theta) )/ 2;
return destX + 'px'; })
.style('opacity', 0)
.style('width', 0)
.style('height', 0)
.remove();
}
/**
* On mouseover of a bubble, populate the tooltip with that elements info
* (if this.createTooltip is true of course)
*/
_tooltipMouseOver(color, el, d, i) {
if (!this.createTooltip) return;
for (var {css, prop, display} of this.tooltipProps) {
this.tooltip.select('.' + css).html((display ? display + ': ' : '') + d.data[prop]);
}
// Fade the popup fill mixing the shape fill with 80% white
const fill = color(d.data.colorValue);
const backgroundColor = d3.rgb(
d3.rgb(fill).r + 0.8 * (255 - d3.rgb(fill).r),
d3.rgb(fill).g + 0.8 * (255 - d3.rgb(fill).g),
d3.rgb(fill).b + 0.8 * (255 - d3.rgb(fill).b)
);
this.tooltip.style('display', 'block');
const tooltipNode = this.tooltip.node();
if (this.tooltipFunc) {
this.tooltipFunc(tooltipNode, d, fill)
}
const width = tooltipNode.offsetWidth + 1; // +1 for rounding reasons
const height = tooltipNode.offsetHeight;
const buffer = 5;
// calculate where the top is going to be. ideally it is
// (d.y - height/2) which'll put the tooltip in the middle of the bubble.
// we need to account for if this'll put it out of bounds.
var top;
// if it goes above the bounds, have the top be the buffer
if (d.y - height < 0) {
top = buffer;
// if it goes below the bounds, have its buttom be a buffer length away
} else if (d.y + height/2 > el.offsetHeight) {
top = el.offsetHeight - height - buffer;
// otherwise smack this bad boy in the middle of its bubble
} else {
top = d.y - height/2;
}
// calculate where the left is going to be. ideally it is
// (d.x + d.r + buffer) which will put the tooltip to the right
// of the bubble. we need to account for the case where this puts
// the tooltip out of bounds.
var left;
// if there's room to put it on the right of the bubble, do so
if (d.x + d.r + width + buffer < el.offsetWidth) {
left = d.x + d.r + buffer;
// if there's room to put it on the left of the bubble, do so
} else if (d.x - d.r - width - buffer > 0) {
left = d.x - d.r - width - buffer;
// otherwise put it on the right part of its container
} else {
left = el.offsetWidth - width - buffer;
}
this.tooltip.style('background-color', backgroundColor)
.style('border-color', fill)
.style('width', width + 'px')
.style('left', left + 'px')
.style('top', top + 'px');
}
/**
* On tooltip mouseout, hide the tooltip.
*/
_tooltipMouseOut(d, i) {
if (!this.createTooltip) return;
this.tooltip.style('display', 'none')
.style('width', '')
.style('top', '')
.style('left', '');
}
/** Any necessary cleanup */
destroy(el) { }
}