Open In App

Chart.js Scatter Chart

Chart.js Scatter Chart is a data visualization concept that allows users to represent and analyze the relationship between two numerical variables. Scatter charts use points or markers to represent the data points for showing the patterns, trends, and correlations. Mainly, the scatter chart can be used for exploring the distribution of data points and also to identify potential outliers in the data analysis process.

Scatter Chart Dataset Properties

There are some of the dataset properties that are mentioned below:



Approach

CDN link

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js”></script><script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.1.2/chart.umd.js”></script>

Syntax

new Chart($("#ID"), {
type: 'scatter'
data: { ... },
options: { ... }
});

 



Example 1: In this example, we have created a simple representation of a Scatter Chart using Chart.js. The chart mainly describes the visual form of data related to Visitors on GeeksforGeeks. On the horizontal axis, we have the months of the year and on the vertical axis, we have the visitors value per month.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Scatter Chart</title>
    <script src=
    </script>
    <script src=
    </script>
</head>
  
<body>
    <div>
        <h2 style="color:green">
            GeeksforGeeks
        </h2>
          
        <h5>Chart JS Scatter Chart 1</h5>
          
        <div>
            <canvas id="visitorsChart" 
                width="700" height="300"></canvas>
        </div>
    </div>
  
    <script>
        const visitorsData = [
            { x: 1, y: 500 },
            { x: 2, y: 700 },
            { x: 3, y: 1200 },
            { x: 4, y: 900 },
            { x: 5, y: 1500 },
        ];
  
        const config = {
            type: 'scatter',
            data: {
                datasets: [{
                    label: 'Visitors Data',
                    data: visitorsData,
                    backgroundColor: 'rgba(75, 192, 192, 0.7)',
                }],
            },
            options: {
                scales: {
                    x: {
                        type: 'linear',
                        position: 'bottom',
                        title: {
                            display: true,
                            text: 'Months',
                        },
                    },
                    y: {
                        type: 'linear',
                        position: 'left',
                        title: {
                            display: true,
                            text: 'Number of Visitors',
                        },
                    },
                },
                elements: {
                    point: {
                        radius: 8,
                        hoverRadius: 10,
                    },
                },
                plugins: {
                    title: {
                        display: true,
                        text: 'GeeksforGeeks Monthly Visitors',
                    },
                },
            },
        };
        const ctx = document.getElementById(
            'visitorsChart').getContext('2d');
              
        new Chart(ctx, config);
    </script>
</body>
  
</html>

Output:

Example 2: In this example, we have created the Scatter Chart using Chart.js. The chart represents the Courses and Enrollment data related to GeeksforGeeks subjects. We have also customized the color of the scatter data point by applying some attractive styling options to it like hovering.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Scatter Chart</title>
    <script src=
    </script>
    <script src=
    </script>
</head>
  
<body>
    <div>
        <h2 style="color:green;">
            GeeksforGeeks
        </h2>
          
        <h5>Chart JS Scatter Chart 2</h5>
          
        <div>
            <canvas id="coursesChart" 
                width="700" height="300"></canvas>
        </div>
    </div>
  
    <script>
        const coursesData = [
            { x: 'Java', y: 150 },
            { x: 'Python', y: 300 },
            { x: 'JavaScript', y: 250 },
            { x: 'C++', y: 200 },
            { x: 'Data Structures', y: 180 },
        ];
  
        const config = {
            type: 'scatter',
            data: {
                datasets: [{
                    label: 'Courses Enrollment',
                    data: coursesData,
                    backgroundColor: 'rgba(255, 99, 132, 0.7)',
                }],
            },
            options: {
                scales: {
                    x: {
                        type: 'category',
                        position: 'bottom',
                        title: {
                            display: true,
                            text: 'Courses',
                        },
                    },
                    y: {
                        type: 'linear',
                        position: 'left',
                        title: {
                            display: true,
                            text: 'Enrollment Count',
                        },
                    },
                },
                elements: {
                    point: {
                        radius: 9,
                        hoverRadius: 20,
                    },
                },
                plugins: {
                    title: {
                        display: true,
                        text: 'GeeksforGeeks Courses Enrollment',
                    },
                },
            },
        };
        const ctx = document.getElementById(
            'coursesChart').getContext('2d');
              
        new Chart(ctx, config);
    </script>
</body>
  
</html>

Output:


Article Tags :