The chart seem to be async so you will probably need to provide a callback when the animation has finished or else the canvas will be empty.

var options = {
    bezierCurve : false,
    onAnimationComplete: done  /// calls function done() {} at end
};
Answer from user1693593 on Stack Overflow
🌐
npm
npmjs.com › package › chartjs-to-image
chartjs-to-image - npm
This is a wrapper for exporting Chart.js as an image. It works on the server side as well as client side (although on the client you may prefer to use toBase64Image). The renderer is based on QuickChart, a free and open-source web service for generating static charts. View the main QuickChart repository here. ... This library provides a ChartJsImage object.
      » npm install chartjs-to-image
    
Published   Nov 11, 2023
Version   1.2.2
Author   Ian Webster
🌐
QuickChart
quickchart.io › documentation › chart-js › image-export
How to download and export Chart.js images | QuickChart
Because it outsources rendering to QuickChart, an open-source web service, it requires almost no other dependencies and uses much less CPU. ... const ChartJsImage = require('chartjs-to-image'); // Generate the chart const chart = new ChartJsImage(); chart.setConfig({ type: 'bar', data: { labels: ['Hello world', 'Foo bar'], datasets: [{ label: 'Foo', data: [1, 2] }] }, }); // Save it chart.toFile('/tmp/mychart.png');
🌐
GitHub
github.com › image-charts › chartjs-image-javascript
GitHub - image-charts/chartjs-image-javascript: Render Chart.JS chart as image
Creates a file containing generated chart image and yield a promise. When file is a filename, asynchronously writes data to the file, replacing the file if it already exists. When file is a file descriptor, the behavior is similar to calling ...
Starred by 13 users
Forked by 2 users
Languages   JavaScript
🌐
GitHub
github.com › typpo › chartjs-to-image
GitHub - typpo/chartjs-to-image: Simple library for converting Chart.js to image on backend and frontend
This is a wrapper for exporting Chart.js as an image. It works on the server side as well as client side (although on the client you may prefer to use toBase64Image). The renderer is based on QuickChart, a free and open-source web service for generating static charts. View the main QuickChart repository here. ... This library provides a ChartJsImage object.
Starred by 50 users
Forked by 8 users
Languages   JavaScript
Top answer
1 of 2
3

its not possible to render without attaching canvas to dom, but you can hide it and it will work as expected.


const chartEl = document.createElement("canvas");
chartEl.setAttribute("width", "400");
chartEl.setAttribute("height", "400");
chartEl.style.display = "none";

document.body.append(chartEl);
let chart = new Chart(chartEl, {
  type: "bar",
  data: {
    datasets: [
      {
        barPercentage: 0.5,
        barThickness: 6,
        maxBarThickness: 8,
        minBarLength: 2,
        data: [10, 20, 30, 40, 50, 60, 70]
      }
    ]
  },
  options: {
    scales: {},
    animation: false
  }
});

chart.render();
console.log(chart.toBase64Image());
chartEl.remove();

2 of 2
2

The only way to render a chart without you specifically adding it to the dom is by making use of the offscreen-canvas as explained here in the documentation of chart.js: https://www.chartjs.org/docs/3.7.1/general/performance.html#parallel-rendering-with-web-workers-chromium-only

Downside is that the offscreen canvas API is only available in chromium based browsers.

Other approach you can take is by adding the canvas to the dom, let chart.js render to it, get the base64 representation and then remove the canvas directly after that like so:

let ctx = document.createElement("canvas")
document.documentElement.appendChild(ctx)
ctx.setAttribute("width", "400")
ctx.setAttribute("height", "400")
let chart = new Chart(ctx, {
  type: "bar",
  data: {
    labels: ['a', 'b', 'c', 'd', 'e', 'f', ' g'],
    datasets: [{
      barPercentage: 0.5,
      barThickness: 6,
      maxBarThickness: 8,
      minBarLength: 2,
      data: [10, 20, 30, 40, 50, 60, 70]
    }]
  },
  options: {
    scales: {}
  }
})

const base64 = chart.toBase64Image();

ctx.remove();

