Skip to content

Commit

Permalink
fix(runtime): process data (#5494)
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini authored Aug 31, 2023
1 parent 81a2912 commit b851ca3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 3 additions & 1 deletion __tests__/plots/static/gauge-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { G2Spec } from '../../../src';

export function gaugeDefault(): G2Spec {
return {
type: 'gauge',
type: 'view',
legend: false,
data: {
value: {
target: 120,
total: 400,
name: 'score',
},
},
children: [{ type: 'gauge' }],
};
}
10 changes: 9 additions & 1 deletion src/runtime/plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,16 @@ async function transformMarks(
const { composite = true } = props;
if (!composite) flattenMarks.push(mark);
else {
// Unwrap data from { value: data } to data,
// then the composite mark can process the normalized data.
const { data } = mark;
const newMark = {
...mark,
data: data ? (Array.isArray(data) ? data : data.value) : data,
};

// Convert composite mark to marks.
const marks = await useMark(mark, context);
const marks = await useMark(newMark, context);
const M = Array.isArray(marks) ? marks : [marks];
discovered.unshift(...M.map((d, i) => ({ ...d, key: `${key}-${i}` })));
}
Expand Down
13 changes: 12 additions & 1 deletion src/runtime/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,20 @@ export async function applyDataTransform(
const transform = [connector, ...T];
const transformFunctions = transform.map(useData);
const transformedData = await composeAsync(transformFunctions)(data);

// Maintain the consistency of shape between input and output data.
// If the shape of raw data is like { value: any }
// and the returned transformedData is Object,
// returns the wrapped data: { value: transformedData },
// otherwise returns the processed tabular data.
const newData =
data && !Array.isArray(data) && !Array.isArray(transformedData)
? { value: transformedData }
: transformedData;

return [
Array.isArray(transformedData) ? indexOf(transformedData) : [],
{ ...mark, data: transformedData },
{ ...mark, data: newData },
];
}

Expand Down

0 comments on commit b851ca3

Please sign in to comment.