Created
February 14, 2011 16:57
-
-
Save anonymous/826156 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var jsdom = require('jsdom'), | |
spawn = require('child_process').spawn; | |
function createHighchartsWindow(callback) { | |
var window = jsdom.jsdom().createWindow(), | |
script = window.document.createElement('script'); | |
// Convince Highcharts that our window supports SVG's | |
window.SVGAngle = true; | |
// jsdom doesn't yet support createElementNS, so just fake it up | |
window.document.createElementNS = function(ns, tagName) { | |
var elem = doc.createElement(tagName); | |
elem.getBBox = function() { | |
return { | |
x: elem.offsetLeft, | |
y: elem.offsetTop, | |
width: elem.offsetWidth, | |
height: elem.offsetHeight | |
}; | |
}; | |
return elem; | |
}; | |
// Load scripts | |
jsdom.jQueryify(window,'http://code.jquery.com/jquery-1.4.1.js', function(w,jq) { | |
script.src = 'file://' + __dirname + '/highcharts/highcharts.src.js'; | |
script.onload = function() { | |
if (this.readyState === 'complete') { | |
callback(window); | |
} | |
} | |
}); | |
} | |
function render(options, completed) { | |
createHighchartsWindow(function(window) { | |
var $ = window.jQuery, | |
Highcharts = window.Highcharts, | |
document = window.document, | |
$container = $('<div id="container" />'), | |
chart, svg, convert, buffer; | |
$container.appendTo(document.body); | |
// merge options | |
var totalOptions = options[0]; | |
for (i = 1; i < options.length; ++i) { | |
$.extend(true, totalOptions, options[i]); | |
} | |
chart = new Highcharts.Chart(totalOptions); | |
svg = $container.children().html(); | |
// Start convert | |
convert = spawn('convert', ['svg:-', '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) { | |
var prevBufferLength = (buffer ? buffer.length : 0), | |
newBuffer = new Buffer(prevBufferLength + data.length); | |
if (buffer) { | |
buffer.copy(newBuffer, 0, 0); | |
} | |
data.copy(newBuffer, prevBufferLength, 0); | |
buffer = newBuffer; | |
}); | |
// When we're done, we're done | |
convert.on('exit', function(code) { | |
completed(buffer); | |
console.log("done 3"); | |
}); | |
}); | |
} | |
exports.render = render; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hc = require('../lib/node-highcharts'); | |
var fs = require('fs'); | |
var options = new Array(); | |
options.push({ | |
series: [{ | |
animation: false, | |
data: [1,2,3,4,5] | |
}] | |
}); | |
options.push({ | |
chart: { | |
defaultSeriesType: 'column', | |
renderTo: 'container', | |
renderer: 'SVG', | |
width: 800, | |
height: 600 | |
} | |
}); | |
hc.render(options, function(result, err) { | |
fs.writeFile('chart.png', 'result', function() { console.log('done 2'); }); | |
}); | |
console.log("done 1"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment