Introduction

This article is all about the basics of D3.JS, not the full details but thorough enough to provide at least a feeling and fundamental understanding of it. In this article, I am including a basic overview of this entire subject, an introductory part of D3.JS, some important features, the procedure for creating a chart, and some other details too.
Outlines
Overview
As I promised in my previous article, I'll take you on a journey of D3.JS. I am here as your guide and will take you through this interesting ride of Data Visualization (using D3.JS). In this article, I'll be covering its introductory part likely to be its basics and how it is started, its features, the procedure for creating charts, some key points and finally the conclusion of the entire journey.
SO guys buckle up and get ready for this journey of D3.JS. This journey is fun.

Definition | D3.JS

In very short and crispy words we can define D3.JS as:
“D3.Js is a JavaScript library that is being used for data visualization and charting.”

So what does Data Visualization mean?

Data Visualization is data we get from various resources like social networking, government sources, shopping portals and so on and is generally unstructured data. It can be structured or flat. So all these types of data is neither reusable nor visualizable.
So converting this type of rough data into some usable form or some sort of productive or understandable form is called as Data Visualization.
Data visualization can be any of the following forms.

Charts

Bar, column, pie, histogram, line, donut charts and so on.

Diagrams

Some sort of data representation.

Other forms of data diagrams

Something else.

Features

Some very good and handsome features of D3.JS are as follows:
handsome features of D3 JS
Procedure
The 3 main steps of creating a chart are using D3.JS as shown in the following figure:
steps of creating chart using D3 JS

Chart Creation

First of all, we simply need to include D3.Js functionality in our snippet, for that either you can download its a library from the official website or you can directly use its functionality by simply including its script source as:
  1. <script src=http://d3js.org/d3.v2.min.js type=”text/javascript”></script>
Now for the structure of D3.JS.
  1. <script>
  2. // Initialization
  3. d3.Chart("Chart_Type",
  4. {
  5. initialize function()
  6. {
  7. ----
  8. ----
  9. Do_Something;
  10. ----
  11. ----
  12. }
  13. });
  14. // chart rendering properties
  15. var mychart = d3.select(#container)
  16. .append("div#is")
  17. .chart("Chart_Type");
  18. // Providing data values
  19. myChart.draw([1,2,3,4,5]);
  20. </script>
Demo
bar chart creation using D3 JS
Here I am providing a partial demo of a simple bar chart creation using D3.JS and I am using CSV data for the values.
HTML Snippet
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html>
  3. <head>
  4. <title>Bar Chart</title>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9">
  6. <link href="Style1.css" type="text/css" />
  7. </head>
  8. <body>
  9. <div id="chart"></div>
  10. <script src="http://d3js.org/d3.v2.min.js"></script>
  11. <script>
  12. function renderChart() {
  13. // var width = 1020,
  14. // height = 720,
  15. // Attributes
  16. var data = d3.csv.parse(d3.select('#csv').text());
  17. var valueLabelWidth = 40; // space reserved for value labels (right)
  18. var barHeight = 20; // height of one bar
  19. var barLabelWidth = 100; // space reserved for bar labels
  20. var barLabelPadding = 5; // padding between bar and bar labels (left)
  21. var gridLabelHeight = 18; // space reserved for gridline labels
  22. var gridChartOffset = 3; // space between start of grid and first bar
  23. var maxBarWidth = 420; // width of the bar with the max value
  24. // Accessor functions
  25. var barLabel = function (d) { return d['Name']; };
  26. var barValue = function (d) { return parseFloat(d['Salary(PM)']); };
  27. // Scales
  28. var yScale = d3.scale.ordinal().domain(d3.range(0, data.length)).rangeBands([0, data.length * barHeight]);
  29. var y = function (d, i) { return yScale(i); };
  30. var yText = function (d, i) { return y(d, i) + yScale.rangeBand() / 2; };
  31. var x = d3.scale.linear().domain([0, d3.max(data, barValue)]).range([0, maxBarWidth]);
  32. // Svg container element
  33. var chart = d3.select('#chart').append("svg")
  34. .attr('width', maxBarWidth + barLabelWidth + valueLabelWidth)
  35. .attr('height', gridLabelHeight + gridChartOffset + data.length * barHeight);
(It's just a sample code snippet of that chart. For the full details check out my previous article.)
http://www.codeproject.com/Tips/811446/Creating-Bar-Chart-using-D-JS
Output
CSV data for values
Key Points
Some key points of D3.JS that you need to remember are:

Conclusion

I hope you now have a basic understanding of creating a chart for data visualization using D3.JS. it's simple and reusable. For more details or tutorials you can visit its official website that contains the detailed steps and a description of creating various sorts of charts.
Meanwhile, if you experience any problem creating charts or learning D3.Js then feel free to ping me or message me.