main.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. document.addEventListener('DOMContentLoaded', function() {
  2. // 获取DOM元素
  3. const simpleDataContainer = document.getElementById('simple-data-container');
  4. const seriesDataContainer = document.getElementById('series-data-container');
  5. const csvDataContainer = document.getElementById('csv-data-container');
  6. const generateBtn = document.getElementById('generate-btn');
  7. const sampleDataBtn = document.getElementById('sample-data-btn');
  8. const exportPngBtn = document.getElementById('export-png-btn');
  9. const exportJpgBtn = document.getElementById('export-jpg-btn');
  10. const copyImgBtn = document.getElementById('copy-img-btn');
  11. const manualFormatSelect = document.getElementById('manual-format');
  12. const fileUploadInput = document.getElementById('file-upload');
  13. const manualFormatContainer = document.getElementById('manual-format-container');
  14. const donateLink = document.querySelector('.x-donate-link');
  15. const otherToolsLink = document.querySelector('.x-other-tools');
  16. const manualInputContainers = [simpleDataContainer, seriesDataContainer, csvDataContainer];
  17. // 初始化显示状态
  18. const initialMethod = document.querySelector('input[name="data-input-method"]:checked').value;
  19. toggleManualInputs(initialMethod === 'manual');
  20. fileUploadInput.parentElement.style.display = initialMethod === 'upload-csv' ? 'block' : 'none';
  21. // 初始化图表类型画廊
  22. initChartTypeGallery();
  23. function toggleManualInputs(show) {
  24. manualFormatContainer.style.display = show ? 'block' : 'none';
  25. const selectedFormat = manualFormatSelect.value;
  26. manualInputContainers.forEach(container => {
  27. const containerId = container.id.split('-')[0]; // 'simple', 'series', 'csv'
  28. container.style.display = (show && containerId === selectedFormat) ? 'block' : 'none';
  29. });
  30. }
  31. // 初始化时调用updateChartTypeOptions函数
  32. // 无论当前选择的是什么输入方式,都初始化图表类型
  33. // 默认使用'series'格式以显示最多的图表类型选项
  34. updateChartTypeOptions('series');
  35. // 监听数据输入方式切换
  36. document.querySelectorAll('input[name="data-input-method"]').forEach(radio => {
  37. radio.addEventListener('change', function() {
  38. const method = this.value;
  39. toggleManualInputs(method === 'manual');
  40. fileUploadInput.parentElement.style.display = method === 'upload-csv' ? 'block' : 'none';
  41. // 在切换到"上传Excel/CSV"时,更新图表类型选项为多系列数据
  42. if (method === 'upload-csv') {
  43. updateChartTypeOptions('series');
  44. } else if (method === 'manual') {
  45. // 切换回"手动录入"时,根据当前选择的格式更新图表类型选项
  46. updateChartTypeOptions(manualFormatSelect.value);
  47. uploadedData = null;
  48. fileUploadInput.value = ''; // 清空文件选择
  49. }
  50. });
  51. });
  52. // 监听打赏链接点击事件
  53. donateLink.addEventListener('click', function(event) {
  54. event.preventDefault();
  55. chrome.runtime.sendMessage({
  56. type: 'fh-dynamic-any-thing',
  57. thing: 'open-donate-modal',
  58. params: { toolName: 'chart-maker' }
  59. });
  60. });
  61. // 监听探索更多工具链接点击事件
  62. otherToolsLink.addEventListener('click', function(event) {
  63. event.preventDefault();
  64. chrome.runtime.openOptionsPage();
  65. });
  66. // 监听手动格式选择变化
  67. manualFormatSelect.addEventListener('change', function() {
  68. const format = this.value;
  69. manualInputContainers.forEach(container => {
  70. const containerId = container.id.split('-')[0];
  71. container.style.display = (containerId === format) ? 'block' : 'none';
  72. });
  73. // 更新图表类型选项
  74. updateChartTypeOptions(format);
  75. });
  76. // 文件上传处理
  77. fileUploadInput.addEventListener('change', function(event) {
  78. const file = event.target.files[0];
  79. if (!file) {
  80. uploadedData = null;
  81. return;
  82. }
  83. const reader = new FileReader();
  84. reader.onload = function(e) {
  85. try {
  86. const data = new Uint8Array(e.target.result);
  87. const workbook = XLSX.read(data, { type: 'array' });
  88. const firstSheetName = workbook.SheetNames[0];
  89. const worksheet = workbook.Sheets[firstSheetName];
  90. const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
  91. uploadedData = parseExcelData(jsonData);
  92. showNotification('文件上传成功,可以点击"生成图表"');
  93. // 上传Excel文件后,更新图表类型选项为多系列数据类型
  94. updateChartTypeOptions('series');
  95. } catch (error) {
  96. showNotification('文件解析失败: ' + error.message, true);
  97. uploadedData = null;
  98. fileUploadInput.value = ''; // 清空文件选择
  99. }
  100. };
  101. reader.onerror = function() {
  102. showNotification('文件读取失败', true);
  103. uploadedData = null;
  104. fileUploadInput.value = ''; // 清空文件选择
  105. };
  106. reader.readAsArrayBuffer(file);
  107. });
  108. // 生成图表按钮点击事件 (修改为独立的函数)
  109. function generateChart() {
  110. try {
  111. console.log('开始生成图表...');
  112. let parsedData;
  113. const method = document.querySelector('input[name="data-input-method"]:checked').value;
  114. console.log('数据输入方式:', method);
  115. if (method === 'upload-csv' && uploadedData) {
  116. parsedData = uploadedData;
  117. console.log('使用上传的数据');
  118. } else if (method === 'manual') {
  119. parsedData = parseInputData(); // 使用现有的手动数据解析函数
  120. console.log('使用手动输入的数据');
  121. } else if (method === 'upload-csv' && !uploadedData) {
  122. throw new Error('请先上传文件');
  123. } else {
  124. throw new Error('请选择有效的数据输入方式并提供数据');
  125. }
  126. console.log('解析后的数据:', parsedData);
  127. if (!parsedData ||
  128. (parsedData.labels && parsedData.labels.length === 0) ||
  129. (parsedData.datasets && parsedData.datasets.length === 0)) {
  130. throw new Error('无法解析数据或数据为空');
  131. }
  132. // 保存数据到全局变量,方便其他函数访问
  133. window.chartData = parsedData;
  134. const chartSettings = getChartSettings();
  135. // 将简单数据标记添加到设置中
  136. if (parsedData.isSimpleData) {
  137. chartSettings.isSimpleData = true;
  138. }
  139. console.log('图表设置:', chartSettings);
  140. // 调用chart-generator.js中的createChart函数
  141. if (typeof createChart !== 'function') {
  142. throw new Error('createChart函数未定义,请确保chart-generator.js正确加载');
  143. }
  144. createChart(parsedData, chartSettings);
  145. console.log('图表生成成功');
  146. exportPngBtn.disabled = false;
  147. exportJpgBtn.disabled = false;
  148. copyImgBtn.disabled = false;
  149. } catch (error) {
  150. console.error('生成图表时出错:', error);
  151. showNotification(error.message, true);
  152. }
  153. }
  154. // 将generateChart函数暴露为全局函数
  155. window.generateChart = generateChart;
  156. generateBtn.addEventListener('click', generateChart);
  157. // 监听图表设置的变化事件,实时更新图表 (仅在有图表时)
  158. ['chart-title', 'x-axis-label', 'y-axis-label', 'color-scheme', 'legend-position'].forEach(id => {
  159. document.getElementById(id).addEventListener('input', function() {
  160. const instance = getChartInstance();
  161. if (instance) { // 检查是否有图表实例
  162. generateChart(); // 重新生成图表以应用设置
  163. }
  164. });
  165. });
  166. // 初始化图表类型画廊
  167. function initChartTypeGallery() {
  168. console.log('初始化图表类型预览画廊...');
  169. // 获取所有图表类型预览项
  170. const chartTypeItems = document.querySelectorAll('.chart-type-item');
  171. console.log(`找到${chartTypeItems.length}个图表类型预览项`);
  172. // 为每个预览项添加点击事件
  173. chartTypeItems.forEach(item => {
  174. item.addEventListener('click', function() {
  175. const chartType = this.getAttribute('data-chart-type');
  176. console.log('选择了图表类型:', chartType);
  177. // 更新活动状态
  178. chartTypeItems.forEach(item => item.classList.remove('active'));
  179. this.classList.add('active');
  180. // 无论是否有图表实例都应该重新生成图表
  181. // 删除之前的检查条件,始终调用generateChart
  182. generateChart();
  183. });
  184. });
  185. // 初始设置默认图表类型为活动状态
  186. const defaultChartType = "bar"; // 默认为柱状图
  187. const activeItem = document.querySelector(`.chart-type-item[data-chart-type="${defaultChartType}"]`);
  188. if (activeItem) {
  189. activeItem.classList.add('active');
  190. }
  191. }
  192. // 加载样例数据
  193. sampleDataBtn.addEventListener('click', function() {
  194. // 确保选中"手动录入"选项
  195. const manualRadio = document.querySelector('input[name="data-input-method"][value="manual"]');
  196. if (manualRadio && !manualRadio.checked) {
  197. manualRadio.checked = true;
  198. // 触发change事件以显示相关的输入控件
  199. manualRadio.dispatchEvent(new Event('change'));
  200. }
  201. const currentFormat = manualFormatSelect.value;
  202. switch(currentFormat) {
  203. case 'simple':
  204. document.getElementById('data-input').value =
  205. '智能手机,2458\n平板电脑,1678\n笔记本电脑,1892\n智能手表,986\n耳机,1342';
  206. document.getElementById('chart-title').value = '2023年电子产品销量(万台)';
  207. document.getElementById('x-axis-label').value = '产品类别';
  208. document.getElementById('y-axis-label').value = '销量(万台)';
  209. break;
  210. case 'series':
  211. document.getElementById('series-data-input').value =
  212. '第一季度,2458,1678,1892,986,1342\n第二季度,2612,1524,1953,1104,1587\n第三季度,2845,1701,2135,1287,1643\n第四季度,3256,1835,2278,1452,1821';
  213. document.getElementById('series-labels').value =
  214. '智能手机,平板电脑,笔记本电脑,智能手表,耳机';
  215. document.getElementById('chart-title').value = '2023年电子产品季度销量(万台)';
  216. document.getElementById('x-axis-label').value = '产品类别';
  217. document.getElementById('y-axis-label').value = '销量(万台)';
  218. break;
  219. case 'csv':
  220. document.getElementById('csv-data-input').value =
  221. '品牌,2021年,2022年,2023年\n华为,786.5,845.2,921.6\n小米,651.2,712.8,768.3\n苹果,598.7,642.1,724.5\n三星,542.3,575.8,612.4\nOPPO,487.6,524.3,547.8\nvivo,452.8,501.7,532.9';
  222. document.getElementById('chart-title').value = '国内智能手机品牌销量趋势(万台)';
  223. document.getElementById('x-axis-label').value = '品牌';
  224. document.getElementById('y-axis-label').value = '销量(万台)';
  225. break;
  226. }
  227. // 提示用户下一步操作
  228. showNotification('已加载样例数据,点击"生成图表"查看效果');
  229. });
  230. // 显示通知
  231. function showNotification(message, isError = false) {
  232. // 移除现有通知
  233. const existingNotification = document.querySelector('.notification');
  234. if (existingNotification) {
  235. existingNotification.remove();
  236. }
  237. // 创建新通知
  238. const notification = document.createElement('div');
  239. notification.className = 'notification' + (isError ? ' error' : '');
  240. notification.textContent = message;
  241. // 添加到文档
  242. document.body.appendChild(notification);
  243. // 显示通知
  244. setTimeout(() => notification.classList.add('show'), 10);
  245. // 自动隐藏
  246. setTimeout(() => {
  247. notification.classList.remove('show');
  248. setTimeout(() => notification.remove(), 300);
  249. }, 3000);
  250. }
  251. // 解析输入数据
  252. function parseInputData() {
  253. const currentFormat = manualFormatSelect.value;
  254. switch(currentFormat) {
  255. case 'simple':
  256. return parseSimpleData();
  257. case 'series':
  258. return parseSeriesData();
  259. case 'csv':
  260. return parseCsvData();
  261. default:
  262. throw new Error('未知的数据格式');
  263. }
  264. }
  265. // 解析简单数据
  266. function parseSimpleData() {
  267. const input = document.getElementById('data-input').value.trim();
  268. if (!input) {
  269. throw new Error('请输入数据');
  270. }
  271. const lines = input.split('\n').filter(line => line.trim());
  272. const labels = [];
  273. const data = [];
  274. lines.forEach(line => {
  275. const parts = line.split(',').map(part => part.trim());
  276. if (parts.length >= 2) {
  277. labels.push(parts[0]);
  278. const value = parseFloat(parts[1]);
  279. if (isNaN(value)) {
  280. throw new Error(`"${parts[1]}"不是有效的数值`);
  281. }
  282. data.push(value);
  283. }
  284. });
  285. if (labels.length === 0 || data.length === 0) {
  286. throw new Error('无法解析数据,请检查格式是否正确');
  287. }
  288. return {
  289. labels: labels,
  290. datasets: [{
  291. data: data,
  292. label: '数值'
  293. }],
  294. isSimpleData: true // 添加标记,表示这是简单数据格式
  295. };
  296. }
  297. // 解析系列数据
  298. function parseSeriesData() {
  299. const input = document.getElementById('series-data-input').value.trim();
  300. const labelsInput = document.getElementById('series-labels').value.trim();
  301. if (!input) {
  302. throw new Error('请输入系列数据');
  303. }
  304. if (!labelsInput) {
  305. throw new Error('请输入标签数据');
  306. }
  307. const lines = input.split('\n').filter(line => line.trim());
  308. const labels = labelsInput.split(',').map(label => label.trim());
  309. const datasets = [];
  310. lines.forEach(line => {
  311. const parts = line.split(',').map(part => part.trim());
  312. if (parts.length >= 2) {
  313. const seriesName = parts[0];
  314. const seriesData = parts.slice(1).map(val => {
  315. const value = parseFloat(val);
  316. if (isNaN(value)) {
  317. throw new Error(`"${val}"不是有效的数值`);
  318. }
  319. return value;
  320. });
  321. datasets.push({
  322. label: seriesName,
  323. data: seriesData
  324. });
  325. }
  326. });
  327. if (labels.length === 0 || datasets.length === 0) {
  328. throw new Error('无法解析数据,请检查格式是否正确');
  329. }
  330. return {
  331. labels: labels,
  332. datasets: datasets
  333. };
  334. }
  335. // 解析CSV数据
  336. function parseCsvData() {
  337. const input = document.getElementById('csv-data-input').value.trim();
  338. const firstRowHeader = document.getElementById('first-row-header').checked;
  339. const firstColLabels = document.getElementById('first-col-labels').checked;
  340. if (!input) {
  341. throw new Error('请输入CSV数据');
  342. }
  343. const lines = input.split('\n').filter(line => line.trim());
  344. if (lines.length < 2) {
  345. throw new Error('CSV数据至少需要两行');
  346. }
  347. const rows = lines.map(line => line.split(',').map(cell => cell.trim()));
  348. let labels = [];
  349. let datasets = [];
  350. if (firstRowHeader && firstColLabels) {
  351. // 第一行是标题,第一列是标签
  352. labels = rows.slice(1).map(row => row[0]);
  353. const headers = rows[0].slice(1);
  354. headers.forEach((header, i) => {
  355. const data = rows.slice(1).map(row => {
  356. const value = parseFloat(row[i+1]);
  357. if (isNaN(value)) {
  358. throw new Error(`"${row[i+1]}"不是有效的数值`);
  359. }
  360. return value;
  361. });
  362. datasets.push({
  363. label: header,
  364. data: data
  365. });
  366. });
  367. } else if (firstRowHeader && !firstColLabels) {
  368. // 第一行是标题,但第一列不是标签
  369. labels = Array.from({length: rows[0].length}, (_, i) => `数据${i+1}`);
  370. const headers = rows[0];
  371. headers.forEach((header, i) => {
  372. const data = rows.slice(1).map(row => {
  373. const value = parseFloat(row[i]);
  374. if (isNaN(value)) {
  375. throw new Error(`"${row[i]}"不是有效的数值`);
  376. }
  377. return value;
  378. });
  379. datasets.push({
  380. label: header,
  381. data: data
  382. });
  383. });
  384. } else if (!firstRowHeader && firstColLabels) {
  385. // 第一行不是标题,第一列是标签
  386. labels = rows.map(row => row[0]);
  387. for (let i = 1; i < rows[0].length; i++) {
  388. const data = rows.map(row => {
  389. const value = parseFloat(row[i]);
  390. if (isNaN(value)) {
  391. throw new Error(`"${row[i]}"不是有效的数值`);
  392. }
  393. return value;
  394. });
  395. datasets.push({
  396. label: `系列${i}`,
  397. data: data
  398. });
  399. }
  400. } else {
  401. // 第一行不是标题,第一列也不是标签
  402. labels = Array.from({length: rows.length}, (_, i) => `标签${i+1}`);
  403. for (let i = 0; i < rows[0].length; i++) {
  404. const data = rows.map(row => {
  405. const value = parseFloat(row[i]);
  406. if (isNaN(value)) {
  407. throw new Error(`"${row[i]}"不是有效的数值`);
  408. }
  409. return value;
  410. });
  411. datasets.push({
  412. label: `系列${i+1}`,
  413. data: data
  414. });
  415. }
  416. }
  417. if (labels.length === 0 || datasets.length === 0) {
  418. throw new Error('无法解析数据,请检查格式是否正确');
  419. }
  420. return {
  421. labels: labels,
  422. datasets: datasets
  423. };
  424. }
  425. // 获取图表设置
  426. function getChartSettings() {
  427. // 从活跃的图表类型项获取图表类型,而不是从下拉框
  428. let chartType = 'bar'; // 默认值
  429. const activeChartTypeItem = document.querySelector('.chart-type-item.active');
  430. if (activeChartTypeItem) {
  431. chartType = activeChartTypeItem.getAttribute('data-chart-type');
  432. }
  433. return {
  434. type: chartType,
  435. title: document.getElementById('chart-title').value,
  436. xAxisLabel: document.getElementById('x-axis-label').value,
  437. yAxisLabel: document.getElementById('y-axis-label').value,
  438. colorScheme: document.getElementById('color-scheme').value,
  439. legendPosition: document.getElementById('legend-position').value,
  440. showGridLines: document.getElementById('show-grid-lines').checked,
  441. animateChart: document.getElementById('animate-chart').checked
  442. };
  443. }
  444. // 导出PNG图像
  445. exportPngBtn.addEventListener('click', function() {
  446. exportChart('png');
  447. });
  448. // 导出JPG图像
  449. exportJpgBtn.addEventListener('click', function() {
  450. exportChart('jpg');
  451. });
  452. // 复制图像到剪贴板
  453. copyImgBtn.addEventListener('click', function() {
  454. copyChartToClipboard();
  455. });
  456. // 导出图表为图像
  457. function exportChart(format) {
  458. const chartWrapper = document.getElementById('chart-wrapper');
  459. // 创建加载指示器
  460. const loadingOverlay = document.createElement('div');
  461. loadingOverlay.className = 'loading-overlay';
  462. loadingOverlay.innerHTML = '<div class="loading-spinner"></div>';
  463. chartWrapper.appendChild(loadingOverlay);
  464. setTimeout(() => {
  465. // 获取原始canvas的尺寸
  466. const originalCanvas = document.getElementById('chart-canvas');
  467. const width = originalCanvas.width;
  468. const height = originalCanvas.height;
  469. // 创建一个临时的高分辨率canvas
  470. const tempCanvas = document.createElement('canvas');
  471. const tempCtx = tempCanvas.getContext('2d');
  472. // 设置更高的分辨率
  473. const scale = 8; // 提升到8倍分辨率
  474. tempCanvas.width = width * scale;
  475. tempCanvas.height = height * scale;
  476. // 优化渲染质量
  477. tempCtx.imageSmoothingEnabled = true;
  478. tempCtx.imageSmoothingQuality = 'high';
  479. html2canvas(originalCanvas, {
  480. backgroundColor: '#ffffff',
  481. scale: scale, // 使用8倍缩放
  482. width: width,
  483. height: height,
  484. useCORS: true,
  485. allowTaint: true,
  486. logging: false,
  487. imageTimeout: 0,
  488. onclone: (document) => {
  489. const clonedCanvas = document.getElementById('chart-canvas');
  490. if(clonedCanvas) {
  491. clonedCanvas.style.width = width + 'px';
  492. clonedCanvas.style.height = height + 'px';
  493. }
  494. },
  495. // 添加高级渲染选项
  496. canvas: tempCanvas,
  497. renderCallback: (canvas) => {
  498. // 应用锐化效果
  499. const ctx = canvas.getContext('2d');
  500. ctx.filter = 'contrast(1.1) saturate(1.2)';
  501. }
  502. }).then(canvas => {
  503. // 移除加载指示器
  504. loadingOverlay.remove();
  505. // 导出图像时使用更高的质量设置
  506. let imgUrl;
  507. if (format === 'jpg') {
  508. // JPEG使用最高质量
  509. imgUrl = canvas.toDataURL('image/jpeg', 1.0);
  510. } else {
  511. // PNG使用无损压缩
  512. imgUrl = canvas.toDataURL('image/png');
  513. }
  514. // 创建下载链接
  515. const link = document.createElement('a');
  516. const chartTitle = document.getElementById('chart-title').value || '图表';
  517. const fileName = `${chartTitle.replace(/[^\w\u4e00-\u9fa5]/g, '_')}_Ultra_HD.${format}`;
  518. link.download = fileName;
  519. link.href = imgUrl;
  520. link.click();
  521. showNotification(`已成功导出超高清${format.toUpperCase()}图像`);
  522. }).catch(error => {
  523. // 移除加载指示器
  524. loadingOverlay.remove();
  525. showNotification('导出图像失败,请重试', true);
  526. console.error('导出图像出错:', error);
  527. });
  528. }, 100);
  529. }
  530. // 复制图表到剪贴板
  531. function copyChartToClipboard() {
  532. const chartWrapper = document.getElementById('chart-wrapper');
  533. // 创建加载指示器
  534. const loadingOverlay = document.createElement('div');
  535. loadingOverlay.className = 'loading-overlay';
  536. loadingOverlay.innerHTML = '<div class="loading-spinner"></div>';
  537. chartWrapper.appendChild(loadingOverlay);
  538. setTimeout(() => {
  539. html2canvas(document.getElementById('chart-canvas'), {
  540. backgroundColor: '#ffffff',
  541. scale: 2
  542. }).then(canvas => {
  543. // 移除加载指示器
  544. loadingOverlay.remove();
  545. canvas.toBlob(blob => {
  546. try {
  547. // 尝试使用现代API复制到剪贴板
  548. if (navigator.clipboard && navigator.clipboard.write) {
  549. const clipboardItem = new ClipboardItem({'image/png': blob});
  550. navigator.clipboard.write([clipboardItem])
  551. .then(() => {
  552. showNotification('图表已复制到剪贴板');
  553. })
  554. .catch(err => {
  555. console.error('剪贴板API错误:', err);
  556. legacyCopyToClipboard(canvas);
  557. });
  558. } else {
  559. legacyCopyToClipboard(canvas);
  560. }
  561. } catch (e) {
  562. console.error('复制到剪贴板出错:', e);
  563. legacyCopyToClipboard(canvas);
  564. }
  565. });
  566. }).catch(error => {
  567. // 移除加载指示器
  568. loadingOverlay.remove();
  569. showNotification('复制图像失败,请重试', true);
  570. console.error('复制图像出错:', error);
  571. });
  572. }, 100);
  573. }
  574. // 兼容性较好的复制方法(通过创建临时链接)
  575. function legacyCopyToClipboard(canvas) {
  576. const imgUrl = canvas.toDataURL('image/png');
  577. // 创建临时链接
  578. const link = document.createElement('a');
  579. link.download = '图表.png';
  580. link.href = imgUrl;
  581. showNotification('已准备下载图表,无法直接复制到剪贴板');
  582. link.click();
  583. }
  584. // 解析Excel数据
  585. function parseExcelData(jsonData) {
  586. if (!jsonData || jsonData.length < 2 || !jsonData[0] || jsonData[0].length < 2) {
  587. throw new Error('Excel数据格式不正确,至少需要表头行和数据行');
  588. }
  589. // 假设第一行为标题,第一列为标签
  590. const labels = jsonData.slice(1).map(row => row && row[0] ? row[0].toString() : '');
  591. const datasets = [];
  592. const headers = jsonData[0].slice(1);
  593. headers.forEach((header, i) => {
  594. const data = jsonData.slice(1).map(row => {
  595. // 确保每个单元格数据都是数值类型
  596. if (!row || !row[i + 1]) return 0;
  597. const value = parseFloat(row[i + 1]);
  598. return isNaN(value) ? 0 : value;
  599. });
  600. datasets.push({
  601. label: header ? header.toString() : `系列${i+1}`,
  602. data: data
  603. });
  604. });
  605. return {
  606. labels: labels,
  607. datasets: datasets
  608. };
  609. }
  610. // 从chart-generator.js中导入图表生成函数
  611. function getChartInstance() {
  612. return window.chartInstance;
  613. }
  614. function setChartInstance(instance) {
  615. window.chartInstance = instance;
  616. }
  617. // 根据数据格式更新图表类型选项
  618. function updateChartTypeOptions(dataFormat) {
  619. // 由于移除了图表类型下拉框,这个函数现在仅记录当前数据格式,不再修改任何选项
  620. console.log('当前数据格式:', dataFormat);
  621. // 未来可以根据数据格式来调整图表类型画廊的可见性或提示,但现在不需要操作
  622. }
  623. });