Building a Bubble Chart (JS): Top 30 Most-Liked YouTube Videos
Tags: javascript, beginners, webdev, datascience
Published: 2023-08-02 16:24:38

Do you want to turn that data chaos into a comprehensible data show? Look no further than [data visualization](https://www.anychart.com/blog/2018/11/20/data-visualization-definition-history-examples/)! And let me tell you, one chart to rule them all is the **bubble chart**. It's like blowing bubbles at your data and watching them pop into understanding! And the best part? With JavaScript, you can easily create an interactive bubble chart, and I will show you how!

Bubble charts are versatile data visualizations that display data points in the form of bubbles, where the bubbles' size, color, and position represent different variables. In this tutorial, I'll walk you through building these graphics step by step, making the process a breeze. For illustration, we'll take data on the popularity of various YouTube videos and use JavaScript to create an interactive bubble chart showcasing the **top 30 most-liked videos** of all time, categorized by year of release, views, and likes.

By the end of this tutorial, you'll be able to create visually-stunning JS-based bubble charts without any hassle. So let's dive into the world of data bubbles and turn numbers into a visual feast for the eyes!
## Bubble Chart That Will Be Created
Take a quick look at the JS bubble chart we will build together in this tutorial, and follow along!
![Bubble Chart Preview](https://ucarecdn.com/0ddd6d61-b839-4fd4-81e1-3f6b4851e683/)

## A. Creating a Basic JS Bubble Chart
Don't be intimidated if you're new to creating JS charts — it's easier than you might think! In fact, all you need to do is follow these four steps to make it happen:
1. Create a web page in HTML.
2. Include the required JavaScript files.
3. Add the data.
4. Write some JS charting code

### 1. Create a web page in HTML
The first step is a breeze — just create a basic HTML web page! From there, we'll add a `<div>` element and give it a unique ID like "container” to create the space for the bubble chart. To ensure that the graph takes up the entire page, we’ll use CSS code within the `<style>` tag to set the width and height to 100%; of course, this can be completely customized to fit your exact needs and preferences.
```html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>JavaScript Bubble Chart</title>
    <style type="text/css">  	
      html, body, #container {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
  </body>
</html>
```
### 2. Include the required JavaScript files
After setting up a basic HTML page, the next step is to include the necessary JavaScript files in the `<head>` section. There are [numerous](https://en.wikipedia.org/wiki/Comparison_of_JavaScript_charting_libraries) charting libraries available. In this tutorial, we'll be using [AnyChart](https://www.anychart.com/). This library supports bubble charts and offers comprehensive documentation. But no matter which library you choose, the basic steps for creating a chart are similar.
  
To create a bubble chart, we’ll use the [Base](https://docs.anychart.com/Quick_Start/Modules#base) module. The data will be in a JSON file, so we’ll need the [Data Adapter](https://docs.anychart.com/Quick_Start/Modules#data_adapter) module to load it.

```html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>JavaScript Bubble Chart</title>
    <script src="https://cdn.anychart.com/releases/8.11.1/js/anychart-base.min.js"></script>
    <script src="https://cdn.anychart.com/releases/8.11.1/js/anychart-data-adapter.min.js"></script>
    <style type="text/css">  	
      html, body, #container {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script>
      // All the code for the bubble chart will come here.
    </script>
  </body>
</html>
```
### 3. Add the data
I compiled the data about the top 30 most liked YouTube videos from [BuzzGuru](https://app.buzzguru.com/youtube/top/100-most-liked-videos) and [YouTube](https://www.youtube.com/) and saved it in a JSON file, which you can find [here](https://gist.githubusercontent.com/awanshrestha/8df51b9b1c44b2d1bd3ebeb0757b5fb0/raw/28ee08902d393e02278a7ef7e3461b33e34c6c90/Top-liked-YouTube-Videos.json).
  
In each object in the dataset represents a YouTube video and is to be visualized as one bubble. The objects have five properties: "x" for the year of upload to YouTube, "value" for the number of YouTube views, "size" for the number of YouTube likes, as well as "title" and "genre".

To load the data into a bubble plot, we’ll use the `anychart.data.loadJsonFile()` function.
```javascript
anychart.data.loadJsonFile("https://gist.githubusercontent.com/awanshrestha/8df51b9b1c44b2d1bd3ebeb0757b5fb0/raw/28ee08902d393e02278a7ef7e3461b33e34c6c90/Top-liked-YouTube-Videos.json",
function (data) {});
```
You will find out where to put this code in a few moments.
### 4. Write some JS charting code
It’s time for a few lines of JavaScript code that will construct the visualization!
  
To begin, wrap the code in an `anychart.onDocumentReady()` function ensures that it only executes once the page is fully loaded.
```html
<script>
  anychart.onDocumentReady(function () {
    // The bubble chart data and code will be in this section.
  });
</script>
```
Add the data from Step 3.
```html
<script>
  anychart.onDocumentReady(function () {
    anychart.data.loadJsonFile("https://gist.githubusercontent.com/awanshrestha/8df51b9b1c44b2d1bd3ebeb0757b5fb0/raw/28ee08902d393e02278a7ef7e3461b33e34c6c90/Top-liked-YouTube-Videos.json",
    function (data) {
      // The upcoming JS code will be here.
    });
  });
</script>
```
Pass the data to the `anychart.bubble()` function to create a bubble chart object.

```javascript
chart = anychart.bubble(data);
```
It's possible to set the minimum and maximum bubble size using the `minBubblesize()` and `maxBubblesize()` functions respectively. 

```javascript
chart.minBubbleSize("3%");
chart.maxBubbleSize("12%");
```
Finally, let’s give the bubble chart a title and insert it into the designated `<div>` container. Once this is done, we can use the `draw()` function to render the visualization on the web page.

```javascript
chart.title("Top 30 Most Liked YouTube Videos");
chart.container("container");
chart.draw();
```
It's here! Our first JavaScript bubble chart is ready. Its interactive version with the full code can be found on [Playground](https://playground.anychart.com/EzsEZceM). The code is also provided below for your convenience.
![Basic JS bubble chart](https://ucarecdn.com/6a828297-eb4e-42e0-9e9d-8ec97b41a5c4/)
```html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>JavaScript Bubble Chart</title>
    <script src="https://cdn.anychart.com/releases/8.11.1/js/anychart-base.min.js"></script>
    <script src="https://cdn.anychart.com/releases/8.11.1/js/anychart-data-adapter.min.js"></script>
    <style type="text/css">  	
      html, body, #container {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script>
      anychart.onDocumentReady(function() {
        // load data
        anychart.data.loadJsonFile("https://gist.githubusercontent.com/awanshrestha/8df51b9b1c44b2d1bd3ebeb0757b5fb0/raw/28ee08902d393e02278a7ef7e3461b33e34c6c90/Top-liked-YouTube-Videos.json",
        function (data) {
          // create a bubble chart    
          chart =  anychart.bubble(data);   
          // set the minimum and maximum bubble sizes
          chart.minBubbleSize("3%");
          chart.maxBubbleSize("12%");
          // add a chart title
          chart.title("Top 30 Most Liked YouTube Videos");
          // set the container
          chart.container("container");
          // initiate the chart drawing
          chart.draw();
        })
      });
    </script>
  </body>
</html>
```
## B. Customizing JS Bubble Chart
Let's take our JavaScript bubble chart to the next level by adding some exciting customizations that will make it look even more visually appealing! We can transform our chart from good to great with just a few tweaks. Let's do it!

1. Name the axes
2. Arrange the ticks
3. Enable the grid
4. Paint the bubbles by genre
5. Prevent clipping
6. Add the legend
7. Enhance the tooltip

**FOR A WALKTHROUGH OF THESE JS BUBBLE CHART CUSTOMIZATIONS, CONTINUE READING THE [TUTORIAL HERE](https://www.anychart.com/blog/2023/08/01/bubble-chart-js/).**