Currently, I am attempting to create a stacked bar chart using D3js. My main issue lies in correctly setting the y
and y0
attributes to ensure that the bars are displayed in their correct positions. It seems like there might be a miscalculation somewhere, but I am struggling to pinpoint it. You can view the code example by visiting this link: FIDDLE
Here is an overview of the scenario:
- I initially group the data by "period," with the periods being represented on the xAxis
- Following that, I further group the data by "type" - MONTH and ENTRY, which should show up as stacked bars in different colors.
- The total "amount" for each type within every period is displayed on the yAxis.
To structure the data, I utilize the nest function with 2 keys. The problem arises when trying to plot the bars in the actual stacked bar chart. I am uncertain whether the issue lies in how I access the data (keys and values) or in how I define the attributes "y" and "height".
selection.selectAll("rect")
.data(function (d) { return d.values; })
.enter().append("rect")
.attr("width", x.rangeBand())
.attr("y", function (d) { return y(d.values); })
.attr("height", function (d) { return y(d.y0) + y(d.values); })
//.attr("height", function (d) { return y(d.y0) - y(d.values); })
.style("fill", function (d) { return color(d.key); })
The primary issues I encounter include one bar being hidden behind another and the second bar appearing beneath the xAxis.
As a beginner in d3js, I am struggling to find a resolution. Any assistance would be greatly appreciated!