views:

335

answers:

3

I'm wondering if there are any 2D graph plotting libraries that run on the client side using JavaScript? The basic idea is that you could put up a plot in a browser and the user can change things like X and Y scales and limits, zooming in and out, without having to constantly reload the web page from the server. The data itself would be fetched via AJAX, which would allow a user to also just wget the data straight from the server if they want to use heavy-duty tools. Something like the 2D part of matplotlib for Python.

This is something I took a look at a long time ago before deciding that it was faster to develop some code that just generated an SVG on the server side (using a built-in eCos web server), but now that I've been reading up on things like Prototype and jQuery, I'm wondering if someone has gone and done this already.

+3  A: 

You may be interested in trying Flot. Flot is a pure Javascript open-source plotting library for jQuery. It produces graphical plots of arbitrary datasets on-the-fly on the client-side.

First of all, make sure to check the following example which uses data fetched via AJAX to plot a chart in rea-ltime:

This is how fetching and plotting the data with AJAX would look like in code:

function fetchData() {

    function onDataReceived(series) {
        data = [ series ];

        $.plot($("#placeholder"), data, options);
    }

    $.ajax({
        url:      "data_feed.php",
        method:   "GET",
        dataType: "json",
        success:  onDataReceived
    });

    setTimeout(fetchData, 1000);
}

Stack Overflow also uses Flot for the zoomable chart in the Reputation tab of user profiles.

For further information on Flot:

Flot example with zooming overview

Daniel Vassallo
I was looking into this same issue a while back and Flot kept coming up on top for me, don't remember why though.
Jason
There also seems to be a Flotr (http://solutoire.com/flotr/) that runs on Prototype instead of jQuery. I know I can pack Prototype into 36k or so, but I don't know jQuery's footprint, so I haven't settled on one library or the other yet. Good to see that there are graphing options for both.
Mike DeSimone
jQuery footprint is 23k gzipped and minified, and 74k just minified. If you serve jQuery (or prototype) from the Google AJAX Libraries CDN, most of your users may not need to download jQuery at all, since it will probably be already cached in their browsers: http://code.google.com/apis/ajaxlibs/.
Daniel Vassallo
1) What do you mean by "minified"? I didn't do that with Prototype. 2) Can't use Google since it needs to be *completely* self-contained, but thanks for the tip.
Mike DeSimone
You can download jQuery 1.4 from http://code.jquery.com/jquery-1.4.min.js. If you open it you'll see that it has been "compressed" by removing all unnecessary characters and whitespace from the source code. This minified version has a 74k footprint. If your web-server has gzip compression enabled, you would be sending 23k to the browsers.
Daniel Vassallo
A: 

This web page compares different javascript plotting tools.

http://sixrevisions.com/javascript/graph_chart_plot_data_javascript/

FLOT: http://code.google.com/p/flot/

Jason
+3  A: 

http://g.raphaeljs.com/ - cross browser compatible JS charting library.

Rudi
+1 I really like Raphael because SVG/VML elements are just regular DOM nodes that you can attach events to an manipulate after the fact.
slebetman
http://g.raphaeljs.com/reference.html -- No docs. ;_; (Line charts are all I need, the pie, bar, and funky symbol charts are dead weight. Also trying to see if it can do logarithmic scales.)
Mike DeSimone