Skip to content

Commit 4d880c9

Browse files
authored
Merge branch 'feat/vchart_editor' into chore/update-jest
2 parents 3d880df + 82105be commit 4d880c9

File tree

6 files changed

+564
-107
lines changed

6 files changed

+564
-107
lines changed

packages/vmind/__tests__/experiment/src/pages/ChartDialogueQA/qaPairRag.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,17 @@ export function QARag() {
156156
console.log('res: ', res.data);
157157
const { keyPathRes = [], qaRes = [], topKeys = [], dslRes, parentKeyPath, aliasKeyPath, error } = res?.data;
158158

159-
vchartSpecAtom.updateContext({
160-
spec: spec,
161-
appendSpec: {
162-
leafSpec: dslRes,
163-
parentKeyPath,
164-
aliasKeyPath
165-
}
166-
});
159+
vchartSpecAtom.updateContext(
160+
{
161+
spec: spec,
162+
appendSpec: {
163+
leafSpec: dslRes,
164+
parentKeyPath,
165+
aliasKeyPath
166+
}
167+
},
168+
true
169+
);
167170
const { spec: newSpec } = await vchartSpecAtom.run();
168171
console.log('newSpec', newSpec);
169172
setSpec(newSpec);
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import { merge } from '@visactor/vutils';
2+
import { mergeAppendSpec } from '../../../src/atom/VChartSpec/utils';
3+
4+
const spec = {
5+
type: 'bar',
6+
data: [
7+
{
8+
id: 'barData',
9+
values: [
10+
{
11+
type: 'Autocracies',
12+
year: '1930',
13+
value: 129
14+
},
15+
{
16+
type: 'Autocracies',
17+
year: '1940',
18+
value: 133
19+
},
20+
{
21+
type: 'Autocracies',
22+
year: '1950',
23+
value: 130
24+
},
25+
{
26+
type: 'Autocracies',
27+
year: '1960',
28+
value: 126
29+
},
30+
{
31+
type: 'Autocracies',
32+
year: '1970',
33+
value: 117
34+
},
35+
{
36+
type: 'Autocracies',
37+
year: '1980',
38+
value: 114
39+
},
40+
{
41+
type: 'Autocracies',
42+
year: '1990',
43+
value: 111
44+
},
45+
{
46+
type: 'Autocracies',
47+
year: '2000',
48+
value: 89
49+
},
50+
{
51+
type: 'Autocracies',
52+
year: '2010',
53+
value: 80
54+
},
55+
{
56+
type: 'Autocracies',
57+
year: '2018',
58+
value: 80
59+
},
60+
{
61+
type: 'Democracies',
62+
year: '1930',
63+
value: 22
64+
},
65+
{
66+
type: 'Democracies',
67+
year: '1940',
68+
value: 13
69+
},
70+
{
71+
type: 'Democracies',
72+
year: '1950',
73+
value: 25
74+
},
75+
{
76+
type: 'Democracies',
77+
year: '1960',
78+
value: 29
79+
},
80+
{
81+
type: 'Democracies',
82+
year: '1970',
83+
value: 38
84+
},
85+
{
86+
type: 'Democracies',
87+
year: '1980',
88+
value: 41
89+
},
90+
{
91+
type: 'Democracies',
92+
year: '1990',
93+
value: 57
94+
},
95+
{
96+
type: 'Democracies',
97+
year: '2000',
98+
value: 87
99+
},
100+
{
101+
type: 'Democracies',
102+
year: '2010',
103+
value: 98
104+
},
105+
{
106+
type: 'Democracies',
107+
year: '2018',
108+
value: 99
109+
}
110+
]
111+
}
112+
],
113+
xField: ['year', 'type'],
114+
yField: 'value',
115+
seriesField: 'type',
116+
legends: {
117+
visible: true,
118+
orient: 'top',
119+
position: 'start'
120+
}
121+
};
122+
123+
describe('mergeAppendSpec of barchart', () => {
124+
it('should reduce duplicated code', () => {
125+
const newSpec = mergeAppendSpec(merge({}, spec), {
126+
leafSpec: {
127+
scales: [
128+
{
129+
domain: [
130+
{
131+
fields: ['yourFieldName']
132+
}
133+
]
134+
}
135+
]
136+
},
137+
parentKeyPath: 'scales'
138+
});
139+
140+
expect(newSpec).toEqual(
141+
mergeAppendSpec(spec, {
142+
parentKeyPath: 'scales[0]',
143+
leafSpec: {
144+
domain: [
145+
{
146+
fields: ['yourFieldName']
147+
}
148+
]
149+
}
150+
})
151+
);
152+
});
153+
154+
it('should parse complicated path', () => {
155+
const append = {
156+
leafSpec: {
157+
'scales[0].domain[0].fields': 'yourFieldName'
158+
},
159+
parentKeyPath: 'scales[0].domain[0].fields'
160+
};
161+
162+
const { newSpec } = mergeAppendSpec(merge({}, spec), append);
163+
164+
expect(newSpec.scales).toEqual([
165+
{
166+
domain: [
167+
{
168+
fields: 'yourFieldName'
169+
}
170+
]
171+
}
172+
]);
173+
});
174+
});

packages/vmind/src/atom/VChartSpec/index.ts

Lines changed: 3 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,7 @@ import { AtomName } from '../../types/atom';
22
import type { BaseOptions } from '../type';
33
import { BaseAtom } from '../base';
44
import type { VChartSpecCtx } from '../../types';
5-
import { merge } from '@visactor/vutils';
6-
import { set } from '../../utils/set';
7-
8-
const parseRealPath = (path: string, aliasKeyPath: string, spec: any) => {
9-
let appendSpec: any;
10-
11-
if (aliasKeyPath) {
12-
const topKeyPath = aliasKeyPath.split('.')[0];
13-
const subPaths = path.split('.');
14-
15-
if (subPaths[0] === 'axes[0]' || subPaths[0] === 'axes') {
16-
if (topKeyPath === 'xAxis') {
17-
appendSpec = { orient: 'bottom', aliasName: topKeyPath };
18-
} else if (topKeyPath === 'yAxis') {
19-
appendSpec = { orient: 'left', aliasName: topKeyPath };
20-
} else if (topKeyPath === 'radiusAxis') {
21-
appendSpec = { orient: 'radius', aliasName: topKeyPath };
22-
} else if (topKeyPath === 'angleAxis') {
23-
appendSpec = { orient: 'angle', aliasName: topKeyPath };
24-
} else if (topKeyPath === 'leftAxis') {
25-
appendSpec = { orient: 'left', aliasName: topKeyPath };
26-
} else if (topKeyPath === 'rightAxis') {
27-
appendSpec = { orient: 'right', aliasName: topKeyPath };
28-
} else if (topKeyPath === 'topAxis') {
29-
appendSpec = { orient: 'top', aliasName: topKeyPath };
30-
} else if (topKeyPath === 'bottomAxis') {
31-
appendSpec = { orient: 'bottom', aliasName: topKeyPath };
32-
}
33-
34-
if (spec.axes) {
35-
let specifiedAxis = spec.axes.find((axis: any) => axis.aliasName === topKeyPath);
36-
37-
if (!specifiedAxis) {
38-
if (topKeyPath === 'xAxis') {
39-
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'bottom');
40-
} else if (topKeyPath === 'yAxis') {
41-
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'left');
42-
} else if (topKeyPath === 'radiusAxis') {
43-
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'radius');
44-
} else if (topKeyPath === 'angleAxis') {
45-
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'angle');
46-
} else if (topKeyPath === 'leftAxis') {
47-
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'left');
48-
} else if (topKeyPath === 'rightAxis') {
49-
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'right');
50-
} else if (topKeyPath === 'topAxis') {
51-
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'top');
52-
} else if (topKeyPath === 'bottomAxis') {
53-
specifiedAxis = spec.axes.find((axis: any) => axis.orient === 'bottom');
54-
}
55-
}
56-
57-
if (specifiedAxis) {
58-
const index = spec.axes.indexOf(specifiedAxis);
59-
60-
subPaths[0] = `axes[${index}]`;
61-
} else {
62-
subPaths[0] = `axes[${spec.axes.length}]`;
63-
}
64-
}
65-
}
66-
67-
return {
68-
appendPath: subPaths[0],
69-
appendSpec,
70-
path: subPaths.join('.')
71-
};
72-
}
73-
74-
return { path };
75-
};
5+
import { mergeAppendSpec } from './utils';
766

