Introduction to D3.js for data visualization
Introduction to D3.js for data visualization. D3.js is a JavaScript library used to manipulate documents based on data. It uses HTML, CSS, and SVG to create visual representations of data which can be viewed on any modern browser. it is a visualization tool.
Create a folder named d3js_projects. Create an HTML file in the folder named project_1.html. You can also learn about visualization from Easyshiksha Certification
create a simple html web page
<html>
<head>
<title>My first page</title>
</head>
<body> Web Page</body>
<html>
after this just setup D3.js with this code in head element. Rember one thing script code will be written in head elament.
<script type=”text/javascript” src=”d3.v2.min.js”></script>
After this setup the source
To test our D3.js setup we open the inspect element tool kit.
In the element tab of webkit Inspector, we open all of the elements so that we can see the whole HTML structure. we then hover over the d3.vs.min.js src.
DOM Section
Using D3.js, we can manipulate the Document Object Model (DOM), meaning we can select elements and apply various transformations on the data visualization.
<html><head>
<title>Learn D3 in 5 minutes</title>
</head><body><h3>Today is a beautiful day!!</h3>
<script src='https://d3js.org/d3.v4.min.js'></script><script>
d3.select('h3').style('color', 'darkblue');
d3.select('h3').style('font-size', '24px');</script>
select method is used to make manupulations and or add data in a element.
So we’re simply chaining the select()
method on the d3
object, and then following up with style()
. The first parameter dictates what we want to change and the second gives the value. Here’s the result:
The next concept you’ll need to learn is data binding, as that’s one of the coolest features of D3. Using it, we can populate or manipulate DOM elements in real-time. Introduction to D3.js for data visualization
In our HTML, we have a simple unordered list <
ul>: <ul> </ul>
We want to create the list elements using a data object. Here’s the code for doing exactly that:
<script>
var languages=[‘java’, ‘python’, ‘javascript’, ‘c++’];
d3.select(“ul”) //firstly we will select the ul
.selectAll(“li”) //now we are selecting all the data is li as a array. but li is no present in code
.data(languages) //language is the array passed in data it will run up to 4 times
.enter() // enter will check if li is present or not
.append(“li”) // with append we will add li
.text(function(d){return d;});
</script>
I hope you like the basic introducation to D3.js to learn more visit hawkscode and Easyshiksha.