forked from dc-js/dc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble-chart.js
142 lines (115 loc) · 4.55 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
import {BubbleMixin} from '../base/bubble-mixin';
import {CoordinateGridMixin} from '../base/coordinate-grid-mixin';
import {transition} from '../core/core';
import {d3compat} from '../core/config';
/**
* A concrete implementation of a general purpose bubble chart that allows data visualization using the
* following dimensions:
* - x axis position
* - y axis position
* - bubble radius
* - color
*
* Examples:
* - {@link http://dc-js.github.com/dc.js/ Nasdaq 100 Index}
* - {@link http://dc-js.github.com/dc.js/vc/index.html US Venture Capital Landscape 2011}
* @mixes BubbleMixin
* @mixes CoordinateGridMixin
*/
export class BubbleChart extends BubbleMixin(CoordinateGridMixin) {
/**
* Create a Bubble Chart.
*
* @example
* // create a bubble chart under #chart-container1 element using the default global chart group
* var bubbleChart1 = new BubbleChart('#chart-container1');
* // create a bubble chart under #chart-container2 element using chart group A
* var bubbleChart2 = new BubbleChart('#chart-container2', 'chartGroupA');
* @param {String|node|d3.selection} parent - Any valid
* {@link https://github.com/d3/d3-selection/blob/master/README.md#select d3 single selector} specifying
* a dom block element such as a div; or a dom element or d3 selection.
* @param {String} [chartGroup] - The name of the chart group this chart instance should be placed in.
* Interaction with a chart will only trigger events and redraws within the chart's group.
*/
constructor (parent, chartGroup) {
super();
this.transitionDuration(750);
this.transitionDelay(0);
this.anchor(parent, chartGroup);
}
_bubbleLocator (d) {
return `translate(${this._bubbleX(d)},${this._bubbleY(d)})`;
}
plotData () {
this.calculateRadiusDomain();
this.r().range([this.MIN_RADIUS, this.xAxisLength() * this.maxBubbleRelativeSize()]);
const data = this.data();
let bubbleG = this.chartBodyG().selectAll(`g.${this.BUBBLE_NODE_CLASS}`)
.data(data, d => d.key);
if (this.sortBubbleSize() || this.keyboardAccessible()) {
// update dom order based on sort
bubbleG.order();
}
this._removeNodes(bubbleG);
bubbleG = this._renderNodes(bubbleG);
this._updateNodes(bubbleG);
this.fadeDeselectedArea(this.filter());
}
_renderNodes (bubbleG) {
const bubbleGEnter = bubbleG.enter().append('g');
bubbleGEnter
.attr('class', this.BUBBLE_NODE_CLASS)
.attr('transform', d => this._bubbleLocator(d))
.append('circle').attr('class', (d, i) => `${this.BUBBLE_CLASS} _${i}`)
.on('click', d3compat.eventHandler(d => this.onClick(d)))
.classed('dc-tabbable', this._keyboardAccessible)
.attr('fill', this.getColor)
.attr('r', 0);
bubbleG = bubbleGEnter.merge(bubbleG);
transition(bubbleG, this.transitionDuration(), this.transitionDelay())
.select(`circle.${this.BUBBLE_CLASS}`)
.attr('r', d => this.bubbleR(d))
.attr('opacity', d => (this.bubbleR(d) > 0) ? 1 : 0);
if (this._keyboardAccessible) {
this._makeKeyboardAccessible(this.onClick);
}
this._doRenderLabel(bubbleGEnter);
this._doRenderTitles(bubbleGEnter);
return bubbleG;
}
_updateNodes (bubbleG) {
transition(bubbleG, this.transitionDuration(), this.transitionDelay())
.attr('transform', d => this._bubbleLocator(d))
.select(`circle.${this.BUBBLE_CLASS}`)
.attr('fill', this.getColor)
.attr('r', d => this.bubbleR(d))
.attr('opacity', d => (this.bubbleR(d) > 0) ? 1 : 0);
this.doUpdateLabels(bubbleG);
this.doUpdateTitles(bubbleG);
}
_removeNodes (bubbleG) {
bubbleG.exit().remove();
}
_bubbleX (d) {
let x = this.x()(this.keyAccessor()(d));
if (isNaN(x) || !isFinite(x)) {
x = 0;
}
return x;
}
_bubbleY (d) {
let y = this.y()(this.valueAccessor()(d));
if (isNaN(y) || !isFinite(y)) {
y = 0;
}
return y;
}
renderBrush () {
// override default x axis brush from parent chart
}
redrawBrush (brushSelection, doTransition) {
// override default x axis brush from parent chart
this.fadeDeselectedArea(brushSelection);
}
}
export const bubbleChart = (parent, chartGroup) => new BubbleChart(parent, chartGroup);