Skip to content

Poc/animation #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
. "$(dirname "$0")/_/husky.sh"

# yarn test
lerna run --concurrency 1 --stream precommit --since HEAD --exclude-dependents
npx lerna run --concurrency 1 --stream precommit --since HEAD --exclude-dependents

6 changes: 5 additions & 1 deletion packages/ez-core/src/utils/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ export const defaultTooltipContext: TooltipContext = {
export const defaultChartContext: ChartContext = {
dimensions: defaultChartDimensions,
padding: defaultChartPadding,
animationOptions: undefined,
animationOptions: {
duration: 0,
delay: 0,
easing: 'easeLinear',
},
data: [],
dataDict: {},
isRTL: false,
Expand Down
4 changes: 2 additions & 2 deletions packages/ez-core/src/utils/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const generateLinePath = (
shapeData: LineData,
curve: LineCurve,
beta?: number
) => {
): string => {
const lineGenerator = line();
if (curve) {
const curves = {
Expand All @@ -50,7 +50,7 @@ export const generateLinePath = (
}
const data = shapeData.map((d) => [d.x, d.y]);
// @ts-ignore <-- remove this when TS version is fix
return lineGenerator(data);
return lineGenerator(data) || '';
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/ez-core/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface RadialConfig extends PieConfig {
export interface ChartContext {
dimensions: Dimensions;
padding: ChartPadding;
animationOptions?: AnimationOptions;
animationOptions: AnimationOptions;
data: NormalizedData;
dataDict: NormalizedDataDict;
isRTL: boolean;
Expand Down
28 changes: 26 additions & 2 deletions packages/ez-react/src/components/Segments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Point } from '@/components/shapes/Point';
import { Points } from '@/components/Points';
import { LinePath } from '@/components/shapes/LinePath';
import { useColorScale } from '@/components/scales/ColorScale';
import { useChart } from '@/lib/use-chart';

export interface SegmentsProps extends SVGAttributes<SVGGElement> {
xDomainKey: string;
Expand All @@ -26,6 +27,9 @@ export const Segments: FC<SegmentsProps> = ({
color: '#FFF',
},
}) => {
const {
animationOptions: { duration },
} = useChart();
const { colorScale } = useColorScale();

const color = useMemo(
Expand Down Expand Up @@ -54,7 +58,9 @@ export const Segments: FC<SegmentsProps> = ({
strokeWidth={line.strokeWidth}
/>
{!marker.hidden &&
pointData.map((pointDatum) => {
pointData.map((pointDatum, idx) => {
const dur = duration / pointData.length;
const delay = dur * (idx + 1);
return (
<Point
key={pointDatum.id}
Expand All @@ -63,7 +69,25 @@ export const Segments: FC<SegmentsProps> = ({
fill={marker.color}
stroke={color}
strokeWidth={line.strokeWidth}
/>
opacity={0}
>
<animate
attributeName="opacity"
attributeType="CSS"
from={0}
to={1}
begin={`${delay}ms`}
dur={`${dur}ms`}
fill={'freeze'}
/>
<animate
attributeName="r"
from={0}
to={marker.radius}
begin={`${delay}ms`}
dur={`${dur}ms`}
/>
</Point>
);
})}
</g>
Expand Down
61 changes: 51 additions & 10 deletions packages/ez-react/src/components/shapes/LinePath.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import React, { FC, SVGAttributes, useMemo } from 'react';
import React, {
FC,
SVGAttributes,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { LineData, LineCurve } from 'eazychart-core/src/types';
import { defaultColor, generateLinePath } from 'eazychart-core/src';
import { useAnimation } from '@/lib/use-animation';
import { useChart } from '@/lib/use-chart';

export interface LinePathProps extends SVGAttributes<SVGPathElement> {
Expand All @@ -16,25 +22,60 @@ export const LinePath: FC<LinePathProps> = ({
beta,
stroke = defaultColor,
strokeWidth = 1,
fill = 'none',
strokeLinejoin = 'round',
strokeLinecap = 'round',
children,
...rest
}) => {
const { animationOptions } = useChart();
const {
animationOptions: { duration },
} = useChart();
const dataPath = useMemo(
() => generateLinePath(shapeData, curve, beta),
[shapeData, curve, beta]
);
const currentData =
useAnimation(dataPath, '', animationOptions, [curve]) || '';
const [pathLength, setPathLength] = useState(0);
const ref = useRef<SVGPathElement | null>(null);

useEffect(() => {
if (ref.current) {
setPathLength(ref.current.getTotalLength());
}
}, [dataPath]);

return (
<path
d={currentData}
ref={ref}
d={dataPath}
stroke={stroke}
strokeWidth={strokeWidth}
fill="none"
strokeLinejoin={'round'}
strokeLinecap={'round'}
fill={fill}
strokeLinejoin={strokeLinejoin}
strokeLinecap={strokeLinecap}
{...rest}
className="ez-line"
/>
>
{children ? (
children
) : (
<>
<animate
attributeName="stroke-dasharray"
attributeType="CSS"
from={pathLength}
to={pathLength}
dur={'0s'}
/>
<animate
attributeName="stroke-dashoffset"
attributeType="CSS"
from={pathLength}
to={0}
dur={`${duration}ms`}
/>
</>
)}
</path>
);
};
22 changes: 12 additions & 10 deletions packages/ez-react/src/components/shapes/Point.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { FC, MouseEventHandler, SVGAttributes } from 'react';
import { PointDatum } from 'eazychart-core/src/types';
import { defaultPointDatum, defaultPointRadius } from 'eazychart-core/src';
import { useAnimation } from '@/lib/use-animation';
import { useTooltip } from '@/components/addons/tooltip/use-tooltip';
import { useChart } from '@/lib/use-chart';

Expand All @@ -15,17 +14,14 @@ export const Point: FC<PointProps> = ({
fill,
stroke,
strokeWidth = 1,
children,
...rest
}) => {
const { showTooltip, hideTooltip, moveTooltip } = useTooltip();
const { animationOptions } = useChart();
const currentShapeDatum = useAnimation(
shapeDatum,
defaultPointDatum,
animationOptions
);

const { x, y, color } = currentShapeDatum;
const {
animationOptions: { duration },
} = useChart();
const { x, y, color } = shapeDatum;

const handleMouseOver: MouseEventHandler<SVGCircleElement> = (event) => {
showTooltip(shapeDatum, event as any as MouseEvent);
Expand Down Expand Up @@ -53,6 +49,12 @@ export const Point: FC<PointProps> = ({
strokeWidth={strokeWidth}
data-testid="ez-point"
className="ez-point"
/>
>
{children ? (
children
) : (
<animate attributeName="r" from={0} to={r} dur={`${duration}ms`} />
)}
</circle>
);
};
15 changes: 8 additions & 7 deletions packages/ez-react/src/lib/use-animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ export const useAnimation = <T extends Interpolables>(
const updateOnAnimate = useCallback(
(v) => {
if (v) {
setCurrentData(
typeof v === 'object'
? {
...v,
}
: v
);
const newVal = Array.isArray(v)
? [...v]
: typeof v === 'object'
? {
...v,
}
: v;
setCurrentData(newVal);
}
},
[setCurrentData]
Expand Down
2 changes: 1 addition & 1 deletion packages/ez-react/src/recipes/line/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const LineChart: FC<LineChartProps> = ({
},
animationOptions = {
easing: 'easeBack',
duration: 400,
duration: 2000,
delay: 0,
},
padding = {
Expand Down