Introduction
ChartJS is one of the most widely used open-source dashboard solutions across many platforms. Leveraging an HTML5 canvas, its great rendering, and responsive performances across browsers make it a developer’s first choice.
This article will explain how to create a simple graph with the below data in simple HTML. We will use the simple dataset below (top 5 longest rivers of the world) to showcase a few of the graphs,
| River | Length in KMs |
| Nile | 6650 |
| Amazon | 6400 |
| Yangtze | 6300 |
| Mississippi | 6275 |
| Yenisei | 5539 |
Getting Started
We start with a simple skeleton of HTML5. Place just a canvas element with id “myChartContainer”.
Now, ChartJS requires jQuery, Bootstrap and its ChartJS libraries. You can link them from CDN in your HTML page –
At this point, the HTML and the webpage would look like,
- <!DOCTYPEhtml>
- <html>
- <head>
- <metacharset="utf-8"/>
- <metahttp-equiv="X-UA-Compatible"content="IE=edge">
- <title>Chart JS Demo</title>
- <scripttype="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
- </script>
- <scripttype="text/javascript"src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js">
- </script>
- <linkrel="stylesheet"href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>
- </head>
- <body>
- <divstyle="width: 100%; text-align: center">
- This space is reserved for my Chart JS graphs!
- <br>
- <br>
- <buttononclick="GenerateChart()">Generate Chart
- </button>
- <br>
- <br>
- <divstyle="position: relative; height:800px; width:800px">
- <canvasid="myChartContainer"style="border:1px solid">
- </canvas>
- </div>
- </body>
- </html>

Adding the ChartJS elements inside your HTML
Now, moving on to the fun part! We have added a button – Generate Chart and have added a function GenerateChart() on its onClick event.
Within the script tags, we will get the context of the canvas where the chart will be rendered and store it in a variable ‘ctx’
To make it easy, we will have a look at the minimal code representation of a dashboard. Below is the entire code – we will have explanations/description of the prime elements after the code,



Join the conversation! Your thoughts help the community grow.