{"id":131335,"date":"2023-01-20T07:16:03","date_gmt":"2023-01-20T07:16:03","guid":{"rendered":"https:\/\/phppot.com\/?p=20257"},"modified":"2023-01-20T07:16:03","modified_gmt":"2023-01-20T07:16:03","slug":"chart-js-stacked-bar-example","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/01\/20\/chart-js-stacked-bar-example\/","title":{"rendered":"Chart JS Stacked Bar Example"},"content":{"rendered":"<div class=\"modified-on\" readability=\"7.1304347826087\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on January 20th, 2023.<\/div>\n<p>The ChartJS JavaScript library supports various types of charts to display on a webpage.<\/p>\n<p>This quick example shows how to display a basic stacked bar chart using this library. The library initiation will be simple for all types of charts as we did in the <a href=\"https:\/\/phppot.com\/javascript\/chartjs-line-chart\/\">ChartJS line chart code<\/a>.<\/p>\n<h2>Quick example<\/h2>\n<div class=\"post-section-highlight\" readability=\"74\">\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 Stacked 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 Stacked Bar Chart Example&lt;\/h1&gt; &lt;canvas id=\"stacker-bar-chart\"&gt;&lt;\/canvas&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; var ctx = document.getElementById(\"stacker-bar-chart\").getContext('2d'); var myChart = new Chart(ctx, { type: 'bar', data: { labels: [\"&lt; 100\", \"100 - 200\", \"300 - 400\", \"500 - 800\", \"900 - 1000\"], datasets: [{ label: 'Lion', backgroundColor: \"#51EAEA\", data: [90, 120, 305, 526, 958], }, { label: 'Tiger', backgroundColor: \"#FCDDB0\", data: [82, 145, 335, 516, 936], }, { label: 'Elephant', backgroundColor: \"#FF9D76\", data: [62, 159, 375, 756, 951], }], }, options: { tooltips: { displayColors: true, callbacks: { mode: 'x', }, }, scales: { x: { stacked: true, }, y: { stacked: true } }, responsive: true } }); &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<p>The graph output displays the stack on a vertical axis. It compares the animals\u2019 counts at each point taken for the graph.<\/p>\n<p><a href=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/quick-example.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-20276\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/quick-example-550x283.jpg\" alt width=\"550\" height=\"283\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/quick-example-550x283.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/quick-example-300x154.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/quick-example-768x395.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/quick-example.jpg 1154w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/a><\/p>\n<p>The stacked bar chart plots a group of bars on top of one another. We have already seen grouped bar charts that combine bars side by side.<\/p>\n<p>The grouped and stacked bar charts are used to show a comparison of data.<\/p>\n<h2>ChartJS Horizontal stacked graph example<\/h2>\n<p>This example is to display the stacked chart horizontally. The ChartJS initiation of this example differs from the above code only by the index axis.<\/p>\n<p>This example specifies <em>indexAxis: \u2018x\u2019<\/em> to show a horizontal stacked bar chart. By specifying this the data points are taking the x-axis about which the bars are stacked on top of one another.<\/p>\n<p class=\"code-heading\">horizontal-stacked-chart.html<\/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 Stacked 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 Stacked Bar Chart Example&lt;\/h1&gt; &lt;canvas id=\"horizontal-stacker-bar-chart\"&gt;&lt;\/canvas&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; var ctx = document.getElementById(\"horizontal-stacker-bar-chart\").getContext('2d'); var myChart = new Chart(ctx, { type: 'bar', data: { labels: [\"&lt; 100\", \"100 - 200\", \"300 - 400\", \"500 - 800\", \"900 - 1000\"], datasets: [{ label: 'Jaguar', backgroundColor: \"#FB3569\", data: [90, 120, 305, 526, 958], }, { label: 'Horse', backgroundColor: \"#FCDDB0\", data: [82, 145, 335, 516, 936], }, { label: 'Leopard', backgroundColor: \"#82CD47\", data: [62, 159, 375, 756, 951], }], }, options: { tooltips: { displayColors: true, callbacks: { mode: 'x', }, }, scales: { x: { stacked: true, }, y: { stacked: true } }, indexAxis: 'y', responsive: true } }); &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p><a href=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-stacked-bar.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-20277\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-stacked-bar-550x284.jpg\" alt width=\"550\" height=\"284\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-stacked-bar-550x284.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-stacked-bar-300x155.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-stacked-bar-768x396.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2023\/01\/chartjs-horizontal-stacked-bar.jpg 1160w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/a><\/p>\n<h2>About the ChartJS target and parameters to render the stacked bar chart<\/h2>\n<p>Let us see some of the important parameters required to create the stacked bar chart for the browser.<\/p>\n<p>The above two examples include the ChartJS library file using the CDN URL. It is preferable compared to having a local copy of this library.<\/p>\n<p>This JS script targets a HTML canvas element to render the stacked bar chart.<\/p>\n<p>It gets the canvas element context and uses it during the ChartJS initiation.<\/p>\n<h3>What are the parameters for rendering the stacked bar using ChartJS?<\/h3>\n<ul>\n<li><em>type<\/em> \u2013 It requires specifying \u2018bar\u2019 as we did for <a href=\"https:\/\/phppot.com\/javascript\/chart-js-bar-chart-example\/\">displaying a ChartJS bar chart<\/a>.<\/li>\n<li><em>data<\/em> \u2013 It prepares the array of&nbsp;<em>labels<\/em> and&nbsp;<em>datasets<\/em>.\n<ul>\n<li><em>labels<\/em> parameter has the data points.<\/li>\n<li><em>datasets<\/em> have the readings with appropriate captions and colors.<\/li>\n<\/ul>\n<\/li>\n<li><em>options<\/em> \u2013 It specifies the chart properties about the direction or axis. For example,\n<ul>\n<li><em>tooltips<\/em><\/li>\n<li><em>scales<\/em><\/li>\n<li><em>indexAxis<\/em><\/li>\n<li><em>responsive<\/em><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>This example displays a responsive chart by setting the option&nbsp;<em>responsive<\/em> as&nbsp;<em>true<\/em>. We have also used <a href=\"https:\/\/phppot.com\/php\/draw-responsive-charts-pie-bar-column-using-google-charts\/\">Google charts to create responsive bar\/column\/pie charts<\/a>.<br \/><a class=\"download\" href=\"https:\/\/phppot.com\/downloads\/javascript\/chartjs-stacked-bar.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-stacked-bar-example\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on January 20th, 2023. The ChartJS JavaScript library supports various types of charts to display on a webpage. This quick example shows how to display a basic stacked bar chart using this library. The library initiation will be simple for all types of charts as we did in the ChartJS line [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":131336,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-131335","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\/131335","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=131335"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131335\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/131336"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=131335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=131335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=131335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}