// 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 = `