Skip to content

Commit

Permalink
fix: fix merge spec of vchart
Browse files Browse the repository at this point in the history
  • Loading branch information
xile611 committed Dec 31, 2024
1 parent 65a13a8 commit 36ade46
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,52 +361,56 @@ export function QARag() {
<div className="recall-content">
<div className="one-content">
<div>QA Recall:</div>
{qaResult.map((item, index) => (
<Card key={index} className="qa-card">
<div className="qa-div">
<span className="title">Score:</span>
<span>{item.scores.toFixed(2)}</span>
</div>
<div className="qa-div">
<div className="title">Question:</div>
<span>{item.question}</span>
</div>
<div className="qa-div">
<div className="title">Explanation:</div>
<span>{item.explanation}</span>
</div>
<div className="qa-div">
<div className="title">Answer:</div>
<span>{item.answer}</span>
</div>
</Card>
))}
{qaResult && qaResult.length
? qaResult.map((item, index) => (
<Card key={index} className="qa-card">
<div className="qa-div">
<span className="title">Score:</span>
<span>{item.scores.toFixed(2)}</span>
</div>
<div className="qa-div">
<div className="title">Question:</div>
<span>{item.question}</span>
</div>
<div className="qa-div">
<div className="title">Explanation:</div>
<span>{item.explanation}</span>
</div>
<div className="qa-div">
<div className="title">Answer:</div>
<span>{`${item.answer}`}</span>
</div>
</Card>
))
: null}
</div>

<div className="one-content">
<div>KeyPath Recall:</div>
{keyPathResult.map((item, index) => (
<Card key={index} className="qa-card">
<div className="qa-div">
<span className="title">Score:</span>
<span>{item.scores.toFixed(2)}</span>
</div>
<div className="qa-div">
<div className="title">content:</div>
<span>{item.text}</span>
</div>
{ragOption.type === 'qa' && (
<div className="qa-div">
<div className="title">Answer:</div>
<span>{item.answer}</span>
</div>
)}
<div className="qa-div">
<div className="title">key:</div>
<span>{item.key}</span>
</div>
</Card>
))}
{keyPathResult && keyPathResult.length
? keyPathResult.map((item, index) => (
<Card key={index} className="qa-card">
<div className="qa-div">
<span className="title">Score:</span>
<span>{item.scores.toFixed(2)}</span>
</div>
<div className="qa-div">
<div className="title">content:</div>
<span>{item.text}</span>
</div>
{ragOption.type === 'qa' ? (
<div className="qa-div">
<div className="title">Answer:</div>
<span>{`${item.answer}`}</span>
</div>
) : null}
<div className="qa-div">
<div className="title">key:</div>
<span>{item.key}</span>
</div>
</Card>
))
: null}
</div>
</div>
</div>
Expand Down
31 changes: 31 additions & 0 deletions packages/vmind/__tests__/unit/vchartSpec/bar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,35 @@ describe('mergeAppendSpec of barchart', () => {
const { newSpec } = mergeAppendSpec(merge({}, spec), append);
expect(newSpec.tooltip).toEqual(append.leafSpec);
});

it('should not create legends array when only one legend', () => {
const { newSpec } = mergeAppendSpec(merge({}, spec), {
parentKeyPath: 'legends[0]',
aliasKeyPath: '',
leafSpec: {
'legends[0]': {
orient: 'left'
}
}
});
expect(newSpec.legends).toEqual({
position: 'start',
visible: true,
orient: 'left'
});
const { newSpec: newSpec2 } = mergeAppendSpec(merge({}, spec), {
parentKeyPath: 'legends',
aliasKeyPath: '',
leafSpec: {
'legends[0]': {
orient: 'left'
}
}
});
expect(newSpec2.legends).toEqual({
position: 'start',
visible: true,
orient: 'left'
});
});
});
84 changes: 58 additions & 26 deletions packages/vmind/src/atom/VChartSpec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,41 +301,75 @@ export const aliasByComponentType: Record<
}
};

const ALIAS_NAME_KEY = '_alias_name';
export const removeUnneedArrayInSpec = (leafSpec: any, compKey: string, parentKeyPath: string) => {
return leafSpec[compKey]
? isArray(leafSpec[compKey])
? leafSpec[compKey][0]
: leafSpec[compKey]
: parentKeyPath in leafSpec
? leafSpec[parentKeyPath]
: `${compKey}[0]` in leafSpec
? leafSpec[`${compKey}[0]`]
: leafSpec;
};

