Create dimensions
A hopefully familiar step by now: creating our chart dimensions.
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the Fullstack D3 Masterclass course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
[00:00 - 02:48] Alright, so now we're doing our second step. We're creating these chart dimensions and one thing to note with the histogram is that they're usually easier to read if they're wider than they are tall, kind of like a timeline, and that's because when these bars, the heights are a little bit closer together, it's easier to compare them and kind of see the chart as a whole rather than a really tall chart where the bars are really tall and it's hard to compare a tall bar to a smaller bar. So this is going to look very similar to what we've done before. Let's just grab the width, let's set it to just 600 pixels, should be big enough to get a good sense of the chart and then we want to create our dimensions object. So dimensions equals and then we know our width already, let's grab the height and since we already know the width, let's just use the width and multiply it by 0.6. So let's just make it 60% as tall as it is wide and then we need our margins. So the only difference here is that we might want a little bit of a bigger top margin to make room for this mean label and maybe these labels for the bars. So let's add, let's do 30 pixels there. On the right side, let's do 10 pixels and then as usual, the bottom and the left sides will need a little bit more room just for those axes. And then down here, let's not forget to create the height and width of our bounds. So the bounded height equals dimensions.height and then let's subtract the top and the bottom margins. I know this is a little bit verbose but oftentimes I'll just copy and paste the code and just reuse it because it's not like this changes very much from chart to chart. And then I usually like to do the width before the height, it's just a convention that I follow and for the width we subtract the left and the right margins.