import { ChartJs, Rect2D, SvgCanvas, SvgCanvas2DGradient } from "./deps.ts";
export type ChartOptions = Omit< ChartJs.ChartOptions, | "responsive" | "responsiveAnimationDuration" | "events" | "legendCallback" | "onHover" | "onClick" | "onResize" | "hover" | "animation">;
export interface ChartConfiguration { width?: number; height?: number; type?: ChartJs.ChartType; data?: ChartJs.ChartData; options?: ChartOptions; plugins?: ChartJs.PluginServiceRegistrationOptions[];}
interface SvgCanvasCanvas { canvas?: { width: number; height: number; style: Record<string, string>; };}
export function chart( { width = 768, height = 384, type, data, options = {} }: ChartConfiguration = {},): string { Object.assign(options, { animation: undefined, events: [], responsive: false, });
const ctx: SvgCanvas & SvgCanvasCanvas = new SvgCanvas(); ctx.canvas = { width, height, style: { width: `${width}px`, height: `${height}px` }, }; ctx.fontHeightRatio = 2; const el: HTMLCanvasElement = { getContext: () => ctx } as any; const savedGradient = globalThis.CanvasGradient; globalThis.CanvasGradient = SvgCanvas2DGradient as typeof CanvasGradient;
try { new ChartJs.Chart(el, { type, data, options }); } finally { if (savedGradient) { globalThis.CanvasGradient = savedGradient; } }
return ctx.render(new Rect2D(0, 0, width, height), "px");}