export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, chartSpec: any) => {
if (!aliasKeyPath) {
return { parentKeyPath };
}
export const addArrayInSpec = (leafSpec: any, compKey: string, parentKeyPath: string) => {
return leafSpec[compKey]
? isArray(leafSpec[compKey])
? leafSpec[compKey][0]
: leafSpec[compKey]
: parentKeyPath in leafSpec
? leafSpec[parentKeyPath]
: leafSpec;
};

const topKeyPath = aliasKeyPath.split('.')[0];
const ALIAS_NAME_KEY = '_alias_name';

export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, chartSpec: any, leafSpec: any) => {
const aliasName = aliasKeyPath ? aliasKeyPath.split('.')[0] : null;
const subPaths = parentKeyPath.split('.');
const compKey = subPaths[0].replace(/\[\d\]/, '');

if (!aliasByComponentType[compKey]) {
return { parentKeyPath };
}
const aliasOptions = aliasByComponentType[compKey];
const isValidAlias = !!aliasOptions.aliasMap[topKeyPath];
const isValidAlias = aliasOptions && aliasName && !!aliasOptions.aliasMap[aliasName];

if (!isValidAlias) {
if (chartSpec[compKey]) {
// 组件配置没有固定为数组类型的时候
if (!aliasOptions.isArray && !isArray(chartSpec[compKey])) {
subPaths[0] = compKey;
if (isArray(chartSpec[compKey])) {
if (subPaths[0] === compKey) {
subPaths[0] = `${compKey}[0]`;
}
return {
path: subPaths.join('.')
parentKeyPath: subPaths.join('.'),
leafSpec: addArrayInSpec(leafSpec, compKey, parentKeyPath)
};
}

if (subPaths[0] !== compKey) {
subPaths[0] = compKey;
}
return {
parentKeyPath: subPaths.join('.'),
leafSpec: removeUnneedArrayInSpec(leafSpec, compKey, parentKeyPath)
};
} else if (aliasOptions && aliasOptions.isArray) {
// 像系列这种只支持数组类型的,需要扩展成数组
if (subPaths[0] === compKey) {
subPaths[0] = `${compKey}[0]`;
}
return {
parentKeyPath: subPaths.join('.'),
leafSpec: addArrayInSpec(leafSpec, compKey, parentKeyPath)
};
}

return { parentKeyPath };
}
const appendSpec = { ...aliasOptions.aliasMap[topKeyPath].appendSpec, [ALIAS_NAME_KEY]: topKeyPath };
const appendSpec = { ...aliasOptions.aliasMap[aliasName].appendSpec, [ALIAS_NAME_KEY]: aliasName };

if (chartSpec[compKey]) {
const isMatchComp = (comp: any) => {
const aliasEntry = aliasOptions.aliasMap[topKeyPath];
const aliasEntry = aliasOptions.aliasMap[aliasName];

if (aliasEntry.filter) {
return aliasEntry.filter(comp);
Expand All @@ -350,7 +384,7 @@ export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, ch

if (isArray(chartSpec[compKey])) {
// 固定为array类型
let specifiedComp = chartSpec[compKey].find((comp: any) => comp[ALIAS_NAME_KEY] === topKeyPath);
let specifiedComp = chartSpec[compKey].find((comp: any) => comp[ALIAS_NAME_KEY] === aliasName);

if (!specifiedComp) {
specifiedComp = chartSpec[compKey].find(isMatchComp);
Expand All @@ -377,7 +411,7 @@ export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, ch

return {
appendSpec,
aliasName: topKeyPath,
aliasName: aliasName,
appendPath: subPaths[0],
parentKeyPath: subPaths.join('.')
};
Expand Down Expand Up @@ -502,17 +536,11 @@ export const mergeAppendSpec = (prevSpec: any, appendSpec: AppendSpecInfo) => {

if (parentKeyPath) {
if (parentKeyPath.startsWith('series') && newSpec.type !== 'common' && !newSpec.series) {
leafSpec = leafSpec.series
? isArray(leafSpec.series)
? leafSpec.series[0]
: leafSpec.series
: parentKeyPath in leafSpec
? leafSpec[parentKeyPath]
: leafSpec;
leafSpec = removeUnneedArrayInSpec(leafSpec, 'series', parentKeyPath);

parentKeyPath = parentKeyPath.indexOf('.') > 0 ? parentKeyPath.slice(parentKeyPath.indexOf('.') + 1) : '';
} else {
const aliasResult = parseAliasOfPath(parentKeyPath, aliasKeyPath, newSpec);
const aliasResult = parseAliasOfPath(parentKeyPath, aliasKeyPath, newSpec, leafSpec);

if (aliasResult.appendSpec && aliasResult.appendPath) {
set(newSpec, aliasResult.appendPath, aliasResult.appendSpec);
Expand All @@ -522,6 +550,10 @@ export const mergeAppendSpec = (prevSpec: any, appendSpec: AppendSpecInfo) => {
parentKeyPath = aliasResult.parentKeyPath;
}

if (isValid(aliasResult.leafSpec)) {
leafSpec = aliasResult.leafSpec;
}

leafSpec = convertFunctionString(
reduceDuplicatedPath(
parentKeyPath,
Expand Down

0 comments on commit 36ade46

Please sign in to comment.