jqBarGraph – jQuery graph plugin

jqBarGraph is jQuery plugin that gives you freedom to easily display your data as graphs. There are three types of graphs: simple, multi and stacked. All you have to do is to pass your data to this plugin. This plugin is compatible and fully tested with Safari 2+, Internet Explorer 6+, Firefox 2+, Google Chrome 3+, Opera 9+.

jQuery Bar Graph

Of course, there are a lot of settings to tune appearance of your graph, as are animation, color scheme, dimensions etc. Also, you can additionally change look and feel of graph via CSS.

You will need jQuery in order to make jqBarGraph working. First, you need to download jQuery and jqBarGraph plugin and include them on your page. Similar to this:

<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jqBarGraph.js" type="text/javascript"></script>

Now, we need array of data from which we want to create graph. There is two type of data array for jqBarGraph. First is for simple bar graph type, and second is for multi and stacked bar type.

For simple bar graph type data for one bar should look like this: [value, label, color]. Label and color are optional. This is example for simple bar graph type:

 arrayOfData = new Array(
[10.3,'Jan','#f3f3f3'],
[15.2,'Feb','#f4f4f4'],
[13.1,'Mar','#cccccc'],
[16.3,'Apr','#333333'],
[14.5,'May','#666666']
); 

For multi and stacked type of graph you should send data in next format: [[value1, value2, value3, ..., valueN], label]. It should look similar to this:

arrayOfData = new Array(
[[14,54,26],'2007'],
[[8,48,38],'2008'],
[[4,36,57],'2009']
); 

After you set your data array you just need to say in which div you want graph to be created. All you have to do is:

 $('#divForGraph').jqBarGraph({ data: arrayOfData }); 

The code above is enough to display your data as bar graph. But if you want to made your graph prettier you can achieve that with next set of parameters:

data: arrayOfData, // array of data for your graph
title: false, // title of your graph, accept HTML
barSpace: 10, // this is default space between bars in pixels
width: 400, // default width of your graph
height: 200, //default height of your graph
color: '#000000', // if you don't send colors for your data this will be default bars color
colors: false, // array of colors that will be used for your bars and legends
lbl: '', // if there is no label in your array
sort: false, // sort your data before displaying graph, you can sort as 'asc' or 'desc'
position: 'bottom', // position of your bars, can be 'bottom' or 'top'. 'top' doesn't work for multi type
prefix: '', // text that will be shown before every label
postfix: '', // text that will be shown after every label
animate: true, // if you don't need animated appearance change to false
speed: 2, // speed of animation in seconds
legendWidth: 100, // width of your legend box
legend: false, // if you want legend change to true
legends: false, // array for legend. for simple graph type legend will be extracted from labels if you don't set this
type: false, // for multi array data default graph type is stacked, you can change to 'multi' for multi bar type
showValues: true, // you can use this for multi and stacked type and it will show values of every bar part
showValuesColor: '#fff' // color of font for values

This graph will work without any additional CSS, but it can easily be styled via CSS. I was paying attention that you will have enough selectors to achieve anything you want with CSS. If you create something cool it will be nice if you share that with all of us.

Changelog

1.1

* Compatibility with jQuery 1.4.2+



