forked from intellipharm-pty-ltd/dc-addons
/
index.js
138 lines (111 loc) · 3.66 KB
/
index.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
// Code copied and changed from https://github.com/vlandham/gates_bubbles
import * as d3 from 'd3';
import dc from 'dc';
export default function(parent, chartGroup) {
var _chart = dc.bubbleMixin(dc.capMixin(dc.bubbleChart(parent)));
var FORCE_STRENGTH = 1;
var FRICTION = 0.25;
var _simulation = null;
var _circles = [];
var _g = null;
var _gs = null;
var _center = {x: _chart.width() / 2, y: _chart.height() / 2};
_chart._doRender = function () {
_chart.resetSvg();
_g = _chart.svg().append('g');
_circles = d3.select();
drawChart();
return _chart;
};
_chart._doRedraw = function () {
drawChart();
return _chart;
};
function drawChart() {
if (_chart.elasticRadius()) {
_chart.r().domain([_chart.rMin(), _chart.rMax()]);
}
_chart.r().range([_chart.MIN_RADIUS, _chart.xAxisLength() * _chart.maxBubbleRelativeSize()]);
if (_circles.empty()) {
createBubbles();
} else {
updateBubbles();
}
highlightFilter();
_simulation = d3.forceSimulation()
.nodes(_chart.data())
.on('tick', function () {
_gs.attr('transform', function (d) {
return 'translate(' + d.x + ',' + d.y + ')';
});
});
_simulation
.force('x', d3.forceX().strength(FORCE_STRENGTH).x(_center.x))
.force('y', d3.forceY().strength(FORCE_STRENGTH).y(_center.y))
.force('charge', d3.forceManyBody().strength(charge))
.force('collide', d3.forceCollide(function (d) {
return _chart.bubbleR(d) + 1;
}))
.velocityDecay(FRICTION);
}
function createBubbles() {
_gs = _g
.selectAll('g')
.data(_chart.data())
.enter()
.append('g')
.attr('class', _chart.BUBBLE_NODE_CLASS)
.on('mouseover', function () {
d3.select(this).raise();
});
_circles = _gs
.append('circle')
.attr('class', _chart.BUBBLE_CLASS)
.attr('r', function (d) {
d.radius = _chart.bubbleR(d);
return d.radius;
})
.attr('fill-opacity', 1)
.attr('fill', function (d, i) {
return _chart.getColor(d, i);
})
.attr('stroke-width', 2)
.on('click', _chart.onClick)
.on('mouseenter', function () {
d3.select(this).attr('stroke', '#303030');
})
.on('mouseout', function () {
d3.select(this).attr('stroke', 'none');
});
_chart._doRenderLabel(_gs);
_chart._doRenderTitles(_gs);
}
function updateBubbles() {
_circles.data(_chart.data())
.attr('r', function (d) {
d.radius = _chart.bubbleR(d);
return d.radius;
});
_chart.doUpdateLabels(_gs);
_chart.doUpdateTitles(_gs);
}
function charge(d) {
return -Math.pow(d.radius, 2.0) * FORCE_STRENGTH;
}
function highlightFilter() {
if (_chart.hasFilter()) {
_gs.each(function (d) {
if (_chart.hasFilter(_chart.keyAccessor()(d))) {
_chart.highlightSelected(this);
} else {
_chart.fadeDeselected(this);
}
});
} else {
_gs.each(function () {
_chart.resetHighlight(this);
});
}
}
return _chart.anchor(parent, chartGroup);
}