Open In App

Charts.js General

Chart.js General Accessibility ensures the accessibility rendered on user-provided canvas elements. Users can add accessibility features like ARIA attributes or fallback content within the canvas elements. This ensures that the content of the canvas, and chart information is accessible to all the user who relies on assistive technologies.

Accessible canvas elements

Colors

Chart.js allows color customization in the charts. For the geometric elements like lines and bars, we can set background and border colors and for the Text elements such as labels and titles, we can set the font colors.



It mainly supports three color options which are set by default.

Data Structures

Chart.js General data structure is the backbone of chat.js or any other charting library, with the help of data structures we can determine how data is organized, manipulated, and finally displayed in the chats. To deep dive into the topic must visit Data Structures.



Different Types of Data Structures:

Fonts

Chart.js General Fonts are the global font settings that can be applied in the visual chart allowing to style of the text elements. Using this setting we can customize the text element by applying options like font family, size, weight, color, and more properties. All these options are present in Chart.default.font.

General Fonts Configuration Options

Below are the configuration options for General Fonts:

Options

Padding

The empty area between the canvas’s edges and the chart elements is known as padding in Chart.js. It can be used to provide a border around the chart or to make sure that none of its components are placed too close to the canvas’ edges.

Padding can be applied in different formats as follows.

Performance

Chart.js charts are speedily rendered thanks to their utilization of canvas elements. This choice of rendering contributes to swift performance, allowing for the creation of dynamic and visually appealing charts.

Data Structure and Format

Example 1: In this example, we will decimate a large data and create a line chart. It will create a simple chart, which will display the subset of a large dataset.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>Decimated Line Chart</title>
    <script src=
      </script>
</head>
 
<body>
 
    <canvas id="myChart"
            width="500"
            height="300">
      </canvas>
 
    <script>
        // Example large dataset
        const rawData =
Array.from({ length: 1000 }, (_, i) =>
               ({ x: i, y: Math.random() * 100 }));
 
        // Decimation function
        function decimateData(data, nth) {
            return data.filter((_, i) => i % nth === 0);
        }
 
        // Decimating the dataset
        const decimatedData = decimateData(rawData, 10);
 
        // Chart data with color options
        const data = {
            datasets: [{
                label: 'My Dataset',
                data: decimatedData,
                borderColor: 'rgb(75, 192, 192)', // Line color
                backgroundColor: 'rgba(75, 192, 192, 0.5)', // Fill color
                fill: false, // Disable filling under the line
            }]
        };
 
        // Chart configuration
        const config = {
            type: 'line',
            data: data,
            options: {
                scales: {
                    x: {
                        type: 'linear',
                        position: 'bottom'
                    }
                }
            }
        };
 
        // Initializing the chart
        new Chart(
            document.getElementById('myChart'),
            config
        );
    </script>
 
</body>
 
</html>

Output:

Example 2: In this example, we are using some styling options like backgroundColor, borderColor, etc. It will create a line chart of a smaller dataset as compared to the previous example.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>Styled Line Chart</title>
    <script src=
      </script>
    <style>
        /* Adding some basic styling to the body */
        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
        }
    </style>
</head>
 
<body>
 
    <canvas id="myChart"
            width="600" height="200">
      </canvas>
 
    <script>
        // Sample dataset
        const data = {
            labels: ['January',
                     'February',
                     'March',
                     'April',
                     'May',
                     'June',
                     'July'],
            datasets: [{
                label: 'Monthly Sales',
                data: [12, 19, 3, 5, 2, 3, 9],
                fill: false,
                borderColor: 'green', // Border color
                backgroundColor: 'green', // Background color for points
                pointBorderColor: 'grren', // Border color for points
                pointBackgroundColor: '#fff', // Fill color for points
                pointBorderWidth: 2,
                tension: 0.1 // Bezier curve tension
            }]
        };
 
        // Chart configuration
        const config = {
            type: 'line',
            data: data,
            options: {
                scales: {
                    y: {
                        beginAtZero: true
                    }
                },
                plugins: {
                    legend: {
                        labels: {
                            color: 'Green', // Color for labels
                            font: {
                                size: 14 // Font size
                            }
                        }
                    }
                }
            }
        };
 
        // Initializing the chart
        new Chart(
            document.getElementById('myChart'),
            config
        );
    </script>
 
</body>
 
</html>

Output:


Article Tags :