Open In App

Chart.js General Fonts

Last Updated : 06 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • family: This option specifies the font family for the text elements in the chart. It follows the CSS font-family options like Arial, Helvetica etc.
  • size: This option specifies the actual font size presented in px format form. By default, the size of the text is 12 px.
  • style: This option is used to set the font style by customizing text into normal, italic, and oblique variants.
  • weight: This option is used to control the thickness of font characters.
  • lineHeight: This option determines the height of each line of text. The default height is 1.2 which can be given in number/string form.

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.

HTML




<!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:

Output1

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.

HTML




<!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:

Output2



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads