QuickChart JavaScript Client
quickchart-js is the official JavaScript client library for QuickChart.io, a web service that renders static Chart.js images from a URL or POST request. It allows developers to programmatically generate highly customizable charts for various applications like email, SMS, or chat platforms. The library wraps the QuickChart API, simplifying the process of setting chart configurations (based on Chart.js syntax), dimensions, formats, and retrieving either long encoded URLs, short shareable URLs, or saving charts directly to a file. The current stable version is 3.1.3, with updates released periodically, typically every few months, reflecting active maintenance. A key differentiator is its ability to convert dynamic Chart.js configurations into static images, offering robust integration options where dynamic JavaScript charting is not feasible.
Common errors
-
TypeError: QuickChartClient is not a constructor
cause Attempting to use the old class name `QuickChartClient` after upgrading to `quickchart-js` v2.0.0 or later.fixChange `new QuickChartClient()` to `new QuickChart()`. -
Error: ENOSPC: no space left on device, write
cause The system lacks sufficient disk space to write the chart image to a file using `toFile()`.fixFree up disk space or choose a different output location with enough available storage. -
SyntaxError: Unexpected token 'function'
cause Passing a Chart.js configuration object directly to `setConfig()` that contains JavaScript functions (e.g., in `options.scales.yAxes.ticks.callback`) without stringifying it.fixStringify your chart configuration object if it contains JavaScript functions: `myChart.setConfig(JSON.stringify(myChartConfigObject));`
Warnings
- breaking Version 3.0.0 dropped support for Node.js 12. Ensure your environment runs Node.js 14 or higher when upgrading to `quickchart-js` v3.x.
- breaking In version 2.0.0, the main class name was changed from `QuickChartClient` to `QuickChart`. Older code using `new QuickChartClient()` will break.
- gotcha Short URLs generated by `getShortUrl()` for free tier users expire after a few days. For persistent short URLs, a QuickChart.io subscription is required.
- gotcha When using Chart.js functions (e.g., custom callbacks for tooltips or axis labels) within your chart configuration, the `setConfig` method expects the configuration to be a string, not a plain JavaScript object. The library internally uses `javascript-stringify` to handle this.
- gotcha The free (community) tier of QuickChart.io has limitations on certain Chart.js features and plugins due to compute restrictions. Access to the global `Chart` object and some advanced plugin properties might be restricted.
Install
-
npm install quickchart-js -
yarn add quickchart-js -
pnpm add quickchart-js
Imports
- QuickChart
const QuickChart = require('quickchart-js');import { QuickChart } from 'quickchart-js'; - QuickChart
new QuickChart({ apiKey: '...', accountId: '...' });new QuickChart(apiKey, accountId);
- QuickChart.getGradientFillHelper
myChart.getGradientFillHelper(...);
QuickChart.getGradientFillHelper('horizontal', ['red', 'green']);
Quickstart
import { QuickChart } from 'quickchart-js';
import * as fs from 'fs/promises';
const myChart = new QuickChart();
myChart.setConfig({
type: 'bar',
data: {
labels: ['January', 'February', 'March', 'April', 'May'],
datasets: [
{ label: 'Dogs', data: [50, 60, 70, 180, 190] },
{ label: 'Cats', data: [100, 200, 300, 400, 500] }
],
},
options: {
plugins: {
legend: { position: 'top' },
title: { display: true, text: 'Monthly Pet Counts' },
},
},
});
myChart.setWidth(800);
myChart.setHeight(400);
myChart.setFormat('png');
myChart.setBackgroundColor('#f8f8f8');
async function generateCharts() {
// Get the direct URL
console.log('Direct Chart URL:', myChart.getUrl());
// Get a short, fixed-length URL (requires network request)
try {
const shortUrl = await myChart.getShortUrl();
console.log('Short Chart URL:', shortUrl);
} catch (error) {
console.error('Failed to get short URL:', error);
}
// Write the chart to a file
const filePath = './mychart.png';
try {
await myChart.toFile(filePath);
console.log(`Chart saved to ${filePath}`);
} catch (error) {
console.error('Failed to save chart to file:', error);
}
}
generateCharts();