diff --git a/packages/vmind/__tests__/experiment/src/pages/ChartDialogueQA/qaPairRag.tsx b/packages/vmind/__tests__/experiment/src/pages/ChartDialogueQA/qaPairRag.tsx index 2723358..34bb8a7 100644 --- a/packages/vmind/__tests__/experiment/src/pages/ChartDialogueQA/qaPairRag.tsx +++ b/packages/vmind/__tests__/experiment/src/pages/ChartDialogueQA/qaPairRag.tsx @@ -361,52 +361,56 @@ export function QARag() {
QA Recall:
- {qaResult.map((item, index) => ( - -
- Score: - {item.scores.toFixed(2)} -
-
-
Question:
- {item.question} -
-
-
Explanation:
- {item.explanation} -
-
-
Answer:
- {item.answer} -
-
- ))} + {qaResult && qaResult.length + ? qaResult.map((item, index) => ( + +
+ Score: + {item.scores.toFixed(2)} +
+
+
Question:
+ {item.question} +
+
+
Explanation:
+ {item.explanation} +
+
+
Answer:
+ {`${item.answer}`} +
+
+ )) + : null}
KeyPath Recall:
- {keyPathResult.map((item, index) => ( - -
- Score: - {item.scores.toFixed(2)} -
-
-
content:
- {item.text} -
- {ragOption.type === 'qa' && ( -
-
Answer:
- {item.answer} -
- )} -
-
key:
- {item.key} -
-
- ))} + {keyPathResult && keyPathResult.length + ? keyPathResult.map((item, index) => ( + +
+ Score: + {item.scores.toFixed(2)} +
+
+
content:
+ {item.text} +
+ {ragOption.type === 'qa' ? ( +
+
Answer:
+ {`${item.answer}`} +
+ ) : null} +
+
key:
+ {item.key} +
+
+ )) + : null}
diff --git a/packages/vmind/__tests__/unit/vchartSpec/bar.test.ts b/packages/vmind/__tests__/unit/vchartSpec/bar.test.ts index ec0bfff..866bd09 100644 --- a/packages/vmind/__tests__/unit/vchartSpec/bar.test.ts +++ b/packages/vmind/__tests__/unit/vchartSpec/bar.test.ts @@ -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' + }); + }); }); diff --git a/packages/vmind/src/atom/VChartSpec/utils.ts b/packages/vmind/src/atom/VChartSpec/utils.ts index 7b30c16..74f4f8d 100644 --- a/packages/vmind/src/atom/VChartSpec/utils.ts +++ b/packages/vmind/src/atom/VChartSpec/utils.ts @@ -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); @@ -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); @@ -377,7 +411,7 @@ export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, ch return { appendSpec, - aliasName: topKeyPath, + aliasName: aliasName, appendPath: subPaths[0], parentKeyPath: subPaths.join('.') }; @@ -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); @@ -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,