|
1 | 1 | import React, { Component, PropTypes } from 'react';
|
2 | 2 | import d3 from 'd3';
|
| 3 | +import copy from 'deepcopy'; |
3 | 4 | import equal from 'deep-equal';
|
4 | 5 |
|
5 | 6 | const WIDTH = 800;
|
6 | 7 | const HEIGHT = 400;
|
| 8 | +const DURATION = 750; |
7 | 9 | const color = d3.scale.category10();
|
8 | 10 |
|
9 | 11 | export default class Treemap extends Component {
|
10 | 12 | static displayName = 'Treemap';
|
11 | 13 | static propTypes = {
|
12 | 14 | data: PropTypes.object.isRequired,
|
| 15 | + path: PropTypes.array.isRequired, |
13 | 16 | onMoveDown: PropTypes.func.isRequired,
|
14 | 17 | onShowDetail: PropTypes.func.isRequired,
|
15 | 18 | onHideDetail: PropTypes.func.isRequired,
|
16 | 19 | };
|
17 | 20 |
|
| 21 | + constructor(props) { |
| 22 | + super(props); |
| 23 | + |
| 24 | + this.treemap = d3.layout.treemap() |
| 25 | + .children(d => d.children) |
| 26 | + .value(d => d.size) |
| 27 | + .size([WIDTH, HEIGHT]); |
| 28 | + |
| 29 | + this.state = { prev: {} }; |
| 30 | + } |
| 31 | + |
18 | 32 | componentDidMount() {
|
19 | 33 | this.renderTreemap();
|
20 | 34 | }
|
21 | 35 |
|
22 | 36 | componentDidUpdate(prevProps) {
|
23 |
| - if (!equal(prevProps.data, this.props.data)) { |
24 |
| - this.renderTreemap(); |
| 37 | + if (!equal(prevProps.path, this.props.path)) { |
| 38 | + this.setState({ |
| 39 | + prev: { |
| 40 | + data: copy(prevProps.data), |
| 41 | + path: copy(prevProps.path), |
| 42 | + } |
| 43 | + }, () => { |
| 44 | + this.renderTreemap(); |
| 45 | + }); |
25 | 46 | }
|
26 | 47 | }
|
27 | 48 |
|
28 | 49 | renderTreemap() {
|
29 |
| - if (this.svg) { |
30 |
| - d3.select(this.refs.content).selectAll('svg').remove(); |
| 50 | + const { data, path, onMoveDown, onShowDetail, onHideDetail } = this.props; |
| 51 | + const { prev: { data: prevData, path: prevPath } } = this.state; |
| 52 | + |
| 53 | + if (!this.svg) { |
| 54 | + this.svg = d3.select(this.refs.content) |
| 55 | + .append('svg') |
| 56 | + .attr('width', WIDTH) |
| 57 | + .attr('height', HEIGHT); |
31 | 58 | }
|
32 | 59 |
|
33 |
| - this.svg = d3.select(this.refs.content).append('svg') |
34 |
| - .attr('width', WIDTH) |
35 |
| - .attr('height', HEIGHT); |
| 60 | + // Populate for current data |
| 61 | + const currentNodes = this.treemap |
| 62 | + .nodes(copy(data)).filter(d => d.depth === 1); |
36 | 63 |
|
37 |
| - const { data, onMoveDown, onShowDetail, onHideDetail } = this.props; |
| 64 | + // Get original position before populating layout |
| 65 | + let x, y, prevNodes; |
| 66 | + if (typeof prevData !== 'undefined') { |
| 67 | + // Populate for prev data |
| 68 | + prevNodes = this.treemap |
| 69 | + .nodes(copy(prevData)).filter(d => d.depth === 1); |
38 | 70 |
|
39 |
| - const layer = this.svg.append('g'); |
| 71 | + // Check UP or DOWN |
| 72 | + if (prevPath.length < path.length) { |
| 73 | + let orig = prevNodes.filter(child => child.name === data.name)[0]; |
| 74 | + // console.log('Down', {...orig}); |
| 75 | + x = d3.scale.linear() |
| 76 | + .domain([orig.x, orig.x + orig.dx]) |
| 77 | + .range([0, WIDTH]); |
| 78 | + y = d3.scale.linear() |
| 79 | + .domain([orig.y, orig.y + orig.dy]) |
| 80 | + .range([0, HEIGHT]); |
| 81 | + } else { |
| 82 | + let orig = currentNodes.filter(child => child.name === prevData.name)[0]; |
| 83 | + // console.log('Up', {...orig}); |
| 84 | + x = d3.scale.linear() |
| 85 | + .domain([0, WIDTH]) |
| 86 | + .range([orig.x, orig.x + orig.dx]); |
| 87 | + y = d3.scale.linear() |
| 88 | + .domain([0, HEIGHT]) |
| 89 | + .range([orig.y, orig.y + orig.dy]); |
| 90 | + } |
| 91 | + } |
40 | 92 |
|
41 |
| - const treemap = d3.layout.treemap() |
42 |
| - .children(d => d.children) |
43 |
| - .value(d => d.size) |
44 |
| - .size([WIDTH, HEIGHT]); |
| 93 | + if (typeof x === 'undefined' && typeof y === 'undefined') { |
| 94 | + x = d3.scale.linear() |
| 95 | + .domain([0, WIDTH]) |
| 96 | + .range([0, WIDTH]); |
| 97 | + y = d3.scale.linear() |
| 98 | + .domain([0, HEIGHT]) |
| 99 | + .range([0, HEIGHT]); |
| 100 | + } |
45 | 101 |
|
46 |
| - const next = d => d.depth === 1; |
47 |
| - const nodes = treemap.nodes(data); |
48 |
| - const children = layer.selectAll('g').data(nodes); |
| 102 | + // console.log('x, y', |
| 103 | + // `${JSON.stringify(x.domain())} => ${JSON.stringify(x.range())}`, |
| 104 | + // `${JSON.stringify(y.domain())} => ${JSON.stringify(y.range())}`); |
49 | 105 |
|
50 |
| - children |
| 106 | + const ix = d => x.invert(d); |
| 107 | + const iy = d => y.invert(d); |
| 108 | + |
| 109 | + if (typeof prevData !== 'undefined') { |
| 110 | + // [1] Layer for Prev Level |
| 111 | + const prev = {}; |
| 112 | + prev.layer = this.svg.select('.current') |
| 113 | + .attr('class', 'prev'); |
| 114 | + |
| 115 | + // [JOIN] prev |
| 116 | + prev.child = prev.layer.selectAll('.child') |
| 117 | + .data(prevNodes); |
| 118 | + |
| 119 | + // [UPDATE] prev: child |
| 120 | + prev.child |
| 121 | + .transition().duration(DURATION) |
| 122 | + .attr('transform', d => `translate(${x(d.x)},${y(d.y)})`); |
| 123 | + |
| 124 | + // [UPDATE] prev: child > rect |
| 125 | + prev.rect = prev.child.select('rect') |
| 126 | + .transition().duration(DURATION) |
| 127 | + .attr('width', d => x(d.x + d.dx) - x(d.x)) |
| 128 | + .attr('height', d => y(d.y + d.dy) - y(d.y)); |
| 129 | + |
| 130 | + // Remove prev layer after transition |
| 131 | + prev.layer |
| 132 | + .transition().duration(DURATION) |
| 133 | + .delay(DURATION) |
| 134 | + .remove(); |
| 135 | + } |
| 136 | + |
| 137 | + // [2] Layer for Current Level |
| 138 | + const current = {}; |
| 139 | + current.layer = this.svg |
| 140 | + .append('g') |
| 141 | + .attr('class', 'current'); |
| 142 | + |
| 143 | + // [JOIN] current |
| 144 | + current.child = current.layer.selectAll('.child') |
| 145 | + .data(currentNodes); |
| 146 | + |
| 147 | + // [ENTER] current: child |
| 148 | + current.enter = current.child |
51 | 149 | .enter().append('g')
|
52 |
| - .filter(next) |
53 |
| - .attr('transform', d => `translate(${d.x},${d.y})`) |
| 150 | + .attr('class', 'child') |
| 151 | + .attr('transform', d => `translate(${ix(d.x)},${iy(d.y)})`) |
54 | 152 | .style('cursor', d => d.children ? 'pointer' : 'normal')
|
55 |
| - .on('click', function (d) { |
| 153 | + .style('opacity', 0) |
| 154 | + .on('click', d => { |
56 | 155 | if (d.children) {
|
57 | 156 | onMoveDown(d.name);
|
58 | 157 | }
|
59 | 158 | })
|
60 |
| - .on('mousemove', function (d) { |
| 159 | + .on('mousemove', d => { |
61 | 160 | const el = document.getElementById('container');
|
62 | 161 | const [x, y] = d3.mouse(el);
|
63 | 162 | onShowDetail({ x, y }, d.name);
|
64 | 163 | })
|
65 |
| - .on('mouseleave', function (d) { |
| 164 | + .on('mouseleave', d => { |
66 | 165 | onHideDetail();
|
67 | 166 | });
|
68 | 167 |
|
69 |
| - children.append('rect') |
70 |
| - .filter(next) |
| 168 | + // [ENTER] current: child (transition) |
| 169 | + current.child |
| 170 | + .transition().duration(DURATION) |
| 171 | + .attr('transform', d => `translate(${d.x},${d.y})`) |
| 172 | + .style('opacity', 1); |
| 173 | + |
| 174 | + // [ENTER] current: child > rect |
| 175 | + current.enter |
| 176 | + .append('rect') |
| 177 | + .attr('width', d => ix(d.x + d.dx) - ix(d.x)) |
| 178 | + .attr('height', d => iy(d.y + d.dy) - iy(d.y)) |
| 179 | + .attr('fill', (d, i) => color(i)) |
| 180 | + .transition().duration(DURATION) |
71 | 181 | .attr('width', d => d.dx)
|
72 |
| - .attr('height', d => d.dy) |
73 |
| - .attr('fill', (d, i) => color(i)); |
| 182 | + .attr('height', d => d.dy); |
74 | 183 | }
|
75 | 184 |
|
76 | 185 | render() {
|
|
0 commit comments