Skip to content
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

fix(data): render empty data #5098

Merged
merged 1 commit into from
May 26, 2023
Merged
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
25 changes: 25 additions & 0 deletions __tests__/integration/api-chart-change-data-empty.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { chartChangeDataEmpty as render } from '../plots/api/chart-change-data-empty';
import { createNodeGCanvas } from './utils/createNodeGCanvas';
import { sleep } from './utils/sleep';
import { kebabCase } from './utils/kebabCase';
import './utils/useSnapshotMatchers';
import './utils/useCustomFetch';

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 } = render({
canvas,
container: document.createElement('div'),
});
await finished;
await sleep(20);
await expect(canvas).toMatchCanvasSnapshot(dir, 'step0');
});

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

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

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

chart.options({
theme: 'classic',
type: 'line',
clip: true,
data: {
type: 'fetch',
value: 'data/aapl.csv',
},
encode: {
x: 'date',
y: 'close',
},
});

const finished = chart.render().then((chart) => chart.changeData([]));

return { chart, finished };
}
1 change: 1 addition & 0 deletions __tests__/plots/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export { chartRenderUpdateAttributes } from './chart-render-update-attributes';
export { chartRenderUpdateNonAnimation } from './chart-render-update-non-animation';
export { chartEmitBrushHighlightX } from './chart-emit-brush-highlight-x';
export { chartRenderBrushEnd } from './chart-render-brush-end';
export { chartChangeDataEmpty } from './chart-change-data-empty';
2 changes: 1 addition & 1 deletion src/mark/area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const Area: MC<AreaOptions> = () => {

// Group data by series field.
const series = S ? Array.from(group(index, (i) => S[i]).values()) : [index];
const I = series.map((group) => group[0]);
const I = series.map((group) => group[0]).filter((i) => i !== undefined);

// A group of data corresponds to one area.
const P = Array.from(series, (SI) => {
Expand Down
2 changes: 1 addition & 1 deletion src/mark/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const line: Mark = (index, scale, value, coordinate) => {
// Group data into series.
// There is only one series without specified series encode.
const series = S ? Array.from(group(index, (i) => S[i]).values()) : [index];
const I = series.map((group) => group[0]);
const I = series.map((group) => group[0]).filter((i) => i !== undefined);

// A group of data corresponds to one line.
const xoffset = (x?.getBandWidth?.() || 0) / 2;
Expand Down
23 changes: 14 additions & 9 deletions src/runtime/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,28 +190,23 @@ function inferScaleDomain(
): Primitive[] {
const { domain } = options;
if (domain !== undefined) return domain;
const { domainMax, domainMin } = options;
switch (type) {
case 'linear':
case 'time':
case 'log':
case 'pow':
case 'sqrt':
case 'quantize':
case 'threshold': {
const [d0, d1] = inferDomainQ(values, options);
return [domainMin ?? d0, domainMax ?? d1];
}
case 'threshold':
return maybeMinMax(inferDomainQ(values, options), options);
case 'band':
case 'ordinal':
case 'point':
return inferDomainC(values);
case 'quantile':
return inferDomainO(values);
case 'sequential': {
const [d0, d1] = inferDomainS(values);
return [domainMin ?? d0, domainMax ?? d1];
}
case 'sequential':
return maybeMinMax(inferDomainS(values), options);
default:
return [];
}
Expand Down Expand Up @@ -403,6 +398,16 @@ function asQuantitativeType(name: string, range: Primitive[]) {
return range ? 'linear' : 'sequential';
}

function maybeMinMax(
domain: Primitive[],
options: G2ScaleOptions,
): Primitive[] {
if (domain.length === 0) return domain;
const { domainMin, domainMax } = options;
const [d0, d1] = domain;
return [domainMin ?? d0, domainMax ?? d1];
}

function inferDomainQ(values: Primitive[][], options: G2ScaleOptions) {
const { zero = false } = options;
let min = Infinity;
Expand Down