« jQuery Comparative Histogram Plugin | Main | Lazy Google App Engine Model Migrations »

Flot Multi-series Bar Graph Support

Hey boys and girls. I just got finished coding support for Multi-series bar graphs into Flot (a jQuery based JavaScript graphing library). Hence, I figured I would share not only the code but a little preview of it below. The code for it can be found at my GitHub account here. Please enjoy the little demo below.

Mouse hovers at (0, 0).

With the multi-series bar graph support, you can have Flot display the series side by side. This is useful when you are interested in visually comparing series and grouping segments you want to columns in series that you are specifically interested in comparing.

The above demo was produced by defining a few code snippets. The first snippet is a snippet of HTML that includes the customized Flot javascript source file. Note: The Flot javascript source file needs to come after the standard jQuery javascript source file. An example of this snippet is as follows:

<!--[if IE]><script language="javascript" type="text/javascript" src="../excanvas.min.js"></script><![endif]-->
<script language="javascript" type="text/javascript" src="../jquery.js"></script>
<script language="javascript" type="text/javascript" src="../jquery.flot.js"></script>

Note: In the above snippet the href values are likely to be different in your case. The second code snippet is simply a single line of HTML that is the div that is to be the Multi-series Bar Graph. It is key that this div has an id that is easily referenced and a set height and width. An example of this snippet is as follows:

<div id="placeholder" style="width: 375px; height: 200px;"></div>

The third code snippet needed is the javascript that defines the data, creates the Multi-series Bar Graph in the specified div, and handles the hover functionality. An example of this is seen below.

<script id="source">
$(function () {
  var ms_data = [{"label":"FOO","data":[[0,80],[1,70],[2,100],[3,60],[4,102]]},
                 {"label":"BAR","data":[[0,10],[1,20],[2,30],[3,40],[4,80]]},
                 {"label":"CAR","data":[[0,5],[1,10],[2,15],[3,20],[4,25]]}]
  var ms_ticks = [[0,"1/28"],[1,"1/29"],[2,"1/30"],[3,"1/31"],[4,"1/32"]];
    
    function plotWithOptions() {
      $.plot($("#placeholder"), ms_data, {
        bars: { show: true, barWidth: 0.6, series_spread: true, align: "center" },
        xaxis: { ticks: ms_ticks, autoscaleMargin: .10 },
        grid: { hoverable: true, clickable: true }
      });
    }
    
		function showTooltip(x, y, contents) {
        $('
' + contents + '
').css( { position: 'absolute', display: 'none', top: y + 5, left: x + 5, border: '1px solid #fdd', padding: '2px', 'background-color': '#fee', opacity: 0.80 }).appendTo("body").show(); } plotWithOptions(); $("#placeholder").bind("plothover", function (event, pos, item) { $("#x").text(pos.x.toFixed(2)); $("#y").text(pos.y.toFixed(2)); if (item) { if (previousPoint != item.datapoint) { previousPoint = item.datapoint; $("#tooltip").remove(); var x = item.datapoint[0].toFixed(2), y = item.datapoint[1].toFixed(2); showTooltip(item.pageX, item.pageY, item.series.label + " Group id: " + Math.floor(x) + ", y = " + y + ", seriesIndex: " + item.seriesIndex); } } else { $("#tooltip").remove(); previousPoint = null; } }); $("#placeholder").bind("plotclick", function (event, pos, item) { if (item) { $("#clickdata").text("You clicked bar " + item.dataIndex + " in " + item.series.label + "."); } }); }); </script>
Hopefully, the demo and quick code example helps get you up and running.

Reader Comments (5)

Hi! I'm using your plugin and it works all good.
Thanks!

January 25, 2012 | Unregistered CommenterLaurent Delcambre

This has been very helpful.

Thanks.

February 17, 2012 | Unregistered CommenterSamuel

Thank you so much for this wonderful support.
I am wondering why this is not supported in latest jquery.flot.js?
I have compared flot js used here in your demo and the latest one available, there are lot of differences.
Am I missing something. Your demo failing with flot version 0.7. (Javascript plotting library for jQuery, v. 0.7.)
Please add this support to latest version.

Thanks again

March 10, 2012 | Unregistered CommenterPrasad

Hey Drew,

First off thanks for posting this to your blog. Like Prasad (from the previous post) I noticed that your multi-series bar graph support is tightly coupled to the jquery.flot.js 0.5 library. Do you have any plans to make your work available as a plugin that would work with jquery.flot.js 0.7?

Thanks and best regards,

/Joe

March 15, 2012 | Unregistered CommenterJoe Finsterwald

Hey Joe,

The project I built this flot plugin for is no longer on my radar so I have not been keeping up with it. Hence, why it is not working with the latest flot. I would love to but at the moment I really don't have time. If you would like to tackle it I would gladly help by answering questions.

April 25, 2012 | Registered CommenterAndrew De Ponte

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>