Using NodeJs to render JavaScript charts on the server

Using NodeJs to render JavaScript charts on the server

var $ = window.jQuery,
Highcharts = window.Highcharts,
document = window.document,
$container = $('<div id="container" />'),
chart;

$container.appendTo(document.body);

chart = new Highcharts.Chart({
chart: {
defaultSeriesType: chartType,
renderTo: $container[0],
renderer: 'SVG',
width: width,
height: height
},
series: [{
animation: false,
data: data
}]
});

svg = $container.children().html();

 

this.server = http.createServer(function(request, response) {
	var url = parse(request.url, true),
		chartTypeMatch = /^\/(\w+)$/.exec(url.pathname),
		chartType	= chartTypeMatch ? chartTypeMatch[1] : null,

	/* Some code omitted */
	createHighchartsWindow(function(window) {
		/* chart generation from above */

		svg = $container.children().html();

		// Start convert reading in an svg and outputting a png
		convert	= spawn('convert', ['svg:-', 'png:-']);

		// We're writing an image, hopefully...
		response.writeHeader(200, {'Content-Type': 'image/png'});

		// Pump in the svg content
		convert.stdin.write(svg);
		convert.stdin.end();

		// Write the output of convert straight to the response
		convert.stdout.on('data', function(data) {
			response.write(data);
		});

		// When we're done rendering, we're done
		convert.on('exit', function(code) {
			response.end();
		});
	});
}).listen(2308); // Start HTTP server listening on port 2308

 

 

 

This entry was posted in nodejs and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s