Download index.html from Examups/chart: direct link, hf CLI and curl.
- Browser
- Download file 39.9 kB
-
https://huggingface.co/spaces/Examups/chart/resolve/main/index.html
- Command line
-
hf download hf://spaces/Examups/chart/index.html
-
curl -L -o index.html https://huggingface.co/spaces/Examups/chart/resolve/main/index.html
39.9 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
| <title>Advanced Chart Maker - Modern UI</title> | |
| <!-- Bootstrap CSS --> | |
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> | |
| <!-- Google Fonts --> | |
| <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet"> | |
| <!-- Chart.js --> | |
| <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | |
| <!-- Chart.js Zoom Plugin --> | |
| <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@1.2.1/dist/chartjs-plugin-zoom.min.js"></script> | |
| <!-- canvas2svg for SVG Export --> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/canvas2svg/1.0.18/canvas2svg.min.js"></script> | |
| <!-- jsPDF for PDF Export --> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script> | |
| <style> | |
| :root { | |
| --primary-color: #007bff; | |
| --secondary-color: #6c757d; | |
| --light-color: #f8f9fa; | |
| --dark-color: #343a40; | |
| --card-bg: #ffffff; | |
| --card-border: #e0e0e0; | |
| --shadow: 0 4px 8px rgba(0, 0, 0, 0.1); | |
| } | |
| body { | |
| background-color: var(--light-color); | |
| font-family: 'Roboto', sans-serif; | |
| margin: 0; | |
| padding: 0; | |
| color: var(--dark-color); | |
| } | |
| .container { | |
| margin-top: 20px; | |
| margin-bottom: 20px; | |
| } | |
| .card { | |
| background-color: var(--card-bg); | |
| border: 1px solid var(--card-border); | |
| box-shadow: var(--shadow); | |
| border-radius: 8px; | |
| transition: transform 0.2s ease; | |
| } | |
| .card:hover { | |
| transform: translateY(-5px); | |
| } | |
| .card-header { | |
| background-color: var(--primary-color); | |
| color: #fff; | |
| border-top-left-radius: 8px; | |
| border-top-right-radius: 8px; | |
| font-weight: 500; | |
| } | |
| .form-label { | |
| font-weight: 500; | |
| } | |
| .chart-container { | |
| position: relative; | |
| width: 100%; | |
| height: 300px; | |
| } | |
| #myChart { | |
| background-color: #fff; | |
| border: 2px solid #cccccc; | |
| border-radius: 8px; | |
| } | |
| .ad-container { | |
| text-align: center; | |
| margin: 20px 0; | |
| } | |
| /* Modern button styles */ | |
| .btn { | |
| border-radius: 50px; | |
| padding: 0.5rem 1.5rem; | |
| font-weight: 500; | |
| text-transform: uppercase; | |
| letter-spacing: 0.5px; | |
| transition: background-color 0.3s ease, transform 0.2s ease; | |
| } | |
| .btn:hover { | |
| transform: translateY(-2px); | |
| } | |
| /* Three-column desktop layout */ | |
| @media (min-width: 992px) { | |
| .col-desktop { | |
| margin-bottom: 20px; | |
| } | |
| } | |
| /* Responsive adjustments */ | |
| @media (max-width: 992px) { | |
| .col-desktop { | |
| margin-bottom: 20px; | |
| } | |
| } | |
| @media (max-width: 576px) { | |
| .color-picker-container { | |
| display: block; | |
| margin: 10px 0; | |
| } | |
| .card { | |
| margin-bottom: 15px; | |
| } | |
| .card-header, .card-body { | |
| padding: 10px; | |
| } | |
| h2 { | |
| font-size: 1.5rem; | |
| } | |
| .chart-container { | |
| height: 250px; | |
| } | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <!-- Header: Chart File Name --> | |
| <div class="container my-3"> | |
| <div class="card"> | |
| <div class="card-header">Chart File Name</div> | |
| <div class="card-body"> | |
| <input type="text" id="chartFileName" class="form-control" value="chart" /> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Main Content: Three Column Layout --> | |
| <div class="container"> | |
| <div class="row"> | |
| <!-- Left Sidebar --> | |
| <div class="col-lg-3 col-md-4 col-desktop"> | |
| <!-- Chart Data & Type Card --> | |
| <div class="card mb-3"> | |
| <div class="card-header">Chart Data & Type</div> | |
| <div class="card-body"> | |
| <div class="mb-3"> | |
| <label for="chartType" class="form-label">Select Chart Type:</label> | |
| <select id="chartType" class="form-select"> | |
| <option value="bar">Bar Chart</option> | |
| <option value="line">Line Chart</option> | |
| <option value="pie">Pie Chart</option> | |
| <option value="doughnut">Doughnut Chart</option> | |
| <option value="area">Area Chart</option> | |
| <option value="scatter">Scatter Plot</option> | |
| <option value="bubble">Bubble Chart</option> | |
| <option value="radar">Radar Chart</option> | |
| <option value="polarArea">Polar Area Chart</option> | |
| <option value="mixed">Mixed Chart</option> | |
| </select> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="tableDataInput" class="form-label">Tabular Data (Markdown):</label> | |
| <textarea id="tableDataInput" rows="6" class="form-control" placeholder="Paste your data here...">| Year | Profit (in lakhs) | Revenue (in crores) | Expenses (in lakhs) | | |
| |-------|------------------|---------------------|---------------------| | |
| | Year 1 | 80 | 10 | 50 | | |
| | Year 2 | 90 | 15 | 60 | | |
| | Year 3 | 85 | 13 | 55 | | |
| | Year 4 | 95 | 18 | 70 | | |
| | Year 5 | 100 | 20 | 80 |</textarea> | |
| </div> | |
| <div class="mt-3 text-center"> | |
| <button onclick="generateChart()" class="btn btn-primary">Generate Chart</button> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Parsed Table Data (Read-Only) Card --> | |
| <div class="card mb-3"> | |
| <div class="card-header">Parsed Table Data (Read-Only)</div> | |
| <div class="card-body" id="tableContainer"> | |
| <!-- Generated table will appear here --> | |
| </div> | |
| </div> | |
| <!-- Customize Row Colors Card --> | |
| <div class="card mb-3"> | |
| <div class="card-header">Customize Row Colors</div> | |
| <div class="card-body" id="rowColorPickers"> | |
| <!-- Row color pickers generated here --> | |
| </div> | |
| </div> | |
| <!-- Customize Column Colors Card --> | |
| <div class="card mb-3"> | |
| <div class="card-header">Customize Column Colors</div> | |
| <div class="card-body" id="colColorPickers"> | |
| <!-- Column color pickers generated here --> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Middle Column --> | |
| <div class="col-lg-6 col-md-4 col-desktop"> | |
| <!-- Chart Display Card --> | |
| <div class="card mb-3"> | |
| <div class="card-header">Chart</div> | |
| <div class="card-body"> | |
| <div class="chart-container"> | |
| <canvas id="myChart"></canvas> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Button Area Below Chart --> | |
| <div class="text-center mb-3"> | |
| <button onclick="downloadChart('png')" class="btn btn-success me-2">Download PNG</button> | |
| <button onclick="downloadChart('jpeg')" class="btn btn-success me-2">Download JPG</button> | |
| <button onclick="copyChartToClipboard()" class="btn btn-warning me-2">Copy to Clipboard</button> | |
| </div> | |
| <!-- Sponsored Card --> | |
| <div class="card"> | |
| <div class="card-header">Sponsored</div> | |
| <div class="card-body ad-container"> | |
| <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2810665891608163" crossorigin="anonymous"></script> | |
| <ins class="adsbygoogle" | |
| style="display:block" | |
| data-ad-client="ca-pub-2810665891608163" | |
| data-ad-slot="8588732226" | |
| data-ad-format="auto" | |
| data-full-width-responsive="true"></ins> | |
| <script> | |
| (adsbygoogle = window.adsbygoogle || []).push({}); | |
| </script> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Right Sidebar --> | |
| <div class="col-lg-3 col-md-4 col-desktop"> | |
| <!-- Advanced Chart Options Card --> | |
| <div class="card mb-3"> | |
| <div class="card-header">Advanced Chart Options</div> | |
| <div class="card-body"> | |
| <div class="mb-3"> | |
| <label for="chartTitle" class="form-label">Chart Title:</label> | |
| <input type="text" id="chartTitle" class="form-control" placeholder="Enter chart title" /> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="xAxisLabel" class="form-label">X Axis Label:</label> | |
| <input type="text" id="xAxisLabel" class="form-control" placeholder="Enter X axis label" /> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="yAxisLabel" class="form-label">Y Axis Label:</label> | |
| <input type="text" id="yAxisLabel" class="form-control" placeholder="Enter Y axis label" /> | |
| </div> | |
| <div class="form-check mb-2"> | |
| <input type="checkbox" id="toggleLegend" class="form-check-input" checked> | |
| <label for="toggleLegend" class="form-check-label">Show Legend</label> | |
| </div> | |
| <div class="form-check mb-2"> | |
| <input type="checkbox" id="toggleGrid" class="form-check-input" checked> | |
| <label for="toggleGrid" class="form-check-label">Show Gridlines</label> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="legendPosition" class="form-label">Legend Position:</label> | |
| <select id="legendPosition" class="form-select"> | |
| <option value="top" selected>Top</option> | |
| <option value="bottom">Bottom</option> | |
| <option value="left">Left</option> | |
| <option value="right">Right</option> | |
| </select> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Watermark Options Card --> | |
| <div class="card mb-3"> | |
| <div class="card-header">Watermark Options</div> | |
| <div class="card-body"> | |
| <div class="mb-3"> | |
| <label for="watermarkText" class="form-label">Watermark Text:</label> | |
| <input type="text" id="watermarkText" class="form-control" value="examups.com" /> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="watermarkOpacity" class="form-label">Opacity (0-1):</label> | |
| <input type="number" id="watermarkOpacity" class="form-control" step="0.1" min="0" max="1" value="0.5" /> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="watermarkFontSize" class="form-label">Font Size (px):</label> | |
| <input type="number" id="watermarkFontSize" class="form-control" value="20" /> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="watermarkPosition" class="form-label">Position:</label> | |
| <select id="watermarkPosition" class="form-select"> | |
| <option value="center" selected>Center</option> | |
| <option value="top-left">Top Left</option> | |
| <option value="top-right">Top Right</option> | |
| <option value="bottom-left">Bottom Left</option> | |
| <option value="bottom-right">Bottom Right</option> | |
| </select> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="watermarkImage" class="form-label">Upload Image (Optional):</label> | |
| <input type="file" id="watermarkImage" class="form-control" accept="image/*" /> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Additional Customizations Card --> | |
| <div class="card mb-3"> | |
| <div class="card-header">Additional Customizations</div> | |
| <div class="card-body"> | |
| <div class="row g-3"> | |
| <div class="col-12 mb-3"> | |
| <label for="colorTheme" class="form-label">Color Theme:</label> | |
| <select id="colorTheme" class="form-select"> | |
| <option value="default" selected>Default</option> | |
| <option value="pastel">Pastel</option> | |
| <option value="vibrant">Vibrant</option> | |
| <option value="dark">Dark</option> | |
| </select> | |
| </div> | |
| <div class="col-6 mb-3"> | |
| <label for="gridColor" class="form-label">Grid Color:</label> | |
| <input type="color" id="gridColor" class="form-control form-control-color" value="#e9ecef" /> | |
| </div> | |
| <div class="col-6 mb-3"> | |
| <label for="tooltipBgColor" class="form-label">Tooltip BG Color:</label> | |
| <input type="color" id="tooltipBgColor" class="form-control form-control-color" value="#000000" /> | |
| </div> | |
| <div class="col-6 mb-3"> | |
| <label for="titleFontFamily" class="form-label">Title Font:</label> | |
| <input type="text" id="titleFontFamily" class="form-control" value="Arial" /> | |
| </div> | |
| <div class="col-6 mb-3"> | |
| <label for="titleFontSize" class="form-label">Title Size (px):</label> | |
| <input type="number" id="titleFontSize" class="form-control" value="18" /> | |
| </div> | |
| <div class="col-6 mb-3"> | |
| <label for="chartBgColor" class="form-label">Chart Background:</label> | |
| <input type="color" id="chartBgColor" class="form-control form-control-color" value="#ffffff" /> | |
| </div> | |
| <div class="col-6 mb-3"> | |
| <label for="chartBorderColor" class="form-label">Chart Border Color:</label> | |
| <input type="color" id="chartBorderColor" class="form-control form-control-color" value="#cccccc" /> | |
| </div> | |
| <div class="col-12 form-check"> | |
| <input type="checkbox" id="showAnnotations" class="form-check-input" checked> | |
| <label for="showAnnotations" class="form-check-label">Show Data Annotations</label> | |
| </div> | |
| </div> | |
| <div class="text-center"> | |
| <button onclick="updateCustomizations()" class="btn btn-primary">Apply Customizations</button> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Right after main content, no footer area --> | |
| <!-- Top Footer Google Ad remains at the bottom --> | |
| <div class="ad-container"> | |
| <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2810665891608163" crossorigin="anonymous"></script> | |
| <ins class="adsbygoogle" | |
| style="display:block" | |
| data-ad-client="ca-pub-2810665891608163" | |
| data-ad-slot="8588732226" | |
| data-ad-format="auto" | |
| data-full-width-responsive="true"></ins> | |
| <script> | |
| (adsbygoogle = window.adsbygoogle || []).push({}); | |
| </script> | |
| </div> | |
| <!-- Scripts --> | |
| <script> | |
| let chartInstance = null; | |
| let currentChartConfig = {}; | |
| let rowColors = []; | |
| let colColors = []; | |
| // Global variable for data annotations | |
| let showAnnotations = document.getElementById("showAnnotations")?.checked || true; | |
| // Custom Annotation Plugin (for non-pie charts) | |
| const annotationPlugin = { | |
| id: 'annotationPlugin', | |
| afterDatasetsDraw: (chart, args, options) => { | |
| if (!options.showAnnotations) return; | |
| const ctx = chart.ctx; | |
| chart.data.datasets.forEach((dataset, datasetIndex) => { | |
| const meta = chart.getDatasetMeta(datasetIndex); | |
| meta.data.forEach((element, index) => { | |
| const value = dataset.data[index]; | |
| ctx.fillStyle = options.fontColor || 'white'; | |
| ctx.font = `${options.fontSize || 12}px ${options.fontFamily || 'Arial'}`; | |
| ctx.textAlign = 'center'; | |
| ctx.textBaseline = 'bottom'; | |
| ctx.fillText(value, element.x, element.y - 5); | |
| }); | |
| }); | |
| } | |
| }; | |
| Chart.register(annotationPlugin); | |
| // Helper: Blend two hex colors (50/50 average). | |
| function blendColors(c1, c2) { | |
| const r1 = parseInt(c1.slice(1, 3), 16); | |
| const g1 = parseInt(c1.slice(3, 5), 16); | |
| const b1 = parseInt(c1.slice(5, 7), 16); | |
| const r2 = parseInt(c2.slice(1, 3), 16); | |
| const g2 = parseInt(c2.slice(3, 5), 16); | |
| const b2 = parseInt(c2.slice(5, 7), 16); | |
| const r = Math.round((r1 + r2) / 2); | |
| const g = Math.round((g1 + g2) / 2); | |
| const b = Math.round((b1 + b2) / 2); | |
| return "#" + [r, g, b].map(x => x.toString(16).padStart(2, "0")).join(""); | |
| } | |
| // Parse table data from markdown table. | |
| function parseTableData(input) { | |
| const rows = input.trim().split("\n"); | |
| const headers = rows[0].split("|").map(h => h.trim()).filter(h => h); | |
| const labels = []; | |
| const datasets = headers.slice(1).map(header => ({ | |
| label: header, | |
| data: [], | |
| borderColor: "#333", | |
| borderWidth: 1 | |
| })); | |
| for (let i = 2; i < rows.length; i++) { | |
| const cells = rows[i].split("|").map(cell => cell.trim()).filter(cell => cell); | |
| if (cells.length === headers.length) { | |
| labels.push(cells[0]); | |
| for (let j = 1; j < cells.length; j++) { | |
| datasets[j - 1].data.push(parseFloat(cells[j])); | |
| } | |
| } | |
| } | |
| return { headers, labels, datasets }; | |
| } | |
| // Return a random hex color. | |
| function getRandomColor() { | |
| const letters = "0123456789ABCDEF"; | |
| let color = "#"; | |
| for (let i = 0; i < 6; i++) { | |
| color += letters[Math.floor(Math.random() * 16)]; | |
| } | |
| return color; | |
| } | |
| // Update composite colors by blending row and column colors. | |
| function updateCompositeColors(datasets, labels) { | |
| datasets.forEach((dataset, dIndex) => { | |
| const composite = []; | |
| for (let r = 0; r < labels.length; r++) { | |
| composite.push(blendColors(rowColors[r], colColors[dIndex])); | |
| } | |
| dataset.backgroundColor = composite; | |
| }); | |
| } | |
| // Generate HTML table for parsed data. | |
| function generateTable(headers, labels, datasets) { | |
| let html = `<div class="table-responsive"><table class="table table-bordered"><thead class="table-light"><tr>`; | |
| headers.forEach(h => { html += `<th>${h}</th>`; }); | |
| html += `</tr></thead><tbody>`; | |
| for (let i = 0; i < labels.length; i++) { | |
| html += `<tr><td>${labels[i]}</td>`; | |
| datasets.forEach(ds => { html += `<td>${ds.data[i]}</td>`; }); | |
| html += `</tr>`; | |
| } | |
| html += `</tbody></table></div>`; | |
| document.getElementById("tableContainer").innerHTML = html; | |
| } | |
| // Generate the chart using Chart.js. | |
| function generateChart() { | |
| const chartType = document.getElementById("chartType").value; | |
| const tableData = document.getElementById("tableDataInput").value; | |
| const { headers, labels, datasets } = parseTableData(tableData); | |
| // Initialize row and column colors. | |
| rowColors = labels.map(() => getRandomColor()); | |
| colColors = datasets.map(() => getRandomColor()); | |
| updateCompositeColors(datasets, labels); | |
| // Adjust for special chart types. | |
| let finalType = chartType; | |
| if (chartType === "area") { | |
| finalType = "line"; | |
| datasets.forEach(ds => ds.fill = true); | |
| } else if (chartType === "scatter") { | |
| finalType = "scatter"; | |
| datasets.forEach(ds => { | |
| ds.data = ds.data.map((v, i) => ({ x: i, y: v })); | |
| }); | |
| } else if (chartType === "bubble") { | |
| finalType = "bubble"; | |
| datasets.forEach(ds => { | |
| ds.data = ds.data.map((v, i) => ({ x: i, y: v, r: 10 })); | |
| }); | |
| } else if (chartType === "mixed") { | |
| finalType = "bar"; | |
| datasets.forEach((ds, i) => { ds.type = (i % 2 === 0) ? "bar" : "line"; }); | |
| } | |
| // Set click event for drill-down. | |
| const onClickHandler = function(evt, activeElements) { | |
| if (activeElements.length > 0) { | |
| const element = activeElements[0]; | |
| const dsIndex = element.datasetIndex; | |
| const index = element.index; | |
| const label = this.data.labels[index]; | |
| const value = this.data.datasets[dsIndex].data[index]; | |
| alert(`Drill-down: ${label} - ${value}`); | |
| } | |
| }; | |
| if (chartInstance) { chartInstance.destroy(); } | |
| generateTable(headers, labels, datasets); | |
| const ctx = document.getElementById("myChart").getContext("2d"); | |
| chartInstance = new Chart(ctx, { | |
| type: finalType, | |
| data: { labels, datasets }, | |
| options: { | |
| responsive: true, | |
| maintainAspectRatio: false, | |
| onClick: onClickHandler, | |
| plugins: { | |
| legend: { display: true, position: document.getElementById("legendPosition").value }, | |
| title: { | |
| display: true, | |
| text: `${headers[0]} vs Multiple Columns`, | |
| font: { family: document.getElementById("titleFontFamily").value, size: parseInt(document.getElementById("titleFontSize").value) } | |
| }, | |
| tooltip: { backgroundColor: document.getElementById("tooltipBgColor").value }, | |
| annotationPlugin: { | |
| showAnnotations: document.getElementById("showAnnotations").checked, | |
| fontColor: "white", | |
| fontSize: parseInt(document.getElementById("titleFontSize").value), | |
| fontFamily: document.getElementById("titleFontFamily").value | |
| }, | |
| zoom: { | |
| pan: { enabled: true, mode: 'xy' }, | |
| zoom: { wheel: { enabled: true }, pinch: { enabled: true }, mode: 'xy' } | |
| } | |
| }, | |
| scales: { | |
| x: { grid: { display: true, color: document.getElementById("gridColor").value }, title: { display: false } }, | |
| y: { grid: { display: true, color: document.getElementById("gridColor").value }, title: { display: false } } | |
| } | |
| } | |
| }); | |
| // Set chart background and border. | |
| document.getElementById("myChart").style.backgroundColor = document.getElementById("chartBgColor").value; | |
| document.getElementById("myChart").style.border = "2px solid " + document.getElementById("chartBorderColor").value; | |
| currentChartConfig = { type: finalType, data: { labels, datasets }, options: chartInstance.options }; | |
| generateRowColorPickers(labels); | |
| generateColColorPickers(datasets); | |
| } | |
| // Generate row color pickers. | |
| function generateRowColorPickers(labels) { | |
| const container = document.getElementById("rowColorPickers"); | |
| container.innerHTML = ""; | |
| labels.forEach((label, index) => { | |
| const div = document.createElement("div"); | |
| div.className = "color-picker-container"; | |
| div.innerHTML = `<label for="rowColor${index}">${label}</label> | |
| <input type="color" id="rowColor${index}" class="form-control form-control-color" value="${rowColors[index]}" onchange="updateRowColor(${index}, this.value)">`; | |
| container.appendChild(div); | |
| }); | |
| } | |
| // Generate column color pickers. | |
| function generateColColorPickers(datasets) { | |
| const container = document.getElementById("colColorPickers"); | |
| container.innerHTML = ""; | |
| datasets.forEach((ds, index) => { | |
| const div = document.createElement("div"); | |
| div.className = "color-picker-container"; | |
| div.innerHTML = `<label for="colColor${index}">${ds.label}</label> | |
| <input type="color" id="colColor${index}" class="form-control form-control-color" value="${colColors[index]}" onchange="updateColColor(${index}, this.value)">`; | |
| container.appendChild(div); | |
| }); | |
| } | |
| function updateRowColor(index, color) { | |
| rowColors[index] = color; | |
| chartInstance.data.datasets.forEach((ds, dIndex) => { | |
| ds.backgroundColor[index] = blendColors(rowColors[index], colColors[dIndex]); | |
| }); | |
| chartInstance.update(); | |
| } | |
| function updateColColor(index, color) { | |
| colColors[index] = color; | |
| chartInstance.data.datasets[index].backgroundColor = | |
| chartInstance.data.labels.map((_, rIndex) => blendColors(rowColors[rIndex], colColors[index])); | |
| chartInstance.update(); | |
| } | |
| function updateChartOptions() { | |
| const newTitle = document.getElementById("chartTitle").value; | |
| const newX = document.getElementById("xAxisLabel").value; | |
| const newY = document.getElementById("yAxisLabel").value; | |
| const showLegend = document.getElementById("toggleLegend").checked; | |
| const showGrid = document.getElementById("toggleGrid").checked; | |
| if (chartInstance.options.plugins.title) { | |
| chartInstance.options.plugins.title.text = newTitle || chartInstance.options.plugins.title.text; | |
| } | |
| if (chartInstance.options.scales.x) { | |
| chartInstance.options.scales.x.title = { display: newX ? true : false, text: newX }; | |
| chartInstance.options.scales.x.grid.display = showGrid; | |
| } | |
| if (chartInstance.options.scales.y) { | |
| chartInstance.options.scales.y.title = { display: newY ? true : false, text: newY }; | |
| chartInstance.options.scales.y.grid.display = showGrid; | |
| } | |
| chartInstance.options.plugins.legend.display = showLegend; | |
| chartInstance.options.plugins.legend.position = document.getElementById("legendPosition").value; | |
| chartInstance.options.plugins.tooltip.backgroundColor = document.getElementById("tooltipBgColor").value; | |
| chartInstance.options.scales.x.grid.color = document.getElementById("gridColor").value; | |
| chartInstance.options.scales.y.grid.color = document.getElementById("gridColor").value; | |
| chartInstance.options.plugins.title.font = { | |
| family: document.getElementById("titleFontFamily").value, | |
| size: parseInt(document.getElementById("titleFontSize").value) | |
| }; | |
| chartInstance.update(); | |
| currentChartConfig.options = chartInstance.options; | |
| } | |
| function updateCustomizations() { | |
| updateChartOptions(); | |
| const theme = document.getElementById("colorTheme").value; | |
| if (theme !== "default") { | |
| let preset; | |
| if (theme === "pastel") { | |
| preset = ["#A8D5BA", "#F9E79F", "#FAD7A0", "#D2B4DE"]; | |
| } else if (theme === "vibrant") { | |
| preset = ["#FF5733", "#33FF57", "#3357FF", "#FF33A6"]; | |
| } else if (theme === "dark") { | |
| preset = ["#555555", "#666666", "#777777", "#888888"]; | |
| } | |
| rowColors = rowColors.map((_, i) => preset[i % preset.length]); | |
| colColors = colColors.map((_, i) => preset[i % preset.length]); | |
| const { labels, datasets } = currentChartConfig.data; | |
| updateCompositeColors(datasets, labels); | |
| chartInstance.update(); | |
| } | |
| document.getElementById("myChart").style.backgroundColor = document.getElementById("chartBgColor").value; | |
| document.getElementById("myChart").style.border = "2px solid " + document.getElementById("chartBorderColor").value; | |
| } | |
| // Draw tooltips for pie/doughnut charts in white. | |
| function drawPieTooltips(ctx) { | |
| if (chartInstance.config.type === "pie" || chartInstance.config.type === "doughnut") { | |
| const meta = chartInstance.getDatasetMeta(0); | |
| const ds = chartInstance.data.datasets[0]; | |
| const total = ds.data.reduce((s, v) => s + v, 0); | |
| meta.data.forEach((arc, index) => { | |
| const pos = arc.tooltipPosition(); | |
| const percentage = ((ds.data[index] / total) * 100).toFixed(1) + "%"; | |
| ctx.fillStyle = "white"; | |
| ctx.font = "bold 12px Arial"; | |
| ctx.textAlign = "center"; | |
| ctx.textBaseline = "middle"; | |
| ctx.fillText(percentage, pos.x, pos.y); | |
| }); | |
| } | |
| } | |
| // Compute watermark coordinates. | |
| function getWatermarkCoordinates(width, height, position, wmWidth) { | |
| let x, y; | |
| switch (position) { | |
| case "top-left": x = 10 + wmWidth / 2; y = 10; break; | |
| case "top-right": x = width - 10 - wmWidth / 2; y = 10; break; | |
| case "bottom-left": x = 10 + wmWidth / 2; y = height - 10; break; | |
| case "bottom-right": x = width - 10 - wmWidth / 2; y = height - 10; break; | |
| default: x = width / 2; y = height / 2; break; | |
| } | |
| return { x, y }; | |
| } | |
| // Export high-quality image by scaling offscreen canvas. | |
| function downloadChart(format) { | |
| const origCanvas = document.getElementById("myChart"); | |
| const scaleFactor = window.devicePixelRatio || 2; | |
| const offCanvas = document.createElement("canvas"); | |
| offCanvas.width = origCanvas.width * scaleFactor; | |
| offCanvas.height = origCanvas.height * scaleFactor; | |
| const offCtx = offCanvas.getContext("2d"); | |
| offCtx.scale(scaleFactor, scaleFactor); | |
| if (format === "jpeg") { | |
| offCtx.fillStyle = "white"; | |
| offCtx.fillRect(0, 0, origCanvas.width, origCanvas.height); | |
| } | |
| offCtx.drawImage(origCanvas, 0, 0); | |
| if (chartInstance.config.type === "pie" || chartInstance.config.type === "doughnut") { | |
| drawPieTooltips(offCtx); | |
| } | |
| const watermarkImage = document.getElementById("watermarkImage"); | |
| const wmOpacity = document.getElementById("watermarkOpacity").value; | |
| const wmPos = document.getElementById("watermarkPosition").value; | |
| const wmFontSize = document.getElementById("watermarkFontSize").value; | |
| offCtx.font = `${wmFontSize}px Arial`; | |
| offCtx.fillStyle = `rgba(255,165,0,${wmOpacity})`; | |
| let textAlign = "center", textBaseline = "middle"; | |
| if (wmPos.includes("left")) textAlign = "left"; else if (wmPos.includes("right")) textAlign = "right"; | |
| if (wmPos.includes("top")) textBaseline = "top"; else if (wmPos.includes("bottom")) textBaseline = "bottom"; | |
| offCtx.textAlign = textAlign; | |
| offCtx.textBaseline = textBaseline; | |
| const coords = getWatermarkCoordinates(origCanvas.width, origCanvas.height, wmPos, parseInt(wmFontSize)); | |
| if (watermarkImage.files && watermarkImage.files[0]) { | |
| const reader = new FileReader(); | |
| reader.onload = function (e) { | |
| const img = new Image(); | |
| img.onload = function () { | |
| offCtx.globalAlpha = parseFloat(wmOpacity); | |
| offCtx.drawImage(img, coords.x - img.width / 2, coords.y - img.height / 2); | |
| offCtx.globalAlpha = 1.0; | |
| let fName = document.getElementById("chartFileName").value || "chart"; | |
| const link = document.createElement("a"); | |
| link.download = `${fName}.${format}`; | |
| link.href = offCanvas.toDataURL(`image/${format}`); | |
| link.click(); | |
| } | |
| img.src = e.target.result; | |
| } | |
| reader.readAsDataURL(watermarkImage.files[0]); | |
| } else { | |
| offCtx.fillText(document.getElementById("watermarkText").value, coords.x, coords.y); | |
| let fName = document.getElementById("chartFileName").value || "chart"; | |
| const link = document.createElement("a"); | |
| link.download = `${fName}.${format}`; | |
| link.href = offCanvas.toDataURL(`image/${format}`); | |
| link.click(); | |
| } | |
| } | |
| // Download as SVG. | |
| function downloadSVG() { | |
| const origCanvas = document.getElementById("myChart"); | |
| const width = origCanvas.width, height = origCanvas.height; | |
| const svgCtx = new C2S(width, height); | |
| new Chart(svgCtx, currentChartConfig); | |
| const svgData = svgCtx.getSerializedSvg(); | |
| const blob = new Blob([svgData], { type: "image/svg+xml;charset=utf-8" }); | |
| let fName = document.getElementById("chartFileName").value || "chart"; | |
| const link = document.createElement("a"); | |
| link.href = URL.createObjectURL(blob); | |
| link.download = `${fName}.svg`; | |
| link.click(); | |
| } | |
| // Download as PDF. | |
| async function downloadPDF() { | |
| const canvas = document.getElementById("myChart"); | |
| const scaleFactor = window.devicePixelRatio || 2; | |
| const offCanvas = document.createElement("canvas"); | |
| offCanvas.width = canvas.width * scaleFactor; | |
| offCanvas.height = canvas.height * scaleFactor; | |
| const offCtx = offCanvas.getContext("2d"); | |
| offCtx.scale(scaleFactor, scaleFactor); | |
| offCtx.drawImage(canvas, 0, 0); | |
| const imgData = offCanvas.toDataURL("image/png"); | |
| const { jsPDF } = window.jspdf; | |
| const pdf = new jsPDF("landscape"); | |
| const pdfWidth = pdf.internal.pageSize.getWidth(); | |
| const pdfHeight = pdf.internal.pageSize.getHeight(); | |
| pdf.addImage(imgData, "PNG", 10, 10, pdfWidth - 20, pdfHeight - 20); | |
| let fName = document.getElementById("chartFileName").value || "chart"; | |
| pdf.save(`${fName}.pdf`); | |
| } | |
| // Copy chart to clipboard (PNG). | |
| async function copyChartToClipboard() { | |
| const origCanvas = document.getElementById("myChart"); | |
| const scaleFactor = window.devicePixelRatio || 2; | |
| const offCanvas = document.createElement("canvas"); | |
| offCanvas.width = origCanvas.width * scaleFactor; | |
| offCanvas.height = origCanvas.height * scaleFactor; | |
| const offCtx = offCanvas.getContext("2d"); | |
| offCtx.scale(scaleFactor, scaleFactor); | |
| offCtx.drawImage(origCanvas, 0, 0); | |
| if (chartInstance.config.type === "pie" || chartInstance.config.type === "doughnut") { | |
| drawPieTooltips(offCtx); | |
| } | |
| const watermarkImage = document.getElementById("watermarkImage"); | |
| const wmOpacity = document.getElementById("watermarkOpacity").value; | |
| const wmPos = document.getElementById("watermarkPosition").value; | |
| const wmFontSize = document.getElementById("watermarkFontSize").value; | |
| offCtx.font = `${wmFontSize}px Arial`; | |
| offCtx.fillStyle = `rgba(255,165,0,${wmOpacity})`; | |
| let textAlign = "center", textBaseline = "middle"; | |
| if (wmPos.includes("left")) textAlign = "left"; else if (wmPos.includes("right")) textAlign = "right"; | |
| if (wmPos.includes("top")) textBaseline = "top"; else if (wmPos.includes("bottom")) textBaseline = "bottom"; | |
| offCtx.textAlign = textAlign; | |
| offCtx.textBaseline = textBaseline; | |
| const coords = getWatermarkCoordinates(origCanvas.width, origCanvas.height, wmPos, parseInt(wmFontSize)); | |
| if (watermarkImage.files && watermarkImage.files[0]) { | |
| const reader = new FileReader(); | |
| reader.onload = function (e) { | |
| const img = new Image(); | |
| img.onload = function () { | |
| offCtx.globalAlpha = parseFloat(wmOpacity); | |
| offCtx.drawImage(img, coords.x - img.width / 2, coords.y - img.height / 2); | |
| offCtx.globalAlpha = 1.0; | |
| offCanvas.toBlob(blob => { | |
| const item = new ClipboardItem({ "image/png": blob }); | |
| navigator.clipboard.write([item]) | |
| .then(() => alert("Chart copied with watermark!")) | |
| .catch(err => alert("Error copying: " + err)); | |
| }, "image/png"); | |
| } | |
| img.src = e.target.result; | |
| } | |
| reader.readAsDataURL(watermarkImage.files[0]); | |
| } else { | |
| offCtx.fillText(document.getElementById("watermarkText").value, coords.x, coords.y); | |
| offCanvas.toBlob(blob => { | |
| const item = new ClipboardItem({ "image/png": blob }); | |
| navigator.clipboard.write([item]) | |
| .then(() => alert("Chart copied with watermark!")) | |
| .catch(err => alert("Error copying: " + err)); | |
| }, "image/png"); | |
| } | |
| } | |
| // Export parsed data as CSV. | |
| function exportDataAsCSV() { | |
| const tableData = document.getElementById("tableDataInput").value; | |
| const { headers, labels, datasets } = parseTableData(tableData); | |
| let csv = headers.join(",") + "\n"; | |
| for (let i = 0; i < labels.length; i++) { | |
| let row = [labels[i]]; | |
| datasets.forEach(ds => { row.push(ds.data[i]); }); | |
| csv += row.join(",") + "\n"; | |
| } | |
| const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); | |
| let fName = document.getElementById("chartFileName").value || "chart"; | |
| const link = document.createElement("a"); | |
| link.href = URL.createObjectURL(blob); | |
| link.download = `${fName}.csv`; | |
| link.click(); | |
| } | |
| // Export chart configuration as JSON. | |
| function exportChartConfigAsJSON() { | |
| const jsonStr = JSON.stringify(currentChartConfig, null, 2); | |
| const blob = new Blob([jsonStr], { type: 'application/json' }); | |
| let fName = document.getElementById("chartFileName").value || "chart"; | |
| const link = document.createElement("a"); | |
| link.href = URL.createObjectURL(blob); | |
| link.download = `${fName}_config.json`; | |
| link.click(); | |
| } | |
| // Generate embed code. | |
| function generateEmbedCode() { | |
| const url = window.location.href; | |
| const embed = `<iframe src="${url}" width="600" height="400" frameborder="0" scrolling="no"></iframe>`; | |
| document.getElementById("embedCode").value = embed; | |
| } | |
| // Additional Customizations. | |
| function updateCustomizations() { | |
| updateChartOptions(); | |
| const theme = document.getElementById("colorTheme").value; | |
| if (theme !== "default") { | |
| let preset; | |
| if (theme === "pastel") { | |
| preset = ["#A8D5BA", "#F9E79F", "#FAD7A0", "#D2B4DE"]; | |
| } else if (theme === "vibrant") { | |
| preset = ["#FF5733", "#33FF57", "#3357FF", "#FF33A6"]; | |
| } else if (theme === "dark") { | |
| preset = ["#555555", "#666666", "#777777", "#888888"]; | |
| } | |
| rowColors = rowColors.map((_, i) => preset[i % preset.length]); | |
| colColors = colColors.map((_, i) => preset[i % preset.length]); | |
| const { labels, datasets } = currentChartConfig.data; | |
| updateCompositeColors(datasets, labels); | |
| chartInstance.update(); | |
| } | |
| document.getElementById("myChart").style.backgroundColor = document.getElementById("chartBgColor").value; | |
| document.getElementById("myChart").style.border = "2px solid " + document.getElementById("chartBorderColor").value; | |
| } | |
| function updateChartOptions() { | |
| const newTitle = document.getElementById("chartTitle").value; | |
| const newX = document.getElementById("xAxisLabel").value; | |
| const newY = document.getElementById("yAxisLabel").value; | |
| const showLegend = document.getElementById("toggleLegend").checked; | |
| const showGrid = document.getElementById("toggleGrid").checked; | |
| if (chartInstance.options.plugins.title) { | |
| chartInstance.options.plugins.title.text = newTitle || chartInstance.options.plugins.title.text; | |
| } | |
| if (chartInstance.options.scales.x) { | |
| chartInstance.options.scales.x.title = { display: newX ? true : false, text: newX }; | |
| chartInstance.options.scales.x.grid.display = showGrid; | |
| } | |
| if (chartInstance.options.scales.y) { | |
| chartInstance.options.scales.y.title = { display: newY ? true : false, text: newY }; | |
| chartInstance.options.scales.y.grid.display = showGrid; | |
| } | |
| chartInstance.options.plugins.legend.display = showLegend; | |
| chartInstance.options.plugins.legend.position = document.getElementById("legendPosition").value; | |
| chartInstance.options.plugins.tooltip.backgroundColor = document.getElementById("tooltipBgColor").value; | |
| chartInstance.options.scales.x.grid.color = document.getElementById("gridColor").value; | |
| chartInstance.options.scales.y.grid.color = document.getElementById("gridColor").value; | |
| chartInstance.options.plugins.title.font = { | |
| family: document.getElementById("titleFontFamily").value, | |
| size: parseInt(document.getElementById("titleFontSize").value) | |
| }; | |
| chartInstance.update(); | |
| currentChartConfig.options = chartInstance.options; | |
| } | |
| </script> | |
| <!-- Bootstrap Bundle with Popper --> | |
| <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> | |
| </body> | |
| </html> | |