Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Created November 20, 2010 21:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfsiii/708166 to your computer and use it in GitHub Desktop.
Save jfsiii/708166 to your computer and use it in GitHub Desktop.
Creating a Flot chart using node-canvas
var document = require("jsdom").jsdom(),
window = document.createWindow(),
jQuery = require('jquery').create(window),
fs = require('fs'),
script = document.createElement("script");
window.Canvas = require('canvas');
script.src = 'https://gist.github.com/raw/790339/3b0171c3e9b749bb93fff5d642eaa687f02939a5/jquery.flot.node-canvas.js';
script.onload = function ()
{
var i, d1 = [], d2 = [], d3 = [];
for (i = 10; --i;) d1.push([i, parseInt(Math.random() * 30)]);
for (i = 10; --i;) d2.push([i, parseInt(Math.random() * 30)]);
for (i = 10; --i;) d3.push([i, parseInt(Math.random() * 30)]);
var defaults = {
lines: { show: true, fill: true, steps: false },
bars: { show: false, barWidth: 0.6 }
};
var data = [
jQuery.extend(true, {}, defaults, {data: d1}),
jQuery.extend(true, {}, defaults, {data: d2}),
jQuery.extend(true, {}, defaults, {data: d3})
];
var options = {
width: 600, height: 300,
grid: {clickable: true, hoverable: true}
};
var placeholder = jQuery(''), // empty jQuery object
plot = jQuery.plot(placeholder, data, options),
node_canvas = plot.getCanvas(),
ctx = node_canvas.getContext('2d'),
out = fs.createWriteStream(__dirname + '/flot.png'),
stream = node_canvas.createPNGStream();
stream.on('data', function ( chunk ) {
out.write(chunk);
});
};
document.head.appendChild(script);
@mgan59
Copy link

mgan59 commented Jan 18, 2011

I have the necessary node modules installed via npm (htmlparser, jsdom, jquery, canvas) and I get the following error message

evalmachine.:1

^
SyntaxError: Unexpected token <
at Object.javascript (/usr/local/lib/node/.npm/jsdom/0.1.20/package/lib/jsdom/level2/languages/javascript.js:5:37)
at IncomingMessage. (/usr/local/lib/node/.npm/jsdom/0.1.20/package/lib/jsdom/level2/html.js:989:37)
at IncomingMessage.emit (events.js:48:20)
at HTTPParser.onMessageComplete (http.js:110:23)
at Client.onData as ondata
at Client._onReadable (net.js:618:27)
at IOWatcher.onReadable as callback
18 Jan 12:27:23 - function ( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
}

can you confirm if this gist still works, or did it break with recent changes to node or the dependancies listed?

@jfsiii
Copy link
Author

jfsiii commented Jan 21, 2011

The script works when run from my computer :)

I think https://gist.github.com/708166#LID8 script.src = 'http://localhost/~jschulz/node-flot/jquery.flot.svn.js'; is the source of your issues. You're likely trying to load a file you down have (jquery.flot.svn.js) have from a non-existent directory (~jschulz/node-flot/ in the web root of the computer running the script).

https://gist.github.com/708172 shows the changes I made to SVN revision 270 of jquery.flot.js. I've also put the patched file up at https://gist.github.com/790339.

Just save that file somewhere Web accessible and change script.src to that address.

@viera00
Copy link

viera00 commented Feb 4, 2011

Hi, I'm having a segmentation fault when trying to execute node-flot.js.

Did you experienced the same problem ? Any clue where I can investigate ?

Thanks.

GV.

@jfsiii
Copy link
Author

jfsiii commented Feb 7, 2011

I did a quick test and it ran fine in 0.2.6 but segfaulted with 0.3.8. Some quick debugging showed it failing after the call to jQuery.plot.

I'll do some more digging soon, especially with node 0.4 right around the corner, but I think the issue is with node-canvas.

@mgan59
Copy link

mgan59 commented Feb 22, 2011

I've done some follow up with this and took a different approach using jsdom.jquerify as a wrapper. Unfortunately, I ran into a dispatchEvent issue with my node-canvas going through jsdom's level2-events. Will require further investigation but I do believe there may be some integration issues with node-canvas.

@jfsiii
Copy link
Author

jfsiii commented Feb 24, 2011

@mgan59 see my updated gist @ https://gist.github.com/842175 which uses jsdom 0.2.0

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