Vanilla JavaScript and SVG

Vanilla JavaScript combined with Scalable Vector Graphics can be used to create interactive data visualizations and SVG provides a flexible and scalable way to render graphics on the web.

Example: In this example, we are using SVG to create a bar chart with two bars of different heights. When hovering over a bar, its color changes from pink to green.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>SVG Chart</title>
    <style>
        .bar {
            fill: Pink;
            transition: fill 0.2s;
        }
  
        .bar:hover {
            fill: green;
        }
    </style>
</head>
  
<body>
    <svg id="chart" width="250" height="500">
    </svg>
    <script>
        const data = [200, 80];
        const svg = document.getElementById('chart');
        const Width = svg.getAttribute('width') / data.length;
        const GFG = 10;
        for (let i = 0; i < data.length; i++) {
            const rect =
                document.createElementNS(
                    "...", 'rect');
            rect.setAttribute('class', 'bar');
            rect.setAttribute('x', i * (Width + GFG));
            rect.setAttribute('y', svg.getAttribute('height') - data[i]);
            rect.setAttribute('width', Width);
            rect.setAttribute('height', data[i]);
            svg.appendChild(rect);
        }
    </script>
</body>
  
</html>


Output:

Interactive Data Visualizations in JavaScript

In this article we will learn about Interactive Data visualization in JavaScript, Data visualizations serve as a powerful tool for visually representing information and allowing viewers to easily perceive trends, patterns, and relationships in large datasets. Static visuals often fall short when providing an immersive experience that encourages exploration and understanding.

Table of Content

  • Using Vanilla JavaScript and SVG
  • Using D3.js Library
  • Using jQuery

We will explore all the above methods along with their basic implementation with the help of examples.

Similar Reads

Approach 1: Vanilla JavaScript and SVG

...

Approach 2: Using D3.js Library

Vanilla JavaScript combined with Scalable Vector Graphics can be used to create interactive data visualizations and SVG provides a flexible and scalable way to render graphics on the web....

Approach 3: Using jQuery

...

Contact Us