# HG changeset patch # User mzeidler # Date 1380496843 14400 # Node ID af2085ce70013fa0f5a95b054a50d6f3d5a6578e # Parent 4699815b211a4ba05904cc1d7e74d053606cdbd5 Uploaded diff -r 4699815b211a -r af2085ce7001 jqBarGraph.2.1.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jqBarGraph.2.1.js Sun Sep 29 19:20:43 2013 -0400 @@ -0,0 +1,676 @@ +/** + * jqBarGraph - jQuery plugin + * @version: 1.1 (2011/04/03) + * @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 + * + * @param data: arrayOfData // array of data for your graph + * @param title: false // title of your graph, accept HTML + * @param barSpace: 10 // this is default space between bars in pixels + * @param width: 400 // default width of your graph ghhjgjhg + * @param height: 200 //default height of your graph + * @param color: '#000000' // if you don't send colors for your data this will be default bars color + * @param colors: false // array of colors that will be used for your bars and legends + * @param lbl: '' // if there is no label in your array + * @param sort: false // sort your data before displaying graph, you can sort as 'asc' or 'desc' + * @param position: 'bottom' // position of your bars, can be 'bottom' or 'top'. 'top' doesn't work for multi type + * @param prefix: '' // text that will be shown before every label + * @param postfix: '' // text that will be shown after every label + * @param animate: true // if you don't need animated appereance change to false + * @param speed: 2 // speed of animation in seconds + * @param legendWidth: 100 // width of your legend box + * @param legend: false // if you want legend change to true + * @param legends: false // array for legend. for simple graph type legend will be extracted from labels if you don't set this + * @param type: false // for multi array data default graph type is stacked, you can change to 'multi' for multi bar type + * @param showValues: true // you can use this for multi and stacked type and it will show values of every bar part + * @param showValuesColor: '#fff' // color of font for values + + * @example $('#divForGraph').jqBarGraph({ data: arrayOfData }); + +**/ + +(function($) { + var opts = new Array; + var level = new Array; + + $.fn.jqBarGraph = $.fn.jqbargraph = function(options){ + + init = function(el){ + + opts[el.id] = $.extend({}, $.fn.jqBarGraph.defaults, options); + $(el).css({ 'width': opts[el.id].width, 'height': opts[el.id].height, 'position':'relative', 'text-align':'center' }); + + + doGraph(el); + + }; + + // sum of array elements + sum = function(ar,index){ + total = 0; + for(val in ar){ + total += ar[val][index]; + } + return total.toFixed(2); + }; + + // count max value of array + max = function(ar,index){ + maxvalue = 0; + for(var val in ar){ + value = ar[val][0]; + if(value instanceof Array) value = sum(value,index); + if (parseFloat(value) > parseFloat(maxvalue)) maxvalue=value; + } + return maxvalue; + }; + + // max value of multi array + maxMulti = function(ar,index){ + maxvalue = 0; + maxvalue2 = 0; + + for(var val in ar){ + ar2 = ar[val][0]; + + for(var val2 in ar2){ + if(ar2[val2][index]>maxvalue2) maxvalue2 = ar2[val2][index]; + } + + if (maxvalue2>maxvalue) maxvalue=maxvalue2; + } + + return maxvalue; + }; + + + doGraph = function(el){ + + arr = opts[el.id]; + data = arr.data; + if(arr.tab=='reads'){ + val_index=0; + } + else{ + val_index=1; + } + + //check if array is bad or empty + if(data == undefined) { + $(el).html('There is not enought data for graph'); + return; + } + //sorting ascending or descending + if(arr.tab == 'reads'){ + if(arr.sort == 'asc'){ + data.sort(sortReadsAsc); + } + if(arr.sort == 'desc'){ + data.sort(sortReadsDesc); + } + } + if(arr.tab == 'basepairs'){ + if(arr.sort == 'asc'){ + data.sort(sortBasesAsc); + } + if(arr.sort == 'desc'){ + data.sort(sortBasesDesc); + } + } + if(arr.sortBar == 'asc') sortBars(data, barAsc); + if(arr.sortBar == 'desc')sortBars(data, barDesc); + + + + legend = ''; + prefix = arr.prefix; + postfix = arr.postfix; + space = arr.barSpace; //space between bars + legendWidth = arr.legend ? arr.legendWidth : 0; //width of legend box + fieldWidth = ($(el).width()-legendWidth)/data.length; //width of bar + totalHeight = $(el).height(); //total height of graph box + var leg = new Array(); //legends array + + //max value in data, I use this to calculate height of bar + max = max(data,val_index); + + color_map={}; + color_map["pathogen"]=arr.colors[0]; + color_map["ambiguous"]=arr.colors[1]; + color_map["human"]=arr.colors[2]; + + if(arr.tab=='reads'){ + val_index=0; + } + else{ + val_index=1; + + } + + for(var val in data){ + + valueData = data[val][0]; + if (valueData instanceof Array) + value = sum(valueData,val_index); + else + value = valueData; + + lbl = data[val][1]; + unique = val+el.id; //unique identifier + divid=""+el.id; + + if(arr.type == 'multi') color = 'none'; + + if (lbl == undefined) lbl = arr.lbl; + + margin_top=14/(data.length); + + + out = "
family | region | #reads | #basepairs | region length | unaligned fasta | bam alignment | consensus fasta | consensus diagram | "+directory+" | "+region+" | "+last[0]+" | "+last[1]+" | "+last[2]+" | "+generateDownloadLink(directory,region,"unaligned.fa.bzip2")+" | "+generateDownloadLink(directory,region,"alignment.bam")+" | "+generateDownloadLink(directory,region,"consensus.fa")+" | "+generateShowLink(directory,region)+" "+generateDownloadLink(directory,region,"consensus.png")+" | "; + } + } + out += "
---|---|---|---|---|---|---|---|---|
"+dir+" | "+region+" | "+last[0]+" | "+last[1]+" | "+last[2]+" | "+generateDownloadLink(dir,region,"unaligned.fa.bzip2")+" | "+generateDownloadLink(dir,region,"alignment.bam")+" | "+generateDownloadLink(dir,region,"consensus.fa")+" | "+generateShowLink(dir,region)+" "+generateDownloadLink(dir,region,"consensus.png")+" |
"+dir+" | "+region+" | "+last[0]+" | "+last[1]+" | "+last[2]+" | "+generateDownloadLink(dir,region,"unaligned.fa.bzip2")+" | "+generateDownloadLink(dir,region,"alignment.bam")+" | "+generateDownloadLink(dir,region,"consensus.fa")+" | "+generateShowLink(dir,region)+" "+generateDownloadLink(dir,region,"consensus.png")+" |