I have try to customize my existing code of Stacked Bar graph with the help of this link https://bl.ocks.org/mbostock/1134768.
I have done this graph with the help of given link but i have some confusion about the data points on y axis as shown below in picture
Here is my full code of Stacked bar Graph
var data = [
{month: 'Jan',total:100 ,A: 35, B: 5, C: 10},
{month: 'Feb',total:200 ,A: 30, B: 10, C: 20},
{month: 'Mar',total:300 ,A: 0, B: 10, C: 20}
];
var xData = ["A", "B", "C"];
var margin = {top: 20, right: 50, bottom: 30, left: 50},
width = 400 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .35);
var y = d3.scale.linear()
.rangeRound([height, 0]);
var color = d3.scale.category20();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("#chart").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 + ")");
data.forEach(function(d) {
xData.forEach(function(c) {
d[c] = +d[c];}); })
var dataIntermediate = xData.map(function (c) {
return data.map(function (d) {
return {x: d.month, y: d[c]};});});
var dataStackLayout = d3.layout.stack()(dataIntermediate);
x.domain(dataStackLayout[0].map(function (d) {
return d.x; }));
y.domain([0,d3.max(dataStackLayout[dataStackLayout.length - 1],
function (d) { return d.y0 + d.y;}) ]).nice();
var layer = svg.selectAll(".stack")
.data(dataStackLayout)
.enter().append("g")
.attr("class", "stack")
.style("fill", function (d, i) {
console.info(i, color(i));
return color(i);});
layer.selectAll("rect")
.data(function (d) {
return d;})
.enter().append("rect")
.attr("x", function (d) {
console.info("dx", d.x,x(d.x), x.rangeBand());
return x(d.x);})
.attr("y", function (d) {
return y(d.y + d.y0);})
.attr("height", function (d) {
return y(d.y0) - y(d.y + d.y0); })
.attr("width", x.rangeBand() -1);
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "axis axis--y")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end");
As i have taken help from the given url, i have change my code which take array data as input instead of CSV file. But i think my data points(label) on y axis is not according to given data(key:total).
Any help should be appreciated.
