-
Notifications
You must be signed in to change notification settings - Fork 61
/
AnimatedSvgD3Chord.js
133 lines (126 loc) · 4.43 KB
/
AnimatedSvgD3Chord.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
// @flow
import React, { Component } from 'react';
import { Animated } from 'react-native';
import * as d3 from 'd3-chord';
import omit from 'lodash/omit';
import G from './AnimatedSvgG';
import D3ShapeArc from './AnimatedSvgD3ShapeArc';
import D3Ribbon from './AnimatedSvgD3Ribbon';
import { listen, removeListeners } from '../animatedListener';
import type { AnimatedListener } from '../animatedListener';
type ChordLayout = d3.ChordLayout;
type Chords = d3.Chords;
type Props = {
matrix: number[][]
};
export const args = ['padAngle', 'sortGroups', 'sortSubgroups', 'sortChords'];
function createGenerator(props, generator?: ChordLayout): ChordLayout {
generator = generator || d3.chord();
return args.reduce((acc: ChordLayout, arg) => {
const prop = props[arg];
if (prop) {
return acc[arg](prop);
}
return acc;
}, generator);
}
function getChords(generator: ChordLayout, matrix): Chords {
return generator(matrix);
}
const defaultProps = {
chordProps: () => ({}),
groupProps: () => ({})
};
class SvgD3Chord extends Component {
props: Props;
generator: ChordLayout;
matrix: AnimatedListener;
chords: Chords;
static defaultProps = typeof defaultProps;
constructor(props) {
super(props);
this.generator = createGenerator(props);
this.matrix = listen(props.matrix, _ =>
this.setNativeProps({ _listener: true })
);
this._components = {};
}
setNativeProps = (props = {}) => {
const argChanged = args.some((key, index) => props[key] != null);
if (argChanged) {
this.generator = createGenerator(props, this.generator);
}
if (argChanged || props._listener) {
const chords = getChords(this.generator, this.matrix.values);
chords.groups.forEach((group, i) => {
const key = 'group' + group.index;
const component = this._components[key];
component && component.setNativeProps(group);
});
chords.forEach((chord, i) => {
const key = chord.source.index + ',' + chord.source.subindex;
const component = this._components[key];
component && component.setNativeProps(chord);
});
}
this._component && this._component.setNativeProps(props);
};
shouldComponentUpdate(nextProps) {
const argChanged = args.some(
(key, index) => nextProps[key] !== this.props[key]
);
const matrixChanged = nextProps.matrix !== this.props.matrix;
if (argChanged) {
this.generator = createGenerator(nextProps, this.generator);
}
if (matrixChanged) {
removeListeners(this.matrix);
this.matrix = listen(nextProps.matrix, _ =>
this.setNativeProps({ _listener: true })
);
}
return argChanged || matrixChanged;
}
componentWillUnmount() {
removeListeners(this.matrix);
}
render() {
const filteredProps = omit(this.props, args);
const chords = getChords(this.generator, this.matrix.values);
return (
<G
ref={component => (this._component = component)}
{...filteredProps}
>
{chords.groups.map(group => {
const key = 'group' + group.index;
return (
<D3ShapeArc
key={key}
ref={component =>
(this._components[key] = component)}
{...this.props.groupProps(group)}
{...group}
/>
);
})}
{chords.map(chord => {
const key =
chord.source.index + ',' + chord.source.subindex;
return (
<D3Ribbon
key={key}
ref={component =>
(this._components[key] = component)}
{...this.props.chordProps(chord)}
{...chord}
/>
);
})}
</G>
);
}
}
SvgD3Chord.defaultProps = defaultProps;
SvgD3Chord = Animated.createAnimatedComponent(SvgD3Chord);
export default SvgD3Chord;