Skip to content

Commit

Permalink
fix(composition): fix composition lost chart value options (#5995)
Browse files Browse the repository at this point in the history
* fix(composition): fix composition lost chart value options

* fix(composition): cr

---------

Co-authored-by: liwenbo <liwenbo@kanzhun.com>
  • Loading branch information
li1615882553 and liwenbo committed Jan 3, 2024
1 parent ae4ab58 commit 7792a37
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
21 changes: 21 additions & 0 deletions __tests__/unit/api/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,27 @@ describe('chart api and options', () => {
});
});

it('chart.options({...}) should update composition.', () => {
const chart = new Chart({
canvas: createNodeGCanvas(640, 480),
autoFit: true,
padding: 10,
});

chart.options({
type: 'spaceFlex',
padding: 20,
children: [{ type: 'interval', data: [1, 2, 3] }],
});

expect(chart.options()).toEqual({
type: 'spaceFlex',
autoFit: true,
padding: 20,
children: [{ type: 'interval', data: [1, 2, 3] }],
});
});

it('chart.options({...}) should update node with same height and index.', () => {
const chart = new Chart({
canvas: createNodeGCanvas(640, 480),
Expand Down
45 changes: 35 additions & 10 deletions src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,44 @@ function isMark(
return new Set(Object.keys(mark)).has(type);
}

function isComposition(
type: string | ((...args: any[]) => any),
composition: Record<string, new () => Node>,
): boolean {
return (
typeof type !== 'function' && new Set(Object.keys(composition)).has(type)
);
}

function normalizeRootOptions(
node: Node,
options: G2ViewTree,
previousType: string,
marks: Record<string, new () => Node>,
composition: Record<string, new () => Node>,
) {
const { type: oldType } = node;
const { type = previousType || oldType } = options;
if (type === 'view') return options;
if (!isMark(type, marks)) return options;
const view = { type: 'view' };
const mark = { ...options };
for (const key of VIEW_KEYS) {
if (mark[key] !== undefined) {
view[key] = mark[key];
delete mark[key];
if (isComposition(type, composition)) {
for (const key of VIEW_KEYS) {
if (node.attr(key) !== undefined && options[key] === undefined) {
options[key] = node.attr(key);
}
}
return options;
}
if (isMark(type, marks)) {
const view = { type: 'view' };
const mark = { ...options };
for (const key of VIEW_KEYS) {
if (mark[key] !== undefined) {
view[key] = mark[key];
delete mark[key];
}
}
return { ...view, children: [mark] };
}
return { ...view, children: [mark] };
return options;
}

function typeCtor(
Expand Down Expand Up @@ -232,7 +251,13 @@ export function updateRoot(
mark: Record<string, new () => Node>,
composition: Record<string, new () => Node>,
) {
const rootOptions = normalizeRootOptions(node, options, definedType, mark);
const rootOptions = normalizeRootOptions(
node,
options,
definedType,
mark,
composition,
);
const discovered: [Node, Node, G2ViewTree][] = [[null, node, rootOptions]];
while (discovered.length) {
const [parent, oldNode, newNode] = discovered.shift();
Expand Down

0 comments on commit 7792a37

Please sign in to comment.