Files
seal-use/src/domains/seal/reports/charts/duration-histogram.ts
T

249 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { renderHtmlToImageBuffer } from "../browser.js";
export interface HistogramSeries {
name: string;
count: number;
color: string;
/** 每单审核耗时数组(秒);若提供则自动分箱计算高度 */
durations?: number[];
/** 直接提供的各分箱频数数组(长度对应 bins 数量) */
binCounts?: number[];
}
export interface DurationHistogramOptions {
title?: string;
series: HistogramSeries[];
averageSeconds?: number;
averageLabel?: string;
binWidth?: number;
binStart?: number;
binEnd?: number;
width?: number;
height?: number;
}
export interface DurationHistogramPngOptions extends DurationHistogramOptions {
/** 渲染倍率:默认 4x 超高分屏/印刷级采样 (4800×2400) */
scale?: number;
chrome?: string;
}
/** 赢家时尚 (EEKA) 2026年8月全量 240 单样本的真实审核耗时分布基线数据 */
export const DEFAULT_EEKA_DURATION_SERIES: HistogramSeries[] = [
{
name: "采购付款单-月结款",
count: 15,
color: "#48a9c5",
binCounts: [2, 2, 0, 1, 0, 1, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
},
{
name: "其他报销及业务单据",
count: 12,
color: "#b19cd9",
binCounts: [0, 0, 0, 0, 1, 0, 0, 1, 2, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0]
},
{
name: "差旅报销单-定额",
count: 22,
color: "#f7a072",
binCounts: [0, 0, 6, 3, 2, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
},
{
name: "采购申请与预付款类单据",
count: 24,
color: "#fed766",
binCounts: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0]
},
{
name: "一般费用报销单【收据】- 个人报销",
count: 33,
color: "#a8d5ba",
binCounts: [0, 0, 0, 0, 0, 0, 2, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0]
},
{
name: "一般费用报销单【发票】- 对外支付",
count: 64,
color: "#f5a9b8",
binCounts: [0, 0, 1, 1, 2, 2, 2, 0, 4, 3, 4, 0, 0, 0, 1, 3, 0, 1, 1, 0]
},
{
name: "一般费用报销单【发票】- 个人报销",
count: 70,
color: "#7fe3d7",
binCounts: [0, 2, 2, 5, 5, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
}
];
/**
* 自动生成各单据模板审核耗时分布对比的多系列堆叠直方图 (SVG)。
* 100% 纯代码矢量计算,数据驱动自动分箱,支持 7 组模板色彩层次堆叠与平均值虚线标注。
*/
export function renderDurationHistogramSvg(options?: Partial<DurationHistogramOptions>): string {
const {
title = "各单据模板审核耗时分布对比 (2026年8月全量240单样本)",
series = DEFAULT_EEKA_DURATION_SERIES,
averageSeconds = 445.4,
averageLabel = "总体平均耗时: 445.4秒 (约7.4分钟)",
binWidth = 50,
binStart = 150,
binEnd = 1150,
width = 1200,
height = 600
} = options ?? {};
const numBins = Math.round((binEnd - binStart) / binWidth);
const plotLeft = 80;
const plotRight = width - 40;
const plotTop = 60;
const plotBottom = height - 80;
const plotWidth = plotRight - plotLeft;
const plotHeight = plotBottom - plotTop;
// 计算分箱数据矩阵 [binIndex][seriesIndex] = count
const binData: number[][] = Array.from({ length: numBins }, () => Array(series.length).fill(0));
series.forEach((s, sIdx) => {
if (s.binCounts && s.binCounts.length >= numBins) {
for (let b = 0; b < numBins; b++) binData[b]![sIdx] = s.binCounts[b] ?? 0;
} else if (s.durations) {
for (const d of s.durations) {
if (d >= binStart && d < binEnd) {
const b = Math.min(numBins - 1, Math.floor((d - binStart) / binWidth));
binData[b]![sIdx] = (binData[b]![sIdx] ?? 0) + 1;
}
}
}
});
// 计算最大 Y 轴高度(默认向上取整到偶数,至少 11)
const maxStack = Math.max(11, ...binData.map((counts) => counts.reduce((acc, c) => acc + c, 0)));
const yDomainMax = Math.ceil(maxStack / 2) * 2;
const yUnit = plotHeight / yDomainMax;
// X 轴映射 (秒 -> 像素)
const xDomainMin = 140;
const xDomainMax = 1220;
const mapX = (sec: number) => plotLeft + ((sec - xDomainMin) / (xDomainMax - xDomainMin)) * plotWidth;
// 平均线坐标
const avgX = mapX(averageSeconds);
// 生成水平网格线与 Y 轴刻度 (步长 2)
const yTicks: Array<{ val: number; y: number }> = [];
for (let val = 0; val <= yDomainMax; val += 2) {
yTicks.push({ val, y: plotBottom - val * yUnit });
}
// 生成垂直网格线与 X 轴刻度 (步长 200秒)
const xTicks: Array<{ val: number; x: number }> = [];
for (let val = 200; val <= 1200; val += 200) {
xTicks.push({ val, x: mapX(val) });
}
// 渲染堆叠条块
const barElements: string[] = [];
for (let b = 0; b < numBins; b++) {
const bStartSec = binStart + b * binWidth;
const bEndSec = bStartSec + binWidth;
const bx1 = mapX(bStartSec);
const bx2 = mapX(bEndSec);
const bWidth = bx2 - bx1;
let currentBottomY = plotBottom;
for (let sIdx = 0; sIdx < series.length; sIdx++) {
const count = binData[b]![sIdx] ?? 0;
if (count > 0) {
const segHeight = count * yUnit;
const segY = currentBottomY - segHeight;
const color = series[sIdx]!.color;
barElements.push(
`<rect x="${bx1.toFixed(1)}" y="${segY.toFixed(1)}" width="${bWidth.toFixed(1)}" height="${segHeight.toFixed(1)}" fill="${color}" fill-opacity="0.8" stroke="#333333" stroke-width="0.75"/>`
);
currentBottomY = segY;
}
}
}
// 图例项目(按业务重要性逆序排在右上角图例框中)
const legendItems = [...series].reverse();
const legendBoxWidth = 300;
const legendRowHeight = 23;
const legendBoxHeight = (legendItems.length + 1) * legendRowHeight + 16;
const legendBoxX = plotRight - legendBoxWidth - 12;
const legendBoxY = plotTop + 12;
return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif">
<rect width="${width}" height="${height}" fill="#ffffff"/>
<!-- Title -->
<text x="${(width / 2).toFixed(1)}" y="38" font-size="20" font-weight="600" fill="#111111" text-anchor="middle">${title}</text>
<!-- Horizontal Gridlines -->
<g stroke="#f0f0f0" stroke-width="1">
${yTicks.filter((t) => t.val > 0).map((t) => `<line x1="${plotLeft}" y1="${t.y.toFixed(1)}" x2="${plotRight}" y2="${t.y.toFixed(1)}"/>`).join("\n ")}
</g>
<!-- Vertical Gridlines -->
<g stroke="#f5f5f5" stroke-width="1">
${xTicks.map((t) => `<line x1="${t.x.toFixed(1)}" y1="${plotTop}" x2="${t.x.toFixed(1)}" y2="${plotBottom}"/>`).join("\n ")}
</g>
<!-- Stacked Bars -->
<g>
${barElements.join("\n ")}
</g>
<!-- Plot Border -->
<rect x="${plotLeft}" y="${plotTop}" width="${plotWidth}" height="${plotHeight}" fill="none" stroke="#222222" stroke-width="1.2"/>
<!-- Red Average Line -->
<line x1="${avgX.toFixed(1)}" y1="${plotTop}" x2="${avgX.toFixed(1)}" y2="${plotBottom}" stroke="#e02424" stroke-width="2.4" stroke-dasharray="6,4"/>
<!-- Y-axis Ticks & Labels -->
<g font-size="14" fill="#222222" text-anchor="end">
${yTicks.map((t) => `<line x1="${plotLeft - 5}" y1="${t.y.toFixed(1)}" x2="${plotLeft}" y2="${t.y.toFixed(1)}" stroke="#222222" stroke-width="1.2"/><text x="${plotLeft - 10}" y="${(t.y + 5).toFixed(1)}">${t.val}</text>`).join("\n ")}
</g>
<text x="25" y="${(plotTop + plotHeight / 2).toFixed(1)}" font-size="16" font-weight="500" fill="#222222" text-anchor="middle" transform="rotate(-90 25 ${(plotTop + plotHeight / 2).toFixed(1)})">单据数量</text>
<!-- X-axis Ticks & Labels -->
<g font-size="14" fill="#222222" text-anchor="middle">
${xTicks.map((t) => `<line x1="${t.x.toFixed(1)}" y1="${plotBottom}" x2="${t.x.toFixed(1)}" y2="${plotBottom + 5}" stroke="#222222" stroke-width="1.2"/><text x="${t.x.toFixed(1)}" y="${plotBottom + 24}">${t.val}</text>`).join("\n ")}
</g>
<text x="${(plotLeft + plotWidth / 2).toFixed(1)}" y="${plotBottom + 56}" font-size="16" font-weight="500" fill="#222222" text-anchor="middle">审核耗时(秒)</text>
<!-- Legend Box (Top Right) -->
<g transform="translate(${legendBoxX}, ${legendBoxY})">
<rect width="${legendBoxWidth}" height="${legendBoxHeight}" fill="#ffffff" stroke="#bbbbbb" stroke-width="1" rx="3"/>
${legendItems.map((item, idx) => {
const rowY = 12 + idx * legendRowHeight;
return `<rect x="12" y="${rowY}" width="22" height="12" fill="${item.color}" fill-opacity="0.8" stroke="#333333" stroke-width="0.75"/>
<text x="42" y="${rowY + 10}" font-size="12" fill="#222222">${item.name} (${item.count}单)</text>`;
}).join("\n ")}
<!-- Average Line in Legend -->
<line x1="12" y1="${12 + legendItems.length * legendRowHeight + 6}" x2="34" y2="${12 + legendItems.length * legendRowHeight + 6}" stroke="#e02424" stroke-width="2.4" stroke-dasharray="5,3"/>
<text x="42" y="${12 + legendItems.length * legendRowHeight + 10}" font-size="12" fill="#222222">${averageLabel}</text>
</g>
</svg>
`;
}
/**
* 采用本地 Headless Chrome 原生渲染管线输出高清晰度耗时分布直方图 PNG 图片。
* 默认 4x 超高分采样 (4800×2400),完美匹配 24 寸 4K 屏与高精度 PDF 导出。
*/
export async function renderDurationHistogramPng(options?: Partial<DurationHistogramPngOptions>): Promise<Buffer> {
const svg = renderDurationHistogramSvg(options);
const width = options?.width ?? 1200;
const height = options?.height ?? 600;
const scale = options?.scale ?? 4;
const html = `<!DOCTYPE html><html><head><meta charset="utf-8"><style>* { box-sizing: border-box; margin: 0; padding: 0; } body { width: ${width}px; height: ${height}px; background: #ffffff; display: flex; justify-content: center; align-items: center; font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif; -webkit-font-smoothing: antialiased; }</style></head><body>${svg}</body></html>`;
return renderHtmlToImageBuffer({
html,
width,
height,
scale,
chrome: options?.chrome
});
}