Chart.js Node.js Canvas Renderer

5.0.0 · active · verified Sun Apr 19

chartjs-node-canvas is a Node.js library that enables server-side rendering of Chart.js charts into static image files (PNG, JPEG). It achieves this by utilizing the `canvas` package, which provides a Canvas API implementation compatible with Node.js environments, bypassing browser-specific rendering requirements. The current stable version is 5.0.0, which maintains compatibility with Chart.js v3.x and v4.x. While there isn't a strict, rapid release cadence, the project is actively maintained, with regular updates for dependency bumps and Node.js version support. Its primary differentiator is providing a reliable, performant way to generate Chart.js images outside of a browser, essential for tasks like generating reports, social media share images, or email attachments from server-side applications. It integrates directly with Chart.js as a peer dependency, requiring Chart.js itself to be installed separately, and allowing developers to manage its version.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create an instance of `ChartJSNodeCanvas`, register necessary Chart.js components, define chart configuration, render it to a PNG buffer, and save it to a file on disk. This example shows a basic bar chart.

import { ChartJSNodeCanvas } from 'chartjs-node-canvas';
import { Chart, CategoryScale, LinearScale, BarController, BarElement, Title, Tooltip, Legend } from 'chart.js';
import { writeFile } from 'node:fs/promises';

// Register Chart.js components (required since Chart.js v3)
Chart.register(CategoryScale, LinearScale, BarController, BarElement, Title, Tooltip, Legend);

const width = 800; // px
const height = 600; // px
const backgroundColour = '#ffffff'; // Optional, defaults to white

const chartJSNodeCanvas = new ChartJSNodeCanvas({
  width,
  height,
  backgroundColour,
  plugins: { // Optional, global Chart.js plugins
    // id: 'customPlugin',
    // beforeDraw: (chart) => { /* ... */ }
  },
  chartCallback: (ChartJS) => {
    // This is a special hook to globally configure Chart.js (e.g., register custom fonts or plugins)
    // ChartJS.defaults.font.family = '"Custom Font", sans-serif';
  }
});

const configuration = {
  type: 'bar' as const,
  data: {
    labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
    datasets: [{
      label: 'Dataset 1',
      data: [65, 55, 80, 81, 56, 55, 40],
      backgroundColor: 'rgba(75, 192, 192, 0.6)'
    }, {
      label: 'Dataset 2',
      data: [28, 48, 40, 19, 86, 27, 90],
      backgroundColor: 'rgba(153, 102, 255, 0.6)'
    }]
  },
  options: {
    scales: {
      y: {
        beginAtZero: true
      }
    },
    plugins: {
      title: {
        display: true,
        text: 'Chart.js Bar Chart Example'
      }
    }
  }
};

(async () => {
  try {
    const buffer = await chartJSNodeCanvas.renderToBuffer(configuration);
    await writeFile('./chart.png', buffer);
    console.log('Chart saved to chart.png');
  } catch (error) {
    console.error('Error generating chart:', error);
  }
})();

view raw JSON →