How to Add Axis Titles to Chart.js Charts?

14 minutes read

To add axis titles to Chart.js charts, you can use the scales option in the configuration object for each respective axis. Here's how you can do it for both the x-axis (horizontal) and y-axis (vertical):

  1. For the x-axis title: Within the options object of your Chart.js chart configuration, add a scales object if it doesn't already exist. Inside the scales object, create a xAxes array if it doesn't already exist. Within the xAxes array, create an object to configure the x-axis properties. Inside the x-axis object, add a scaleLabel object and set its display property to true. Within the scaleLabel object, you can set the axis title by defining the labelString property as the desired title text.
  2. For the y-axis title: Similarly, within the options object of your chart configuration, add a scales object if needed. Inside the scales object, create a yAxes array if it doesn't exist yet. Within the yAxes array, create an object to configure the y-axis properties. Inside the y-axis object, add a scaleLabel object and set its display property to true. Within the scaleLabel object, set the labelString property with the desired title text.


Remember to adjust your configuration based on the specific chart type you are using.

Best JavaScript Books to Read in 2024

1
JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

Rating is 5 out of 5

JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

2
Web Design with HTML, CSS, JavaScript and jQuery Set

Rating is 4.9 out of 5

Web Design with HTML, CSS, JavaScript and jQuery Set

3
JavaScript and jQuery: Interactive Front-End Web Development

Rating is 4.8 out of 5

JavaScript and jQuery: Interactive Front-End Web Development

  • JavaScript Jquery
  • Introduces core programming concepts in JavaScript and jQuery
  • Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
4
JavaScript: The Comprehensive Guide to Learning Professional JavaScript Programming (The Rheinwerk Computing)

Rating is 4.7 out of 5

JavaScript: The Comprehensive Guide to Learning Professional JavaScript Programming (The Rheinwerk Computing)

5
JavaScript from Beginner to Professional: Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages

Rating is 4.6 out of 5

JavaScript from Beginner to Professional: Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages

6
JavaScript All-in-One For Dummies

Rating is 4.5 out of 5

JavaScript All-in-One For Dummies

7
Learn JavaScript Quickly: A Complete Beginner’s Guide to Learning JavaScript, Even If You’re New to Programming (Crash Course With Hands-On Project)

Rating is 4.4 out of 5

Learn JavaScript Quickly: A Complete Beginner’s Guide to Learning JavaScript, Even If You’re New to Programming (Crash Course With Hands-On Project)

8
Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming

Rating is 4.3 out of 5

Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming

  • It can be a gift option
  • Comes with secure packaging
  • It is made up of premium quality material.
9
Head First JavaScript Programming: A Brain-Friendly Guide

Rating is 4.2 out of 5

Head First JavaScript Programming: A Brain-Friendly Guide

10
Learning JavaScript: JavaScript Essentials for Modern Application Development

Rating is 4.1 out of 5

Learning JavaScript: JavaScript Essentials for Modern Application Development

11
Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

Rating is 4 out of 5

Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

12
Learning JavaScript Design Patterns: A JavaScript and React Developer's Guide

Rating is 3.9 out of 5

Learning JavaScript Design Patterns: A JavaScript and React Developer's Guide

13
Professional JavaScript for Web Developers

Rating is 3.8 out of 5

Professional JavaScript for Web Developers


What is the default visibility of axis titles in Chart.js?

The default visibility of axis titles in Chart.js is true, which means that axis titles are displayed by default.


How to change the font family of axis titles in Chart.js?

To change the font family of axis titles in Chart.js, you can use the title object in the scales property of the chart options. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var chartOptions = {
  scales: {
    x: {
      title: {
        display: true,
        text: 'X-axis Title',
        font: {
          family: 'Arial',
          size: 16,
          weight: 'bold'
        }
      }
    },
    y: {
      title: {
        display: true,
        text: 'Y-axis Title',
        font: {
          family: 'Arial',
          size: 16,
          weight: 'bold'
        }
      }
    }
  }
};

var chart = new Chart(ctx, {
  type: 'bar',
  data: data,
  options: chartOptions
});


In the above example, font.family sets the font family to Arial for both x-axis and y-axis titles. Adjust the size and weight properties as desired.


What is the recommended font size for axis titles in Chart.js?

There is no specific recommended font size for axis titles in Chart.js. The best font size for axis titles depends on the specific chart's size and the amount of text you need to display.


In general, it is recommended to use a font size that is large enough to be easily readable but small enough to fit within the available space. You can start with a font size of around 12-14 pixels and adjust it as needed based on the chart's size and the length of the axis titles. Additionally, you can also consider using a slightly larger font size for larger charts and a slightly smaller font size for smaller charts to maintain readability.


How to display multiline axis titles in Chart.js?

Currently, Chart.js does not support multiline axis titles out-of-the-box. However, you can achieve multiline axis titles by manually adjusting the chart layout using the layout option.


