Combine black and bright defect charts in analysis reports

This commit is contained in:
2026-10-24 10:23:31 +08:00
parent ec71609343
commit e2fb128911
10 changed files with 221 additions and 11 deletions
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
import {
buildAnalysisVisualOption,
buildAnalysisVisualExportOption,
combineStageDefectVisuals,
formatVisualValue,
parseAnalysisVisuals,
renderVisualTableHtml,
@@ -93,6 +94,31 @@ test('parser preserves zero and null as different values', () => {
assert.equal(option.animation, false);
});
test('same-scope black and bright defect changes become one stage-colored chart', () => {
const defect = (stage, values, product = '规格 A') => visual({
id: `defect-${stage}-${product}`, type: 'bar', title: `${stage}具体缺陷`, subtitle: '来源字段',
unit: 'kg', metric: 'source_defect_delta_weight', sectionKey: `defect_${stage}`, dimension: 'defect', stage,
scope: { product, stage }, period: { year: 2026, month: 9 }, comparisonPeriod: { year: 2026, month: 8 },
categories: values.map((_, index) => ({ key: `field-${index}`, label: `缺陷 ${index}` })),
series: [{ key: 'delta', label: '增减', values }], evidenceRefs: [`ref-${stage}`],
requiredFields: values.map((_, index) => `field-${index}`),
coverage: { totalRows: values.length, returnedRows: values.length, truncated: false },
});
const [black, bright, other] = parseAnalysisVisuals([
defect('black', [265.85, 0, -12]), defect('bright', [123.42, 0, 39.25]), defect('bright', [9], '规格 B'),
]).visuals;
const combined = combineStageDefectVisuals([black, bright, other]);
assert.equal(combined.length, 2);
assert.equal(combined[0].sectionKey, 'defect_combined');
assert.deepEqual(combined[0].evidenceRefs, ['ref-black', 'ref-bright']);
assert.deepEqual(combined[0].categories.map(item => item.label), ['黑片 · 缺陷 0', '亮片 · 缺陷 0', '亮片 · 缺陷 2', '黑片 · 缺陷 2']);
assert.deepEqual(combined[0].series[0].values, [265.85, null, null, -12]);
assert.deepEqual(combined[0].series[1].values, [null, 123.42, 39.25, null]);
assert.equal(combined[0].chartable, true);
assert.equal(combined[1].scope.product, '规格 B');
assert.equal(combineStageDefectVisuals(combined).length, 2);
});
test('ratio charts use 0 to 100 percent and convert raw ratios only for plotting', () => {
const parsed = parseAnalysisVisuals([visual()]);
const option = buildAnalysisVisualOption(parsed.visuals[0]);