|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import * as d3s from 'd3-shape'; |
| 3 | +import * as d3i from 'd3-interpolate'; |
| 4 | + |
| 5 | +const maxDuration = 500; |
| 6 | +const step = 10; |
| 7 | +const padRadians = 2 * Math.PI * 0.01; |
| 8 | +const minPercentageOfTotal = 0.02; // 2 percent |
| 9 | + |
| 10 | +const pieColors = d3i.interpolateRgbBasis([ |
| 11 | + '#7C98B3', |
| 12 | + '#B3B7EE', |
| 13 | + '#AF90A9', |
| 14 | + '#A05C7B', |
| 15 | + '#944654', |
| 16 | +]); |
| 17 | + |
| 18 | +class PieSlice extends Component { |
| 19 | + render() { |
| 20 | + const { slice, arcGen, progress, color } = this.props; |
| 21 | + |
| 22 | + return <g> |
| 23 | + <path |
| 24 | + d={arcGen({ |
| 25 | + startAngle: slice.startAngle, |
| 26 | + endAngle: slice.interpolator(progress), |
| 27 | + })} |
| 28 | + fill={pieColors(color)} /> |
| 29 | + </g>; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class AlignPie extends Component { |
| 34 | + constructor(props) { |
| 35 | + super(props); |
| 36 | + this.state = { |
| 37 | + intervalId: null, |
| 38 | + tick: 0, |
| 39 | + }; |
| 40 | + this.handleInterval = this.handleInterval.bind(this); |
| 41 | + } |
| 42 | + |
| 43 | + handleInterval() { |
| 44 | + if (this.state.tick >= maxDuration) { |
| 45 | + clearInterval(this.state.intervalId); |
| 46 | + } else { |
| 47 | + this.setState({ tick: this.state.tick + step }); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + componentWillMount() { |
| 52 | + this.setState({intervalId: setInterval(this.handleInterval, 10)}); |
| 53 | + } |
| 54 | + |
| 55 | + render() { |
| 56 | + const size = 500; |
| 57 | + const radius = size * 0.8 / 2; |
| 58 | + const total = Array.from(this.props.data.values()) |
| 59 | + .reduce((acc, val) => acc + val, 0); |
| 60 | + const dataArray = Array.from(this.props.data.entries()) |
| 61 | + .map(([key, value]) => value / total > minPercentageOfTotal ? [key, value] : [key, total * minPercentageOfTotal]); |
| 62 | + const pie = d3s.pie() |
| 63 | + .value((d) => d[1])(dataArray) |
| 64 | + .map((slice) => { |
| 65 | + slice.interpolator = d3i.interpolateNumber(slice.startAngle, slice.endAngle - padRadians); |
| 66 | + return slice; |
| 67 | + }); |
| 68 | + |
| 69 | + const path = d3s.arc() |
| 70 | + .outerRadius(radius) |
| 71 | + .innerRadius(0) |
| 72 | + |
| 73 | + const label = d3s.arc() |
| 74 | + .outerRadius(radius * 0.8) |
| 75 | + .innerRadius(radius * 0.8); |
| 76 | + |
| 77 | + return <svg width={`${size}px`} height={`${size}px`}> |
| 78 | + <g transform={`translate(${size/2},${size/2})`}> |
| 79 | + {pie.map((angle) => <PieSlice |
| 80 | + key={angle.data[0].replace(/ /g, '')} |
| 81 | + progress={this.state.tick/maxDuration} |
| 82 | + color={angle.data[1]/total} |
| 83 | + slice={angle} |
| 84 | + arcGen={path} />)} |
| 85 | + {pie.map((angle) => { |
| 86 | + const textPosition = label.centroid(angle); |
| 87 | + const textAnchor = textPosition[0] <= 0 ? 'start' : 'middle'; |
| 88 | + return <text |
| 89 | + key={`text_${angle.data[0].replace(/ /g, '')}`} |
| 90 | + transform={`translate(${textPosition})`} |
| 91 | + textAnchor={textAnchor} |
| 92 | + dy="0.035em"> |
| 93 | + {angle.data[0]} |
| 94 | + </text>; |
| 95 | + })} |
| 96 | + </g> |
| 97 | + </svg>; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export default AlignPie; |
0 commit comments