You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB
HTML

<!-- Jspreadsheet CSS & JS -->
<script src="https://cdn.jsdelivr.net/npm/jspreadsheet-ce"></script>
<link rel="stylesheet" href="https://bossanova.uk/jspreadsheet/v5/jspreadsheet.css" type="text/css" />
<!-- Chart.js -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<canvas id="chartCanvas" width="600" height="400"></canvas>
<button onclick="renderChart()">Generate Chart</button>
<script>
const data = [
['Year', 'Sales'],
['2020', 100],
['2021', 150],
['2022', 130],
['2023', 180]
];
const spreadsheet = jspreadsheet(document.getElementById('spreadsheet'), {
data,
columns: [
{ type: 'text', title: 'Year', width: 100 },
{ type: 'numeric', title: 'Sales', width: 100 },
]
});
function renderChart() {
const sheetData = spreadsheet.getData();
const labels = sheetData.slice(1).map(row => row[0]);
const values = sheetData.slice(1).map(row => parseFloat(row[1]));
const ctx = document.getElementById('chartCanvas').getContext('2d');
// Clear previous chart instance if any
if (window.myChart) window.myChart.destroy();
window.myChart = new Chart(ctx, {
type: 'bar', // or 'line', 'pie', etc.
data: {
labels,
datasets: [{
label: 'Sales',
data: values,
backgroundColor: 'rgba(75, 192, 192, 0.5)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
}
</script>
<div id="spreadsheet"></div>