Skip to content

Commit

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

export function gaugeDefault(): G2Spec {
return {
type: 'gauge',
type: 'view',
data: {
value: {
target: 120,
total: 400,
name: 'score',
},
},
children: [{ type: 'gauge' }],
};
}
20 changes: 14 additions & 6 deletions src/composition/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { mergeData } from './utils';

export type ViewOptions = Omit<ViewComposition, 'type'>;

function fallback(markValue, viewValue) {
if (markValue === false || markValue === null) return false;
if (markValue === undefined) return viewValue;
const object = typeof markValue === 'boolean' ? {} : markValue;
if (typeof viewValue === 'object') return deepMix({}, viewValue, object);
return object;
}

/**
* @todo Propagate more options to children.(e.g. filter)
* @todo Propagate encode options to children. This is useful for Matrix composition.
Expand All @@ -18,17 +26,17 @@ export const View: CC<ViewOptions> = () => {
data: viewData,
scale: viewScale = {},
axis: viewAxis = {},
legend: viewLegend = {},
encode: viewEncode = {},
legend: viewLegend,
encode: viewEncode,
transform: viewTransform = [],
...rest
} = restOptions;
const marks = children.map(
({
data,
scale = {},
axis = {},
legend = {},
axis,
legend,
encode = {},
transform = [],
...rest
Expand All @@ -37,8 +45,8 @@ export const View: CC<ViewOptions> = () => {
scale: deepMix({}, viewScale, scale),
encode: deepMix({}, viewEncode, encode),
transform: [...viewTransform, ...transform],
axis: axis && viewAxis ? deepMix({}, viewAxis, axis) : false,
legend: legend && viewLegend ? deepMix({}, viewLegend, legend) : false,
axis: fallback(axis, viewAxis),
legend: fallback(legend, viewLegend),
...rest,
}),
);
Expand Down
1 change: 1 addition & 0 deletions src/mark/gauge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export const Gauge: CC<GaugeOptions> = (options) => {
// pointer + pin
const indicatorStyle = filterPrefixObject(style, ['pointer', 'pin']);

console.log(resOptions);
return [
deepMix({}, DEFAULT_OPTIONS, {
type: 'interval',
Expand Down
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 6541f51

Please sign in to comment.