Dubinsko ronjenje u D3.js, Dygraphs, Chart.js i Google Charts

Kôd grafikona koje sam kreirao na slici zaglavlja nalazi se na GitHubu.
Kad sam počeo stvarati grafikone i vizualizirati podatke, jedine stvari koje sam znao bile su „Razmislite o platnu za velike skupove podataka“ i „D3 je magija“. Nisam imao pojma da postoji čitav ekosustav grafičkih knjižnica. Te su knjižnice besplatne, dostupne i sadrže primjere i dokumentaciju.
Što je još važnije, svaka knjižnica ima svoje prednosti i nedostatke s obzirom na raznolikost karata, krivulju učenja, razinu prilagodbe i izvanmrežnu interaktivnost. Pa kako se netko odlučuje?
U ovom ću članku usporediti nekoliko popularnih JavaScript knjižnica grafikona, posebno D3.js , Dygraphs , Chart.js i Google Charts . Očekujte da ćete naučiti kako stvoriti JavaScript grafikon, usporedbu na visokoj razini u knjižnicama gore spomenutih čimbenika (raznolikost grafikona, prilagodba itd.) I slučaj upotrebe za koji smatram da je najprikladniji za svaku knjižnicu grafikona.
Ali prvo, brz uvod u to zašto vizualizacija podataka postaje sve važnija. Možete preskočiti usporednu usporedbu ( Ctrl+F
"Usporedimo!").



Zašto grafički i vizualizirati podatke?
Oduvijek sam vizualizaciju podataka smatrao boljim načinom učenja i privlačenja publike. Nisu svi prirodni u upijanju informacija kroz tekst. Oči mi se slijepe kad pokušavam izvući brojeve iz bloka riječi. Tekst također pretpostavlja da ste upoznati s jezikom na kojem je napisan. Borio sam se s čitanjem udžbenika na fakultetu. Uvjerljivo je da su i strani govornici engleskog jezika imali poteškoća.
Alternativno, kad god bih naišao na prekrasan, pojašnjavajući dijagram usred gomile informacija, odmah sam shvatio koncepte i također ih se bolje sjetio.
Naš um nije povezan s brzim i temeljitim razumijevanjem velikih dijelova teksta ili gomila Excel redova. Ali ono u čemu se ističu je prepoznavanje sličnosti, simetrije, povezanosti između objekata i kontinuiteta, koji su temelji vizualizacije podataka. Razmislite o Gestalt principima.

Evo isječka nekih podataka Zavoda za statistiku rada o stopama nezaposlenosti širom američkih županija (predstavljenih kodom FIPS) u 2016. godini

Kako bi uočio trendove ili uhvatio outliers, prosječna bi osoba provela značajnu količinu vremena zureći u ove podatke. Mogu skenirati svaki redak i prepisivati figure na drugi list papira. Nije idealno.
Ali ako podatke vizualiziramo kao geografsku kartu, kao što je to učinio Mike Bostock u svojoj bilježnici Observable:

Odmah možete vidjeti žarišta veće nezaposlenosti. Umjesto sati, sada ste u sekundi otkrili zanimljive uzorke . Ta razlika u vremenu za razumijevanje može značiti razliku između napuštanja naizgled "nerazumljivog" skupa podataka ili, pak, daljnjeg istraživanja . Stvaranje točnih i dostupnih vizualizacija također omogućava gledateljima da uhvate nedosljednosti ili rupe u skupu podataka, što podatke drži odgovornijima .
Anatomija karte
Prije nego što uskočim u usporedbu knjižnice, mislim da osnovna "anatomija" JavaScript grafikona zahtijeva pregled. Dok sam radio kroz ove knjižnice, primijetio sam da su svi, osim D3 *, usvojili isti obrazac za generiranje karata.
- Uvezite knjižnicu grafikona u HTML.
- Stvoriti
iv> with an ID identifier, such as “my-first-chart”. Fetch and load data in the JS. You may also define the data directly in the JS. Make sure you’ve linked this JS file in the HTML.
Pass the data, the
iv> container, and an options object to a chart generator function. Some libraries, like Google Charts, require calling
draw()
to draw the generated chart.Serve the code up on a Python server with
http-server -c-1 -p 8000
and see your first chart atlocalhost:8000
.
Examples
Basic Dygraphs example
Basic D3.js example
Basic Chart.js example
Basic Google Charts example
*D3 has been primarily used for charting, but it’s more of a collection of toolkits than your standard charting library. See this article for a better explanation.
Let’s compare!
Let’s compare!
When picking any library, I like to start with these evaluation questions:
What’s the learning curve? (quality of documentation, code complexity)
How much can I customize my charts?
Is the library actively supported?
What types of data does this library take?
What modes of interactivity are offered?
Does the library offer responsive charts?
Learning curve
Learning curve

