-
Notifications
You must be signed in to change notification settings - Fork 16
/
ScatterPlotImpls.js
73 lines (60 loc) · 2.02 KB
/
ScatterPlotImpls.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
import D3Component from '../charts/D3Component';
import d3 from 'd3';
import Axis from '../charts/common/Axis';
export default class ScatterPlotImpls extends D3Component {
willMount() {
if (this.props.xaxis) {
this.setAxis(new Axis(this.props.xaxis, this.props.xScale));
}
if (this.props.yaxis) {
this.setAxis(new Axis(this.props.yaxis, this.props.yScale));
}
}
onMount(props) {
super.onMount(props);
this._render();
}
preRender() {
let {data, xAccessor, yAccessor, xScale, yScale, dotRadiusScale, dotRadiusAccessor} = this.props;
xScale.range([0, this.width]);
xScale.domain(d3.extent(data, d => xAccessor(d))).nice();
yScale.range([this.height, 0]);
yScale.domain(d3.extent(data, d => yAccessor(d))).nice();
if (dotRadiusScale && dotRadiusAccessor) {
dotRadiusScale.domain(d3.extent(data, d => dotRadiusAccessor(d)));
}
}
render() {
let {data, xAccessor, yAccessor, xScale, yScale, dotRadius, dotRadiusScale, dotRadiusAccessor, dotColorScale, dotColorAccessor} = this.props;
const dots = this.base.selectAll('.dot').data(data);
dots.exit().remove();
dots.enter()
.append('circle')
.attr('class', 'dot');
dots
.attr('r', (d) => {
if (!dotRadiusScale && !dotRadiusAccessor) return dotRadius;
return dotRadiusScale(dotRadiusAccessor(d));
})
.attr('cx', d => xScale(xAccessor(d)))
.attr('cy', d => yScale(yAccessor(d)))
.style('fill', (d) => {
if (!dotColorScale || !dotColorAccessor) return 'lime';
return dotColorScale(dotColorAccessor(d));
});
}
postRender() {
let {selected, selectionAccessor, interactive} = this.props;
const dots = this.base.selectAll('.dot');
if (interactive) {
dots
.on('click', this.dispatch.click)
.on('mouseover', this.dispatch.mouseOver)
.on('mouseout', this.dispatch.mouseOut);
}
if (selected) {
dots
.classed('selected', d => selectionAccessor(d) === selected);
}
}
}