Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 917 of the free egghead.io lessons, plus get D3 content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Using D3 to augment an existing UI

4:11 D3 lesson by

D3 isn't just for charts and graphs. It can be used for all sorts of drawing tasks. Learn how to modify an existing UI – in this case a Bootstrap calendar compnent – using D3 "from the outside".

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

D3 isn't just for charts and graphs. It can be used for all sorts of drawing tasks. Learn how to modify an existing UI – in this case a Bootstrap calendar compnent – using D3 "from the outside".

I recently had a project where I needed a calendar component that was going to be pretty central to the project. If possible, I wanted to avoid building my own. I ended up finding this really great one based on Bootstrap, that has the ability to have dates and events on dates.

I thought it was going to be exactly what I needed, so I went through the exercise of modifying it. I want to show you how to do that in this video. This is the GitHub repo that this calendar lives in. Again, it's great. If you need something and this fits the bill, I would highly recommend it.

I have created a simplified version here just to conserve space and make things simpler. What I've done is I've stripped down a lot of the markup in the HTML file, but really all I've added to it is just these two tags.

I'm including d3, and then I'm including our demo script that we're going to work with here. You can see that all I have in this script right now is d3.selectAll using this CSS selector of cal-day-inmonth, and then I'm setting the background color to orange.

You can see here that that has actually worked. That cal-day-inmonth selection, if we inspect here, we can see this div has that class on it. The inmonth is to denote the days that are actually in the month you're looking at versus days from the previous month, here. These are called cal-day-outmonth, so you could style those as well. Let's close this.

Here, you can see that it is working exactly as we would expect, where it's just setting the background color statically. We could change that value, save it, go refresh. There, everything's light blue. That's all well and good, but what I needed to do was actually tie these things to some data.

Before I actually tie it to data, I just want to show you how we can integrate a colorScale here. If we create a colorScale just using d3.scale.linear and give it a domain running from 0 to 30. We're cheating here because we know the range. It's actually 31 days, so let's change it to that. That's our domain, and then our range will be from white to green.

What we're going to do now is just set the background color using this function. If you remember, the first argument to a function is the datum, and the second one is the index. We're just going to use the index here. The position in the month, we're going to pass that to the colorScale.

Depending on its position between 0 and 31, it's going to give us a color somewhere between white and green. If we save that and go refresh this now, you can see that we get a nice progression from white to green as we move through the month.

For the next step, we'll actually create some data and show that on the chart. I'm just going to create an array of the length that I know we need and run it through a mapping function. All this function does is it's going to create a random number and round it.

If that happens to get rounded up to one, we'll just use another random number. If it gets rounded down to zero, we're going to get a negative number. Basically, our data is going to range from -1 all the way up to positive 1 as our potential data domain there.

You can see that I've also adjusted the range to have three elements to match our domain. Anything between -1 and 0 is going to fall somewhere between red and white. Anything from zero to one will be between white and green. I've updated the background-color function here to pass the datum to the colorScale, rather than the index.

If we save this and go refresh, we have a nice Christmas-themed, data-driven calendar where each day is colored based on a piece of data behind it. You can then actually create a meaningful calendar that communicates data to your users at a glance.


Featured course:

Create an SVG Icon System

Create an SVG Icon System

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?