Skip to content

Commit

Permalink
fix: vchart should not throw error when the values of series data is …
Browse files Browse the repository at this point in the history
…empty, fix #3082
  • Loading branch information
xile611 committed Aug 20, 2024
1 parent df6ff91 commit c629cc0
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 29 deletions.
93 changes: 92 additions & 1 deletion packages/vchart/__tests__/unit/core/update-spec.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IBarChartSpec } from '../../../src';
import type { IBarChartSpec, ICommonChartSpec } from '../../../src';
import { default as VChart } from '../../../src';
import { series } from '../../../src/theme/builtin/common/series';
import { createDiv, removeDom } from '../../util/dom';
Expand Down Expand Up @@ -1106,6 +1106,97 @@ describe('vchart updateSpec of same spec', () => {
reTransformSpec: false
});
});

it('should not throw error when data is empty in series', () => {
const spec: any = {
type: 'common',
seriesField: 'color',
data: [
{
id: 'id0',
values: [
{ x: '周一', type: '早餐', y: 15 },
{ x: '周一', type: '午餐', y: 25 },
{ x: '周二', type: '早餐', y: 12 },
{ x: '周二', type: '午餐', y: 30 },
{ x: '周三', type: '早餐', y: 15 },
{ x: '周三', type: '午餐', y: 24 },
{ x: '周四', type: '早餐', y: 10 },
{ x: '周四', type: '午餐', y: 25 },
{ x: '周五', type: '早餐', y: 13 },
{ x: '周五', type: '午餐', y: 20 },
{ x: '周六', type: '早餐', y: 10 },
{ x: '周六', type: '午餐', y: 22 },
{ x: '周日', type: '早餐', y: 12 },
{ x: '周日', type: '午餐', y: 19 }
]
},
{
id: 'id1',
values: [
{ x: '周一', type: '饮料', y: 22 },
{ x: '周二', type: '饮料', y: 43 },
{ x: '周三', type: '饮料', y: 33 },
{ x: '周四', type: '饮料', y: 22 },
{ x: '周五', type: '饮料', y: 10 },
{ x: '周六', type: '饮料', y: 30 },
{ x: '周日', type: '饮料', y: 50 }
]
}
],
series: [
{
type: 'bar',
id: 'bar',
data: {
id: 'id0'
},
label: { visible: true },
seriesField: 'type',

xField: ['x', 'type'],
yField: 'y'
},
{
type: 'line',
id: 'line',
data: {
id: 'id1'
},
label: { visible: true },
seriesField: 'type',
xField: 'x',
yField: 'y',
stack: false
}
],
axes: [
{ orient: 'left', seriesIndex: [0] },
{ orient: 'right', seriesId: ['line'], grid: { visible: false } },
{ orient: 'bottom', label: { visible: true }, type: 'band' }
],
legends: {
visible: true,
orient: 'bottom'
}
};

vchart = new VChart(spec, {
dom
});
vchart.renderSync();
const updateRes = (vchart as any)._updateSpec(spec, false);

expect(updateRes).toEqual({
change: false,
changeTheme: false,
reCompile: false,
reMake: false,
reRender: true,
reSize: false,
reTransformSpec: false
});
});
});

describe('vchart updateSpec of different about label', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/vchart/src/data/transforms/add-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface IAddVChartPropertyOpt {

export const addVChartProperty = (data: Array<any>, op: IAddVChartPropertyOpt) => {
const context = op.beforeCall();
data.forEach((d, i) => op.call(d, i, context));
data && data.forEach((d, i) => op.call(d, i, context));

if (context.keyMap) {
context.keyMap.clear();
Expand Down
2 changes: 1 addition & 1 deletion packages/vchart/src/data/transforms/copy-data-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function copyOneDataView(d: DataView, deep = false) {
return cloneDeep(d.latestData);
}

return d.latestData.slice();
return d.latestData && d.latestData.slice();
}

/**
Expand Down
11 changes: 6 additions & 5 deletions packages/vchart/src/data/transforms/dimension-statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ export const dimensionStatisticsOfSimpleData = (
let allValid = true;
fValues.length = 0;

latestData.forEach((d: Datum) => {
if (d) {
fValues.push(d[key]);
}
});
latestData &&
latestData.forEach((d: Datum) => {
if (d) {
fValues.push(d[key]);
}
});
const len = fValues.length;

if (isNumberField) {
Expand Down
43 changes: 22 additions & 21 deletions packages/vchart/src/data/transforms/stack-split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,32 @@ export const stackSplit = (data: Array<DataView>, op: IStackOption) => {
let nextNode: ISeriesStackDataNode;
let leaf: ISeriesStackDataLeaf;
data.forEach(dv => {
dv.latestData.forEach((d: Datum) => {
temp = result;
for (let i = 0; i < fields.length; i++) {
const f = fields[i];
const fV = d[f];
if (isNil(fV)) {
break;
}
temp.groupField = f;
if (!temp.nodes[fV]) {
dv.latestData &&
dv.latestData.forEach((d: Datum) => {
temp = result;
for (let i = 0; i < fields.length; i++) {
const f = fields[i];
const fV = d[f];
if (isNil(fV)) {
break;
}
temp.groupField = f;
if (!temp.nodes[fV]) {
if (i === lastFieldIndex) {
temp.nodes[fV] = { values: [] };
} else {
nextNode = { nodes: {} };
temp.nodes[fV] = nextNode;
}
}
if (i === lastFieldIndex) {
temp.nodes[fV] = { values: [] };
leaf = temp.nodes[fV] as ISeriesStackDataLeaf;
leaf.values.push(d);
} else {
nextNode = { nodes: {} };
temp.nodes[fV] = nextNode;
temp = temp.nodes[fV] as ISeriesStackDataNode;
}
}
if (i === lastFieldIndex) {
leaf = temp.nodes[fV] as ISeriesStackDataLeaf;
leaf.values.push(d);
} else {
temp = temp.nodes[fV] as ISeriesStackDataNode;
}
}
});
});
});
return result;
};

0 comments on commit c629cc0

Please sign in to comment.