Introduction

- <script src="js/Chart.min.js"></script>
I am using a minified version of chart.js. Add an <canvas> element with height, width, and unique id.
Example 1
In the first example we will create a Pie Chart using chart.js. For creating the pie chart, a variable array (named pieChartData) is declared that contains value and color properties. Set the values and color depending upon your chart.

Chart.js provides various options for changing the animation and look. You can change these options depending on your wishes.
For creating the chart, we must initialize the chart class and our canvas element and "2D" drawing context and call the pie method.
- <!Doctype HTML>
- <html>
- <head>
- <title>Pie Chart</title>
- <script src="js/Chart.min.js"></script>
- </head>
- <body>
- <canvas id="pieChartLoc" height="300" width="300"></canvas>
- <script>
- var pieChartData = [
- {
- value: 50,
- color: "lightblue"
- },
- {
- value: 10,
- color: "red"
- },
- {
- value: 40,
- color: "green"
- }
- ]
- var myLine = new Chart(document.getElementById("pieChartLoc").getContext("2d")).Pie(pieChartData);
- </script>
- </body>
- </html>
Example 2: Creating the Doughnut Chart
A Doughnut chart looks much similar to a pie chart except they have the center cut out. Everything is similar to the above process except we need to call the doughnut method this time.
- <!Doctype HTML>
- <html>
- <head>
- <title>Doughnut Chart</title>
- <script src="js/Chart.min.js"></script>
- </head>
- <body>
- <canvas id="doughNutChartLoc" height="300" width="300"></canvas>
- <script>
- var DoughNutChartData = [
- {
- value: 50,
- color: "lightblue"
- },
- {
- value: 10,
- color: "red"
- },
- {
- value: 40,
- color: "green"
- }
- ]
- var myDoughnut = new Chart(document.getElementById("doughNutChartLoc").getContext("2d")).Doughnut(DoughNutChartData);
- </script>
- </body>
- </html>
Preview

Example 3: Creating the Bar Chart
For creating the Bar Chart, we need two labels (displayed on the x-axis) and datasets (to contain information about fillColor, strokeColor and data that is basically the value on the y-axis).

- <!Doctype HTML>
- <html>
- <head>
- <title>Bar Chart</title>
- <script src="js/Chart.min.js"></script>
- </head>
- <body>
- <canvas id="barChartLoc" height="300" width="300"></canvas>
- <script>
- var barChartLocData = {
- labels: ["January", "Feburary", "March"],
- datasets: [{ fillColor: "lightblue", strokeColor: "blue", data: [15, 20, 35] }]
- };
- var mybarChartLoc = new Chart(document.getElementById("barChartLoc").getContext("2d")).Bar(barChartLocData);
- </script>
- </body>
- </html>
I hope you like it. Thanks.

Bharath Radhekrishna ChennuPosted Feb 25, 2016, 2:18 AM
Followed same but unable to see chart. Plz help
Jayendra MistryPosted May 29, 2014, 7:58 AM
thanks but i have doubt that how to attch this chart dynamicaly with sql stored procedure or query & get data using javascript
Gopi ChandPosted May 12, 2014, 9:19 AM
Very good article...
Mahesh ChandPosted May 11, 2014, 10:24 AM
Great Anoop. I can see we using Chart.js on C# Corner My Account.