298 lines
9.7 KiB
JavaScript
298 lines
9.7 KiB
JavaScript
// chart.js - 图表功能模块
|
|
|
|
const ChartModule = (function() {
|
|
let daqChart = null;
|
|
let chartData = {
|
|
ch1: [],
|
|
ch2: []
|
|
};
|
|
|
|
function getCurrentSn() {
|
|
// 从设备信息中获取SN并移除所有空格和所有斜杠
|
|
return $('#daq_info').text().replace(/\s+/g, '').replace(/\//g, '');
|
|
}
|
|
|
|
// 初始化图表模块
|
|
function init() {
|
|
// 初始化图表
|
|
initChart();
|
|
|
|
// 设置拖动和缩放模式切换
|
|
initZoomControls();
|
|
|
|
// 将处理DAQ数据的函数暴露到全局
|
|
window.handleDaqData = handleDaqData;
|
|
|
|
// 添加删除行按钮的事件委托
|
|
const tbody = document.getElementById('stats_table_body');
|
|
tbody.addEventListener('click', function(event) {
|
|
if (event.target.closest('.delete-row')) {
|
|
const row = event.target.closest('tr');
|
|
row.remove();
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// 初始化图表
|
|
function initChart() {
|
|
const ctx = document.getElementById('daq_chart').getContext('2d');
|
|
daqChart = new Chart(ctx, {
|
|
type: 'line',
|
|
data: {
|
|
labels: [],
|
|
datasets: [
|
|
{
|
|
label: 'CH1',
|
|
data: [],
|
|
borderColor: 'rgba(219, 24, 67, 1)',
|
|
backgroundColor: 'rgba(219, 24, 67, 1)',
|
|
borderWidth: 1,
|
|
fill: false,
|
|
tension: 0,
|
|
pointRadius: 3,
|
|
pointBackgroundColor: 'rgba(219, 24, 67, 1)',
|
|
pointBorderColor: 'rgba(219, 24, 67, 1)',
|
|
pointHoverRadius: 5,
|
|
pointHoverBackgroundColor: 'rgba(219, 24, 67, 0.8)',
|
|
pointHoverBorderColor: 'rgba(219, 24, 67, 1)'
|
|
},
|
|
{
|
|
label: 'CH2',
|
|
data: [],
|
|
borderColor: 'rgba(10, 49, 223, 1)',
|
|
backgroundColor: 'rgba(10, 49, 223, 1)',
|
|
borderWidth: 1,
|
|
fill: false,
|
|
tension: 0,
|
|
pointRadius: 3,
|
|
pointBackgroundColor: 'rgba(10, 49, 223, 1)',
|
|
pointBorderColor: 'rgba(10, 49, 223, 1)',
|
|
pointHoverRadius: 5,
|
|
pointHoverBackgroundColor: 'rgba(10, 49, 223, 0.8)',
|
|
pointHoverBorderColor: 'rgba(10, 49, 223, 1)'
|
|
}
|
|
]
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
scales: {
|
|
x: {
|
|
title: {
|
|
display: true,
|
|
text: '样本点'
|
|
},
|
|
grid: {
|
|
color: 'rgba(0, 0, 0, 0.1)'
|
|
}
|
|
},
|
|
y: {
|
|
title: {
|
|
display: true,
|
|
text: '数值 (uint16)'
|
|
},
|
|
min: 0,
|
|
max: 65535,
|
|
grid: {
|
|
color: 'rgba(0, 0, 0, 0.1)'
|
|
}
|
|
}
|
|
},
|
|
animation: false,
|
|
plugins: {
|
|
zoom: {
|
|
pan: {
|
|
enabled: true,
|
|
mode: 'xy',
|
|
speed: 10,
|
|
threshold: 10,
|
|
scaleMode: 'xy'
|
|
},
|
|
zoom: {
|
|
wheel: {
|
|
enabled: true,
|
|
speed: 0.1
|
|
},
|
|
drag: {
|
|
enabled: false,
|
|
borderColor: 'rgba(255, 255, 255, 0.5)',
|
|
borderWidth: 1,
|
|
backgroundColor: 'rgba(225, 225, 225, 0.3)',
|
|
drawTime: 'beforeDatasetsDraw'
|
|
},
|
|
pinch: {
|
|
enabled: true
|
|
},
|
|
mode: 'xy',
|
|
speed: 0.1,
|
|
scaleMode: 'xy'
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// 初始化缩放控制
|
|
function initZoomControls() {
|
|
// 重置缩放按钮
|
|
$('#reset_zoom').on('click', function() {
|
|
if (daqChart) {
|
|
daqChart.resetZoom();
|
|
}
|
|
});
|
|
|
|
// 缩放模式切换按钮
|
|
$('.zoom-mode-btn').on('click', function() {
|
|
const mode = $(this).data('mode');
|
|
|
|
// 更新按钮状态
|
|
$('.zoom-mode-btn').removeClass('active');
|
|
$(this).addClass('active');
|
|
|
|
// 更新图表缩放模式
|
|
if (daqChart) {
|
|
daqChart.options.plugins.zoom.pan.mode = mode;
|
|
daqChart.options.plugins.zoom.zoom.mode = mode;
|
|
daqChart.update('none');
|
|
}
|
|
});
|
|
}
|
|
|
|
// 处理DAQ数据
|
|
function handleDaqData(daqData) {
|
|
try {
|
|
// 解析数据
|
|
const parsedData = parseDaqData(daqData);
|
|
|
|
// 更新图表数据
|
|
chartData.ch1 = parsedData.ch1;
|
|
chartData.ch2 = parsedData.ch2;
|
|
|
|
// 更新图表
|
|
updateChart();
|
|
|
|
// 计算并显示统计信息
|
|
calculateAndDisplayStats(parsedData.ch1, parsedData.ch2);
|
|
} catch (error) {
|
|
console.error('解析DAQ数据失败:', error);
|
|
showToast('解析DAQ数据失败');
|
|
}
|
|
}
|
|
|
|
// 计算并显示统计信息
|
|
function calculateAndDisplayStats(ch1Data, ch2Data) {
|
|
// 计算各通道统计值
|
|
const ch1Stats = calculateStats(ch1Data);
|
|
const ch2Stats = calculateStats(ch2Data);
|
|
|
|
// 获取当前时间
|
|
const currentTime = new Date().toLocaleString();
|
|
|
|
const diff = Math.abs(ch1Stats.average - ch2Stats.average) / (Math.max(ch1Stats.average, ch2Stats.average) || 1) * 100;
|
|
|
|
// 创建新的表格行
|
|
const newRow = `
|
|
<tr>
|
|
<td>${currentTime}</td>
|
|
<td>${ch1Data.length}</td>
|
|
<td>${ch1Stats.average.toFixed(0)}</td>
|
|
<td>${ch1Stats.peakToPeak.toFixed(0)}</td>
|
|
<td>${ch1Stats.snr.toFixed(0)}%</td>
|
|
<td>${ch2Stats.average.toFixed(0)}</td>
|
|
<td>${ch2Stats.peakToPeak.toFixed(0)}</td>
|
|
<td>${ch2Stats.snr.toFixed(0)}%</td>
|
|
<td>${diff.toFixed(0)}%</td>
|
|
<td>
|
|
<button class="btn btn-danger btn-sm delete-row">
|
|
<i class="bi bi-trash"></i> 删除
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
`;
|
|
|
|
// 添加到表格顶部
|
|
const tbody = document.getElementById('stats_table_body');
|
|
tbody.insertAdjacentHTML('afterbegin', newRow);
|
|
|
|
// 限制表格行数(最多保留100条记录)
|
|
const maxRows = 100;
|
|
while (tbody.rows.length > maxRows) {
|
|
tbody.deleteRow(tbody.rows.length - 1);
|
|
}
|
|
}
|
|
|
|
// 计算单通道统计值
|
|
function calculateStats(data) {
|
|
if (!data || data.length === 0) {
|
|
return { average: 0, peakToPeak: 0, snr: 0 };
|
|
}
|
|
|
|
// 计算平均值
|
|
const sum = data.reduce((acc, val) => acc + val, 0);
|
|
const average = sum / data.length;
|
|
|
|
// 计算峰峰值
|
|
const max = Math.max(...data);
|
|
const min = Math.min(...data);
|
|
const peakToPeak = max - min;
|
|
|
|
// 计算信噪比 (SNR)
|
|
// 简单实现:假设噪声是信号的标准差
|
|
|
|
const snr = average === 0 ? 0 : peakToPeak / average * 100;
|
|
|
|
return { average, peakToPeak, snr };
|
|
}
|
|
|
|
// ========== DAQ数据处理相关 ==========
|
|
// 解析uint16小端数据
|
|
function parseDaqData(arrayBuffer) {
|
|
// 将ArrayBuffer转换为DataView
|
|
const dataView = new DataView(arrayBuffer);
|
|
|
|
// 解析uint16小端数据
|
|
const data = [];
|
|
for (let i = 4; i < arrayBuffer.byteLength; i += 2) { // 跳过前4字节头
|
|
data.push(dataView.getUint16(i, true)); // true表示小端
|
|
}
|
|
|
|
// 分离CH1和CH2数据
|
|
const ch1 = [];
|
|
const ch2 = [];
|
|
for (let i = 0; i < data.length; i += 2) {
|
|
ch1.push(data[i]);
|
|
if (i + 1 < data.length) {
|
|
ch2.push(data[i + 1]);
|
|
}
|
|
}
|
|
|
|
return { ch1, ch2 };
|
|
}
|
|
|
|
// 更新图表
|
|
function updateChart() {
|
|
if (!daqChart) {
|
|
initChart();
|
|
}
|
|
|
|
// 更新标签和数据
|
|
const labels = chartData.ch1.map((_, index) => index);
|
|
|
|
daqChart.data.labels = labels;
|
|
daqChart.data.datasets[0].data = chartData.ch1;
|
|
daqChart.data.datasets[1].data = chartData.ch2;
|
|
|
|
daqChart.update('none');
|
|
}
|
|
|
|
// 暴露公共方法
|
|
return {
|
|
init: init,
|
|
updateChart: updateChart
|
|
};
|
|
})();
|