Data Visualisation Using D3 Javascript Charting Library

Posted on by By Krupal, in Data Visualization | 0

D3 stands for Data-Driven Documents. D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers. D3.js is written in JavaScript and uses a functional style which means you can reuse code and add specific functions.

Below is demo of how to create Bar chart in D3.

To set a D3 to your webpage you need only one file:

  • D3 JavaScript file

You can choose solution to link to external services which host these file for you.

<!-- These belongs to the HTML file where you want D3 to work - put these lines into your <head> tag -->
<script type="text/javascript"src="//"></script> </head>

We need to include style and javascript code inside HTML file itself.

body {
  font: 10px sans-serif;

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;

.bar {
  fill: orange; 
.x.axis path {
  display: none;

We have to take data as input file in .json or .csv or .tsv format.

One of the key feature of D3 charts is SVG (Scalable Vector Graphics). Scalable Vector Graphics (SVG) is a family of specifications for creating two-dimensional vector graphics. 

SVG comes with a basic set of shape elements:

  • Rectangle
  • Circle
  • Ellipse
  • Straight Line
  • Polyline
  • Polygon
<!---  Below is example of creating Rectangle in SVG -->
<svg width="50" height="50">
 <rect x="0" y="0" width="50" height="50" fill="green" />
<!-- This is how we add SVG element -->"body")
.attr("width", 50)
.attr("height", 50)
.attr("x", 0)
.attr("y", 0) .style("fill", "purple");
<!-- This is how we bound to DOM element -->
var theData = [ 1, 2, 3 ]
var p ="body").selectAll("p")
.text("hello ");

Inside script tag we will append SVG and javascript code.


var margin = {top: 40, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - - margin.bottom;

var formatPercent = d3.format(".0%");

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
    .range([height, 0]);

var xAxis = d3.svg.axis()

var yAxis = d3.svg.axis()

var svg ="body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + + margin.bottom)
    .attr("transform", "translate(" + margin.left + "," + + ")");

d3.tsv("data.tsv", type, function(error, data) {
  x.domain( { return d.letter; }));
  y.domain([0, d3.max(data, function(d) { return d.frequency; })]);

      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")

      .attr("class", "y axis")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")

      .attr("class", "bar")
      .attr("x", function(d) { return x(d.letter); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.frequency); })
      .attr("height", function(d) { return height - y(d.frequency); })

function type(d) {
  d.frequency = +d.frequency;
  return d;


At the end this is how Bar chart will look like.



Best Open Source Business Intelligence Software Helical Insight is Here


A Business Intelligence Framework


Krupal Barot

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments