123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- function initCharts() {
-
-
-
- enableChartInteractions();
-
-
- initDataTableSorting();
- }
- function enableChartInteractions() {
-
- addChartDownloadButtons();
-
-
- addTimeRangeSelectors();
- }
- function addChartDownloadButtons() {
- const chartContainers = document.querySelectorAll('.chart-container');
-
- chartContainers.forEach(container => {
- const canvas = container.querySelector('canvas');
- if (!canvas) return;
-
- const header = container.querySelector('.chart-header');
- const downloadBtn = document.createElement('button');
- downloadBtn.className = 'btn btn-sm';
- downloadBtn.innerHTML = '下载图表';
- downloadBtn.onclick = function() {
- const chartInstance = Chart.getChart(canvas);
- if (!chartInstance) return;
-
-
- const a = document.createElement('a');
- a.href = chartInstance.toBase64Image();
- a.download = (container.querySelector('.chart-title').textContent || 'chart') + '.png';
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- };
-
- header.appendChild(downloadBtn);
- });
- }
- function addTimeRangeSelectors() {
-
-
- }
- function initDataTableSorting() {
- const tables = document.querySelectorAll('.data-table');
-
- tables.forEach(table => {
- const headers = table.querySelectorAll('th');
-
- headers.forEach((header, index) => {
-
- if (header.classList.contains('no-sort')) return;
-
- header.style.cursor = 'pointer';
- header.dataset.sortDirection = 'none';
-
-
- const sortIcon = document.createElement('span');
- sortIcon.className = 'sort-icon';
- sortIcon.innerHTML = ' ⇅';
- header.appendChild(sortIcon);
-
-
- header.addEventListener('click', () => {
-
- headers.forEach(h => {
- if (h !== header) h.dataset.sortDirection = 'none';
- });
-
-
- const currentDirection = header.dataset.sortDirection;
- if (currentDirection === 'none' || currentDirection === 'desc') {
- header.dataset.sortDirection = 'asc';
- } else {
- header.dataset.sortDirection = 'desc';
- }
-
-
- sortTable(table, index, header.dataset.sortDirection);
- });
- });
- });
- }
- function sortTable(table, columnIndex, direction) {
- const tbody = table.querySelector('tbody');
- const rows = Array.from(tbody.querySelectorAll('tr'));
-
-
- rows.sort((a, b) => {
- const aValue = a.querySelectorAll('td')[columnIndex].textContent.trim();
- const bValue = b.querySelectorAll('td')[columnIndex].textContent.trim();
-
-
- const aNum = parseFloat(aValue.replace(/[¥,]/g, ''));
- const bNum = parseFloat(bValue.replace(/[¥,]/g, ''));
-
- if (!isNaN(aNum) && !isNaN(bNum)) {
- return direction === 'asc' ? aNum - bNum : bNum - aNum;
- }
-
-
- return direction === 'asc'
- ? aValue.localeCompare(bValue, 'zh-CN')
- : bValue.localeCompare(aValue, 'zh-CN');
- });
-
-
- rows.forEach(row => tbody.appendChild(row));
- }
- document.addEventListener('DOMContentLoaded', initCharts);
|