This is a Javascript client for quickchart.io, a web service for generating static charts. View the main QuickChart repository here.
If you are using npm:
npm install quickchart-js
This library provides a QuickChart object. Import it, instantiate it, and set your Chart.js config:
const QuickChart = require('quickchart-js');
const myChart = new QuickChart();
myChart.setConfig({
type: 'bar',
data: { labels: ['Hello world', 'Foo bar'], datasets: [{ label: 'Foo', data: [1, 2] }] },
});
Use getUrl()
on your quickchart object to get the encoded URL that renders your chart:
console.log(myChart.getUrl());
// Prints: https://quickchart.io/chart?c=%7Btype%3A%27bar%27%2Cdata%3A%7Blabels%3A%5B%27Hello+world%27%2C%27Foo+bar%27%5D%2Cdatasets%3A%5B%7Blabel%3A%27Foo%27%2Cdata%3A%5B1%2C2%5D%7D%5D%7D%7D&w=500&h=300&bkg=transparent&f=png
If you have a large or complicated chart, use getShortUrl()
on your quickchart object to get a fixed-length URL using the quickchart.io web service:
const url = await myChart.getShortUrl();
console.log(url);
// Prints: https://quickchart.io/chart/render/f-a1d3e804-dfea-442c-88b0-9801b9808401
Or write it to disk:
myChart.toFile('/tmp/mychart.png');
The URLs produce this chart image:
If you have an account ID and API key, authenticate using the QuickChart constructor:
const qc = new QuickChart(apiKey, accountId);
To use the free (community) version, leave it blank:
const qc = new QuickChart();
Use this config to customize the Chart.js config object that defines your chart. You must set this before generating a URL!
Sets the width of the chart in pixels. Defaults to 500.
Sets the height of the chart in pixels. Defaults to 300.
Sets the format of the chart. Defaults to png
. svg
is also valid.
Sets the background color of the chart. Any valid HTML color works. Defaults to #ffffff
(white). Also takes rgb
, rgba
, and hsl
values.
Sets the device pixel ratio of the chart. This will multiply the number of pixels by the value. This is usually used for retina displays. Defaults to 1.0.
Sets the Chart.js version to use (e.g. 2.9.4
or 3.4.0
). Valid options are shown in the documentation.
Sets the host of generated URLs. quickchart.io
by default.
Sets the scheme of generated URLs. https
by default.
There are two ways to get a URL for your chart object.
Returns a URL that will display the chart image when loaded.
Uses the quickchart.io web service to create a fixed-length chart URL that displays the chart image. The Promise resolves with a URL such as https://quickchart.io/chart/render/f-a1d3e804-dfea-442c-88b0-9801b9808401
.
Note that short URLs expire after a few days for users of the free service. You can subscribe to keep them around longer.
Returns a URL that displays the chart image. It is signed with your user account to bypass rate limitations.
Creates a binary buffer that contains your chart image.
Returns a base 64 data URL beginning with data:image/png;base64
.
Given a filepath string or a writable file handle, creates a file containing your chart image.
Check out the examples/
directory to see other usage. Here's a simple test that uses some of the custom parameters:
const qc = new QuickChart();
qc.setConfig({
type: 'bar',
data: { labels: ['Hello world', 'Foo bar'], datasets: [{ label: 'Foo', data: [1, 2] }] },
});
qc.setWidth(500).setHeight(300).setBackgroundColor('transparent');
console.log(qc.getUrl());
// https://quickchart.io/chart?c=%7Btype%3A%27bar%27%2Cdata%3A%7Blabels%3A%5B%27Hello+world%27%2C%27Foo+bar%27%5D%2Cdatasets%3A%5B%7Blabel%3A%27Foo%27%2Cdata%3A%5B1%2C2%5D%7D%5D%7D%7D&w=500&h=300&bkg=transparent&f=png
Here's a more complicated chart that includes some Javascript:
qc.setConfig({
type: 'bar',
data: {
labels: ['January', 'February', 'March', 'April', 'May'],
datasets: [
{
label: 'Dogs',
data: [50, 60, 70, 180, 190],
},
],
},
options: {
scales: {
yAxes: [
{
ticks: {
callback: function (value) {
return '$' + value;
},
},
},
],
},
},
});
qc.setWidth(500).setHeight(300).setBackgroundColor('#0febc2');
console.log(qc.getUrl());
// https://quickchart.io/chart?c=%7Btype%3A%27bar%27%2Cdata%3A%7Blabels%3A%5B%27January%27%2C%27February%27%2C%27March%27%2C%27April%27%2C%27May%27%5D%2Cdatasets%3A%5B%7Blabel%3A%27Dogs%27%2Cdata%3A%5B50%2C60%2C70%2C180%2C190%5D%7D%5D%7D%2Coptions%3A%7Bscales%3A%7ByAxes%3A%5B%7Bticks%3A%7Bcallback%3Afunction+%28value%29+%7B%0A++return+%27%24%27+%2B+value%3B%0A%7D%7D%7D%5D%7D%7D%7D&w=500&h=300&bkg=%230febc2&f=png
As we customize these charts, the URLs are getting a little long for my liking. There's a getShortUrl
function that uses the QuickChart.io web service to generate a short(er), fixed-length URL:
// Fill the chart with data from 0 to 100
const data = [...Array(100).keys()];
qc.setConfig({
type: 'bar',
data: { labels: ['Hello world', 'Foo bar'], datasets: [{ label: 'Foo', data }] },
});
async function printShortUrl() {
const url = await qc.getShortUrl();
console.log(url);
}
printShortUrl();
// https://quickchart.io/chart/render/f-a1d3e804-dfea-442c-88b0-9801b9808401
QuickChart has builtin functions: getImageFill
, getGradientFill
, getGradientFillHelper
, and pattern.draw
. These functions can be accessed via the QuickChart
class. For example:
const qc = new QuickChart();
qc.setConfig({
type: 'bar',
data: {
labels: ['Hello world', 'Foo bar'],
datasets: [
{
label: 'Foo',
data: [1, 2],
backgroundColor: QuickChart.getGradientFillHelper('horizontal', ['red', 'green']),
},
],
},
});
To build this library locally, run:
yarn build
To run tests:
yarn test
If you're editing the library and running examples, you may want to continuously build the library in the background:
yarn build:watch
# ...
node examples/simple_example.js