How to Add a Title to Your Graph

Learn to add a title to the graph.

We'll cover the following

If you’ve completed the lessons about adding the axis labels, then most of this will come as no surprise.

What we want to do to add a title to the graph is add a text element (just a few words) that will appear above the graph and centered left to right.

We’ll start with our default code for our simple graph.

Preparation

Because we’re going to be adding a title to the top of the graph, we need to increase the top margin. Changes like the following should suffice:

var margin = {top: 40, right: 20, bottom: 50, left: 70},

Code to add the title

To add the title, this is the code we’re going to add to the simple line graph script. A nice logical place to put the block of code would be towards the end of the JavaScript. In fact, I would put it as the last element that we add.

Get hands-on with 1200+ tech skills courses.