Skip to content

Commit

Permalink
fix: chart.options changeData problem (#4936)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepper-nice committed May 5, 2023
1 parent e8ac19a commit 99bdec2
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
26 changes: 26 additions & 0 deletions __tests__/integration/api-chart-options-change-data.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { chartOptionsChangeData as render } from '../plots/api/chart-options-change-data';
import { createNodeGCanvas } from './utils/createNodeGCanvas';
import { sleep } from './utils/sleep';
import './utils/useSnapshotMatchers';
import './utils/useCustomFetch';

describe('chart.options.changeData', () => {
const canvas = createNodeGCanvas(800, 500);

it('chart.options.changeData should rerender expected chart', async () => {
const { finished, button, chart } = render({
canvas,
container: document.createElement('div'),
});
await finished;
button.dispatchEvent(new CustomEvent('click'));
await new Promise<void>((resolve) => chart.on('afterrender', resolve));
const dir = `${__dirname}/snapshots/api`;
await sleep(20);
await expect(canvas).toMatchCanvasSnapshot(dir, render.name);
});

afterAll(() => {
canvas?.destroy();
});
});
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions __tests__/plots/api/chart-options-change-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Chart } from '../../../src';

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

const button = document.createElement('button');
button.innerText = 'Update Data';
button.style.display = 'block';
container.appendChild(button);

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

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

chart.options({
type: 'interval',
coordinate: { type: 'theta' },
transform: [{ type: 'stackY' }],
data: [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
],
encode: {
y: 'sold',
color: 'genre',
},
animate: {
enter: {
type: 'waveIn',
},
},
});

const finished = chart.render();

button.onclick = () => {
chart.changeData([
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
]);
};

return { chart, finished, button };
}
1 change: 1 addition & 0 deletions __tests__/plots/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export { chartChangeSizePolar } from './chart-change-size-polar';
export { chartChangeDataFacet } from './chart-change-data-facet';
export { chartRenderClearAnimation } from './chart-render-clear-animation';
export { chartOnBrushFilter } from './chart-on-brush-filter';
export { chartOptionsChangeData } from './chart-options-change-data';
7 changes: 7 additions & 0 deletions src/api/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ export class Chart extends View<ChartOptions> {
return this;
}

// @todo Remove it when implement updateRoot.
changeData(data: any): Promise<Chart> {
// Update options data.
this.options({ data });
return super.changeData(data);
}

getContainer(): HTMLElement {
return this._container;
}
Expand Down

0 comments on commit 99bdec2

Please sign in to comment.