You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

37 lines
968 B

const { createCanvas, registerFont } = require('canvas');
const Chart = require('chart.js/auto');
// Create a canvas and chart
const width = 800;
const height = 400;
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
const chartConfig = {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [
{
label: 'My First Dataset',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: ['red', 'blue', 'yellow', 'green', 'purple', 'orange'],
},
],
},
options: {
responsive: false,
maintainAspectRatio: false,
},
};
new Chart(ctx, chartConfig);
// Save the chart image as a file
const fs = require('fs');
const out = fs.createWriteStream('chart.png');
const stream = canvas.createPNGStream();
stream.pipe(out);
out.on('finish', () => console.log('The chart image was saved.'));