import type { PersistedTestRecord } from '@/api/tests'
|
import { toTestDisplayValue } from './testDataUnits'
|
|
export type ReportMetricKey = 'voltage' | 'current' | 'positiveVoltage' | 'negativeVoltage' | 'temperature'
|
export interface MeasurementSeries { name: string; zeroTime: number | null; records: PersistedTestRecord[]; hidden?: boolean }
|
|
/** 页面和打印共用测量、时间及单位口径;不连接缺失值,也不限制曲线条数。 */
|
export function measurementChartOption(metric: { key: ReportMetricKey; unit: string }, items: MeasurementSeries[], colors: string[]) {
|
const field: Record<ReportMetricKey, keyof PersistedTestRecord> = { voltage: 'voltage', current: 'current', positiveVoltage: 'positivePotential', negativeVoltage: 'negativePotential', temperature: 'temperature' }
|
const series = items.map(item => item.records.flatMap(row => {
|
// 柜与采集器分别使用自己的设备时间,共享已冻结的所选范围零点。
|
const time = metric.key === 'voltage' || metric.key === 'current' ? row.deviceTime : row.collectorDeviceTime
|
if (time == null || item.zeroTime == null) return []
|
const value = row[field[metric.key]]
|
return [[(Number(time) - item.zeroTime) / 1000, value == null ? null : toTestDisplayValue(metric.key, Number(value))] as [number, number | null]]
|
}).sort((left, right) => left[0] - right[0]))
|
const [dataMin, dataMax] = series.reduce<[number, number]>(([min, max], points) => points.reduce<[number, number]>(([low, high], [, value]) =>
|
value === null ? [low, high] : [Math.min(low, value), Math.max(high, value)], [min, max]), [0, 0])
|
const range = Math.max(dataMax - dataMin, 1)
|
const timeMax = series.reduce((max, points) => points.reduce((value, [time]) => Math.max(value, time), max), 0)
|
const timeUnit = timeMax >= 3600 ? 3600 : timeMax >= 180 ? 60 : 1
|
const timeLabel = timeUnit === 3600 ? 'h' : timeUnit === 60 ? 'min' : 's'
|
const axisMax = Math.max(metric.unit === '℃' ? Math.ceil(dataMax / 10) * 10 : Math.ceil(dataMax * 10) / 10, 1)
|
const yAxis = (position: 'left' | 'right') => ({
|
type: 'value', name: metric.unit, position, min: dataMin - range * 0.05, max: axisMax,
|
nameTextStyle: { color: '#8795a3', fontSize: 10 },
|
axisLine: { show: true, lineStyle: { color: '#cbd8e4' } },
|
axisTick: { show: true, lineStyle: { color: '#cbd8e4' } },
|
splitLine: { show: position === 'left', lineStyle: { color: '#e8eef4' } },
|
axisLabel: { color: '#8795a3', fontSize: 10, formatter: (value: number) => Number(value.toPrecision(6)).toString() },
|
})
|
return {
|
animation: false, color: colors, grid: { top: 40, right: 32, bottom: 42, left: 32, containLabel: true },
|
tooltip: { trigger: 'axis' }, legend: { show: false },
|
xAxis: { type: 'value', min: 0, max: timeMax > 0 ? timeMax : undefined, splitNumber: 4, name: `相对时间 / ${timeLabel}`, nameLocation: 'middle', nameGap: 28,
|
axisLine: { lineStyle: { color: '#cbd8e4' } }, axisLabel: { color: '#8795a3', fontSize: 10, hideOverlap: true,
|
formatter: (value: number) => Number((value / timeUnit).toFixed(3)).toString() },
|
nameTextStyle: { color: '#26333f', fontSize: 11, fontWeight: 600 } },
|
yAxis: [yAxis('left'), yAxis('right')],
|
// 全部冻结点先参与坐标计算;隐藏仅控制渲染,不重算零点、不缩短范围、不移动其他来源颜色。
|
series: items.flatMap((item, index) => item.hidden ? [] : [{ name: item.name, type: 'line', smooth: false,
|
connectNulls: false, showSymbol: true, symbolSize: 5, itemStyle: { color: colors[index % colors.length] },
|
lineStyle: { width: 2, color: colors[index % colors.length] }, data: series[index] || [] }]),
|
}
|
}
|