{"id":131142,"date":"2023-01-05T12:07:16","date_gmt":"2023-01-05T12:07:16","guid":{"rendered":"https:\/\/phppot.com\/?p=20215"},"modified":"2023-01-05T12:07:16","modified_gmt":"2023-01-05T12:07:16","slug":"chart-js-bar-chart-example","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/01\/05\/chart-js-bar-chart-example\/","title":{"rendered":"Chart JS Bar Chart Example"},"content":{"rendered":"<div class=\"modified-on\" readability=\"7.1304347826087\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on January 11th, 2023.<\/div>\n<p>In this tutorial, we will see examples of using the Chartjs JavaScript library to create bar charts.<\/p>\n<p>This quick example gives you the code to display a simple bar chart on the browser.<\/p>\n<p>The below JavaScript code shows how to include and initiate the ChartJS library. It uses static data to set the bar chart labels and dataset in JavaScript.<\/p>\n<h2>Quick example \u2013 Simple bar chart example via ChartJS<\/h2>\n<div class=\"post-section-highlight\" readability=\"55\">\n<pre class=\"prettyprint\"><code class=\"language-php-template\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Chart JS Bar Chart Example&lt;\/title&gt;\n&lt;link rel='stylesheet' href='style.css' type='text\/css' \/&gt;\n&lt;\/head&gt;\n&lt;body&gt; &lt;div class=\"phppot-container\"&gt; &lt;h1&gt;Chart JS Bar Chart Example&lt;\/h1&gt; &lt;div&gt; &lt;canvas id=\"bar-chart\"&gt;&lt;\/canvas&gt; &lt;\/div&gt; &lt;\/div&gt; &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/chart.js@4.0.1\/dist\/chart.umd.min.js\"&gt;&lt;\/script&gt; &lt;script&gt; \/\/ Bar chart new Chart(document.getElementById(\"bar-chart\"), { type: 'bar', data: { labels: [\"Elephant\", \"Horse\", \"Tiger\", \"Lion\", \"Jaguar\"], datasets: [ { label: \"Animals Count\", backgroundColor: [\"#51EAEA\", \"#FCDDB0\", \"#FF9D76\", \"#FB3569\", \"#82CD47\"], data: [478, 267, 829, 1732, 1213] } ] }, options: { legend: { display: false }, title: { display: true, text: 'Chart JS Bar Chart Example' } } }); &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p>This example sets the chart parameters to the ChartJS&nbsp;<em>data<\/em> and the&nbsp;<em>options<\/em> array and displays the following bar chat to the browser.<\/p>\n<p>This is a very easy client-side solution to display professional graphical representation in the form of a bar chart.<\/p>\n<p>You can simply use this code in your project by tweaking the data points slightly.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-20228 size-large\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/simple-chartjs-bar-chart-550x265.jpg\" alt width=\"550\" height=\"265\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/simple-chartjs-bar-chart-550x265.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/simple-chartjs-bar-chart-300x144.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/simple-chartjs-bar-chart-768x369.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/simple-chartjs-bar-chart.jpg 1156w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<h2>Example 2: Horizontal bar chart<\/h2>\n<p>This library supports creating both vertical and horizontal bar charts. But, the default is the vertical bar chart.<\/p>\n<p>In this example, it creates a horizontal bar chart using the ChartJS plugin.<\/p>\n<p>For the vertical or horizontal bar charts, the direction depends on the <em>indexAxis<\/em> option.<\/p>\n<p>This example sets <em>indexAxis=\u2019y\u2019<\/em> to display the bars horizontally.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php-template\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Chart JS Horizontal Bar Chart Example&lt;\/title&gt;\n&lt;link rel='stylesheet' href='style.css' type='text\/css' \/&gt;\n&lt;\/head&gt;\n&lt;body&gt; &lt;div class=\"phppot-container\"&gt; &lt;h1&gt;Chart JS Horizontal Bar Chart Example&lt;\/h1&gt; &lt;div&gt; &lt;canvas id=\"horizontal-bar-chart\"&gt;&lt;\/canvas&gt; &lt;\/div&gt; &lt;\/div&gt; &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/chart.js@4.0.1\/dist\/chart.umd.min.js\"&gt;&lt;\/script&gt; &lt;script&gt; new Chart(document.getElementById(\"horizontal-bar-chart\"), { type: 'bar', data: { labels: [\"Elephant\", \"Horse\", \"Tiger\", \"Lion\", \"Jaguar\"], datasets: [{ label: \"Animals count\", backgroundColor: [\"#51EAEA\", \"#FCDDB0\", \"#FF9D76\", \"#FB3569\", \"#82CD47\" ], data: [478, 267, 829, 1732, 1213] }] }, options: { indexAxis: 'y', legend: { display: false }, title: { display: true, text: 'Chart JS Horizontal Bar Chart Example' } } }); &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>This output screenshot plots the bar chart data index on the \u2018y\u2019 axis and has the readings on the \u2018x-axis.<\/p>\n<p><a href=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-bar.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-20231 size-large\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-bar-550x268.jpg\" alt width=\"550\" height=\"268\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-bar-550x268.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-bar-300x146.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-bar-768x375.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-bar.jpg 1140w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/a><\/p>\n<p>Previously, we used this JS library to create a pie chart using this ChartJS library. But, it is not supporting to display of 3D pie charts. See the linked article that uses <a href=\"https:\/\/phppot.com\/javascript\/chartjs-pie-chart\/\">Google charts for creating the 3D pie chart<\/a>.<\/p>\n<h2>Example 3: Grouped bar chart<\/h2>\n<p>Grouped bar charts are useful for showing a comparative reading.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php-template\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Chart JS Grouped Bar Chart Example&lt;\/title&gt;\n&lt;link rel='stylesheet' href='style.css' type='text\/css' \/&gt;\n&lt;\/head&gt;\n&lt;body&gt; &lt;div class=\"phppot-container\"&gt; &lt;h1&gt;Chart JS Grouped Bar Chart Example&lt;\/h1&gt; &lt;div&gt; &lt;canvas id=\"bar-chart\"&gt;&lt;\/canvas&gt; &lt;\/div&gt; &lt;\/div&gt; &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/chart.js@4.0.1\/dist\/chart.umd.min.js\"&gt;&lt;\/script&gt; &lt;script&gt; new Chart(document.getElementById(\"bar-chart\"), { type: 'bar', data: { labels: [\"900\", \"950\", \"999\", \"1050\"], datasets: [{ label: \"Lion\", backgroundColor: \"#FF9D76\", data: [234, 345, 567, 568] }, { label: \"Tiger\", backgroundColor: \"#51EAEA\", data: [233, 566, 234, 766] }] }, options: { legend: { display: false }, title: { display: true, text: 'Chart JS Grouped Bar Chart Example' } } }); &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>It displays the record of two animals\u2019 counts at a particular point. This will help compare the readings of two or more indexes.<\/p>\n<p><a href=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-grouped-bar.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-20226 size-large\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-grouped-bar-550x272.jpg\" alt width=\"550\" height=\"272\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-grouped-bar-550x272.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-grouped-bar-300x148.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-grouped-bar-768x379.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-grouped-bar.jpg 1150w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/a><\/p>\n<h2>ChartJS <em>type<\/em> parameter<\/h2>\n<p>ChartJS is a dependable JS library to create and display graphs on a webpage.<\/p>\n<p>In a previous tutorial, we have seen <a href=\"https:\/\/phppot.com\/javascript\/chartjs-line-chart\/\">how to create a line chart using this ChartJS library<\/a>.<\/p>\n<p>Also, it supports generating more types of charts. The&nbsp;<em>type<\/em> parameter is used to define the type of the chart.<\/p>\n<p>The dataset and the options definition may vary based on the type of chart.<br \/><a class=\"download\" href=\"https:\/\/phppot.com\/downloads\/javascript\/chartjs-bar-chart.zip\">Download<\/a><\/p>\n<p> <!-- #comments --> <\/p>\n<div class=\"related-articles\">\n<h2>Popular Articles<\/h2>\n<\/p><\/div>\n<p> <a href=\"https:\/\/phppot.com\/javascript\/chart-js-bar-chart-example\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on January 11th, 2023. In this tutorial, we will see examples of using the Chartjs JavaScript library to create bar charts. This quick example gives you the code to display a simple bar chart on the browser. The below JavaScript code shows how to include and initiate the ChartJS library. It [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":131143,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-131142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-updates"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=131142"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131142\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/131143"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=131142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=131142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=131142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}