Tame stacked bar charts in Sketch
Drawing a stacked bar chart is a forever struggle. I know you.
Shawnbot on Github wrote a Sketch script that is able to make all rectangles the same width. I was able to tweak (I just changed all “width” to “height” haha) and adapt it for my stacked bar chart.✌️ I’d like to share my steps so that people like you may benefit.
In our imaginary sales product, we are going to illustrate how 5 key numbers change quarter over quarter. This results in a stacked bar chart with 4 groups and each has 5 segments.
Step 1. Make one group of rectangles
Let’s start with the first group of 5 rectangles.
You may wonder how to fit in data. The secret is in the shape name. In order for the Sketch script to work out the graphic for us, we put data in the name of the shape. In the example I have selected the bottom bar of the first group, as you can see the name is “value=50”. This means the height of this rectangle will stand for $50.
Step 2. Duplicate the group of rectangles and draw 1 other group
Duplicate the group of rectangles into four. Now we have one year worth of rectangles!
I added a special group, Group 0, so that I can get my y-axis. Based on my data, $400 will be a good maximum, so I made four rectangles in an increment of $100.
Step 3. Select all the rectangles
Use a combination of Shift and Command to select all rectangles. For example, in order to select the first 5 rectangles, click the value=80 rectangle when holding Command, and click the value=40 rectangle with Shift. Selecting on the group will not work! You should see selections highlighted like this:
Step 4. Let the magic happen
Go to Plugin > Run Script, or Control+Shit+K to bring up the script dialog.
Paste the following script into the top text area and press Run.
Step 5. Rearrange rectangles and adjust styles
Voila!🎉