777
export class VChartSpec extends BaseAtom<VChartSpecCtx, BaseOptions> {
788
name = AtomName.VCHART_SPEC;
@@ -102,20 +32,10 @@ export class VChartSpec extends BaseAtom<VChartSpecCtx, BaseOptions> {
10232
if (!appendSpec || !appendSpec.leafSpec) {
10333
return this.context;
10434
}
105-
const { leafSpec, parentKeyPath = '', aliasKeyPath = '' } = appendSpec;
106-
let newSpec = merge({}, this.context.spec);
107-
108-
if (parentKeyPath) {
109-
const aliasResult = parseRealPath(parentKeyPath, aliasKeyPath, newSpec);
11035

111-
if (aliasResult.appendSpec && aliasResult.appendPath) {
112-
set(newSpec, aliasResult.appendPath, aliasResult.appendSpec);
113-
}
36+
const { newSpec, code } = mergeAppendSpec(this.context.spec, appendSpec);
11437

115-
set(newSpec, aliasResult.path ?? parentKeyPath, leafSpec);
116-
} else {
117-
newSpec = leafSpec;
118-
}
38+
this.context.appendCode = code;
11939
this.context.prevSpec = this.context.spec;
12040
this.context.spec = newSpec;
12141

0 commit comments

Comments
 (0)