JavaScript function to create the line chart:
function TyLyReport() {
var lineChartData = {
labels : ["Jan","Feb","Mar"],
datasets : [
{
label: 'Dataset 1',
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
data : [1000, 2000, 2500],
fill: false
},
{
label: 'Dataset 2',
borderColor: '#FF4848',
backgroundColor: 'rgba(255, 72, 72, 0.2)',
data : [1016, 1800, 2900],
fill: false
}
]
};
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx, {
type: 'line',
data: lineChartData,
options: {
responsive : true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
// Export chart image to Excel
exportToExcel();
}
// Function to export chart image to Excel
function exportToExcel() {
var canvas = document.getElementById("canvas");
var image = canvas.toDataURL("image/png");
var workbook = new ExcelJS.Workbook();
var worksheet = workbook.addWorksheet('Chart Image');
var imageId = workbook.addImage({
base64: image,
extension: 'png',
});
worksheet.addImage(imageId, 'A1');
workbook.xlsx.writeBuffer().then(function(buffer) {
saveAs(new Blob([buffer]), 'chart_image.xlsx');
});
}
// Call the TyLyReport function when the window loads
window.onload = TyLyReport;
By using the above code, I can able to download the excel and export the chart image seperately but image is not created inside the excel. Can anyone please guide me to export inside the excel. I need to export image in excel like the below image
