Data Visualization With D3.js. This Blog Deals With Visualization of - by Ankit Agarwal - Medium
Data Visualization With D3.js. This Blog Deals With Visualization of - by Ankit Agarwal - Medium
This blog deals with visualization of data with the help of D3.js. It is the
second blog in continuation of my last blog, which provides basic idea of
Data Science Life-cycle.
SVG — Scalable Vector Graphics (SVG) is used to define graphics for the
Web. SVG is a text-based image format. Meaning, you can specify what
an SVG image should look like by writing simple markup code, sort of
like HTML tags. In fact, SVG code can be included directly within any
HTML document. A basic example can be:
Since you have shown good amount of patience, I will attach all reference
links at the end 👼
Installation of D3.js
For NPM use npm install d3. For Yarn use yarn add d3. Otherwise,
download the latest release or load it directly from d3js.org.
<script src="https://d3js.org/d3.v5.js"></script>
Few words of caution : We are extensively going to use HTML,SVG, CSS and
JavaScript along with D3. Also, method chaining would play a crucial role.
Kindly get yourself familiar with all of these. To help you, References are
attached at the end.
(…) represents any parameter in method in the coming sections
Run the file and you will see nothing in browser. Now append the following
code under script tag in body
Save and refresh, and voila! There is text “New paragraph by D3!”in the
formerly empty browser window.
Let’s decode what just happened here, magic ? of course not. Each method
can be explained as:
1. select(…) — D3’s select method selects a single element from the DOM
using CSS selector syntax. (We selected the body.)
2. append(…) — This method creats a new element and append that to the
end of our selection, we appended <p> just before the closing </body>
tag in this case.
3. text(…) — It sets the text content of that new, empty paragraph to “New
paragraph by D3!”
Data binding:
In D3, we can bind our data input values to elements in the DOM and can
create new elements based on our data.
Let’s just replace script tag content of last example with the following:
1. select(…) — Finds the body in the DOM and hands a reference off to the
next step in the chain..
2. selectAll(…) — Selects all paragraphs in the DOM. Since none exist yet,
this returns an empty selection.
3. data(…) — Counts and parses our data values. There are five values in
our data set, so everything past this point is executed five times, once for
each value.
7. style(…) — Takes the reference to the newly created <p>and use css
property on selections using a custom function.
Data visuals:
Bored, maybe yes/ maybe no 😁 Let’s do some drawing.
We are going to build simple bar chart. To display bar we can simply use div
of HTML with few properties of css :
After seeing this, You must be saying Wow, What a beautiful piece of Art 😉
Want to decode methods listed in the code. We are already familiar with
most methods. Let’see what else we got here:
1. attr(…) — attr is used to set and HTML attribute and its value on an
element. In the example we used attr to select(/to create) div with class bar.
In this example we are going to build bar chart with transitions to show the
data in more appealing way. Now try to maintain your pace with below
code snippets and their explanations.
It is a simple JS object of XYZ company with sales data at the end of first six
months.
A SVG container of size 500*400 with margins to plot bar chart properly.
(we are considering Y scale, and calculated numbers based on our dataset)
ticks(…) — takes any number, but it will modify that number to make sure
the axis is a multiple of 2, 5, or 10.
We have already covered methods listed here. “g” element under append is
used to group svg shapes together.
here we are adding axis to the chart. The methods used here are already
covered.
Curious about the result of this exercise, check the following link and try
out your own experiments with the code:
https://jsfiddle.net/ankit1ag/0Lpbksgd/
https://jsfiddle.net/ankit1ag/0Lpbksgd/
A true developer never breaks his promises. Brace yourselves with the help
of following links:
JavaScript
JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled
programming language with first-class…
developer.mozilla.org
SVG Tutorial
SVG stands for Scalable Vector Graphics. SVG defines vector-based
graphics in XML format. With our "Try it Yourself"…
www.w3schools.com
Method chaining
Method chaining, also known as named parameter idiom, is a
common syntax for invoking multiple method calls in…
en.wikipedia.org
So now, you are familiar with basic methods of D3.js and can draw some
basic bar charts using D3.js — but there’s so much more to learn. Follow the
above links to get more knowledge about it. I hope this tutorial shed some
light on D3
To all my riders, ride ends here, how was it, did you like it?
you can write your comments here or can connect with me at LinkedIn. I’d
love to hear from you.
ABC. Always be clappin’.
JavaScript and Building Content-Rich The Best and Most Vue From The TOP — Vue
Accessibility: Don’t Blame Pages on React Native Different Way to Create CLI 3
the Language Vanya Deasy Safrina in
Styles in React Apps Vamshi Krishna in My Point of
Myplanet in Myplanet Musings Traveloka Engineering
using JSS and TypeScript Vue
Omid Shah Hosseini in Weekly
Webtips
Angular 8 — Automatic Creating a REST API with Javascript Methods Learn React, Redux and
application refresh on NestJS — Part 04 — Quick Shuvo Das
Typescript in 2021 —
user’s side Adding a Dockerfile with Shopping Cart Example -
Julien Schneider
Reduced Image Size Cart Implementation #4
Marcos Henrique da Silva in Wojciech Bilicki
JavaScript in Plain English