Comments [ 126 Comments ]

  1. lilsizzo
    August 3, 2011

    solve it…do not know if it jeopardise everything but here goes

    ….return maxvalue;};maxMulti=function(ar)….

    replace to

    return 1;};maxMulti=function(ar)

  2. Jeff
    August 6, 2011

    Hey, instead of using a generated placeholder to store both the title and the graph (which goes into the user defined div), could you switch it around so the title and graph both go into the user-defined div?

    For example, when I do $(’#graph1′).jqbargraph(blah); it would be nice to have the title and graph go under the #graph1 div.

    I just had a lot of difficulty getting the graphs side by side because I had to anticipate the generated graph name (it changes according to the number of bars too! :( ) In the end, I ended up making my own div and title to wrap around the graph div

    I don’t know if I’m making sense, I’m pretty bad at explaining things. Thanks for the library, it’s really nice and helped me a lot!

  3. Jeff
    August 6, 2011

    Also no bars show if you don’t specify the colour or something. Maybe add a default value?

  4. Ben Styles
    August 23, 2011

    Dude, you seriously rock. This is the most inventive chart/graph jquery lib I’ve seen. Love the small footprint. Love the way it renders without all the canvas crap. I can have a gazillion bar charts on a single page with virtually no strain on load time.

    I’m running this against multiple db queries and the results are highly impressive. Small, fast, light, colorful, informational, and impressive. Keep up the great work!!

  5. Ben Styles
    August 24, 2011

    I was finding that all totaled numbers, regardless of decimal or not, were being totaled with “.00″ because of the toFixed() function. I replaced that with the following:


    myToFixed = function (n, digits) {
    digits = digits || 0;
    return n.toFixed(digits).replace(new RegExp("\\.0{" + digits + "}"), "");
    }

    This allows me to pass rounded whole numbers as follows:

    myToFixed(32.1212, 2) –> “32.12″
    myToFixed(32.1212, 0) –> “32″
    myToFixed(32, 2) –> “32″
    myToFixed(32.1, 2) –> “32.10″

    and get the desired result.

    function per : http://stackoverflow.com/questions/1393332/javascript-tofixed-function

  6. Bruce Kessel
    October 10, 2011

    Nice presentation!

    Is it possible with this plugin to have horizontal graphs?

  7. Jacob D
    October 30, 2011

    This is an excellent plugin. The small footprint is fantastic. With just a few simple changes it’s possible to add a nice baseline to the graph, which I think really completes the appearance…

    Find this line:

    Change to:

    Find this line:

    Change to:

    Find this line:

    Change to:

    After making the changes add this to your style sheet and customize the style to your taste:
    .graphLabeljqGraph {
    border-top: solid 4px black;
    }

    example of the result here (scroll down, click the button): http://hikeitlikeit.com/2011/photography-and-backpacking-3/

    It would be nice to see these changes implemented into the plugin with an additional parameter added to specify the baseline color & thickness. Making those changes was a bit more than I wanted to tackle though :)

    Cheers!
    Jacob.

  8. Jacob D
    October 30, 2011

    oops… my code was stripped out of the above post, let’s give this a try…

    Find this line:
    div class=’graphValue”+el.id+”‘ id=’graphValue”+unique+”‘

    Change to:
    div class=’graphValue”+el.id+”‘ id=’graphValue”+unique+”‘ style=’position:relative; left:”+space/2+”px;’

    Find this line:
    div class=’graphBar”+el.id+”‘ id=’graphFieldBar”+unique+”‘ style=’background-color:”+color+”;position: relative; overflow: hidden;’

    Change to:
    div class=’graphBar”+el.id+”‘ id=’graphFieldBar”+unique+”‘ style=’background-color:”+color+”;position:relative; left:”+space/2+”px; overflow: hidden;’

    Find this line:
    div class=’graphLabel”+el.id+”‘ id=’graphLabel”+unique+”‘

    Change to:
    div class=’graphLabel”+el.id+”‘ id=’graphLabel”+unique+”‘ style=’width:”+fieldWidth+”px;’

    After making the changes add this to your style sheet and customize the style to your taste:
    .graphLabeljqGraph {
    border-top: solid 4px black;
    }

  9. Swot template
    October 31, 2011

    This is great, just what I was looking for.

  10. vikas
    November 2, 2011

    Hi Lazarevic Ivan,

    I am a fresher to the bargraph,
    my question is, after adding the two js files where the array should be added can you please explain me with the code.

  11. Brian
    November 30, 2011

    Near line 180 there is a bug with stacked bar graphs. If one of the stacked bars has the value zero, the zero will still display as if that sub bar exists, overlapping with the next bar that has a non-zero value. This renders the overlain values unreadable. Here is a simple fix. – Add the line with the comment.

    if (arr.showValues) {
    sv = arr.prefix+valueData[i]+arr.postfix;
    if (sv == 0) { sv = ”; } // eliminates zero overlap for bars that don’t exist.
    fs = 12;
    }

  12. Will
    December 29, 2011

    Is there any easy solution to make this display with the bars stacked horizontally? It doesn’t appear so when looking through the source (it looks like most of the code is written presuming a vertical display of the bars). If not is this possibly planned for a future update?

  13. Scott
    January 3, 2012

    I have tied this plugin to a form calculator that supplements the array data with var data = [
    [parseInt($('#one').text()), 'ROI w/ Local Spending'],
    [parseInt($('#two').text()), 'ROI w/o Local Spending']
    ]; When you press the ‘Calculate’ button it runs the function, but if the button is pressed more than once the bar graph starts stacking text on top of itself. Any ideas how to reset and/or stop this?

  14. Raj
    January 29, 2012

    Hello Friend,

    Thanks for publishing fantastic Bar Charts. It works fine in IE 7 also.

    One comment : I could not print these charts in IE / Chrome. It just shows the number in printed output. When you get a chance, please review and let us know your comments.

    Thanks and Regards,
    Raj

  15. Lazarevic Ivan
    January 30, 2012

    @Will
    There’s no plans for vertical bars for now.

    @Scott
    Try to first clear jqBarGraph container on button click. Something like $(’#divForGraph’).html(”)

    @Raj
    Options for printing background colors is disabled in browser by default. Only way that I know for printing background colors is to enable that option.

  16. BK
    February 8, 2012

    Great Graphs,
    How do I remove the totals at the top of the columns?

  17. Lazarevic Ivan
    February 11, 2012

    You can hide totals from the top with CSS. I think this should be enough .ID_OF_GRAPHgraphValue { display: none }

  18. Andrey
    March 7, 2012

    is not going on JSP upon Spring security:

    <script type="text/javascript" src="”>
    <script type="text/javascript" src="”>

    arrayOfDataMulti = new Array(
    [[14,44,26],’2007′],
    [[18,38,38],’2008′],
    [[24,32,57],’2009′]
    );

    $(’#exampleMulti’).jqBarGraph({
    data: arrayOfDataMulti,
    colors: ['#242424','#437346','#97D95C'] });

  19. James
    March 8, 2012

    Hi,

    I love this plugin….BUT!

    When you turn off javascript, the plugin breaks. This is obvious, but its a shame it doesnt degrade nicely, like for example showing the infomration in a table. Is this something that could happen?

  20. Gilbert
    March 30, 2012

    How do I,Values over graphics bar on Multi-graphic.

    Tks.



» Trackbacks and pingbacks click to expand


Leave a Reply

More Articles


more on WORKSHOP.rs