Skip to content

Server side image generator #9997

Answered by kurkle
kpetrow asked this question in Q&A
Dec 16, 2021 · 2 comments · 3 replies
Discussion options

You must be logged in to vote

I've used node-canvas before for this (based on cairo though).

npm install canvas chart.js

simple_test.js

const fs = require('fs');
const {createCanvas} = require('canvas');
const Chart = require('chart.js');
const canvas = createCanvas(512, 512);
const ctx = canvas.getContext('2d');
const chart = new Chart(ctx, {/* chart config*/});

function writePng(canvas, name) {
  return new Promise((resolve, reject) => {
    const file = fs.createWriteStream(path.join(__dirname, name));
    const stream = canvas.createPNGStream();
    stream.pipe(file);
    file.on('finish', () => {
      console.log(name + ' created.');
      resolve();
    });
  });
}

writePng(canvas, 'test.png').then(() => chart.

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@kpetrow
Comment options

@LeeLenaleee
Comment options

@kpetrow
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by kpetrow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
3 participants
Converted from issue

This discussion was converted from issue #9996 on December 16, 2021 18:15.