Open In App

Chart.js General 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.

Syntax:

const config = {
type: 'bar', // or any other chart type
data: {
labels: [...],
datasets: [...],
},
options: {
plugins: {
legend: {
labels: {
font: {
family: 'Arial',
size: 12,
style: 'normal',
weight: 'bold',
lineHeight: 1.2,
},
},
},
},
},
};
new Chart($("#chart"), config);

General Fonts Configuration Options

Below are the configuration options for General Fonts:



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>

Example 1: In this example, a Doughnut Chart using Chart.js showcases programming language data. The chart includes a customized font, presenting legend labels in bold Courier New and a distinctive font for the overall title.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>GeeksforGeeks Doughnut Chart</title>
    <script src=
    </script>
    <script src=
    </script>
</head>
 
<body>
    <div>
        <h1 style="color:green;">GeeksforGeeks</h1>
        <h3>Chart.js General Fonts - Example 1</h3>
        <div>
            <canvas id="doughnutChart"
                    width="400"
                    height="400">
            </canvas>
        </div>
    </div>
    <script>
        const data = {
            labels: ["Java", "Python",
                     "C++", "JavaScript",
                     "C#"],
            datasets: [{
                data: [25, 30, 15, 20, 10],
                backgroundColor: ["#FF6384", "#36A2EB",
                                  "#FFCE56", "#4CAF50",
                                  "#9C27B0"],
            }]
        };
        const config = {
            type: 'doughnut',
            data: data,
            options: {
                plugins: {
                    legend: {
                        labels: {
                            font: {
                                family: 'Courier New',
                                size: 12,
                                style: 'normal',
                                weight: 'bold',
                            },
                        },
                    },
                },
                responsive: true,
                maintainAspectRatio: false,
                title: {
                    display: true,
                    text:
                'GeeksforGeeks Doughnut Chart with Different Font Customization',
                },
            }
        };
        new Chart($("#doughnutChart"), config);
    </script>
</body>
 
</html>

Output:



Example 2: In the below example, we have used the Radar chart, in which the the label (Programming Languages) we have given the custom font options like the font family is specified as Verdana and the size I have been given as 30. The style is given in italics. So using these options, we have automatized the font text of the Chart.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>GeeksforGeeks Doughnut Chart</title>
    <script src=
    </script>
    <script src=
    </script>
</head>
 
<body>
    <div>
        <h1 style="color:green;">GeeksforGeeks</h1>
        <h3>Chart.js General Fonts - Example 1</h3>
        <div>
            <canvas id="doughnutChart"
                    width="400"
                    height="400">
            </canvas>
        </div>
    </div>
    <script>
        const data = {
            labels: ["Java", "Python",
                     "C++", "JavaScript",
                     "C#"],
            datasets: [{
                data: [25, 30, 15, 20, 10],
                backgroundColor: ["#FF6384", "#36A2EB",
                                  "#FFCE56", "#4CAF50",
                                  "#9C27B0"],
            }]
        };
        const config = {
            type: 'doughnut',
            data: data,
            options: {
                plugins: {
                    legend: {
                        labels: {
                            font: {
                                family: 'Courier New',
                                size: 12,
                                style: 'normal',
                                weight: 'bold',
                            },
                        },
                    },
                },
                responsive: true,
                maintainAspectRatio: false,
                title: {
                    display: true,
                    text:
                'GeeksforGeeks Doughnut Chart with Different Font Customization',
                },
            }
        };
        new Chart($("#doughnutChart"), config);
    </script>
</body>
 
</html>

Output:


Article Tags :