Autoplay

    Get Started With D3

    Ben ClinkinbeardBen Clinkinbeard

    This is the first video in a series on D3 from Ben Clinkinbeard. D3 can be... dense, but Ben breaks down the bare essentials and will guide you to D3 mastery.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 In this video, we're going to use D3 to create a simple column chart using the dataset array you see defined. To do that we're going to say D3.select. We're going to tell it to select the body because that's where we want out visualization to be defined.

    00:17 We're then going to select all of the divs that it can find, and tell it which data to use which is just our dataset array. You will notice there are no divs defined on the page yet, and the way we're going to fix that is by using D3's enter selection mechanism and tell it whenever it goes into the enter selection.

    00:39 Which means that it has found a date element that doesn't have a UI element corresponding to it. In that case we want you to append the div. After you append the div we want you to set an attribute.

    00:54 In this case it's the class attribute, and we want that class attribute to be bar which you'll see here has already come up, and that was thanks to the style defined up here which just sets a width and a height and a margin and a color.

    01:12 Once we do that we can actually get a little bit more control and we'll say, "I want your height to actually be 30 pixels." We're going to override that style that's defined there. Now, we have our column chart defined, but it's not terribly useful to have everything be the same size, and we're not really using the data that we have there.

    01:36 Let's change that by changing that static stream to a function. Each one of our data elements is going to be passed in here, and we can use that to create a dynamic value. We'll say D * 7, and then add on the pixel units. Now we have a chart that is actually using our data.

    01:58 Obviously, you can play with the value here to modify the scaling of the chart, but that's it.

    Discuss

    Discuss