Just call the function: downloadImage();
, so when the page loads it performs the function without the need of click.
I put the function in your Jsfiddle, and I had the code run again and downloaded the image without the need to click.
var chart_variable = new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
label: 'My First Dataset',
data: [3, 1, 4, 2, 5],
backgroundColor: 'rgba(0, 119, 290, 0.2)',
borderColor: 'rgba(0, 119, 290, 0.6)'
}]
},
options: {
title: {
display: true,
text: 'Chart Title'
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
stepSize: 1
}
}]
}
}
});
function downloadImage() {
/* set new title */
chart_variable.options.title.text = 'New Chart Title';
chart_variable.update({
duration: 0
});
// or, use
// chart_variable.update(0);
/* save as image */
var link = document.createElement('a');
link.href = chart_variable.toBase64Image();
link.download = 'myImage.png';
link.click();
/* rollback to old title */
chart_variable.options.title.text = 'Chart Title';
chart_variable.update({
duration: 0
});
// or, use
// chart_variable.update(0);
}
//executa a função ao abrir a página.
downloadImage();
What exactly do you want? Download when entering the page? What is the difficulty you have?
– lazyFox
I know almost nothing about javascript. I wanted to automatically save the image when displaying the page without needing a button. With the saved image I will generate a PDF report with the FPDF in PHP that knows well.
– Wellington Telles Cunha