main.js 28 KB

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