Open In App

How to apply animation in D3.js?

Last Updated : 11 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

D3.js is a Javascript library that is mostly used for data-driven documents. D3 makes the use of SVG as in HTML5. Many things along with making charts, adding animations, and visualizing data can be done with d3.js. Animations can be applied using d3.js transition feature That provides many functionalities like delay, easing, fading, duration, scaling and many.

Approach: Animations in d3 can be done with the help of transitions provide by D3.js. First of all, make a div element and add some text to it. then select this element using the d3.select() function. After this use transition function available in d3.js to apply animations on the selected HTML element.

Example 1: This example use the d3.select() function along with d3.transition() function to apply effects to the html element.




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width, 
                 initial-scale=1.0">
  <title>GeeksforGeeks</title>
</head>
<body>
  <div>
    <h3 style="color:green"> Geeks for Geeks</h3>
  </div>
  <script type = "text/javascript" 
          src
      </script>
  <script>
     d3.selectAll("h3").transition()
        .style(
"font-size", "50px").delay(1000).duration(1000)
  </script>
</body>
</html>


Output:

Example 2:




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width, 
                 initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  div{
    color:green;
    display: flex;
    margin: 0;
    font-size: xx-large;
    padding: 10px;
    width: 100%;
    justify-content: center;
    align-items: center;
    height: 100vh;
  }
</style>
<body>
  <div>
    <p> Geeks for Geeks</p>
  </div>
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js">
  </script>
  <script>
     d3.selectAll("p").transition()
        .style(
"transform", "rotate(180deg)").delay(1000).duration(1000)
        .style("color", "black").duration(1000)
  </script>
</body>
</html>


Output :



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads