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

Feat/vchart editor merge spec #172

Merged
merged 3 commits into from
Dec 30, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,17 @@ export function QARag() {
console.log('res: ', res.data);
const { keyPathRes = [], qaRes = [], topKeys = [], dslRes, parentKeyPath, aliasKeyPath, error } = res.data;

vchartSpecAtom.updateContext({
spec: spec,
appendSpec: {
leafSpec: dslRes,
parentKeyPath,
aliasKeyPath
}
});
vchartSpecAtom.updateContext(
{
spec: spec,
appendSpec: {
leafSpec: dslRes,
parentKeyPath,
aliasKeyPath
}
},
true
);
const { spec: newSpec } = await vchartSpecAtom.run();
console.log('newSpec', newSpec);
setSpec(newSpec);
Expand Down
174 changes: 174 additions & 0 deletions packages/vmind/__tests__/unit/vchartSpec/bar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { merge } from '@visactor/vutils';
import { mergeAppendSpec } from '../../../src/atom/VChartSpec/utils';

const spec = {
type: 'bar',
data: [
{
id: 'barData',
values: [
{
type: 'Autocracies',
year: '1930',
value: 129
},
{
type: 'Autocracies',
year: '1940',
value: 133
},
{
type: 'Autocracies',
year: '1950',
value: 130
},
{
type: 'Autocracies',
year: '1960',
value: 126
},
{
type: 'Autocracies',
year: '1970',
value: 117
},
{
type: 'Autocracies',
year: '1980',
value: 114
},
{
type: 'Autocracies',
year: '1990',
value: 111
},
{
type: 'Autocracies',
year: '2000',
value: 89
},
{
type: 'Autocracies',
year: '2010',
value: 80
},
{
type: 'Autocracies',
year: '2018',
value: 80
},
{
type: 'Democracies',
year: '1930',
value: 22
},
{
type: 'Democracies',
year: '1940',
value: 13
},
{
type: 'Democracies',
year: '1950',
value: 25
},
{
type: 'Democracies',
year: '1960',
value: 29
},
{
type: 'Democracies',
year: '1970',
value: 38
},
{
type: 'Democracies',
year: '1980',
value: 41
},
{
type: 'Democracies',
year: '1990',
value: 57
},
{
type: 'Democracies',
year: '2000',
value: 87
},
{
type: 'Democracies',
year: '2010',
value: 98
},
{
type: 'Democracies',
year: '2018',
value: 99
}
]
}
],
xField: ['year', 'type'],
yField: 'value',
seriesField: 'type',
legends: {
visible: true,
orient: 'top',
position: 'start'
}
};

describe('mergeAppendSpec of barchart', () => {
it('should reduce duplicated code', () => {
const newSpec = mergeAppendSpec(merge({}, spec), {
leafSpec: {
scales: [
{
domain: [
{
fields: ['yourFieldName']
}
]
}
]
},
parentKeyPath: 'scales'
});

expect(newSpec).toEqual(
mergeAppendSpec(spec, {
parentKeyPath: 'scales[0]',
leafSpec: {
domain: [
{
fields: ['yourFieldName']
}
]
}
})
);
});

it('should parse complicated path', () => {
const append = {
leafSpec: {
'scales[0].domain[0].fields': 'yourFieldName'
},
parentKeyPath: 'scales[0].domain[0].fields'
};

const { newSpec } = mergeAppendSpec(merge({}, spec), append);

expect(newSpec.scales).toEqual([
{
domain: [
{
fields: 'yourFieldName'
}
]
}
]);
});
});
2 changes: 1 addition & 1 deletion packages/vmind/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
preset: 'ts-jest',
runner: 'jest-electron/runner',
testEnvironment: 'jest-electron/environment',
testMatch: ['<rootDir>/__tests__/unit/*.test.(js|ts)'],
testMatch: ['<rootDir>/__tests__/unit/**/*.test.(js|ts)'],
silent: true,
globals: {
'ts-jest': {
Expand Down
86 changes: 3 additions & 83 deletions packages/vmind/src/atom/VChartSpec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,7 @@ import { AtomName } from '../../types/atom';
import type { BaseOptions } from '../type';
import { BaseAtom } from '../base';
import type { VChartSpecCtx } from '../../types';
import { merge } from '@visactor/vutils';
import { set } from '../../utils/set';

const parseRealPath = (path: string, aliasKeyPath: string, spec: any) => {
let appendSpec: any;

if (aliasKeyPath) {
const topKeyPath = aliasKeyPath.split('.')[0];
const subPaths = path.split('.');

if (subPaths[0] === 'axes[0]' || subPaths[0] === 'axes') {
if (topKeyPath === 'xAxis') {
appendSpec = { orient: 'bottom', aliasName: topKeyPath };
} else if (topKeyPath === 'yAxis') {
appendSpec = { orient: 'left', aliasName: topKeyPath };
} else if (topKeyPath === 'radiusAxis') {
appendSpec = { orient: 'radius', aliasName: topKeyPath };
} else if (topKeyPath === 'angleAxis') {
appendSpec = { orient: 'angle', aliasName: topKeyPath };
} else if (topKeyPath === 'leftAxis') {
appendSpec = { orient: 'left', aliasName: topKeyPath };
} else if (topKeyPath === 'rightAxis') {
appendSpec = { orient: 'right', aliasName: topKeyPath };
} else if (topKeyPath === 'topAxis') {
appendSpec = { orient: 'top', aliasName: topKeyPath };
} else if (topKeyPath === 'bottomAxis') {
appendSpec = { orient: 'bottom', aliasName: topKeyPath };
}

if (spec.axes) {
let specifiedAxis = spec.axes.find((axis: any) => axis.aliasName === topKeyPath);

if (!specifiedAxis) {
if (topKeyPath === 'xAxis') {
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'bottom');
} else if (topKeyPath === 'yAxis') {
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'left');
} else if (topKeyPath === 'radiusAxis') {
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'radius');
} else if (topKeyPath === 'angleAxis') {
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'angle');
} else if (topKeyPath === 'leftAxis') {
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'left');
} else if (topKeyPath === 'rightAxis') {
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'right');
} else if (topKeyPath === 'topAxis') {
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'top');
} else if (topKeyPath === 'bottomAxis') {
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'bottom');
}
}

if (specifiedAxis) {
const index = spec.axes.indexOf(specifiedAxis);

subPaths[0] = `axes[${index}]`;
} else {
subPaths[0] = `axes[${spec.axes.length}]`;
}
}
}

return {
appendPath: subPaths[0],
appendSpec,
path: subPaths.join('.')
};
}

return { path };
};
import { mergeAppendSpec } from './utils';

export class VChartSpec extends BaseAtom<VChartSpecCtx, BaseOptions> {
name = AtomName.VCHART_SPEC;
Expand Down Expand Up @@ -102,20 +32,10 @@ export class VChartSpec extends BaseAtom<VChartSpecCtx, BaseOptions> {
if (!appendSpec || !appendSpec.leafSpec) {
return this.context;
}
const { leafSpec, parentKeyPath = '', aliasKeyPath = '' } = appendSpec;
let newSpec = merge({}, this.context.spec);

if (parentKeyPath) {
const aliasResult = parseRealPath(parentKeyPath, aliasKeyPath, newSpec);

if (aliasResult.appendSpec && aliasResult.appendPath) {
set(newSpec, aliasResult.appendPath, aliasResult.appendSpec);
}
const { newSpec, code } = mergeAppendSpec(this.context.spec, appendSpec);

set(newSpec, aliasResult.path ?? parentKeyPath, leafSpec);
} else {
newSpec = leafSpec;
}
this.context.appendCode = code;
this.context.prevSpec = this.context.spec;
this.context.spec = newSpec;

Expand Down
Loading
Loading