You are currently viewing the United Kingdom version of the site.
Would you like to switch to your local site?
14 MIN READ TIME

Turn your data plots into visual information

Mihalis Tsoukalos explains how to create impressive visualisations with the JavaScript library D3.js and a big pile of data.

OUR EXPERT

Mihalis Tsoukalos is a DataOps engineer and a technical writer. He’s the author of Go Systems Programming and Mastering Go. Get in touch with Mihalis at @mactsouk.

QUICK TIP

D3.js (https://d3js.org) is a low-level JavaScript library, which means that you’ll need to write lots of code even for the simplest of the examples. On the other hand, D3.js gives you absolute control over the output.

This month’s coding tutorial is on D3.js, a powerful low-level JavaScript library that can create unique, highly customisable and impressive graphical output based on your data. For reasons of simplicity most of the examples shown here will include the data that will be visualised in the HTML file that contains the JavaScript code. However, D3.js enables you to read data from text files that reside either on your local machine or online.

The D3.js (Data Driven Documents) library can be downloaded using a tag in the HTML page that contains the JavaScript code. The code processes and creates your visualisations, so there’s no need to download it locally. Additionally, you’re free to read the JavaScript code of D3.js and make any changes or improvements you want to it!

There are several advantages to using D3.js. First, it can create professional output on the fly without the need to store PNG and PDF files that you’ll have to embed into HTML code. Second, if you’re reading data from the Internet or from a local file that changes, a page refresh is enough to obtain the new data and automatically update the plots. Third, D3 can create animated and interactive graphics - and there are plenty of visualisations that can benefit from these two features. Finally, if you already know JavaScript, using D3 is straightforward as long as you’re willing to learn its rich API.

When creating plots with D3.js, bear in mind that each D3 graph requires an HTML file that contains the DOM tree and the JavaScript code for manipulating the DOM tree and the data. If you’re familiar with JavaScript you might already know that you can organise your JavaScript code using multiple files that you can call using the tag. However, this tutorial will put all JavaScript code in a single HTML file.

Drawing the canvas

The canvas is the area that you give D3 for drawing. It can be shorter than the browser windows, which is the norm. Note that the coordinates of the upper left corner of the canvas are always (0,0) whereas the coordinates of the bottom left corner are (0, height), whatever the value of the height variable is. The coordinates of the upper right corner are (width, 0), based on the value of the width variable, and the coordinates of the bottom right corner of the canvas are (width, height).

Unlock this article and much more with
You can enjoy:
Enjoy this edition in full
Instant access to 600+ titles
Thousands of back issues
No contract or commitment
Try for 99p
SUBSCRIBE NOW
30 day trial, then just £9.99 / month. Cancel anytime. New subscribers only.


Learn more
Pocketmags Plus
Pocketmags Plus

This article is from...


View Issues
Linux Format
August 2020
VIEW IN STORE

Other Articles in this Issue


In This Issue
Generation Linux
We’re all quite old here at Linux Format. Effy’s looking
NEWS
Open source to sweep across Europe
On 14 May the European Parliament moved to endorse a
Penguins in space!
On its second attempt (reboot? - Ed) the SpaceX-built Falcon
More Linux tech
We reported last issue that Fedora was officially being listed
Case closed!
We reported in LXF258 on the patent case brought against
BIFROST BOOST
With only free software, a Mali G31 chip can now
TRYING TOO HARD
Every once in a while, you see a discussion that
Kernel Watch
Linus Torvalds announced the release of Linux 5.7. The new
ONGOING DEVELOPMENT
Sean Kelly has begun to post enablement patches for CXL
REVIEWS
Intel Frost Canyon NUC
Frost Canyon is the latest iteration of the Intel NUC
Purism Librem 5
Announced in 2017 as a crowdfunder that was impressively supported,
DXT2 1.2
The Windows 7 EOL was too good a hook to
Mabox Linux 20.02
Manjaro is one of the best renditions of Arch Linux
Lair of the Clockwork God
Genuinely funny games are still something of a rarity. So
LED Strip HAT Mini
LED Strip HAT Mini is a Swiss Army knife for
ROUNDUP
File managers
His co-workers have only seen him by video link and
INTERVIEW
NEXT-GEN DISTROS
Cast your mind back 15 years, to that pre-credit crunch
OPEN BRITANNIA
Amanda Brock is CEO of OpenUK (https://openuk.uk), a new non-profit
Pi USER
LEVEL UP!
Les Pounder works for Tom’s Hardware UK writing about the
Raspbian we love you, but this is goodbye…
Things are afoot in the world of Raspberry Pi operating
Official Ubuntu
There’s been a spin of Ubuntu that runs on the
Official 64-bit
But wait, there’s more OS news! Since the Pi 2
Create stop-motion animation with your Pi
Les Pounder is an Associate Editor at Tom’s Hardware and
Hack Stadia stream gaming on to the Pi
Christian Cawley got a bit peckish for Pi and spent
CODING ACADEMY
Creating Pac-Man style games in Python
Calvin Robinson is a former assistant principal and computer science
REGULARS AT A GLANCE
Your free downloads
To replace our regular covermounted DVD we’re offering four top
Mailserver
Write to us at Linux Format, Future Publishing, Quay House,
Answers
Got a burning question about open source or the kernel?
Inkscape
Neil Mohr is in the HotPicks seat which has been
Worker
You give Jonni six entire pages to write up the
Birdfont
While Comic Sans gets a bad rep in graphic design
PeaZip
PeaZip is currently one of the best-know crossplatform GUI frontends
Mermaid
We first took a look at Mermaid over three years
Zeal
Taking inspiration from OS X’s Dash, this is a useful
Mixxx
Certain members of the open source community believe it lacks
Stella
One of the most comprehensive emulators known to man is
BZFlag
Here at self-isolating LXF Towers, virtually blowing stuff up has
TheSSS
Spun off from the main 4MLinux project, TheSSS isn’t some
Eqonomize
We first looked at Eqonomize when it was originally launched
TUTORIALS
Manage your files
Jonni Bidwell uses vim and i3 on an Eee PC,
Design and publish your very own eBooks
Nick Peers dreams of republishing his best work in book
Model and simulate your circuit projects
Mats Tage Axelsson is keen to show you how using
Monitor systems and Docker deployments
Mihalis Tsoukalos is a DataOps engineer and a technical writer
IN-DEPTH
BROWSER WARS 2020
We suspect most readers remember with bitterness and rolling of
Chat
X
Pocketmags Support