Original text

Dygraphs, Chart.js, and Google Charts have relatively small learning curves. They are great if you need to whip up charts in a couple of hours.
D3 has the highest learning curve, and the reason for this is the fine-grain, low-level control it offers. It’s more of a well-written library of advanced helper functions. D3 can theoretically be used in conjunction with other charting libraries.
To explore a bit further, I created the same chart across all 4 libraries using Boston weather data from meteoblue. The code is up on GitHub.

…. and recorded the lines of code needed to make each chart:

The lines of code support the original comparison of learning curves. D3 needs significantly more lines to get a basic chart up and running but provides more opportunity for customization.
Customization

D3 shines in the customization arena. D3’s granularity and modularity is exactly why designers and developers favor it as the medium for stunning and unique visualizations. Chart.js and Google Charts offer numerous options that can be passed into a generator function, such as legend font size and thickness of a line.
Active development

I define library development as the frequency of releases and the responsiveness of library maintainers to opened issues and feedback for improvement. A supportive and large community of users is also a plus. Usage encourages healthy change and accountability as the JavaScript ecosystem evolves.
Looking at the respective GitHub repositories, I discovered releases and resolved issues for Dygraphs and Google Charts to be more sporadic than D3 and Chart.js. D3 will not reach a halt in development any time soon. Its creator and contributors recently released a major version (v5.0) in 2018. They still actively contribute to the visualization community. Chart.js’s latest release also occurred pretty recently in 2018. The release addressed issues and enhancements. They are documented thoroughly in the release notes.
Types of data

This speaks for itself. Fun fact: I used D3’s fetch library to fetch data. I used other libraries to chart it. D3 has fetch functions for almost all common data formats used in data visualization.
Interactivity

Dygraphs, Chart.js, and Google Charts all have some out-of-box interactivity features, like tool tips, zoom, and events. It’s difficult to introduce highly custom interactions because each library is so encapsulated. With D3, you accept that complicated and unique interactions are possible. The tradeoff is simple interactions, like a tool tip, must also be constructed from the ground up.
Responsiveness

Chart.js and D3 offer responsive charts out of the box (for D3, specify a viewBox
instead of width
and height
for the svg
container). Dygraphs and Google Charts need some additional work to create responsive charts, like adding position: relative
to the chart container or redrawing the chart on $(window).resize()
.
Dygraphs responsive chart (inspect the chart containers to see the CSS classes)
Responsive Google Charts Stack Overflow thread
Best used for?
Last but not least, I’ve listed the use case that I think each library is best suited for:

D3 is worth investing time in if you a) need a highly custom visualization and/or b) want helper functions to use in conjunction with other libraries.
I enjoyed Dygraphs for time series because the user can pan over the series and see the date and corresponding point by default. You can also highlight specific periods of time and select ranges of time.
Chart.js allows you to create simple, aesthetically pleasing charts that pop into the page seamlessly on load.
Finally, Google Charts offered the most variety of out-of-the-box charts, compared to the other libraries. In addition to standard charts, Google Charts also supports geographic maps, tree maps, sankey diagrams, etc.
3, 2, 1 … recap!
We’ve covered the many reasons why data visualization is powerful, the basic structure and steps to create a chart using a charting library, and a play-by-play comparison of 4 popular JavaScript libraries.
The most important step after you’ve selected a library and generated some visualizations is to communicate, and then iterate. Show your charts to others and ask them what they can and cannot interpret. Listen to their feedback and keep tweaking your charts. They’re teaching tools, and teaching tools should constantly evolve with the content and the viewers.
Thank you for reading!
- — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Code for the charts I created are up on GitHub.
Here are the presentation slides that led to this article.
If you want to read about Bokeh and D3, check out Charting the waters: between Bokeh and D3.
If you have any suggestions or feedback, drop a comment.