Here's an example of how you can do it:

  1. First, set up your chart as usual:
1
2
3
4
5
6
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {...},
    options: {...}
});


  1. Then, modify the chart layout options to create multiline axis titles:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var originalScaleDraw = Chart.scaleService.prototype.draw;

Chart.scaleService.prototype.draw = function() {
    originalScaleDraw.apply(this, arguments);

    var ctx = this.chart.ctx;
    var fontSize = this.options.ticks.fontSize;
    var fontStyle = this.options.ticks.fontStyle;
    var fontFamily = this.options.ticks.fontFamily;
    var titleFont = Chart.helpers.fontString(fontSize, fontStyle, fontFamily);

    ctx.save();
    ctx.font = titleFont;
    ctx.textBaseline = 'middle';

    var yAxis = this.chart.scales['y-axis-0'];
    var xAxis = this.chart.scales['x-axis-0'];

    // Display multiline y-axis title
    ctx.translate(yAxis.left, yAxis.top + (yAxis.bottom - yAxis.top) / 2);
    ctx.rotate(-0.5 * Math.PI);
    ctx.textAlign = 'center';
    ctx.fillText('Your multi-line y-axis title', 0, 0);

    // Display multiline x-axis title
    ctx.translate((xAxis.right - xAxis.left) / 2, xAxis.bottom + xAxis.options.offset);
    ctx.textAlign = 'center';
    ctx.fillText('Your multi-line x-axis title', 0, 0);

    ctx.restore();
};


In the example above, the draw function of the Chart.scaleService prototype is overridden to manually draw the multiline axis titles. Inside the overridden function, the current chart context is obtained, and the necessary font and alignment settings are applied.


The multiline titles are then drawn manually using the fillText method of the canvas context. In this example, the y-axis title is rotated by -0.5 * π (90 degrees counter-clockwise) to display it vertically. The x-axis title is displayed normally.


Make sure to replace 'Your multi-line y-axis title' and 'Your multi-line x-axis title' with your desired multiline axis titles.


With these modifications, the axis titles in your Chart.js chart should be displayed as multiline.


Note: This solution works for the latest version of Chart.js (currently 3.x). If you are using an older version of Chart.js, some adjustments to the code might be necessary.


How to add a title to the Y-axis in Chart.js?

To add a title to the Y-axis in Chart.js, you can use the yAxes configuration option. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],
      borderColor: [
        'rgba(255, 99, 132, 1)',
        'rgba(54, 162, 235, 1)',
        'rgba(255, 206, 86, 1)',
        'rgba(75, 192, 192, 1)',
        'rgba(153, 102, 255, 1)',
        'rgba(255, 159, 64, 1)'
      ],
      borderWidth: 1
    }]
  },
  options: {
    scales: {
      yAxes: [{
        scaleLabel: {
          display: true,
          labelString: 'Number of Votes'
        }
      }]
    }
  }
});


In the above example, the options object specifies the scales property, which contains the yAxes array. Inside the yAxes array, you can use the scaleLabel property to add a title to the Y-axis. The display property is set to true to display the title, and the labelString property specifies the actual title text.


How to add a border to axis titles in Chart.js?

In Chart.js, you can add a border to axis titles by using the title option and creating a custom CSS class for the titles that includes a border style.


Here's an example of how you can achieve this:

  1. First, define your chart options including the title configuration:
1
2
3
4
5
6
7
var options = {
  title: {
    display: true,
    text: 'My Chart Title'
  },
  ...
};


  1. Next, create a custom CSS class for the axis titles. In this example, we'll use the class name axis-title:
1
2
3
4
5
.axis-title {
  border: 1px solid black;
  padding: 5px;
  background-color: white;
}


  1. Finally, apply the custom CSS class to the axis titles using the callback function for the title option. This function allows you to modify the title element directly:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var options = {
  title: {
    display: true,
    text: 'My Chart Title',
    callback: function(value, index, values) {
      return "<div class='axis-title'>" + value + "</div>";
    }
  },
  ...
};


Now, the axis titles in your Chart.js chart will have a border style applied to them. You can customize the border style by modifying the CSS class .axis-title.

Facebook Twitter LinkedIn Telegram

Related Posts:

To add a dataset toggle to a chart using Chart.js, you can follow these steps:Begin by including the Chart.js library in your HTML file. You can either download it and host it locally or include it from a CDN. Make sure to include both the Chart.js library and...
To export a chart.js chart to Excel, you can follow these steps:Prepare your chart: Create a chart using the chart.js library in your web application. Make sure the chart is fully rendered and visible on the webpage. Include the necessary libraries: To perform...
To set plural label values in Chart.js, you can follow these steps:Make sure you have included the Chart.js library in your project. This can be done by adding the script tag for Chart.js in your HTML file. Create a canvas element in your HTML where you want t...