How to Create Stacked Bar Chart using d3.js
source link: http://www.adeveloperdiary.com/d3-js/create-stacked-bar-chart-using-d3-js/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
March 22, 2016 By Abhisek Jana 15 Comments
How to Create Stacked Bar Chart using d3.js
In this How to Create Stacked Bar Chart using d3.js post we will learn not only to code but the mathematical calculation behind creating a stacked bar chart using d3. Even if you have probably copy pasted a working version the code, I strongly recommend you to go though this tutorial in order to get a solid understanding on how this works.
No fancy stuff today, we will create a very simple basic stacked bar chart. Here is the demo we will be creating. Let’s assume we have three products A , B & C. We want to display the monthly sales in using a stacked bar chart.
Our JSON data object for the chart above would look like this:
Our Final SVG would look like below. Each category (A,B or C) would be part of one group (g
svg element).So we will first draw all rect
for A , then for B & C. Lets not worry about x
pos and width
since they are generic.
Now we need to change our JSON object (data
) so that we can easily draw above svg elements. At first we will change our data
to dataIntermediate
. Here we will simplify by creating one array for each category. The x would repeat for each array.
Here is the code for changing the data
to dataIntermediate
.
Then we will pass dataIntermediate
json to d3.layout.stack()
and it will provide the below output. We can however create this by our own but why not d3 take care of this for us. The d3.layout.stack()
will add a y0
attribute to our array.
Next we will create the xScale
& yScale
with the domain value. The domain for xScale
should be straight forward, just take the first element from the array and get all the x
values, ['Jan','Feb']
) in this case.
We will set a max & min value to the yDomain
. In order to calculate the max value, we will take the last element from the dataStackLayout
array and sum d.y0
& d.y
, then get the max out of all the values. The min would be 0 in most of the cases.
Now its time to draw the chart. We will focus on the 4 attributes, x
, y
, height
& width
. The x and width is straight forward. The y would be summation of d.y0
& d.y
, since we should start drawing from top. The calculation for height might be confusing to you, but remember in svg the left-top corner is the center [0,0]
and yScale(0) > yScale(10)
. So yScale(d.y0)
represents the bottom position and yScale(d.y + d.y0)
is the top position. The subtraction would give us the height of the rect element.
Find the full code here:
I hope now you would know How to Create Stacked Bar Chart using d3.js. Later we will use React to create stacked bar charts.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK