{"id":7623,"date":"2019-04-03T08:31:33","date_gmt":"2019-04-03T08:31:33","guid":{"rendered":"https:\/\/www.anychart.com\/blog\/?p=7623"},"modified":"2022-08-13T10:35:57","modified_gmt":"2022-08-13T10:35:57","slug":"create-interactive-bar-charts-using-javascript-tutorial","status":"publish","type":"post","link":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/","title":{"rendered":"How to Create Stunning Interactive Bar Charts Using JavaScript \u2014 Tutorial"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg\" alt=\"Tutorial on creating stunning interactive bar charts using JavaScript\" width=\"100%\" \/><em>Web developer Alfrick Opidi shows how to create beautiful interactive <a href=\"https:\/\/www.anychart.com\/chartopedia\/chart-type\/bar-chart\/\">bar charts<\/a> using JavaScript (HTML5), taking the AnyChart JS charting library as an example along the tutorial.<\/em><\/p>\n<p><!--more Read the JS charting tutorial \u00bb--><\/p>\n<p><a href=\"https:\/\/www.anychart.com\/blog\/2018\/11\/20\/data-visualization-definition-history-examples\/\">Data visualization<\/a> is an important concept that is used to assist audiences in understanding complex ideas easily, identifying patterns and trends quickly, and making the most of the presented data. With the current proliferation of data generation, finding ways of extracting, cleaning, and visualizing data to enable easier interpretation is increasingly on demand.<\/p>\n<p>Furthermore, if\u00a0data visualization\u00a0is accompanied with interactive capabilities, the audience can drill down to the finer details of the charts, graphs, maps, and dashboards and retrieve useful analysis and insights.<\/p>\n<p>In the world of JavaScript programming, there are several charting libraries that allow developers to create stunning charts and visualize data without much hassle. Whether you require an open sourced library, paid-for library, or any other type, you can find something to take your data visualization efforts to the next level.<\/p>\n<p>Some of the best JavaScript charting libraries include Google Charts, AnyChart, D3, and Highcharts. Basically, the process of creating interactive charts with any of them tends to be quite similar. So, once you\u2019ve mastered how to use one, you can easily extrapolate the process to the other libraries, especially if you need a unique feature present in any of them.<\/p>\n<p>For this JS chart tutorial, we\u2019ll use examples from\u00a0<a href=\"https:\/\/www.anychart.com\/\">AnyChart<\/a>. It\u2019s a flexible and easy to use HTML5 JavaScript charting library that comes with exhaustive documentation, an extensive variety of supported chart types, code playground to test out things, and lots of other interesting features.<\/p>\n<p>Now, let\u2019s start getting our hands dirty and see how AnyChart can assist in meeting our data visualization needs.<\/p>\n<h2>How to Create Simple JS Bar Chart in 3 Steps<\/h2>\n<p>Here are three simple steps you can use to create a basic chart in JavaScript and integrate it into your website or application.<\/p>\n<ul>\n<li>Prepare the data.<\/li>\n<li>Connect to a JS charts library.<\/li>\n<li>Write some code.<\/li>\n<\/ul>\n<p>Now, let\u2019s talk about each of the steps.<\/p>\n<h3>1. Preparing the Data<\/h3>\n<p>If you have unstructured data, you need to prepare it for easier loading into the charting library. Depending on the library you choose, you\u2019ll need to prepare the data in the format it accepts. In the case of AnyChart, it supports a wide range of data formats, including Google Spreadsheet, XML, CSV, HTML, and JSON.<\/p>\n<p>When creating most charts, you just need to provide the values for the X and Y axes. Creating bar charts is even simpler because you only need to provide values for the Y-axis, and an index number or an item number will be taken to represent the X-axis values.<\/p>\n<p>For example, if we prepare the data in an array format, X will be the item numbers and Y will be the data values.<\/p>\n<pre><code class=\"javascript\">var data = [120, 60, 30, 80, 50];\r\n   \/\/ X is item numbers [0,1,2,3,4,5]\r\n   \/\/ Y is the data values<\/code><\/pre>\n<p>Also, if the data is available in JSON format, it could look like the following<\/p>\n<pre><code class=\"javascript\">[{\r\n        'x': Item One',\r\n        'value': 120\r\n    },{\r\n        'x': 'Item Two',\r\n        'value': 60\r\n    }...]<\/code><\/pre>\n<h3>2. Connecting to a Charting Library<\/h3>\n<p>To connect to your preferred JavaScript charting library, you can choose to download the relevant package and store it locally or use a CDN service. A CDN is usually recommended because it allows you to load the library\u2019s files from the server closest to your audience, which provides quick page load speeds and enhanced performance.<\/p>\n<p>AnyChart uses a modular-based system that enables you to connect only the chart types and features you need in your project, considerably shrinking the size of the code running on our application.<\/p>\n<p>For example, if we want to create a bar chart using AnyChart JS, we\u2019ll need to add the following Core and basic Cartesian modules:<\/p>\n<pre><code class=\"html\">&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-cartesian.min.js\"&gt;&lt;\/script&gt;<\/code><\/pre>\n<h3>3. Writing Some Code<\/h3>\n<p>Now, here are the steps to follow to write some code for creating a basic JS bar chart.<\/p>\n<p>a. We\u2019ll begin by creating a container on the HTML page that will reference the bar chart.<\/p>\n<pre><code class=\"html\">&lt;div id=\"container\"&gt;&lt;\/div&gt;<\/code><\/pre>\n<p>b. Then, let\u2019s include the data we prepared (note that the data is comprised of arbitrary values of the web traffic that some websites receive per minute).<\/p>\n<pre><code class=\"javascript\">var data = [\r\n        {x: 'Amazon', y: 120},\r\n        {x: 'DZone', y: 60},\r\n        {x: 'Gizmodo', y: 30},\r\n        {x: 'StackOverFlow', y: 80},\r\n        {x: 'CNET', y: 50}\r\n    ];<\/code><\/pre>\n<p>c. Next, let\u2019s define the type of chart we want to create by calling the applicable chart constructor function.<\/p>\n<pre><code class=\"javascript\">var chart = anychart.bar();<\/code><\/pre>\n<p>d. Let\u2019s set the title for the chart and the axes.<\/p>\n<pre><code class=\"javascript\">chart.title('Website Traffic Stats');\r\nchart.xAxis().title(\"Website\");\r\nchart.yAxis().title(\"Traffic Per Minute\");<\/code><\/pre>\n<p>e. Let\u2019s create a bar series and pass the data.<\/p>\n<pre><code class=\"javascript\">var series = chart.bar(data);<\/code><\/pre>\n<p>f. Let\u2019s point the chart to the\u00a0<code>container<\/code> id we created.<\/p>\n<pre><code class=\"javascript\">chart.container(\"container\");<\/code><\/pre>\n<p>g. Let\u2019s initiate drawing the bar chart.<\/p>\n<pre><code class=\"javascript\">chart.draw();<\/code><\/pre>\n<p>Here\u2019s how the chart looks on a browser:<br \/>\n<img decoding=\"async\" class=\"fr-fin fr-dib\" src=\"https:\/\/lh4.googleusercontent.com\/Cqx_05BAq_KsiQOUrbgiQvWoNk-vFlU4PfcLg-ZfYn2g4_TrLGuyfWYcB0ZCB8wsN61RdTw3E9Vm2wwsWfLthoN40gsigbXVimFMu5PCygIvAfBOnDpnVbtcP62urf6OOWelJX36uJPXqOPIpw\" width=\"100%\" \/><\/p>\n<p>Here is the entire code for creating a basic bar chart using the AnyChart JavaScript library:<\/p>\n<pre><code>&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-cartesian.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div id=\"container\"&gt;\r\n&lt;script&gt;\r\nanychart.onDocumentLoad(function() {\r\n\/\/ create the data\r\nvar data = [\r\n        {x: 'Amazon', y: 120},\r\n        {x: 'DZone', y: 60},\r\n        {x: 'Gizmodo', y: 30},\r\n        {x: 'StackOverFlow', y: 80},\r\n        {x: 'CNET', y: 50}\r\n    ];\r\nvar chart = anychart.bar(); \/\/ create a chart\r\nchart.title('Website Traffic Stats'); \/\/ create title for the chart\r\nchart.xAxis().title(\"Website\"); \/\/ create name for X axis\r\nchart.yAxis().title(\"Traffic Per Minute\"); \/\/ create name for Y axis\r\nvar series = chart.bar(data); \/\/ create bar series and pass data\r\nchart.container(\"container\"); \/\/ reference the container Id\r\nchart.draw(); \/\/ initiate drawing the bar chart\r\n  });\r\n\r\n&lt;\/script&gt;\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<p>That\u2019s it!<\/p>\n<p>As you can see, using the AnyChart library for creating beautiful charts is easy and straightforward.<\/p>\n<p>Now, let\u2019s talk about how to enhance the look and feel of the charts using some more advanced steps.<\/p>\n<h2>How to Create Advanced JavaScript Bar Charts<\/h2>\n<p>Since we already know how to create a bar chart using AnyChart, let&#8217;s see what else is possible in case we have a bit more sophisticated data visualization tasks to perform. You\u2019ll see that JavaScript charting is not a rocket science for web developers.<\/p>\n<p>For example, here are some advanced out-of-the-box capabilities you can make use of in the same library.<\/p>\n<h3>1. Creating Multi-Series Bar Charts<\/h3>\n<p>Apart from the single-series bar chart we built before, you can use AnyChart JS to create a multi-series bar chart that showcases multiple sets of data on the chart\u2019s plot. In AnyChart, a series refers to a single data set that can be displayed on a chart\u2019s plot. With a multi-series chart, you can visualize detailed information and assist your audience in deriving more insights from the data.<\/p>\n<p>For example, let\u2019s say the data is present in an HTML table (check below for the table\u2019s code).<\/p>\n<p>Here\u2019s how the table looks on a browser:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"fr-fin fr-dib\" title=\"Data visualization\" src=\"https:\/\/lh6.googleusercontent.com\/aI1wqBCb5zmbn4alsj2trYg7Wxt8JgSn7E-XKOxy28-Vd_YsRLX3qFi5tGmcRC6JKD953DwOqNg7QiTgy_YyiGBMqRl8A3ScLBYDb9R7iVEvgQMp0fsvnq3gJZWWpDZpOGL5QgIJ_wbYiYy2CA\" alt=\"Data visualization\" width=\"366\" height=\"175\" \/><\/p>\n<p>To use the AnyChart JS library to build a multi-series bar chart out of the data, we\u2019ll first need to include the Data Adapter module that assists in loading the HTML data into our work environment.<\/p>\n<pre><code class=\"html\">&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-data-adapter.min.js\"&gt;&lt;\/script&gt;<\/code><\/pre>\n<p>Next, we\u2019ll need to parse the data from the HTML table.<\/p>\n<pre><code class=\"javascript\">var dataTable = anychart.data.parseHtmlTable('#dataTable');<\/code><\/pre>\n<p>Then, we\u2019ll create a chart and also specify the source of the data.<\/p>\n<pre><code class=\"javascript\">var chart = anychart.bar();\r\nchart.data(dataTable);<\/code><\/pre>\n<p>We can also add a legend to the bar chart and enable readers to easily read and understand the meaning of the various values.<\/p>\n<pre><code class=\"javascript\">var legend = chart.legend(); \r\nlegend.enabled(true); \/\/ enable legend<\/code><\/pre>\n<p>If we run the code, we\u2019ll get a multi-series bar chart that displays the composition of every value, allowing for easy comparison and understanding of the data.<\/p>\n<p><img decoding=\"async\" class=\"fr-fin fr-dib\" src=\"https:\/\/lh3.googleusercontent.com\/vt0mPbIwOJpqm9kFVBOLmSMn_sw3BZCk5Fsdz4cAQtD3lUIRAeARsb8m5AAGBSANYxY3F7bTsBVbnaElij_cf4yjRqJpOduUd0j1IQziU_VypazNWgXC7TpxYbGfaDKJNBN4UTp9Ej12eDxPRw\" width=\"100%\" \/><\/p>\n<p>Here is the entire code for creating a JavaScript-based multi-series bar chart:<\/p>\n<pre><code>&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-cartesian.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-data-adapter.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div id=\"container\"&gt;\r\n&lt;table id=\"dataTable\" border=\"1\"&gt;\r\n      &lt;caption&gt;Website Traffic&lt;\/caption&gt;\r\n      &lt;thead&gt;\r\n        &lt;tr&gt;\r\n          &lt;th&gt;Websites \/ Traffic Source&lt;\/th&gt;\r\n          &lt;th&gt;Organic&lt;\/th&gt;\r\n          &lt;th&gt;Direct&lt;\/th&gt;\r\n          &lt;th&gt;Referral&lt;\/th&gt;\r\n        &lt;\/tr&gt;\r\n      &lt;\/thead&gt;\r\n      &lt;tbody&gt;\r\n        &lt;tr&gt;\r\n          &lt;td&gt;Amazon&lt;\/td&gt;\r\n          &lt;td&gt;60&lt;\/td&gt;\r\n          &lt;td&gt;50&lt;\/td&gt;\r\n          &lt;td&gt;10&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n          &lt;td&gt;Dzone&lt;\/td&gt;\r\n          &lt;td&gt;30&lt;\/td&gt;\r\n          &lt;td&gt;20&lt;\/td&gt;\r\n          &lt;td&gt;10&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n          &lt;td&gt;Gizmodo&lt;\/td&gt;\r\n          &lt;td&gt;20&lt;\/td&gt;\r\n          &lt;td&gt;5&lt;\/td&gt;\r\n          &lt;td&gt;5&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n          &lt;td&gt;StackOverFlow&lt;\/td&gt;\r\n          &lt;td&gt;40&lt;\/td&gt;\r\n          &lt;td&gt;30&lt;\/td&gt;\r\n          &lt;td&gt;10&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n          &lt;td&gt;CNET&lt;\/td&gt;\r\n          &lt;td&gt;25&lt;\/td&gt;\r\n          &lt;td&gt;5&lt;\/td&gt;\r\n          &lt;td&gt;20&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n      &lt;\/tbody&gt;\r\n&lt;\/table&gt;\r\n&lt;\/div&gt;\r\n&lt;script&gt;\r\nanychart.onDocumentLoad(function() {\r\nvar dataTable = anychart.data.parseHtmlTable('#dataTable'); \/\/ parse data from HTML table\r\nvar chart = anychart.bar(); \/\/ create chart\r\n\r\nchart.xAxis().title(\"Website\"); \/\/ create name for X axis\r\nchart.yAxis().title(\"Traffic Per Minute\"); \/\/ create name for Y axis\r\nchart.data(dataTable); \/\/ specify data source\r\n\r\nvar legend = chart.legend(); \r\n\r\nlegend.enabled(true); \/\/ enable legend\r\nchart.container(\"container\"); \/\/ reference the container Id\r\nchart.draw(); \/\/ initiate drawing the bar chart\r\n});\r\n\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>2. Creating Stacked Bar Charts<\/h3>\n<p>You can create a\u00a0<a href=\"https:\/\/docs.anychart.com\/Basic_Charts\/Stacked\/Value\/Bar_Chart\" target=\"_blank\" rel=\"nofollow\">stacked bar chart<\/a>\u00a0on the fly without making extensive changes to the multi-series bar chart. AnyChart has two ways of stacking charts: value stacking and percent stacking. You can choose one of these values by setting the scale of the\u00a0<code>stackMode<\/code> method to \u201cvalue\u201d and \u201cpercent\u201d respectively.<\/p>\n<p>Here\u2019s how to set the\u00a0<code>stackMode<\/code> for a value-based JS stacked bar chart:<\/p>\n<pre><code class=\"javascript\">chart.yScale().stackMode('value');<\/code><\/pre>\n<p>Here\u2019s the result:<\/p>\n<p><img decoding=\"async\" class=\"fr-fin fr-dib\" title=\"data visualization\" src=\"https:\/\/lh3.googleusercontent.com\/zzlWzKwdFAnnJ5aifaauV5KxLdf9BRVWq-HI5-TsCYqisQgPZzIngcapuXxmG51odZ1ZdhlRngWrWqZBjkbbxJ08LL-XSVzvIpvAdsZznOqaW6x_SduDjvQy72n4xYuvMtPXm5rvRdHAWs5HFA\" alt=\"data visualization\" width=\"100%\" \/><\/p>\n<p>Here\u2019s how to set the mode for a 100% stacked bar chart:<\/p>\n<pre><code class=\"javascript\">chart.yScale().stackMode('percent');<\/code><\/pre>\n<p>Here\u2019s the result:<\/p>\n<p><img decoding=\"async\" class=\"fr-fin fr-dib\" title=\"data visualization\" src=\"https:\/\/lh4.googleusercontent.com\/gLEmX_bG-9rRBHqAVZ8bYaf8xtA7HKVH3_HOfcI52ACWkIl1_NtKWC-kbUJFNI3NzaKkdTxJePG78zwZ_bOwvyr5TILK8Q6jTsgv04LwtAnSgGXMdYoPdqzgZjl7naHN3mPoAzMWIivrAJ-yPA\" alt=\"data visualization\" width=\"100%\" \/><\/p>\n<h3>3. Adding Interactivity to Charts<\/h3>\n<p>By default, JS charts created using the AnyChart library are interactive. Some of the default chart behaviors include highlighting series and points when hovered over, showing tooltips of hovered points, highlighting a series when its respective legend is hovered over, showing or hiding a series when its respective legend item is clicked on, etc.<\/p>\n<p>Furthermore, you can modify the charts\u2019 default interactivity to suit your needs and preferences. For example, we can configure the tooltip in the 100% stacked bar chart we\u2019ve just built to give more information every time a user hovers to a point on the bar chart.<\/p>\n<pre><code class=\"javascript\">chart.tooltip().title(\"Traffic Data\");\r\nchart.tooltip().format(\"Website: {%categoryName} \\n{%seriesName}: {%value}\");<\/code><\/pre>\n<p>Here\u2019s the result:<\/p>\n<p><img decoding=\"async\" class=\"fr-fin fr-dib\" title=\"data visualization\" src=\"https:\/\/lh6.googleusercontent.com\/gSkCB6GltwzNcccweu_2yEWiYX7BLCdoo153uOhbCwHhT_rw0ZaDKKtIg6cFU-SWJceZdLSzuhRbSb1U0-GKCTjUZRT45rlcJRX1wyczS1CJWPvI0Y-84MUlkiR7FX0Nr9yzmK_AZDsOjIGGWQ\" alt=\"data visualization\" width=\"100%\" \/><\/p>\n<p>Here\u2019s the entire code for creating this JS chart with added interactivity:<\/p>\n<pre><code>&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-cartesian.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/v8\/js\/anychart-data-adapter.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div id=\"container\"&gt;\r\n&lt;table id=\"dataTable\" border=\"1\"&gt;\r\n      &lt;caption&gt;Website Traffic&lt;\/caption&gt;\r\n      &lt;thead&gt;\r\n        &lt;tr&gt;\r\n          &lt;th&gt;Websites \/ Traffic Source&lt;\/th&gt;\r\n          &lt;th&gt;Organic&lt;\/th&gt;\r\n          &lt;th&gt;Direct&lt;\/th&gt;\r\n          &lt;th&gt;Referral&lt;\/th&gt;\r\n        &lt;\/tr&gt;\r\n      &lt;\/thead&gt;\r\n      &lt;tbody&gt;\r\n        &lt;tr&gt;\r\n          &lt;td&gt;Amazon&lt;\/td&gt;\r\n          &lt;td&gt;60&lt;\/td&gt;\r\n          &lt;td&gt;50&lt;\/td&gt;\r\n          &lt;td&gt;10&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n          &lt;td&gt;Dzone&lt;\/td&gt;\r\n          &lt;td&gt;30&lt;\/td&gt;\r\n          &lt;td&gt;20&lt;\/td&gt;\r\n          &lt;td&gt;10&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n          &lt;td&gt;Gizmodo&lt;\/td&gt;\r\n          &lt;td&gt;&gt;20&lt;\/td&gt;\r\n          &lt;td&gt;5&lt;\/td&gt;\r\n          &lt;td&gt;5&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n          &lt;td&gt;StackOverFlow&lt;\/td&gt;\r\n          &lt;td&gt;40&lt;\/td&gt;\r\n          &lt;td&gt;30&lt;\/td&gt;\r\n          &lt;td&gt;10&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n          &lt;td&gt;CNET&lt;\/td&gt;\r\n          &lt;td&gt;25&lt;\/td&gt;\r\n          &lt;td&gt;5&lt;\/td&gt;\r\n          &lt;td&gt;20&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n      &lt;\/tbody&gt;\r\n    &lt;\/table&gt;\r\n&lt;script&gt;\r\nanychart.onDocumentLoad(function() {\r\nvar dataTable = anychart.data.parseHtmlTable('#dataTable'); \/\/ parse data from HTML table\r\nvar chart = anychart.bar(); \/\/ create chart\r\n\r\nchart.xAxis().title(\"Website\"); \/\/ create name for X axis\r\nchart.yAxis().title(\"Traffic Per Minute\"); \/\/ create name for Y axis\r\nchart.data(dataTable); \/\/ specify data source\r\nchart.yScale().stackMode('percent'); \/\/ setting percent stacking\r\n\r\nvar legend = chart.legend(); \r\n\r\nlegend.enabled(true); \/\/ enable legend\r\nchart.tooltip().title(\"Traffic Data\"); \/\/ configuring the tooltip\r\nchart.tooltip().format(\"Website: {%categoryName} \\n{%seriesName}: {%value}\");\r\nchart.container(\"container\"); \/\/ reference the container Id\r\nchart.draw(); \/\/ initiate drawing the bar chart\r\n});\r\n\r\n&lt;\/script&gt;\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h2>Wrapping Up<\/h2>\n<p>As you can see, creating versatile and interactive JavaScript (HTML5) charts is easy with a good JS library. In this tutorial, we\u2019ve just scratched the surface of what you can accomplish.<\/p>\n<p>You can go to\u00a0<a href=\"https:\/\/docs.anychart.com\/Quick_Start\/Quick_Start\" target=\"_blank\" rel=\"nofollow\">AnyChart\u2019s documentation<\/a>\u00a0and learn how to create other amazing charts and elevate your data visualization expertise.<\/p>\n<p>You can use the skills learned in this tutorial to try the\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Comparison_of_JavaScript_charting_libraries\" target=\"_blank\" rel=\"nofollow\">other JavaScript charting libraries<\/a>\u00a0and gauge their suitability to visualize data and enhance the engagement of your website or application.<\/p>\n<p>Do you have any questions or comments? Please post them below.<\/p>\n<p>All the best.<\/p>\n<hr \/>\n<p><em>The &#8220;How to Create Stunning Interactive Bar Charts Using JavaScript \u2014 Tutorial&#8221;\u00a0article is published on the AnyChart blog with permission of Alfrick Opidi.<\/p>\n<p>Originally published on <a href=\"https:\/\/dzone.com\/articles\/how-to-create-stunning-interactive-bar-charts-usin\" target=\"_blank\" rel=\"nofollow\">DZone, Big Data Zone,<\/a> under the title &#8220;How to Create Stunning, Interactive Bar Charts Using JavaScript&#8221; on March 22, 2019.<\/em><\/p>\n<p><!-- SyntaxHighlighter --><link rel=\"stylesheet\" href=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/styles\/default.min.css\"><link rel=\"stylesheet\" href=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/styles\/atom-one-light.min.css\"><script src=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/highlight.min.js\"><\/script><script>hljs.initHighlightingOnLoad();<\/script><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Web developer Alfrick Opidi shows how to create beautiful interactive bar charts using JavaScript (HTML5), taking the AnyChart JS charting library as an example along the tutorial.<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,22,23,13,279,4],"tags":[53,70,71,265,35,258,44,54,256,803,805,32,55,144,36,141,81,57,58,65,56,30,172,804],"class_list":["post-7623","post","type-post","status-publish","format-standard","hentry","category-anychart-charting-component","category-charts-and-art","category-html5","category-javascript","category-javascript-chart-tutorials","category-tips-and-tricks","tag-anychart","tag-bar-chart","tag-bar-charts","tag-big-data","tag-business-intelligence","tag-charts","tag-charts-and-art","tag-data-visualization","tag-data-visualization-examples","tag-dzone","tag-front-end-development","tag-html5","tag-html5-charts","tag-infographics","tag-javascript","tag-javascript-charting","tag-javascript-charting-library","tag-javascript-charts","tag-js-chart","tag-js-charting","tag-js-charts","tag-tips-and-tricks","tag-tutorial","tag-web-development","wpautop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Create Stunning Interactive Bar Charts Using JavaScript \u2014 Tutorial<\/title>\n<meta name=\"description\" content=\"Web developer Alfrick Opidi explains how to create stunning interactive bar charts using JavaScript HTML5, easily embedded in any web and mobile project.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create Stunning Interactive Bar Charts Using JS \u2014 Tutorial\" \/>\n<meta property=\"og:description\" content=\"Web developer Alfrick Opidi explains how to create stunning interactive bar charts using JavaScript HTML5, easily embedded in any web and mobile project.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"AnyChart News\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/AnyCharts\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-03T08:31:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-13T10:35:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg\" \/>\n<meta name=\"author\" content=\"Alfrick Opidi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"How to Create Stunning Interactive Bar Charts Using JS \u2014 Tutorial\" \/>\n<meta name=\"twitter:description\" content=\"Web developer Alfrick Opidi explains how to create stunning interactive bar charts using JavaScript HTML5, easily embedded in any web and mobile project.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg\" \/>\n<meta name=\"twitter:creator\" content=\"@AnyChart\" \/>\n<meta name=\"twitter:site\" content=\"@AnyChart\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alfrick Opidi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/\"},\"author\":{\"name\":\"Alfrick Opidi\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/b04f8646463ba21baf9c48d4e46809f3\"},\"headline\":\"How to Create Stunning Interactive Bar Charts Using JavaScript \u2014 Tutorial\",\"datePublished\":\"2019-04-03T08:31:33+00:00\",\"dateModified\":\"2022-08-13T10:35:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/\"},\"wordCount\":1464,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg\",\"keywords\":[\"AnyChart\",\"bar chart\",\"bar charts\",\"big data\",\"Business Intelligence\",\"charts\",\"Charts and Art\",\"Data Visualization\",\"data visualization examples\",\"DZone\",\"front-end development\",\"HTML5\",\"html5 charts\",\"infographics\",\"JavaScript\",\"javascript charting\",\"JavaScript charting library\",\"javascript charts\",\"js chart\",\"js charting\",\"js charts\",\"Tips and tricks\",\"tutorial\",\"web development\"],\"articleSection\":[\"AnyChart Charting Component\",\"Charts and Art\",\"HTML5\",\"JavaScript\",\"JavaScript Chart Tutorials\",\"Tips and Tricks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/\",\"url\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/\",\"name\":\"How to Create Stunning Interactive Bar Charts Using JavaScript \u2014 Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg\",\"datePublished\":\"2019-04-03T08:31:33+00:00\",\"dateModified\":\"2022-08-13T10:35:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/b04f8646463ba21baf9c48d4e46809f3\"},\"description\":\"Web developer Alfrick Opidi explains how to create stunning interactive bar charts using JavaScript HTML5, easily embedded in any web and mobile project.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#primaryimage\",\"url\":\"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg\",\"contentUrl\":\"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.anychart.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create Stunning Interactive Bar Charts Using JavaScript \u2014 Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\",\"url\":\"https:\/\/www.anychart.com\/blog\/\",\"name\":\"AnyChart News\",\"description\":\"AnyChart JS Charts\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.anychart.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/b04f8646463ba21baf9c48d4e46809f3\",\"name\":\"Alfrick Opidi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c544d1b91e74e29c40ec7e45a0d2281753ea00fbc1c76efb32e865a081574823?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c544d1b91e74e29c40ec7e45a0d2281753ea00fbc1c76efb32e865a081574823?s=96&r=g\",\"caption\":\"Alfrick Opidi\"},\"url\":\"https:\/\/www.anychart.com\/blog\/author\/alfrick_opidi\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create Stunning Interactive Bar Charts Using JavaScript \u2014 Tutorial","description":"Web developer Alfrick Opidi explains how to create stunning interactive bar charts using JavaScript HTML5, easily embedded in any web and mobile project.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"How to Create Stunning Interactive Bar Charts Using JS \u2014 Tutorial","og_description":"Web developer Alfrick Opidi explains how to create stunning interactive bar charts using JavaScript HTML5, easily embedded in any web and mobile project.","og_url":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/","og_site_name":"AnyChart News","article_publisher":"https:\/\/www.facebook.com\/AnyCharts","article_published_time":"2019-04-03T08:31:33+00:00","article_modified_time":"2022-08-13T10:35:57+00:00","og_image":[{"url":"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg","type":"","width":"","height":""}],"author":"Alfrick Opidi","twitter_card":"summary_large_image","twitter_title":"How to Create Stunning Interactive Bar Charts Using JS \u2014 Tutorial","twitter_description":"Web developer Alfrick Opidi explains how to create stunning interactive bar charts using JavaScript HTML5, easily embedded in any web and mobile project.","twitter_image":"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg","twitter_creator":"@AnyChart","twitter_site":"@AnyChart","twitter_misc":{"Written by":"Alfrick Opidi","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/"},"author":{"name":"Alfrick Opidi","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/b04f8646463ba21baf9c48d4e46809f3"},"headline":"How to Create Stunning Interactive Bar Charts Using JavaScript \u2014 Tutorial","datePublished":"2019-04-03T08:31:33+00:00","dateModified":"2022-08-13T10:35:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/"},"wordCount":1464,"commentCount":0,"image":{"@id":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg","keywords":["AnyChart","bar chart","bar charts","big data","Business Intelligence","charts","Charts and Art","Data Visualization","data visualization examples","DZone","front-end development","HTML5","html5 charts","infographics","JavaScript","javascript charting","JavaScript charting library","javascript charts","js chart","js charting","js charts","Tips and tricks","tutorial","web development"],"articleSection":["AnyChart Charting Component","Charts and Art","HTML5","JavaScript","JavaScript Chart Tutorials","Tips and Tricks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/","url":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/","name":"How to Create Stunning Interactive Bar Charts Using JavaScript \u2014 Tutorial","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg","datePublished":"2019-04-03T08:31:33+00:00","dateModified":"2022-08-13T10:35:57+00:00","author":{"@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/b04f8646463ba21baf9c48d4e46809f3"},"description":"Web developer Alfrick Opidi explains how to create stunning interactive bar charts using JavaScript HTML5, easily embedded in any web and mobile project.","breadcrumb":{"@id":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#primaryimage","url":"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg","contentUrl":"https:\/\/dz2cdn3.dzone.com\/storage\/article-thumb\/11500294-thumb.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.anychart.com\/blog\/2019\/04\/03\/create-interactive-bar-charts-using-javascript-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.anychart.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Create Stunning Interactive Bar Charts Using JavaScript \u2014 Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.anychart.com\/blog\/#website","url":"https:\/\/www.anychart.com\/blog\/","name":"AnyChart News","description":"AnyChart JS Charts","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.anychart.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/b04f8646463ba21baf9c48d4e46809f3","name":"Alfrick Opidi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c544d1b91e74e29c40ec7e45a0d2281753ea00fbc1c76efb32e865a081574823?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c544d1b91e74e29c40ec7e45a0d2281753ea00fbc1c76efb32e865a081574823?s=96&r=g","caption":"Alfrick Opidi"},"url":"https:\/\/www.anychart.com\/blog\/author\/alfrick_opidi\/"}]}},"_links":{"self":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/7623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/comments?post=7623"}],"version-history":[{"count":31,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/7623\/revisions"}],"predecessor-version":[{"id":15487,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/7623\/revisions\/15487"}],"wp:attachment":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/media?parent=7623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/categories?post=7623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/tags?post=7623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}