|
function renderGroupedBarChart (dataset,dom_element_to_append_to,innerColumns,isCurrency, colorScheme){ |
|
var width = 700; |
|
var height = 350 ; |
|
|
|
var margin = {top: 40, right: 30, bottom: 40, left: 60}; |
|
var width = width - margin.left - margin.right; |
|
var height = height - margin.top - margin.bottom; |
|
|
|
var x0 = d3.scale.ordinal() |
|
.rangeRoundBands([0, width], 0.1); |
|
|
|
var x1 = d3.scale.ordinal(); |
|
|
|
var x2 = d3.scale.ordinal() |
|
.rangeRoundBands([0, width], 0.1); |
|
|
|
var y = d3.scale.linear() |
|
.range([height, 0]); |
|
|
|
var xAxis = d3.svg.axis() |
|
.scale(x0) |
|
.orient("bottom"); |
|
|
|
var xAxisBottom = d3.svg.axis() |
|
.scale(x2) |
|
.orient("bottom"); |
|
|
|
var yAxis = d3.svg.axis() |
|
.scale(y) |
|
.orient("left"); |
|
|
|
var color = d3.scale.ordinal() |
|
.range(colorScheme); |
|
var svg = d3.select(dom_element_to_append_to).append("svg") |
|
.attr("width", width + margin.left + margin.right) |
|
.attr("height", height + margin.top + margin.bottom) |
|
.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
var yBegin; |
|
|
|
var tip = d3.tip() |
|
.attr('class', 'd3-tip') |
|
.offset([-10, 0]) |
|
.html(function(d) { |
|
if(isCurrency) |
|
return "<div><span>Month:</span> <span style='color:white'>" + d.name + "</div>" + |
|
"<div><span>Value:</span> <span style='color:white'><span>₹</span>" + d3.format(",")(d.yEnd) + "</span></div>"; |
|
else |
|
return "<div><span>Category:</span> <span style='color:white'>" + d.name + "</div>" + |
|
"<div><span>Value:</span> <span style='color:white'>" + d3.format(",")(d.yEnd) + "</span></div>"; |
|
}) |
|
svg.call(tip); |
|
|
|
var data = dataset; |
|
var columnHeaders = d3.keys(data[0]).filter(function(key) { return key !== "State"; }); |
|
|
|
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "State"; })); |
|
|
|
data.forEach(function(d) { |
|
var yColumn = new Array(); |
|
d.columnDetails = columnHeaders.map(function(name) { |
|
for (var ic in innerColumns) { |
|
if($.inArray(name, innerColumns[ic]) >= 0){ |
|
if (!yColumn[ic]){ |
|
yColumn[ic] = 0; |
|
} |
|
yBegin = yColumn[ic]; |
|
yColumn[ic] += +d[name]; |
|
var obj = {name: name, column: ic, yBegin: yBegin, yEnd: +d[name] + yBegin,}; |
|
return obj; |
|
} |
|
} |
|
}); |
|
|
|
d.total = d3.max(d.columnDetails, function(d) { |
|
return d.yEnd; |
|
}); |
|
}); |
|
|
|
|
|
x0.domain(data.map(function(d) { return d.State; })); |
|
x1.domain(d3.keys(innerColumns)).rangeRoundBands([0, x0.rangeBand()]); |
|
|
|
y.domain([0, d3.max(data, function(d) { |
|
return d.total; |
|
})]); |
|
|
|
svg.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0," + -25 + ")") |
|
.call(xAxis); |
|
|
|
svg.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(xAxisBottom); |
|
|
|
svg.append("g") |
|
.attr("class", "y axis") |
|
.call(yAxis) |
|
.append("text") |
|
.attr("transform", "rotate(-90)") |
|
.attr("y", 6) |
|
.attr("dy", ".7em") |
|
.style("text-anchor", "end") |
|
.text(""); |
|
|
|
var project_stackedbar = svg.selectAll(".project_stackedbar") |
|
.data(data) |
|
.enter().append("g") |
|
.attr("class", "g") |
|
.attr("transform", function(d) { return "translate(" + x0(d.State) + ",0)"; }); |
|
|
|
var project_stackedbar_enter = project_stackedbar.selectAll("rect") |
|
.data(function(d) { return d.columnDetails; }) |
|
.enter() |
|
|
|
var rect_elements = project_stackedbar_enter.append("rect") |
|
.attr("class", "bar") |
|
.attr("width", x1.rangeBand()) |
|
.attr("x", function(d) {return x1(d.column);}) |
|
.attr("y", function(d) {return y(d.yEnd);}) |
|
.attr("height", function(d) {return y(d.yBegin) - y(d.yEnd);}) |
|
.attr("fill", function(d) {return color(d.name);}); |
|
|
|
|
|
var text_elements = project_stackedbar_enter.append("text") |
|
.attr("y", function(d) { return x1(d.column) + x1.rangeBand()/2; }) |
|
.attr("x", function(d) { return -y(d.yBegin)-30; }) |
|
.attr("transform", "rotate(-90)") |
|
.attr("dy", ".35em") |
|
.text(function(d){return d.name}); |
|
|
|
rect_elements |
|
.on('mouseover', tip.show) |
|
.on('mouseout', tip.hide); |
|
|
|
|
|
var legend = svg.selectAll(".legend") |
|
.data(columnHeaders.slice().reverse()) |
|
.enter().append("g") |
|
.attr("class", "legend") |
|
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); |
|
|
|
legend.append("rect") |
|
.attr("x", width - 6) |
|
.attr("width", 18) |
|
.attr("height", 18) |
|
.style("fill", color); |
|
|
|
legend.append("text") |
|
.attr("x", width - 12) |
|
.attr("y", 9) |
|
.attr("dy", ".35em") |
|
.style("text-anchor", "end") |
|
.text(function(d) { return d; }); |
|
} |