I'd be grateful for help.
// Implementation Example:
// https://github.com/react-spring/react-spring-examples/tree/renderprops/demos/renderprops/sunburst
import React, { useState, useEffect } from 'react';
import { Group } from '@vx/group';
import { arc as d3arc } from 'd3-shape';
import { scaleLinear, scaleSqrt } from 'd3-scale';
import { interpolate as d3interpolate } from 'd3-interpolate';
import Partition from './Partition';
import styled from 'styled-components';
import { useSpring, animated } from 'react-spring';
const StyledSvg = styled.svg`
background: #fff;
`;
const SunburstDiagram = props => {
const {
nodes,
width,
height,
margin = { top: 0, left: 0, right: 0, bottom: 0 },
} = props;
// Complete set of input values
const [xDomain, setXDomain] = useState([0, 1]);
const [yDomain, setYDomain] = useState([0, 1]);
// Output range
const [xRange] = useState([0, 2 * Math.PI]);
const [yRange, setYRange] = useState([0, width / 2]);
useEffect(() => {
setYRange([yRange[0], width / 2]);
}, [props.count]);
// Scale Axis
const xScale = scaleLinear();
const yScale = scaleSqrt();
// Map input domain to output range
xScale.domain(xDomain).range(xRange);
yScale.domain(yDomain).range(yRange);
// Arc generators produce path data from angle and radius values.
var arcGenerator = d3arc()
.startAngle(d => Math.max(0, Math.min(2 * Math.PI, xScale(d.x0))))
.endAngle(d => Math.max(0, Math.min(2 * Math.PI, xScale(d.x1))))
.innerRadius(d => Math.max(0, yScale(d.y0)))
.outerRadius(d => Math.max(0, yScale(d.y1)));
const nodesDescendants = nodes.descendants();
let { t, fill } = useSpring({
native: true,
reset: true,
from: { t: 0, fill: 'yellow' },
to: { t: 1, fill: 'black' },
config: {
mass: 5,
tension: 500,
friction: 100,
precision: 0.00001,
},
onFrame: ({ t }) => handleUpdate(t, xd, yd, yr),
});
const handleClick = node => {
setXDomain([node.x0, node.x1]);
setYDomain([node.y0, 1]);
setYRange([node.y0 ? 20 : 0, width / 2]);
};
const handleUpdate = (t, xd, yd, yr) => {
console.log({ t: t, xd: xd, yd: yd, yr: yr });
xScale.domain(xd(t));
yScale.domain(yd(t)).range(yr(t));
};
const xd = d3interpolate(xScale.domain(), xDomain);
const yd = d3interpolate(yScale.domain(), yDomain);
const yr = d3interpolate(yScale.range(), yRange);
return (
<StyledSvg width={width} height={height}>
<Partition top={margin.top} left={margin.left} nodes={nodes}>
<Group top={height / 2} left={width / 2}>
{nodesDescendants.map((node, index) => {
return (
<animated.path
onClick={() => handleClick(node)}
className="path"
d={t.interpolate(() => arcGenerator(node))}
stroke="#fff"
strokeWidth="1"
fill={fill.interpolate(d => d)}
fillRule="evenodd"
key={index}
/>
);
})}
</Group>
</Partition>
</StyledSvg>
);
};
export default SunburstDiagram;