UNPKG

chartjs-node-canvas

Version:
129 lines 6.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const canvas_1 = require("canvas"); const freshRequire_1 = require("./freshRequire"); const defaultChartJsFactory = () => freshRequire_1.freshRequire('chart.js'); class CanvasRenderService { /** * Create a new instance of CanvasRenderService. * * @param width The width of the charts to render, in pixels. * @param height The height of the charts to render, in pixels. * @param chartCallback optional callback which is called once with a new ChartJS global reference as the only parameter. * @param type optional The canvas type ('PDF' or 'SVG'), see the [canvas pdf doc](https://github.com/Automattic/node-canvas#pdf-output-support). * @param chartJsFactory optional provider for chart.js. */ constructor(width, height, chartCallback, type, chartJsFactory) { this._width = width; this._height = height; this._chartJs = (chartJsFactory || defaultChartJsFactory)(); //this._createCanvas = freshRequire('canvas', require).createCanvas; this._type = type; if (chartCallback) { chartCallback(this._chartJs); } } /** * Render to a data url. * @see https://github.com/Automattic/node-canvas#canvastodataurl * * @param configuration The Chart JS configuration for the chart to render. * @param mimeType The image format, `image/png` or `image/jpeg`. */ renderToDataURL(configuration, mimeType = 'image/png') { const chart = this.renderChart(configuration); return new Promise((resolve, reject) => { const canvas = chart.canvas; canvas.toDataURL(mimeType, (error, png) => { chart.destroy(); if (error) { return reject(error); } return resolve(png); }); }); } /** * Render to a data url synchronously. * @see https://github.com/Automattic/node-canvas#canvastodataurl * * @param configuration The Chart JS configuration for the chart to render. * @param mimeType The image format, `image/png` or `image/jpeg`. */ renderToDataURLSync(configuration, mimeType = 'image/png') { const chart = this.renderChart(configuration); const canvas = chart.canvas; chart.destroy(); const dataUrl = canvas.toDataURL(mimeType); return dataUrl; } /** * Render to a buffer. * @see https://github.com/Automattic/node-canvas#canvastobuffer * * @param configuration The Chart JS configuration for the chart to render. * @param mimeType A string indicating the image format. Valid options are `image/png`, `image/jpeg` (if node-canvas was built with JPEG support), `raw` (unencoded ARGB32 data in native-endian byte order, top-to-bottom), `application/pdf` (for PDF canvases) and image/svg+xml (for SVG canvases). Defaults to `image/png` for image canvases, or the corresponding type for PDF or SVG canvas. */ renderToBuffer(configuration, mimeType = 'image/png') { const chart = this.renderChart(configuration); return new Promise((resolve, reject) => { const canvas = chart.canvas; canvas.toBuffer((error, buffer) => { chart.destroy(); if (error) { return reject(error); } return resolve(buffer); }, mimeType); }); } /** * Render to a buffer synchronously. * @see https://github.com/Automattic/node-canvas#canvastobuffer * * @param configuration The Chart JS configuration for the chart to render. * @param mimeType A string indicating the image format. Valid options are `image/png`, `image/jpeg` (if node-canvas was built with JPEG support), `raw` (unencoded ARGB32 data in native-endian byte order, top-to-bottom), `application/pdf` (for PDF canvases) and image/svg+xml (for SVG canvases). Defaults to `image/png` for image canvases, or the corresponding type for PDF or SVG canvas. */ renderToBufferSync(configuration, mimeType = 'image/png') { const chart = this.renderChart(configuration); const canvas = chart.canvas; const buffer = canvas.toBuffer(mimeType); chart.destroy(); return buffer; } /** * Render to a stream. * @see https://github.com/Automattic/node-canvas#canvascreatepngstream * * @param configuration The Chart JS configuration for the chart to render. * @param mimeType A string indicating the image format. Valid options are `image/png`, `image/jpeg` (if node-canvas was built with JPEG support), `application/pdf` (for PDF canvases) and image/svg+xml (for SVG canvases). Defaults to `image/png` for image canvases, or the corresponding type for PDF or SVG canvas. */ renderToStream(configuration, mimeType = 'image/png') { const chart = this.renderChart(configuration); const canvas = chart.canvas; chart.destroy(); switch (mimeType) { case 'image/png': return canvas.createPNGStream(); case 'image/jpeg': return canvas.createJPEGStream(); case 'application/pdf': return canvas.createPDFStream(); default: throw new Error(`Un-handled mimeType: ${mimeType}`); } } renderChart(configuration) { //const canvas = this._createCanvas(this._width, this._height, this._type); const canvas = canvas_1.createCanvas(this._width, this._height, this._type); canvas.style = {}; // Disable animation (otherwise charts will throw exceptions) configuration.options = configuration.options || {}; configuration.options.responsive = false; configuration.options.animation = false; const context = canvas.getContext('2d'); return new this._chartJs(context, configuration); } } exports.CanvasRenderService = CanvasRenderService; //# sourceMappingURL=index.js.map