What is a programming language?

A programming language is a set of instructions and rules that a computer can understand and execute in order to perform a specific task or set of tasks. It is used to create software, apps, and…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Introduction to D3.JS

Visual Representation of Data

D3.js is an open source JavaScript library for manipulating HTML documents based on data. D3.js is being used to create visual representations of the Data. It makes use of the widely implemented SVG, HTML5, and CSS standards.D3 stands for Data Driven Documents and is currently the number one choice for web-based analytics and visualization projects when it comes to interactivity, flexibility and features.

1). To get Quick Start with D3.js by just adding open source javascript library into your Project,

The full source code are also available for download,

D3.JS provides much more to its user than just visualization, such as:

1). DOM Selection:

How to select DOM

The code appends a level one header using an h1 tag to the body tag of the document. The h1 tag then has its content set to the text This is DOM Selection. And as we saw earlier, the output in the browser looks like the following screenshot:

Output of DOM Selection

The actual D3.js code in this example consists of the following three functions placed within another <script> tag within the body of the document.

d3.select(‘body’)
.append(‘h1’)
.text(‘This is DOM Selection!!’);

All D3.js statements will start with the use of the d3 namespace. This is the root of where we start accessing all the D3.js functions. In this line, we call the .select() function, passing its body. This is telling D3.js to find the first body element in the document and return it to us for performing other operations upon it.

The .select() function returns a D3.js object representing the body DOM object. We can immediately call .append(‘h1’) to add the header element inside the body of the document.

The .append() function returns another D3.js object, but this one represents the new h1 DOM element. So all we need to do is to call: .text(‘This is DOM Selection!!’), and we get Text into h1 DOM element

2) Data Binding:

In D3.js, we drive the visualization of data through binding using the following functions of a selector.

Function Purpose

.data() Specifies the data to be used to drive the visualization

.enter() Returns a selector representing the new items to be displayed

.exit() Returns a selector representing the items that are no longer to be displayed

Example of Data Binding

Here in this code snippet, we added three integer values to DOM by using .data() function and now using the selector variable, we call the .enter() function and assign it to a variable named entering:

var entering = selector.enter();

The value of entering will represent the new items in the selector that need to be created. selector did not have any div tags selected, and since we bound to three items, this variable represents the three new items in the selector that need to be created.

We can then use the entering value and call functions to specify how to render the visuals for each item:

entering.append(‘div’)
.text(function(d) { return d; });

After execution, the value of selector contains three items, with both values assigned and the DOM elements created..

Output

3) Creating SVG Element:

Scalable Vector Graphics (SVG) is a way to render graphical elements and images in the DOM.
We work with SVG within HTML by using an SVG tag, and placing the SVG elements within that tag. A very simple example is the following, which creates three circles:

<svg width=”720" height=”120">
<circle cx=”40" cy=”20" r=”10"></circle>
<circle cx=”80" cy=”40" r=”15"></circle>
<circle cx=”120" cy=”60" r=”20"></circle>
</svg>

Output of SVG Element

The SVG element itself is not visible on the page, and only provides a holder for the child tags.we will always explicitly set the width and heights of the SVG tag. In this example, it is set to be 720 pixels wide by 120 pixels tall.

The example did not specify a color for these circles, so the default color of the circles is black. Most SVG elements specify the color by using the CSS style attribute, and then by setting the fill attribute of the style.

<svg width=”720" height=”120">
<circle cx=”40" cy=”20" r=”10" style=”fill:red”></circle>
<circle cx=”80" cy=”40" r=”15" style=”fill:green”></circle>
<circle cx=”120" cy=”60" r=”20" style=”fill:blue”></circle>
</svg>

Output of SVG Element with Colors

4) Creating a Bar Graph:

Bar Graph

The code starts with a declaration of the data that is to be represented as a graph. In above image javascript shows that how to render graph on SVG Tag.This example uses a hard-coded array of integers as data. Each item in an Array represents a single bar in Graph, We make use of the fact that bars are just rectangles with variable width or height.

We need to perform two pieces of math to be able to calculate the x and y location of the bars. We are positioning these bars at pixel locations starting at the bottom and the left of graphGroup. We need two functions to calculate these. The first one calculates the x location of the left side of the bar,

function xloc(d, i) { return i * (barWidth + barPadding); }

During binding, this will be passed the current datum and its position within the data array. We do not actually need the value for this calculation. We simply calculate a multiple of the sum of the width and padding for the bar based upon the array position.

Since SVG uses an upper-left origin, we need to calculate the distance from the top of the graph as the location from where we start drawing the bar down towards the bottom of the visual:

function yloc(d) { return maxValue — d; }

When we position each bar, we will use a translate transform that takes advantage of each of these functions. We can facilitate this by declaring a function which, given the current data item and its array position, returns the calculated string for the transform property based upon this data and the functions:

function translator(d, i) {
return “translate(“ + xloc(d, i) + “,” + yloc(d) + “)”;
}
All we need to do now is generate the SVG visuals from the data:
barGroup.selectAll(“rect”)
.data(data)
.enter()
.append(‘rect’)
.attr({
fill: ‘steelblue’,
transform: translator,
width: barWidth,
height: function (d) { return d; }
});

I think that D3.js is Simple and great library from javascript to accomplish some interesting projects.I hope this post on D3.js helps anyone to create new project by understanding basic concepts of Data visualization and different methods of D3.js.

Add a comment

Related posts:

Galore

Medium says to tell my story. There’s nothing to be shared. There is actually, or, there are but, there’s no mental strength whatsoever to go down that path. But I will keep this worthwhile. You’re…

Top 4 Best Laptops for Network Engineers

Network engineering is one of the top occupations out there, with over 300,000 engineers in the field today. As a network engineer, you plan, construct, and manage the infrastructure to make sure…

Skin Whitening In Islamabad

Skin whitening is becoming more popular nowadays because people consider it a symbol of beauty. As it is a non-surgical procedure, it is safe. It is the dream of every person to get glow, healthy…