function (tag, help) { ////////////// vf-sccore ////////////// $(tag).each(function (index, element) { if (($(element).attr('rendered') == undefined || $(element).attr('rendered') == false)) { $(element).html(''); function renderPlot(outputs, element){ //DATA FORMATTING var formattedData = []; var tempArr = []; for(var i = 0; i < outputs.categories.length; i++) { for(var j = 0; j < outputs.teams.length; j++) { tempArr.push({ 'x': outputs.teams[j], 'y': outputs.values.selfCertified[outputs.teams[j]][outputs.categories[i]] }); } formattedData.push(tempArr); tempArr = []; } //END OF DATA FORMATTING //BEGIN PLOTTING. DO NOT TOUCH BELOW THIS LINE //define padding and plot dimentions var mainPadding = {left: 75, right: 75, top: 10, bottom: 50}; var width = $(element).parent().width() - mainPadding.left - mainPadding.right; var height = ($(element).parent().width() * 0.75) - mainPadding.top - mainPadding.bottom; var plotPadding = 30; //padding between each plot var numberOfPlots = outputs.categories.length; //If multiple plots exist, then calculate a summation if (numberOfPlots > 1) { var sum = 0; var sumArray = []; //go through each data set and add each element to form the added for (var j = 0; j < formattedData[0].length; j++) { for (i = 0; i < formattedData.length; i++) { sum += formattedData[i][j].y; } sumArray.push(sum); sum = 0; } for (var i in sumArray) { sumArray[i] = {x: formattedData[0][i].x , y: sumArray[i]}; } numberOfPlots++; //add +1 because of the addition of the Total plot //Append the summation array into the formattedData array formattedData.push(sumArray); } //the actual width of each plot var plotWidth = 0; if (numberOfPlots > 1) //if more than one plot { plotWidth = (width + (1 - numberOfPlots) * plotPadding) / numberOfPlots; } else //if its only one plot { plotWidth = width; } //where each plot starts and ends var firstPadding = 1; var dimensions = []; dimensions.push([firstPadding, plotPadding + plotWidth]); for (var i = 0; i < numberOfPlots; i++) { dimensions.push([dimensions[i][1] + plotPadding, dimensions[i][1] + plotPadding + plotWidth]); } //----------begin plotting---------- //select desired tag and append svg tag to it var svg = d3.select("#" + $(element).attr("id")).append("svg") .attr("width", width + mainPadding.right + mainPadding.left) .attr("height", height + mainPadding.top + mainPadding.bottom) .append("g") .attr("transform", "translate(" + mainPadding.left + "," + mainPadding.top + ")"); //all needed variables to plot var x; var y; var xAxis; var yAxis; var bar; var colorPaletts = d3.scale.category20c(); var colorRange = []; for (var i in colorPaletts) { colorRange.push(colorPaletts(i)); } var counter = 0; //loop through every plot for (var i = 0; i < numberOfPlots; i++) { x = d3.scale.linear() .domain([0, d3.max(formattedData[i], function(d) { return d.y; })]) .range([dimensions[i][0], dimensions[i][1]]); y = d3.scale.ordinal() .domain(formattedData[i].map(function(d) { return d.x; })) .rangeRoundBands([0, height], .1); xAxis = d3.svg.axis() .scale(x) .orient("bottom") .ticks(3) .tickSize(5); yAxis = d3.svg.axis() .scale(y) .orient("left") .tickSize(5); if (i == 0) { //Render the bar for the specificed plot bar = svg.selectAll("g.bar" + i) .data(formattedData[i]) .enter().append("g") .attr("class", function(d) { return d.x.replace(" ", "-"); }) //.attr("id", ) .attr("fill", colorRange[i]) .attr("transform", function(d) { return "translate(" + dimensions[i][0] + "," + y(d.x) + ")"; }); bar.append("rect") .attr("width", function(d) { return x(d.y) - firstPadding; }) .attr("height", y.rangeBand()); } else { //Render the bar for the specificed plot bar = svg.selectAll("g.bar"+i) .data(formattedData[i]) .enter().append("g") .attr("class", function(d) { return d.x.replace(" ", "-"); }) .attr("fill", colorRange[i]) .attr("transform", function(d) { return "translate(" + dimensions[i][0] + "," + y(d.x) + ")"; }); bar.append("rect") .attr("width", function(d) { return x(d.y) - dimensions[i][0]; }) .attr("height", y.rangeBand()); } //add data label bar.append("text") .attr("class", "value") .attr("x", function(d) { return x(d.y) - dimensions[i][0] + plotPadding; }) .attr("y", y.rangeBand() / 2) .attr("dx", -plotPadding) .attr("dy", ".35em") .attr("text-anchor", "start") .text(function(d) { return d.y; }); //Render the bar x axis svg.append("g") .attr("class", "x axis" + i) .attr("transform", "translate(0," + height + ")") .call(xAxis); //rotate all x axis labels by -45deg svg.selectAll("g.x.axis" + i + " > g > text") .attr("text-anchor", "end") .attr("transform", "translate(-3, 0), rotate(-45 0 0)"); //add labels svg.selectAll("g.x.axis" + i).append("text") //.text("Plot " + (i + 1)) .data(outputs.teams) .text(function(d){ if (i == numberOfPlots) { return "Total!"; } else { return outputs.teams[i]; } }) .attr("text-anchor", "middle") .attr("x", (dimensions[i][0] + dimensions[i][1])/2) //center the label .attr("y", 40); //.attr("stroke", "white"); if (i == 0) { //Render the bar y axis svg.append("g") .attr("class", "y axis") .call(yAxis); //add the y axis' data label svg.selectAll("g.y.axis").append("text") .text("Teams") .attr("text-anchor", "middle") .attr("transform", "rotate(-90, 0, 0)") .attr("x", -height / 2) //center the label .attr("y", -mainPadding.left + 10); } } //for }; //function } //if if(!((typeof window[$(element).attr('data')]) == 'undefined')) { renderPlot(window[$(element).attr('data')].scores(), element); $(element).attr('rendered','true'); } }); }