Skip to content

Commit

Permalink
fix(animation): update non-transform attribute (close: #4953)
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed May 15, 2023
1 parent dec8699 commit ee62215
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
31 changes: 31 additions & 0 deletions __tests__/integration/api-chart-render-update-attribute.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { chartRenderUpdateAttributes as render } from '../plots/api/chart-render-update-attributes';
import { createNodeGCanvas } from './utils/createNodeGCanvas';
import { kebabCase } from './utils/kebabCase';
import './utils/useSnapshotMatchers';

describe('chart.options.autoFit', () => {
const dir = `${__dirname}/snapshots/api/${kebabCase(render.name)}`;
const canvas = createNodeGCanvas(800, 500);

it('chart({ autoFit: true }) should fit parent container', async () => {
const { finished, chart, refreshed, refreshed1, button, ...rest } = render({
canvas,
container: document.createElement('div'),
});
await finished;

// To lineDash
button.dispatchEvent(new CustomEvent('click'));
await refreshed;
await expect(canvas).toMatchCanvasSnapshot(dir, 'step0');

// Reset
button.dispatchEvent(new CustomEvent('click'));
await refreshed1;
await expect(canvas).toMatchCanvasSnapshot(dir, 'step1');
});

afterAll(() => {
canvas?.destroy();
});
});
67 changes: 67 additions & 0 deletions __tests__/plots/api/chart-render-update-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Chart } from '../../../src';

export function chartRenderUpdateAttributes(context) {
const { container, canvas } = context;

// button
const button = document.createElement('button');
button.innerText = 'Rerender';
container.appendChild(button);

// wrapperDiv
const wrapperDiv = document.createElement('div');
container.appendChild(wrapperDiv);

const chart = new Chart({
theme: 'classic',
container: wrapperDiv,
canvas,
});

const options = {
type: 'line',
data: {
type: 'fetch',
value: 'data/aapl.csv',
transform: [{ type: 'slice', start: 0, end: 10 }],
},
encode: {
x: 'date',
y: 'close',
},
};

chart.options(options);

const finished = chart.render();

let resolve;
let resolve1;
const refreshed = new Promise((r) => (resolve = r));
const refreshed1 = new Promise((r) => (resolve1 = r));

let lineDash = false;
button.onclick = () => {
if (lineDash) {
chart.options({
...options,
style: {
lineDash: null,
},
});
lineDash = false;
chart.render().then(resolve1);
} else {
chart.options({
...options,
style: {
lineDash: [5, 4],
},
});
lineDash = true;
chart.render().then(resolve);
}
};

return { chart, button, finished, refreshed, refreshed1 };
}
1 change: 1 addition & 0 deletions __tests__/plots/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export { chartOnFocusContext } from './chart-on-focus-context';
export { chartEmitItemTooltip } from './chart-emit-item-tooltip';
export { chartEmitSeriesTooltip } from './chart-emit-series-tooltip';
export { chartEmitPieTooltip } from './chart-emit-pie-tooltip';
export { chartRenderUpdateAttributes } from './chart-render-update-attributes';
5 changes: 5 additions & 0 deletions src/animation/morphing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Path,
} from '@antv/g';
import { AnimationComponent as AC } from '../runtime';
import { copyAttributes } from '../utils/helper';
import { Animation } from './types';
import { attributeKeys, attributeOf, effectTiming } from './utils';

Expand Down Expand Up @@ -181,6 +182,10 @@ function oneToOne(
];
const animation = pathShape.animate(keyframes, timeEffect);

animation.onfinish = () => {
copyAttributes(pathShape, to);
};

// Remove transform because it already applied in path
// converted by convertToPath.
// @todo Remove this scale(1, 1)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DisplayObject } from '@antv/g';
import { lowerFirst, startsWith, upperFirst } from '@antv/util';
import { lowerFirst, upperFirst } from '@antv/util';

export function identity<T>(x: T): T {
return x;
Expand Down

0 comments on commit ee62215

Please sign in to comment.