Kortlægning af vandet (pt. 2): en sammenligning af JavaScript-kortlægningsbiblioteker

Et dybt dyk i D3.js, Dygraphs, Chart.js og Google Charts

Koden til de diagrammer, jeg oprettede i headerbilledet, er ope på GitHub.

Da jeg begyndte at oprette diagrammer og visualisere data, var de eneste ting, jeg vidste, "Overvej Canvas til store datasæt" og "D3 er magisk". Jeg havde ingen idé om, at der eksisterede et helt økosystem med kortlægning af biblioteker. Disse biblioteker er gratis, tilgængelige og komplette med eksempler og dokumentation.

Mere vigtigt er det, at hvert bibliotek har sine egne fordele og ulemper med hensyn til de forskellige diagrammer, indlæringskurve, niveau for tilpasning og interaktivitet uden for kassen. Så hvordan beslutter man sig?

Jeg sammenligner et par populære JavaScript- kortbiblioteker i denne artikel, specifikt D3.js , Dygraphs , Chart.js og Google Charts . Forvent at lære at oprette et JavaScript-diagram, en sammenligning på højt niveau på tværs af biblioteker af de ovennævnte faktorer (forskellige diagrammer, tilpasning osv.), Og den brugssag, jeg opfatter, er bedst egnet til hvert kortbibliotek.

Men først en hurtig introduktion til, hvorfor visualisering af data bliver stadig vigtigere. Du er velkommen til at springe til sammenligningen side om side ( Ctrl+F "Lad os sammenligne!").

Hvorfor kortlægge og visualisere data?

Jeg har altid tænkt på datavisualiseringer som en bedre måde at lære og engagere et publikum på. Ikke alle er naturlige ved at absorbere information gennem tekst. Mine øjne glaser over, når jeg prøver at udtrække tal fra en ordblok. Tekst forudsætter også, at du er fortrolig med det sprog, den er skrevet på. Jeg kæmpede med læseboglæsninger på college. Det er sandsynligt, at ikke-indfødte engelsktalende også havde svært ved det.

Alternativt, hver gang jeg stødte på et smukt, afklarende diagram midt i bunkerne af information, greb jeg straks begreberne og huskede dem også bedre.

Vores sind er ikke kablet til hurtigt og grundigt at forstå store klumper af tekst eller bunker af Excel-rækker. Men hvad de udmærker sig ved er at genkende lighed, symmetri, forbindelser mellem objekter og kontinuitet, som er grundlaget for datavisualisering. Tænk på Gestalt-principper.

Her er et uddrag af nogle data fra Bureau of Labor Statistics om arbejdsløshedsgraden på tværs af amerikanske amter (repræsenteret af en FIPS-kode) i 2016.

For at få øje på tendenser eller fange outliers vil den gennemsnitlige person bruge en betydelig mængde tid på at stirre på disse data. De kan scanne hver række og omskrive figurer på et andet ark papir. Ikke ideel.

Men hvis vi visualiserer dataene som et geografisk kort, som Mike Bostock gjorde i sin observerbare notesbog:

Du kan straks se hotspots for højere arbejdsløshed. I stedet for timer har du nu registreret interessante mønstre på få sekunder. Denne forskel i tid til at forstå kan betyde forskellen mellem at droppe et tilsyneladende "uforståeligt" datasæt eller alternativt fremme din undersøgelse . Oprettelse af nøjagtige og tilgængelige visualiseringer gør det også muligt for seere at fange uoverensstemmelser eller huller i datasættet, hvilket holder dataene mere ansvarlige .

Anatomi af et diagram

Før jeg hopper ind i sammenligningen af ​​biblioteket, tror jeg, at den grundlæggende "anatomi" i et JavaScript-diagram berettiger til et overblik. Mens jeg arbejdede gennem disse biblioteker, bemærkede jeg, at alle undtagen D3 * vedtog det samme mønster til generering af diagrammer.

  1. Importer kortbiblioteket til HTML.
  2. Lave en iv> with an ID identifier, such as “my-first-chart”.
  3. 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.
  4. Pass the data, the iv> container, and an options object to a chart generator function.
  5. Some libraries, like Google Charts, require calling draw() to draw the generated chart.
  6. Serve the code up on a Python server with http-server -c-1 -p 8000 and see your first chart at localhost: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!

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

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.