console.log(base64)
<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.js"></script>
</body>
🌐
DEV Community
dev.to › noemelo › how-to-save-chart-as-image-chart-js-2l0i
How to save chart as image Chart.js - DEV Community
February 4, 2020 - //Download Chart Image document.getElementById("download").addEventListener('click', function(){ /*Get image of canvas element*/ var url_base64jp = document.getElementById("lineChart").toDataURL("image/jpg"); /*get download button (tag: <a></a>) */ var a = document.getElementById("download"); /*insert chart image url to download button (tag: <a></a>) */ a.href = url_base64jp; });
🌐
YouTube
youtube.com › chart js
3. How to Render Images in the chartjs-plugin-labels in Chart js - YouTube
3. How to Render Images in the chartjs-plugin-labels in Chart jsIn this video we will explore how to render images in the plugin chartjs-plugin-labels in Cha...
Published   October 23, 2021
Views   4K
Find elsewhere
🌐
Chart.js
chartjs.org › docs › latest › developers › api.html
API | Chart.js
myLineChart.toBase64Image(); // => returns png data url of the image on the canvas myLineChart.toBase64Image('image/jpeg', 1) // => returns a jpeg data url in the highest quality of the canvas
🌐
Image-charts
documentation.image-charts.com › chart.js
Chart.js - Image-Charts documentation
How to use Chart.js server-side and generate images of chart.js charts.
🌐
CodeSandbox
codesandbox.io › examples › package › chartjs-to-image
chartjs-to-image examples - CodeSandbox
Use this online chartjs-to-image playground to view and fork chartjs-to-image example apps and templates on CodeSandbox.
🌐
Stack Overflow
stackoverflow.com › questions › 77307050 › export-chart-js-as-image
Export chart.js as image
var options = { type: 'line', data: { labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], datasets: [{ label: '# of Votes', data: [12, 19, 3, 5, 2, 3], borderColor: 'pink' }, { label: '# of Points', data: [7, 11, 5, 8, 3, 7], borderColor: 'orange' } ] }, options: { plugins: { customCanvasBackgroundColor: { color: 'pink' } } }, plugins: [{ id: 'customCanvasBackgroundColor', beforeDraw: (chart, args, options) => { const { ctx } = chart; ctx.save(); ctx.globalCompositeOperation = 'destination-over'; ctx.fillStyle = options.color || '#99ffff'; ctx.fillRect(0, 0, chart.width, chart.height); ctx.restore(); } }] } var ctx = document.getElementById('chartJSContainer').getContext('2d'); new Chart(ctx, options);
🌐
DEV Community
dev.to › ektaprasad › how-to-use-chartjs-with-nodejs-export-the-chart-as-an-image-f50
How to use Chart.js with Node.js and save it as an image - DEV Community
December 5, 2021 - For using the chartjs service, you will have to write: const canvasRenderService = new CanvasRenderService(width, height, chartCallback); Here you will be providing width, height, and the callback function.
🌐
YouTube
youtube.com › chart js
How to Download Canvas as Image on Button Click in Chart JS - YouTube
How to Download Canvas as Image on Button Click in Chart JSIn this video we will explore how to download canvas as image on button click in chart js. Chart j...
Published   January 19, 2022
Views   8K
🌐
Medium
prasadekta.medium.com › using-chart-js-in-nodejs-and-exporting-it-as-image-81fb8a492058
Using Chart.js in Nodejs server and exporting it as an image. | by Ekta Prasad | Medium
May 20, 2023 - For using the chartjs service, you will have to write: const canvasRenderService = new CanvasRenderService(width, height, chartCallback); Here you will be providing width, height, and the callback function.
🌐
CodeProject
codeproject.com › Tips › 1120045 › Export-Chart-js-Charts-as-Image
Export Chart.js Charts as Image - CodeProject
// // var url_base64jp = document.getElementById("myChart").toDataURL("image/jpg"); // Add one anchor tag in your HTML. // // <a id="link2" download="ChartJpg.jpg">Save as jpg</a> // Set that converted base64 string as a href for a anchor tag.
🌐
GitHub
github.com › chartjs › Chart.js › discussions › 10210
Save chart as image without filling the canvas · chartjs/Chart.js · Discussion #10210
Please reload this page. ... No you can not do that, you will always need to have a canvas on which it gets drawn. ... (Chromium browsers only) You can use the ofscreen canvas API to render the chart and then download it
Author   chartjs
🌐
CodePen
codepen.io › AdaNothing › pen › vYGeyKg
ChartJS to image
If you want to add classes there that can affect the whole document, this is the place to do it.