57 lines
3.8 KiB
JavaScript
57 lines
3.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import * as echarts from 'echarts/core';
|
|
import {LineChart,BarChart} from 'echarts/charts';
|
|
import {GridComponent,LegendComponent,TooltipComponent,GraphicComponent,TitleComponent} from 'echarts/components';
|
|
import {SVGRenderer} from 'echarts/renderers';
|
|
import {buildAnalysisVisualExportOption} from '../src/analysisVisualsModel.ts';
|
|
echarts.use([LineChart,BarChart,GridComponent,LegendComponent,TooltipComponent,GraphicComponent,TitleComponent,SVGRenderer]);
|
|
function visual(type='line') {
|
|
const values=[.9025,.8931,.9012,.8671,.8914,.8982,.8606,.8639,.8794];
|
|
return {id:'monthly-export',type,title:'正式指标趋势',unit:'ratio',
|
|
categories:values.map((_,i)=>({key:`2026-${String(i+1).padStart(2,'0')}`,label:`2026-${String(i+1).padStart(2,'0')}`})),
|
|
series:[{key:'composite_rate',label:'综合合格率',values}],scope:{releaseId:'release'},
|
|
period:{start:'2026-01',end:'2026-09',granularity:'month'},metric:'composite_rate',sectionKey:'trend',
|
|
evidenceRefs:['Q12345678'],requiredFields:['black_good_weight','black_total_weight','bright_good_weight','bright_total_weight'],
|
|
coverage:{totalRows:9,returnedRows:9,truncated:false},originTurnId:'export-turn'};
|
|
}
|
|
for(const type of ['line','area'])test(`900px report ${type} keeps a positive plot and the actual monthly vertical movement`,()=>{
|
|
const v=visual(type),out=buildAnalysisVisualExportOption(v,900,'report');
|
|
const chart=echarts.init(null,undefined,{renderer:'svg',width:out.width,height:out.height,ssr:true});
|
|
try {
|
|
chart.setOption(out.option,{notMerge:true});
|
|
const rect=chart.getModel().getComponent('grid').coordinateSystem.getRect();
|
|
assert.ok(rect.height>=180,`plot collapsed: ${rect.height}`);
|
|
assert.ok(rect.y+rect.height+30<out.height,'category labels need space below plot');
|
|
const points=Array.from(chart.getModel().getSeriesByIndex(0).getData().getLayout('points'));
|
|
assert.equal(points.length,18);
|
|
const ys=points.filter((_,i)=>i%2===1);
|
|
assert.ok(Math.max(...ys)-Math.min(...ys)>8,'different rates must not become a flat line');
|
|
const series=chart.getModel().getSeriesByIndex(0).getData();
|
|
const [low,high]=chart.getModel().getComponent('yAxis').axis.scale.getExtent();
|
|
assert.ok(low>0 && high-low<20,'rate trends retain their focused percentage axis');
|
|
ys.forEach((y,i)=>{
|
|
const expected=v.series[0].values[i]*100;
|
|
assert.ok(Math.abs(series.get('y',i)-expected)<1e-10,'the plotted value preserves the formal rate');
|
|
assert.ok(expected>=low&&expected<=high,'the value must fit inside the actual axis');
|
|
const pixel=rect.y+rect.height*(high-expected)/(high-low);
|
|
assert.ok(Math.abs(y-pixel)<.001,'SVG coordinates match the formal rate on the actual axis');
|
|
});
|
|
assert.match(chart.renderToSVGString(),/<svg/);
|
|
} finally {chart.dispose();}
|
|
});
|
|
test('report period comparison bars keep enough plot height and preserve their weight ratio',()=>{
|
|
const v={...visual('column'),unit:'kg',metric:'black_total_weight',stage:'black',sectionKey:'stage',
|
|
categories:[{key:'previous',label:'2026-08'},{key:'current',label:'2026-09'}],
|
|
series:[{key:'black_total_weight',label:'黑片投入重量',values:[20,60]}],coverage:{totalRows:2,returnedRows:2,truncated:false}};
|
|
const out=buildAnalysisVisualExportOption(v,900,'report');
|
|
const chart=echarts.init(null,undefined,{renderer:'svg',width:out.width,height:out.height,ssr:true});
|
|
try {
|
|
chart.setOption(out.option,{notMerge:true});
|
|
assert.ok(chart.getModel().getComponent('grid').coordinateSystem.getRect().height>=180);
|
|
const data=chart.getModel().getSeriesByIndex(0).getData();
|
|
const first=Math.abs(data.getItemLayout(0).height),second=Math.abs(data.getItemLayout(1).height);
|
|
assert.ok(first>30&&second>90);assert.ok(Math.abs(second/first-3)<1e-10);
|
|
} finally {chart.dispose();}
|
|
});
|