Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable running on node #5324

Merged
merged 1 commit into from Mar 10, 2018
Merged

Conversation

SeanSobey
Copy link
Contributor

@SeanSobey SeanSobey commented Mar 8, 2018

A pretty small change, which allows ChartJS to run on Node using a virtual canvas like node-canvas.

All that is required is that browser specific APIs are not referenced, like window or document. I only found 1 that was missing an undefined guard.

ChartJS can be used on node like so:

const ChartJs = require('chart.js');
const { createCanvas } = require('canvas');

function renderPNG(configuration) {
	
	const canvas = createCanvas(800, 600);	
	// Disable animation (otherwise charts will throw exceptions)
	configuration.options = configuration.options || {};
	configuration.options.responsive = false;
	configuration.options.animation = false;
	canvas.style = {};
	const context = canvas.getContext('2d');
	const chart = new ChartJs(context, configuration);
	return new Promise((resolve, reject) => {
                // or `pngStream` `toDataURL`, etc
		chart.canvas.toBuffer((error, buffer) => {
			if (error) {
				return reject(error);
			}
			return resolve(buffer);
		});
	});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants