D3.js has always been a bit of a mystery to me, is it a chart library or something else ? join me in finding out with a few code samples !
See the Pen D3.js Hello World by Eugenio - Keno - Leon (@k3no) on CodePen.
Well that was interesting, it’s somehow clear that D3 is not your average chart library, in their words:
“D3 allows you to bind arbitrary data to a Document Object Model (DOM), and then apply data-driven transformations to the document.”
Or in layman terms: Display your data any way you like with D3, with the caveat that you will have to write your own css and javascript (albeit with the API help and framework provided by d3), a jquery for data if you will.
I tried making a regular bar chart, but after a few tries it became obvious I have no idea what I was doing, so I am taking a step back and figuring out D3 basics…
See the Pen D3.js Basics Binding-Appending by Eugenio - Keno - Leon (@k3no) on CodePen.
Ahh, somehow clearer now( check the heavily annotated example) ; You start by creating or importing a dataset ( your arbitrary data) , then you select and/or create DOM elements to which you attach said data, notice the dot notation (short and long) which chains methods,or you could use a more verbose syntax, those seem to be the bare basics.
Most tutorials seem to introduce SVG at this point, so let’s try checking how svg works with D3.js
See the Pen D3.js SVG Basics by Eugenio - Keno - Leon (@k3no) on CodePen.
Like in the previous example, we bind data to DOM elements, in this case svg rectangles, and then modify their properties with the bounded data properties. Notice also that you have to do all of the calculations for the proper display of the bars and their positioningSee the Pen D3.js load & parse data by Eugenio - Keno - Leon (@k3no) on CodePen.
This was a little maddening because most of the examples I could find use the old syntax (read the above note), in essence after importing your data you might want to add one of the included parsers and formatters, it's actually quite simple if you follow their API documentationSee the Pen D3.js Axes & Scales by Eugenio - Keno - Leon (@k3no) on CodePen.
Quite a few extra D3 Functions were introduced in order to make axes and labels, most of them deal with normalizing data for display, here's a quick overview :See the Pen D3.js Line Chart by Eugenio - Keno - Leon (@k3no) on CodePen.
I can see a trend :)like last time, we are introducing a few functions that help us draw the line: And what if we wanted to make the same chart as a bar chart ?See the Pen D3.js Bar Chart by Eugenio - Keno - Leon (@k3no) on CodePen.
It is unfortunately not as simple as with other libraries, but the gist is that you need to change your domains,ranges ( for scales and proper display of information) and the html elements to represent your data, in this case svg rectangles. Additionally, you might need to use some extra functions provided by d3.js ( in this case scaleband and bandwidth) to help you finish the chart.