0% found this document useful (0 votes)
255 views2 pages

JqBarGraph Min Js

This document contains the code for a jQuery plugin called jqBarGraph that generates bar graphs. It takes in data and options to create and style the bar graph elements. Key functions include initializing the graph, calculating max values, generating the bar elements, and adding legends. The plugin defaults define common styling and behavior options.

Uploaded by

Bryan Victoria
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views2 pages

JqBarGraph Min Js

This document contains the code for a jQuery plugin called jqBarGraph that generates bar graphs. It takes in data and options to create and style the bar graph elements. Key functions include initializing the graph, calculating max values, generating the bar elements, and adding legends. The plugin defaults define common styling and behavior options.

Uploaded by

Bryan Victoria
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/** * jqBarGraph - jQuery plugin * @version: 1.0 (2009/11/12) * @requires jQuery v1.2.

2 or later * @author Ivan Lazarevic * Examples and documentation at: http://www.workshop.rs/jqbargraph/ * * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * */ (function($){var opts=new Array;var level=new Array;$.fn.jqBarGraph=$.fn.jqbargr aph=function(options){init=function(el){opts[el.id]=$.extend({},$.fn.jqBarGraph. defaults,options);$(el).css({'width':opts[el.id].width,'height':opts[el.id].heig ht,'position':'relative','text-align':'center'});doGraph(el);};sum=function(ar){ total=0;for(val in ar){total+=ar[val];} return total.toFixed(2);};max=function(ar){maxvalue=0;for(var val in ar){value=a r[val][0];if(value instanceof Array)value=sum(value);if(parseFloat(value)>parseF loat(maxvalue))maxvalue=value;} return maxvalue;};maxMulti=function(ar){maxvalue=0;maxvalue2=0;for(var val in ar ){ar2=ar[val][0];for(var val2 in ar2){if(ar2[val2]>maxvalue2)maxvalue2=ar2[val2] ;} if(maxvalue2>maxvalue)maxvalue=maxvalue2;} return maxvalue;};doGraph=function(el){arr=opts[el.id];data=arr.data;if(data==un defined){$(el).html('There is not enought data for graph');return;} if(arr.sort=='asc')data.sort(sortNumberAsc);if(arr.sort=='desc')data.sort(sortNu mberDesc);legend='';prefix=arr.prefix;postfix=arr.postfix;space=arr.barSpace;leg endWidth=arr.legend?arr.legendWidth:0;fieldWidth=($(el).width()-legendWidth)/dat a.length;totalHeight=$(el).height();var leg=new Array();max=max(data);colPositio n=0;for(var val in data){valueData=data[val][0];if(valueData instanceof Array) value=sum(valueData);else value=valueData;lbl=data[val][1];color=data[val][2];unique=val+el.id;if(color==u ndefined&&arr.colors==false) color=arr.color;if(arr.colors&&!color){colorsCounter=arr.colors.length;if(colors Counter==colPosition)colPosition=0;color=arr.colors[colPosition];colPosition++;} if(arr.type=='multi')color='none';if(lbl==undefined)lbl=arr.lbl;out="<div class= 'graphField"+el.id+"' id='graphField"+unique+"' style='position: absolute'>";out +="<div class='graphValue"+el.id+"' id='graphValue"+unique+"'>"+prefix+value+pos tfix+"</div>";out+="<div class='graphBar"+el.id+"' id='graphFieldBar"+unique+"' style='background-color:"+color+";position: relative; overflow: hidden;'></div>" ;if(!arr.legend arr.legends) out+="<div class='graphLabel"+el.id+"' id='graphLabel"+unique+"'>"+lbl+"</div>"; out+="</div>";$(el).append(out);totalHeightBar=totalHeight-$('.graphLabel'+el.id ).height()-$('.graphValue'+el.id).height();fieldHeight=(totalHeightBar*value)/ma x;$('#graphField'+unique).css({'left':(fieldWidth)*val,'width':fieldWidth-space, 'margin-left':space});if(valueData instanceof Array){if(arr.type=="multi"){maxe= maxMulti(data);totalHeightBar=fieldHeight=totalHeight-$('.graphLabel'+el.id).hei ght();$('.graphValue'+el.id).remove();}else{maxe=max;} for(i in valueData){heig=totalHeightBar*valueData[i]/maxe;wid=parseInt((fieldWid th-space)/valueData.length);sv='';fs=0;if(arr.showValues){sv=arr.prefix+valueDat a[i]+arr.postfix;fs=12;} o="<div class='subBars"+el.id+"' style='height:"+heig+"px; background-color: "+a rr.colors[i]+"; left:"+wid*i+"px; color:"+arr.showValuesColor+"; font-size:"+fs+ "px' >"+sv+"</div>";$('#graphFieldBar'+unique).prepend(o);}} if(arr.type=='multi') $('.subBars'+el.id).css({'width':wid,'position':'absolute','bottom':0});if(arr.p osition=='bottom')$('.graphField'+el.id).css('bottom',0);if(!arr.legends) leg.push([color,lbl,el.id,unique]);if(arr.animate){$('#graphFieldBar'+unique).cs

s({'display':'none','height':0});$('#graphFieldBar'+unique).animate({'height':fi eldHeight},arr.speed*1000);}else{$('#graphFieldBar'+unique).css({'height':fieldH eight});}} for(var l in arr.legends){leg.push([arr.colors[l],arr.legends[l],el.id,l]);} createLegend(leg);if(arr.legend){$(el).append("<div id='legendHolder"+unique+"'> </div>");$('#legendHolder'+unique).css({'width':legendWidth,'float':'right','tex t-align':'left'});$('#legendHolder'+unique).append(legend);$('.legendBar'+el.id) .css({'float':'left','margin':3,'height':12,'width':20,'font-size':0});} if(arr.title){$(el).wrap("<div id='graphHolder"+unique+"'></div>");$('#graphHold er'+unique).prepend(arr.title).css({'width':arr.width+'px','text-align':'center' });}};createLegend=function(legendArr){legend='';for(var val in legendArr){legen d+="<div id='legend"+legendArr[val][3]+"' style='overflow: hidden; zoom: 1;'>";l egend+="<div class='legendBar"+legendArr[val][2]+"' id='legendColor"+legendArr[v al][3]+"' style='background-color:"+legendArr[val][0]+"'></div>";legend+="<div c lass='legendLabel"+legendArr[val][2]+"' id='graphLabel"+unique+"'>"+legendArr[va l][1]+"</div>";legend+="</div>";}};this.each(function() {init(this);})};$.fn.jqBarGraph.defaults={barSpace:10,width:400,height:300,color :'#000000',colors:false,lbl:'',sort:false,position:'bottom',prefix:'',postfix:'' ,animate:true,speed:1.5,legendWidth:100,legend:false,legends:false,type:false,sh owValues:true,showValuesColor:'#fff',title:false};function sortNumberAsc(a,b){if (a[0]<b[0])return-1;if(a[0]>b[0])return 1;return 0;} function sortNumberDesc(a,b){if(a[0]>b[0])return-1;if(a[0]<b[0])return 1;return 0;}})(jQuery);

You might also like