import { createCanvas } from "https://deno.land/x/canvas/mod.ts";
import * as chartjs from 'https://cdn.skypack.dev/chart.js';
function once(f) {
let executed = false;
return () => {
if (executed) return;
executed = true;
f();
};
}
const globalInit = once(() => chartjs.Chart.register(...chartjs.registerables));
class DenoPlatform extends chartjs.BasePlatform {
acquireContext(canvas, _) {
return canvas.getContext("2d");
}
}
export function render(width, height, chartjsConfig, imageType="image/png") {
globalInit();
const canvas = createCanvas(width, height);
chartjsConfig.options = chartjsConfig.options || {};
chartjsConfig.options.responsive = false;
chartjsConfig.options.animation = false;
const oldConsoleError = globalThis.console.error;
globalThis.console.error = () => {};
const chart = new chartjs.Chart(
canvas,
{
...chartjsConfig,
platform: DenoPlatform,
},
);
globalThis.console.error = oldConsoleError;
chart.canvas = canvas;
chart.width = width;
chart.height = height;
chart._initialize();
chart.update();
return canvas.toBuffer(imageType);
}