|
| 1 | +import { Meta } from "@storybook/react"; |
| 2 | +import React, { useEffect } from "react"; |
| 3 | + |
| 4 | +import { VictoryPie } from "../packages/victory-pie"; |
| 5 | +import { VictoryAnimation, VictoryLabel } from "../packages/victory-core"; |
| 6 | +import { storyContainer } from "./decorators"; |
| 7 | + |
| 8 | +const meta: Meta<typeof VictoryAnimation> = { |
| 9 | + title: "Victory Charts/SVG Container/VictoryAnimation", |
| 10 | + component: VictoryAnimation, |
| 11 | + tags: ["autodocs"], |
| 12 | + decorators: [storyContainer], |
| 13 | +}; |
| 14 | + |
| 15 | +export default meta; |
| 16 | + |
| 17 | +const ANIMATION_DATA = [ |
| 18 | + [ |
| 19 | + { x: 1, y: 0 }, |
| 20 | + { x: 2, y: 100 }, |
| 21 | + ], |
| 22 | + [ |
| 23 | + { x: 1, y: 25 }, |
| 24 | + { x: 2, y: 75 }, |
| 25 | + ], |
| 26 | + [ |
| 27 | + { x: 1, y: 50 }, |
| 28 | + { x: 2, y: 50 }, |
| 29 | + ], |
| 30 | + [ |
| 31 | + { x: 1, y: 75 }, |
| 32 | + { x: 2, y: 25 }, |
| 33 | + ], |
| 34 | + [ |
| 35 | + { x: 1, y: 100 }, |
| 36 | + { x: 2, y: 0 }, |
| 37 | + ], |
| 38 | +]; |
| 39 | + |
| 40 | +const AnimationDemo = () => { |
| 41 | + const [data, setData] = React.useState(ANIMATION_DATA[0]); |
| 42 | + const [percent, setPercent] = React.useState(0); |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + const interval = setInterval(() => { |
| 46 | + const nextIndex = |
| 47 | + (ANIMATION_DATA.indexOf(data) + 1) % ANIMATION_DATA.length; |
| 48 | + setData(ANIMATION_DATA[nextIndex]); |
| 49 | + setPercent(ANIMATION_DATA[nextIndex][0].y); |
| 50 | + }, 2000); |
| 51 | + |
| 52 | + // clean up interval on unmount |
| 53 | + return () => clearInterval(interval); |
| 54 | + }, [percent]); |
| 55 | + |
| 56 | + return ( |
| 57 | + <div> |
| 58 | + <svg viewBox="0 0 400 400" width="100%" height="100%"> |
| 59 | + <VictoryPie |
| 60 | + standalone={false} |
| 61 | + animate={{ duration: 1000 }} |
| 62 | + width={400} |
| 63 | + height={400} |
| 64 | + data={data} |
| 65 | + innerRadius={120} |
| 66 | + cornerRadius={25} |
| 67 | + labels={() => null} |
| 68 | + style={{ |
| 69 | + data: { |
| 70 | + fill: ({ datum }) => { |
| 71 | + const color = datum.y > 30 ? "green" : "red"; |
| 72 | + return datum.x === 1 ? color : "transparent"; |
| 73 | + }, |
| 74 | + }, |
| 75 | + }} |
| 76 | + /> |
| 77 | + <VictoryAnimation duration={1000} data={{ percent }}> |
| 78 | + {(newProps) => { |
| 79 | + return ( |
| 80 | + <VictoryLabel |
| 81 | + textAnchor="middle" |
| 82 | + verticalAnchor="middle" |
| 83 | + x={200} |
| 84 | + y={200} |
| 85 | + text={`${Math.round(Number(newProps.percent))}%`} |
| 86 | + style={{ fontSize: 45 }} |
| 87 | + /> |
| 88 | + ); |
| 89 | + }} |
| 90 | + </VictoryAnimation> |
| 91 | + </svg> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}; |
| 95 | + |
| 96 | +export const DefaultRendering = () => { |
| 97 | + return <AnimationDemo />; |
| 98 | +}; |
0 commit comments