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.

Copyvar 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
November 11, 2023 - 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 ...
      » npm install chartjs-to-image
    
Published   Nov 11, 2023
Version   1.2.2
🌐
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');
Discussions

Converting Chart.js canvas chart to image using .toDataUrl() results in blank image
The tutorial (http://www.chartjs.org/docs/) devotes an entire 1 line on the topic: The canvas element also allows for saving the contents as a base 64 string, allowing saving the chart as an image. A canvas element has the method of toDataURL, which returns a base64 string of the image. However, when I do that, the image it renders ... More on stackoverflow.com
🌐 stackoverflow.com
Render chartjs and export image to data not in the DOM
I am using chart.js from a frontend react application. I need to create a plot with chart.js so that I can attach it to a powerpoint slide with pptxgen. This image does not have to be rendered visu... More on stackoverflow.com
🌐 stackoverflow.com
Export chart.js as image - javascript
I know that is complicated to generate a chart with different colors for different contexts. My approach when I have such cases is to render again the chart under a hidden div and to use this one when I have to generate the image. More on stackoverflow.com
🌐 stackoverflow.com
how to re render vuechartjs in composition API
Just add an incremental key to the component? Click the button, increment the counter will automatically re-render the component. https://michaelnthiessen.com/force-re-render/ More on reddit.com
🌐 r/vuejs
2
10
May 3, 2023
🌐
GitHub
github.com › image-charts › chartjs-image-javascript
GitHub - image-charts/chartjs-image-javascript: Render Chart.JS chart as image · GitHub
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 · GitHub
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 7 users
Languages   JavaScript
🌐
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
🌐
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.
🌐
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
Find elsewhere
🌐
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; });
🌐
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.
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.

Copy
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:

Copylet 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)
Copy<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.js"></script>
</body>
Run code snippetEdit code snippet Hide Results Copy to answer Expand

🌐
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.
🌐
Rendex
rendex.dev › home › blog › render a chart to an image on the server (node + python, 2026)
Render a Chart to an Image on the Server (Node + Python, 2026) | Rendex
3 weeks ago - You gain: no node-gyp, no libcairo/libpango in your image, no Chromium-in-CI, no font-package debugging, no kaleido-version breakage. The canonical Node route renders Chart.js onto a node-canvas surface — no browser, but you inherit node-canvas's native Cairo/Pango dependency. ... // npm i chartjs-node-canvas chart.js // + system libs: apt-get install libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev import { ChartJSNodeCanvas } from "chartjs-node-canvas"; import { writeFileSync } from "fs"; const canvas = new ChartJSNodeCanvas({ width: 800, height: 400 }); const png = await canvas.renderToBuffer({ type: "bar", data: { labels: ["Q1","Q2","Q3","Q4"], datasets: [{ label: "Revenue", data: [482,591,623,705] }] }, }); writeFileSync("chart.png", png);
🌐
Stack Overflow
stackoverflow.com › questions › 77307050 › export-chart-js-as-image
Export chart.js as image - javascript
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);
🌐
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
🌐
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
🌐
CanvasJS
canvasjs.com › home › docs › charts › methods & properties › chart › exportchart()
exportChart - export the chart as image | CanvasJS JavaScript Charts
September 17, 2024 - Export the chart as image (jpg / png). Parameters: options: { format: String (“jpg” | “png”), toDataURL: Boolean (default false), fileName: String } Example: chart.exportChart({format: “jpg”}); Note: Chart should be rendered before you can use this method.
🌐
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.