{"id":12275,"date":"2021-03-02T11:24:46","date_gmt":"2021-03-02T11:24:46","guid":{"rendered":"https:\/\/www.anychart.com\/blog\/?p=12275"},"modified":"2022-08-13T11:04:19","modified_gmt":"2022-08-13T11:04:19","slug":"diverging-bar-chart-javascript","status":"publish","type":"post","link":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/","title":{"rendered":"How to Create Diverging Bar Chart with JavaScript"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png\" alt=\"\" width=\"100%\" class=\"alignnone size-full wp-image-12448\" srcset=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png 1920w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript-300x164.png 300w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript-768x420.png 768w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript-1024x560.png 1024w\" sizes=\"(max-width: 1920px) 100vw, 1920px\" \/><strong><em>This article is a step-by-step guide that&#8217;ll show you how to build an interactive JavaScript range chart that visualizes 20 years of the LA Lakers\u2019 performance with Kobe Bryant.<\/em><\/strong><\/p>\n<p>The year 2020 was pretty poignant for obvious reasons. But even before the pandemic, the year started on a sad note with the death of basketball legend <a href=\"https:\/\/en.wikipedia.org\/wiki\/Kobe_Bryant\" target=\"_blank\" rel=\"nofollow\">Kobe Bryant<\/a>. He was a star NBA athlete who had played for 20 years with one and only one team \u2014 the Los Angeles Lakers.<\/p>\n<p>Remembering Kobe one year after that horrible crash, I wondered how the Lakers had performed in his two-decade era. So, I visualized that in an interactive Diverging Bar Chart with the help of pure JavaScript.<\/p>\n<p>Thinking that this project might be helpful for those new to web charting, I also logged the entire process and made a tutorial. Check it out!<\/p>\n<p><!--more Read the JS charting tutorial \u00bb--><\/p>\n<h2>What Is a Diverging Bar Chart?<\/h2>\n<p>First things first, I will give you a brief explanation about what diverging bar charts are and then we&#8217;ll dive into the tutorial.  <\/p>\n<p>A diverging bar chart shows two or more measures that are plotted from a middle baseline, extending either to the right and left (horizontal range bars) or top and bottom (vertical range columns).<\/p>\n<p>The key point of data visualization in such diverging charts is to facilitate the comparison of multiple categories by means of displaying them against a bifurcating midpoint.<\/p>\n<p>In this tutorial, I use the diverging bar chart technique to showcase the wins and losses of the LA Lakers through the 20 years of Kobe Bryant\u2019s career.<\/p>\n<p>Here&#8217;s a sneak peek at the final chart to get you ready for the start of the game! Follow along with me to learn how I create this beautiful range bar chart with JavaScript.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-12276\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/js-diverging-bar-chart.gif\" alt=\"HTML5 JavaScript Diverging Bar Chart data visualization to be created along this JS charting tutorial\" width=\"82%\" \/><\/p>\n<h2>How to Build a JavaScript Diverging Bar Chart in 4 Basic Steps<\/h2>\n<p>There are <a href=\"https:\/\/en.wikipedia.org\/wiki\/Comparison_of_JavaScript_charting_libraries\" target=\"_blank\" rel=\"nofollow\">multiple<\/a> JavaScript libraries out there providing pre-written JS code for commonly needed functions that can make the interactive data visualization process quite quick and straightforward.<\/p>\n<p>I picked one called <a href=\"https:\/\/www.anychart.com\/\">AnyChart<\/a> to create this diverging bar chart. This JS charting library appeared to support (particularly useful in this case) range charts out of the box and was also flexible enough to do what I wanted.<\/p>\n<p>Also, it is quite easy to get started with AnyChart even for beginners because there are many ready-to-use examples and it has intensive <a href=\"https:\/\/docs.anychart.com\/\" target=\"_blank\" rel=\"nofollow\">documentation<\/a>.<\/p>\n<p>Of course, having good HTML and JavaScript skills gives you an edge when visualizing data on the web. But anyway, the best part about making use of good charting libraries is that they make it quite uncomplicated to create interactive charts even without much experience.<\/p>\n<p>The entire process of creating literally any JS chart, including a diverging bar chart like this one, can be broken down into four fundamental steps:<\/p>\n<ol>\n<li>Create an HTML page.<\/li>\n<li>Reference the necessary JS files.<\/li>\n<li>Set the data.<\/li>\n<li>Write the JS code for the chart.<\/li>\n<\/ol>\n<p>Let&#8217;s go through each step in detail now.<\/p>\n<h3>1. Create a basic HTML page<\/h3>\n<p>The first thing we need to do is create a basic HTML page. Let\u2019s give it a title and create an HTML block element to hold the chart. To identify this <code>&lt;div&gt;<\/code> later in the code, we should also give it an id attribute (let it be \u201ccontainer\u201d).<\/p>\n<pre><code class=\"html\">&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;JavaScript Diverging Bar Chart&lt;\/title&gt;\r\n    &lt;style type=&quot;text\/css&quot;&gt;      \r\n        html, body, #container { \r\n            width: 100%; height: 100%; margin: 0; padding: 0; \r\n        } \r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;div id=&quot;container&quot;&gt;&lt;\/div&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<p>Note that it is possible to specify the width and height parameters inside the <code>&lt;style&gt;<\/code> block to modify the space that your chart will occupy. I have put 100% in both parameters so the chart fills the whole page.<\/p>\n<h3>2. Include the necessary JavaScript files<\/h3>\n<p>Next, we need to add the charting library scripts that will help create the data visualization. Since we are working with the AnyChart library here, let\u2019s include the corresponding files from its <a href=\"https:\/\/www.anychart.com\/download\/cdn\/\">CDN<\/a>. (Keep in mind that you can always download the scripts if you want.)<\/p>\n<p>For the diverging bar chart, we need the <a href=\"https:\/\/docs.anychart.com\/Quick_Start\/Modules#base\" target=\"_blank\" rel=\"nofollow\">base module script<\/a> which is to be added to the <code>&lt;head&gt;<\/code> section of the HTML page.<\/p>\n<pre><code class=\"html\">&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;JavaScript Diverging Bar Chart&lt;\/title&gt;\r\n    &lt;script src=&quot;https:\/\/cdn.anychart.com\/releases\/8.9.0\/js\/anychart-base.min.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n    &lt;style type=&quot;text\/css&quot;&gt;\r\n      html,\r\n      body,\r\n      #container {\r\n        width: 100%;\r\n        height: 100%;\r\n        margin: 0;\r\n        padding: 0;\r\n      }\r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;  \r\n    &lt;div id=&quot;container&quot;&gt;&lt;\/div&gt;\r\n    &lt;script&gt;\r\n      \/\/ All the code for the JS diverging bar chart will come here\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>3. Add the data<\/h3>\n<p>I wanted to visualize the number of wins and losses of the LA Lakers team across all seasons from 1996 to 2016. So, I got the data from the <a href=\"https:\/\/www.nba.com\/lakers\/history\/seasonbyseason\/\" target=\"_blank\" rel=\"nofollow\">NBA website<\/a> and created an array with the year, wins, and losses.<\/p>\n<p>Since the amount of data is not huge, we can add it just like that:<\/p>\n<pre><code class=\"javascript\">var winlossData = [\r\n  [65, 17, \"2015-16\"],\r\n  [61, 21, \"2014-15\"],\r\n  [55, 27, \"2013-14\"],\r\n  [37, 45, \"2012-13\"],\r\n  [25, 41, \"2011-12\"],\r\n  [25, 57, \"2010-11\"],\r\n  [25, 57, \"2009-10\"],\r\n  [17, 65, \"2008-09\"],\r\n  [25, 57, \"2007-08\"],\r\n  [40, 42, \"2006-07\"],\r\n  [37, 45, \"2005-06\"],\r\n  [48, 34, \"2004-05\"],\r\n  [26, 56, \"2003-04\"],\r\n  [32, 50, \"2002-03\"],\r\n  [24, 58, \"2001-02\"],\r\n  [26, 56, \"2000-01\"],\r\n  [15, 67, \"1999-00\"],\r\n  [19, 31, \"1998-99\"],\r\n  [21, 61, \"1997-98\"],\r\n  [26, 56, \"1996-97\"]\r\n];<\/code><\/pre>\n<p>Now that the stage is set, let\u2019s start playing around by adding the JavaScript code that will create the interactive Diverging Bar Chart!<\/p>\n<h3>4. Write the JavaScript code for your chart<\/h3>\n<p>Before anything else, we need to add a function enclosing all the JS code, which makes sure that the entire code inside of it will only execute once the page is loaded.<\/p>\n<pre><code class=\"javascript\">&lt;script&gt;\r\n  anychart.onDocumentReady(function() {\r\n    \/\/ The place for the JS diverging bar chart code\r\n  });\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>In general, a JS diverging bar chart is pretty simple to create and I will walk you through each action. So get ready to wiggle, block, and shoot!<\/p>\n<p>Firstly, we create a bar chart and enter the data, all inside the enclosing <code>anychart.onDocumentReady()<\/code> function.<\/p>\n<pre><code class=\"javascript\">\/\/ create a bar chart\r\nvar chart = anychart.bar();\r\n\r\n\/\/ data\r\nvar winlossData = [\r\n  [65, 17, \"2015-16\"],\r\n  [61, 21, \"2014-15\"],\r\n  [55, 27, \"2013-14\"],\r\n  [37, 45, \"2012-13\"],\r\n  [25, 41, \"2011-12\"],\r\n  [25, 57, \"2010-11\"],\r\n  [25, 57, \"2009-10\"],\r\n  [17, 65, \"2008-09\"],\r\n  [25, 57, \"2007-08\"],\r\n  [40, 42, \"2006-07\"],\r\n  [37, 45, \"2005-06\"],\r\n  [48, 34, \"2004-05\"],\r\n  [26, 56, \"2003-04\"],\r\n  [32, 50, \"2002-03\"],\r\n  [24, 58, \"2001-02\"],\r\n  [26, 56, \"2000-01\"],\r\n  [15, 67, \"1999-00\"],\r\n  [19, 31, \"1998-99\"],\r\n  [21, 61, \"1997-98\"],\r\n  [26, 56, \"1996-97\"]\r\n];<\/code><\/pre>\n<p>Next, we create a function that accepts two parameters \u2014 a column number and a name. The column number indicates the column in the dataset and the name indicates the series. In our case, we have two series \u2014 one for the number of wins and one for the number of losses.<\/p>\n<p>Since we want a diverging bar chart, let\u2019s take the center and plot the bars for wins to the right and bars for losses to the left. Then, we should prepare the dataset by adding all the required values through a &#8216;for&#8217; loop.<\/p>\n<p>Don\u2019t worry if this sounds a bit complicated. It is just about making our data ready to be plotted, and when you look into the code below, you\u2019ll likely see that it&#8217;s all completely logical.<\/p>\n<p>There are two more things we need to include in the function. We define a series with the <code>rangeBar<\/code> function and add a line to indicate the names of the series and a separator line between the left and right bars.<\/p>\n<pre><code class=\"javascript\">var createSeries = function (columnNumber, name) {\r\n  var data = [];\r\n  for (var i = 0; i &lt; winlossData.length; i++) {\r\n    var value = winlossData[i][columnNumber];\r\n    var center = 0;\r\n    if (name === \"Wins\") {\r\n      data.push({\r\n        x: winlossData[i][2],\r\n        low: center,\r\n        high: center + value,\r\n        value: value\r\n      });\r\n    } else {\r\n      data.push({\r\n        x: winlossData[i][2],\r\n        low: -center,\r\n        high: -center - value,\r\n        value: value\r\n      });\r\n    }\r\n  }\r\n    \r\n  var series = chart.rangeBar(data);\r\n  series.name(name);\r\n};<\/code><\/pre>\n<p>Now, we create the two series with the desired arguments using the function just defined.<\/p>\n<pre><code class=\"javascript\">createSeries(0, \"Losses\");\r\ncreateSeries(1, \"Wins\");<\/code><\/pre>\n<p>It\u2019s halftime and the most complicated parts are over! Now we just have the setup of the chart.<\/p>\n<p>Add the title to the diverging bar chart:<\/p>\n<pre><code class=\"javascript\">chart\r\n  .title()\r\n  .enabled(true)\r\n  .text(\"20 Years of LA Lakers Win-Loss Record with Kobe Bryant (1996-2016)\");<\/code><\/pre>\n<p>And enable the chart\u2019s legend:<\/p>\n<pre><code class=\"javascript\">chart\r\n  .legend()\r\n  .enabled(true);<\/code><\/pre>\n<p>To make the wins and losses for each year show up adjacent to each other, we should convert the multi-series bar chart into a stacked bar chart. Next, to emphasize divergence, let\u2019s add a line marker at 0. Finally, we assign the container div and draw the chart:<\/p>\n<pre><code class=\"javascript\">\/\/ create a stacked bar chart from the multi-series bar chart\r\nchart.yScale().stackMode(\"value\");\r\n\r\n\/\/ set a container id for the chart\r\nchart.container(\"container\");\r\n  \r\n\/\/ initiate chart drawing\r\nchart.draw();<\/code><\/pre>\n<p>That\u2019s the whistle and there you have it \u2014 a very basic, yet fully functional interactive diverging bar chart built with JavaScript!<\/p>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-6AfC2akA\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/6AfC2akA\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-6AfC2akA{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<p>Although Kobe may have been spectacular in the final games of his career in the NBA, we can see that the Lakers struggled during his last few years with more losses than wins. But the overall record is definitely many more triumphs than losses.<\/p>\n<p><strong>Take a look at this initial version of the diverging bar chart with the full JS\/CSS\/HTML code <a href=\"https:\/\/codepen.io\/shacheeswadia\/pen\/jOVrqLQ\" target=\"_blank\" rel=\"nofollow\">on CodePen<\/a> [or <a href=\"https:\/\/playground.anychart.com\/6AfC2akA\/\" target=\"_blank\" rel=\"nofollow\">on AnyChart Playground<\/a>].<\/strong><\/p>\n<pre><code class=\"html\">&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;JavaScript Diverging Bar Chart&lt;\/title&gt;\r\n    &lt;script src=&quot;https:\/\/cdn.anychart.com\/releases\/8.9.0\/js\/anychart-base.min.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n    &lt;style type=&quot;text\/css&quot;&gt;\r\n      html,\r\n      body,\r\n      #container {\r\n        width: 100%;\r\n        height: 100%;\r\n        margin: 0;\r\n        padding: 0;\r\n      }\r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;  \r\n    &lt;div id=&quot;container&quot;&gt;&lt;\/div&gt;\r\n    &lt;script&gt;\r\n\r\n    anychart.onDocumentReady(function () {\r\n  \r\n      \/\/ create a bar chart\r\n      var chart = anychart.bar();\r\n\r\n      \/\/ data\r\n      var winlossData = [\r\n        [65, 17, &quot;2015-16&quot;],\r\n        [61, 21, &quot;2014-15&quot;],\r\n        [55, 27, &quot;2013-14&quot;],\r\n        [37, 45, &quot;2012-13&quot;],\r\n        [25, 41, &quot;2011-12&quot;],\r\n        [25, 57, &quot;2010-11&quot;],\r\n        [25, 57, &quot;2009-10&quot;],\r\n        [17, 65, &quot;2008-09&quot;],\r\n        [25, 57, &quot;2007-08&quot;],\r\n        [40, 42, &quot;2006-07&quot;],\r\n        [37, 45, &quot;2005-06&quot;],\r\n        [48, 34, &quot;2004-05&quot;],\r\n        [26, 56, &quot;2003-04&quot;],\r\n        [32, 50, &quot;2002-03&quot;],\r\n        [24, 58, &quot;2001-02&quot;],\r\n        [26, 56, &quot;2000-01&quot;],\r\n        [15, 67, &quot;1999-00&quot;],\r\n        [19, 31, &quot;1998-99&quot;],\r\n        [21, 61, &quot;1997-98&quot;],\r\n        [26, 56, &quot;1996-97&quot;]\r\n      ];\r\n\r\n      \/\/ configure a function to create series\r\n      var createSeries = function (columnNumber, name) {\r\n        var data = [];\r\n        for (var i = 0; i &lt; winlossData.length; i++) {\r\n          var value = winlossData[i][columnNumber];\r\n          var center = 0;\r\n          if (name === &quot;Wins&quot;) {\r\n            data.push({\r\n              x: winlossData[i][2],\r\n              low: center,\r\n              high: center + value,\r\n              value: value\r\n            });\r\n          } else {\r\n            data.push({\r\n              x: winlossData[i][2],\r\n              low: -center,\r\n              high: -center - value,\r\n              value: value\r\n            });\r\n          }\r\n        }\r\n    \r\n        var series = chart.rangeBar(data);\r\n        series.name(name);\r\n      };\r\n\r\n      \/\/ create series\r\n      createSeries(0, &quot;Losses&quot;);\r\n      createSeries(1, &quot;Wins&quot;);\r\n\r\n      \/\/ set the chart title\r\n     chart\r\n        .title()\r\n        .enabled(true)\r\n        .text(&quot;20 Years of LA Lakers Win-Loss Record with Kobe Bryant (1996-2016)&quot;);\r\n\r\n      \/\/ enable the chart legend\r\n      chart\r\n        .legend()\r\n        .enabled(true);\r\n  \r\n      \/\/ create a stacked bar chart from the multi-series bar chart\r\n      chart.yScale().stackMode(&quot;value&quot;);\r\n\r\n      \/\/ set a container id for the chart\r\n      chart.container(&quot;container&quot;);\r\n  \r\n      \/\/ initiate chart drawing\r\n      chart.draw();\r\n\r\n    });\r\n\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>How to Customize Our JavaScript Diverging Bar Chart<\/h3>\n<p>A slam dunk thing about interactive data visualization with JavaScript is the freedom we have to customize it so that our data tells better stories. I will show you how to push some quick changes to the basic JS-based diverging bar chart to make it more engaging and informative.<\/p>\n<p>Now I&#8217;ll throw a 3-pointer and customize the chart to improve some of its functionalities and aesthetics.<\/p>\n<h3>1. Basic styling and axes settings<\/h3>\n<p>To start with, let\u2019s change some basic styling and settings for the X and Y axes to make things more readable.<\/p>\n<p>Just remember that in AnyChart, a <a href=\"https:\/\/docs.anychart.com\/Basic_Charts\/Range_Bar_Chart\" target=\"_blank\" rel=\"nofollow\">range bar chart<\/a> is the vertical version of a <a href=\"https:\/\/docs.anychart.com\/Basic_Charts\/Range_Column_Chart\" target=\"_blank\" rel=\"nofollow\">range column chart<\/a>. Consequently, in our diverging bar chart, the horizontal axis is the Y-axis, and the vertical axis is called the X-axis.<\/p>\n<p>So, let\u2019s get rid of ticks, configure the axis title, and customize the labels on the vertical axis. We&#8217;ll also set 80 as the maximum and remove the minus sign from the labels on the horizontal axis:<\/p>\n<pre><code class=\"javascript\">chart\r\n  .xAxis()\r\n  .ticks(false);\r\nchart\r\n  .xAxis()\r\n  .title()\r\n  .enabled(true)\r\n  .text(\"Years\")\r\n  .padding([0, 0, 10, 0]);\r\nchart\r\n  .xAxis()\r\n  .labels()\r\n  .fontSize(11)\r\n  .fontColor(\"#474747\")\r\n  .padding([0, 10, 0, 0]);\r\nchart.yScale().maximum(80);\r\nchart\r\n  .yAxis(0)\r\n  .labels()\r\n  .format(function () {\r\n    return Math.abs(this.value);\r\n  });<\/code><\/pre>\n<p>Next, to emphasize divergence, it would be great to add a white stroke between the two series and a line marker at 0.<\/p>\n<pre><code class=\"javascript\">\/\/ add the stroke by setting it in this line\r\nseries.name(name).stroke(\"3 #fff 1\");\r\n\r\n...\r\n\r\n\/\/ create a line marker at 0\r\nchart\r\n  .lineMarker()\r\n  .value(0)\r\n  .stroke(\"#CECECE\");<\/code><\/pre>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-pMmQUwPR\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/pMmQUwPR\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-pMmQUwPR{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<p>Ah, doesn\u2019t the chart look more polished and easier to read now?<\/p>\n<p><strong>Check out the code for this version of the diverging bar chart <a href=\"https:\/\/codepen.io\/shacheeswadia\/pen\/zYoEMEd\" target=\"_blank\" rel=\"nofollow\">on CodePen<\/a> [or <a href=\"https:\/\/playground.anychart.com\/pMmQUwPR\/\" target=\"_blank\" rel=\"nofollow\">on AnyChart Playground<\/a>].<\/strong><\/p>\n<p>Before we make more customizations, there is one small digression I want to make. I also thought of making the horizontal axis display the wins and losses for every season in percentages rather than absolute values. It\u2019s pretty easy but the result did not offer any extra insights.<\/p>\n<p>Also, the absolute values do represent when the Lakers played more or fewer games through the year. That\u2019s ultimately why I decided to keep the absolute values. But you are welcome to check out the version with percentages <a href=\"https:\/\/codepen.io\/shacheeswadia\/pen\/jOVrqKd\" target=\"_blank\" rel=\"nofollow\">on CodePen<\/a> [or <a href=\"https:\/\/playground.anychart.com\/yw8TSUV4\/\" target=\"_blank\" rel=\"nofollow\">on AnyChart Playground<\/a>].<\/p>\n<p>Well, let&#8217;s move on from that missed shot and back into focus mode.<\/p>\n<h3>2. Tooltip customization<\/h3>\n<p>Next, I customized the tooltip to make it more informative and interesting.<\/p>\n<p>Here, I also got the idea of showcasing the previously calculated percentage values (see the example from the digression just above) as an extra bit of information in the tooltip of our diverging bar chart.<\/p>\n<p>So, the first step is to implement the calculation of percentage values:<\/p>\n<pre><code class=\"javascript\">\/\/ calculate percentages for the tooltip\r\nvar val = winlossData[i][columnNumber] * 100;\r\nif (columnNumber == 0) {\r\n  var percentValue =\r\n    val \/ (winlossData[i][columnNumber] + winlossData[i][columnNumber + 1]);\r\n} else {\r\n  var percentValue =\r\n    val \/ (winlossData[i][columnNumber] + winlossData[i][columnNumber - 1]);\r\n}\r\npercentValue = percentValue.toFixed(2);<\/code><\/pre>\n<p>The percentage calculation goes as part of the series configuration function \u2014 look at how it is included there:<\/p>\n<pre><code class=\"javascript\">\/\/ configure a function to create series\r\nvar createSeries = function (columnNumber, name) {\r\n  var data = [];\r\n  for (var i = 0; i &lt; winlossData.length; i++) {\r\n\r\n    \/\/ calculate percentages for the tooltip\r\n    var val = winlossData[i][columnNumber] * 100;\r\n    if (columnNumber == 0) {\r\n      var percentValue =\r\n        val \/ (winlossData[i][columnNumber] + winlossData[i][columnNumber + 1]);\r\n    } else {\r\n      var percentValue =\r\n        val \/ (winlossData[i][columnNumber] + winlossData[i][columnNumber - 1]);\r\n    }\r\n    percentValue = percentValue.toFixed(2);     \r\n      \r\n    var value = winlossData[i][columnNumber];\r\n    var center = 0;\r\n    if (name === \"Wins\") {\r\n      data.push({\r\n        x: winlossData[i][2],\r\n        low: center,\r\n        high: center + value,\r\n        value: value,\r\n        \/\/ add the calculated percentage value\r\n        percentValue: percentValue\r\n      });\r\n    } else {\r\n      data.push({\r\n        x: winlossData[i][2],\r\n        low: -center,\r\n        high: -center - value,\r\n        value: value,\r\n        \/\/ add the calculated percentage value\r\n        percentValue: percentValue\r\n      });\r\n    }\r\n  }<\/code><\/pre>\n<p>Then we have additional tooltip formatting to make it all look neat and beautiful:<\/p>\n<pre><code class=\"javascript\">\/\/ customize the tooltip\r\nchart\r\n  .tooltip()\r\n  .useHtml(true)\r\n  .fontSize(12)\r\n  .titleFormat(function () {\r\n    return this.getData(&quot;x&quot;) + &quot; &quot; + this.seriesName;\r\n  })\r\n  .format(function () {\r\n    return (\r\n      &quot;&lt;h6 style=&apos;font-size:12px; font-weight:400; margin: 0.25rem 0;&apos;&gt;Total games: &quot; +\r\n      &quot;&lt;b&gt;&quot; +\r\n      this.getData(&quot;value&quot;) +\r\n      &quot;&lt;\/b&gt;&lt;\/h6&gt;&quot; +\r\n      &quot;&lt;h6 style=&apos;font-size:12px; font-weight:400; margin: 0.25rem 0;&apos;&gt;Percentage games: &quot; +\r\n      &quot;&lt;b&gt;&quot; +\r\n      this.getData(&quot;percentValue&quot;) +\r\n      &quot; %&lt;\/b&gt;&lt;\/h6&gt;&quot;\r\n    );\r\n  });<\/code><\/pre>\n<h3>3. Color palette change<\/h3>\n<p>Well, this last customization is definitely a dagger \u2014 the shot that is going to make the chart look completely awesome and win the game! It is simply changing the color palette to match the LA Lakers&#8217; jersey colors. So simple:<\/p>\n<pre><code class=\"javascript\">chart.palette(\r\n  anychart.palettes.distinctColors().items([\"#FDB827\", \"#542583\"])\r\n);<\/code><\/pre>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-vkubwRYZ\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/vkubwRYZ\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-vkubwRYZ{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<p>You see, at the very last second, I also turned off the selection mode by adding the corresponding command to this line:<\/p>\n<pre><code class=\"javascript\">series.name(name).stroke(\"3 #fff 1\").selectionMode(\"none\");<\/code><\/pre>\n<p><strong>Okay! This final interactive JavaScript diverging range bar chart is available <a href=\"https:\/\/codepen.io\/shacheeswadia\/pen\/NWbrpYj\" target=\"_blank\" rel=\"nofollow\">on CodePen<\/a> [and <a href=\"https:\/\/playground.anychart.com\/vkubwRYZ\/\" target=\"_blank\" rel=\"nofollow\">on AnyChart Playground<\/a>].<\/strong><\/p>\n<p>Just in case, the full code for the HTML page is right here:<\/p>\n<pre><code class=\"html\">&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;JavaScript Diverging Bar Chart&lt;\/title&gt;\r\n    &lt;script src=&quot;https:\/\/cdn.anychart.com\/releases\/8.9.0\/js\/anychart-base.min.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n    &lt;style type=&quot;text\/css&quot;&gt;\r\n      html,\r\n      body,\r\n      #container {\r\n        width: 100%;\r\n        height: 100%;\r\n        margin: 0;\r\n        padding: 0;\r\n      }\r\n    &lt;\/style&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;  \r\n    &lt;div id=&quot;container&quot;&gt;&lt;\/div&gt;\r\n    &lt;script&gt;\r\n\r\n    anychart.onDocumentReady(function () {\r\n  \r\n      \/\/ create a bar chart\r\n      var chart = anychart.bar();\r\n\r\n      \/\/ data\r\n      var winlossData = [\r\n        [65, 17, &quot;2015-16&quot;],\r\n        [61, 21, &quot;2014-15&quot;],\r\n        [55, 27, &quot;2013-14&quot;],\r\n        [37, 45, &quot;2012-13&quot;],\r\n        [25, 41, &quot;2011-12&quot;],\r\n        [25, 57, &quot;2010-11&quot;],\r\n        [25, 57, &quot;2009-10&quot;],\r\n        [17, 65, &quot;2008-09&quot;],\r\n        [25, 57, &quot;2007-08&quot;],\r\n        [40, 42, &quot;2006-07&quot;],\r\n        [37, 45, &quot;2005-06&quot;],\r\n        [48, 34, &quot;2004-05&quot;],\r\n        [26, 56, &quot;2003-04&quot;],\r\n        [32, 50, &quot;2002-03&quot;],\r\n        [24, 58, &quot;2001-02&quot;],\r\n        [26, 56, &quot;2000-01&quot;],\r\n        [15, 67, &quot;1999-00&quot;],\r\n        [19, 31, &quot;1998-99&quot;],\r\n        [21, 61, &quot;1997-98&quot;],\r\n        [26, 56, &quot;1996-97&quot;]\r\n      ];\r\n\r\n      \/\/ configure a function to create series\r\n      var createSeries = function (columnNumber, name) {\r\n        var data = [];\r\n        for (var i = 0; i &lt; winlossData.length; i++) {\r\n\r\n          \/\/ calculate percentages for the tooltip\r\n          var val = winlossData[i][columnNumber] * 100;\r\n          if (columnNumber == 0) {\r\n            var percentValue =\r\n              val \/ (winlossData[i][columnNumber] + winlossData[i][columnNumber + 1]);\r\n          } else {\r\n            var percentValue =\r\n              val \/ (winlossData[i][columnNumber] + winlossData[i][columnNumber - 1]);\r\n          }\r\n          percentValue = percentValue.toFixed(2);     \r\n      \r\n          var value = winlossData[i][columnNumber];\r\n          var center = 0;\r\n          if (name === &quot;Wins&quot;) {\r\n            data.push({\r\n              x: winlossData[i][2],\r\n              low: center,\r\n              high: center + value,\r\n              value: value,\r\n              \/\/ add the calculated percentage value\r\n              percentValue: percentValue\r\n            });\r\n          } else {\r\n            data.push({\r\n              x: winlossData[i][2],\r\n              low: -center,\r\n              high: -center - value,\r\n              value: value,\r\n              \/\/ add the calculated percentage value\r\n              percentValue: percentValue\r\n            });\r\n          }\r\n        }\r\n    \r\n        var series = chart.rangeBar(data);\r\n        series.name(name).stroke(&quot;3 #fff 1&quot;).selectionMode(&quot;none&quot;);\r\n      };\r\n\r\n      \/\/ create series\r\n      createSeries(0, &quot;Losses&quot;);\r\n      createSeries(1, &quot;Wins&quot;);\r\n\r\n      \/\/ set the chart title\r\n      chart\r\n        .title()\r\n        .enabled(true)\r\n        .text(&quot;20 Years of LA Lakers Win-Loss Record with Kobe Bryant (1996-2016)&quot;);\r\n\r\n      \/\/ enable the chart legend\r\n      chart\r\n        .legend()\r\n        .enabled(true);\r\n  \r\n      \/\/ create a stacked bar chart from the multi-series bar chart\r\n      chart.yScale().stackMode(&quot;value&quot;);\r\n  \r\n      \/\/ customize the settings of the axes\r\n      chart\r\n        .xAxis()\r\n        .ticks(false);\r\n      chart\r\n        .xAxis()\r\n        .title()\r\n        .enabled(true)\r\n        .text(&quot;Years&quot;)\r\n        .padding([0, 0, 10, 0]);\r\n      chart\r\n        .xAxis()\r\n        .labels()\r\n        .fontSize(11)\r\n        .fontColor(&quot;#474747&quot;)\r\n        .padding([0, 10, 0, 0]);\r\n      chart.yScale().maximum(80);\r\n      chart\r\n        .yAxis(0)\r\n        .labels()\r\n        .format(function () {\r\n          return Math.abs(this.value);\r\n        });\r\n\r\n      \/\/ create a line marker at 0\r\n      chart\r\n        .lineMarker()\r\n        .value(0)\r\n        .stroke(&quot;#CECECE&quot;);\r\n  \r\n      \/\/ customize the tooltip\r\n      chart\r\n        .tooltip()\r\n        .useHtml(true)\r\n        .fontSize(12)\r\n        .titleFormat(function () {\r\n          return this.getData(&quot;x&quot;) + &quot; &quot; + this.seriesName;\r\n        })\r\n        .format(function () {\r\n          return (\r\n            &quot;&lt;h6 style=&apos;font-size:12px; font-weight:400; margin: 0.25rem 0;&apos;&gt;Total games: &quot; +\r\n            &quot;&lt;b&gt;&quot; +\r\n            this.getData(&quot;value&quot;) +\r\n            &quot;&lt;\/b&gt;&lt;\/h6&gt;&quot; +\r\n            &quot;&lt;h6 style=&apos;font-size:12px; font-weight:400; margin: 0.25rem 0;&apos;&gt;Percentage games: &quot; +\r\n            &quot;&lt;b&gt;&quot; +\r\n            this.getData(&quot;percentValue&quot;) +\r\n            &quot; %&lt;\/b&gt;&lt;\/h6&gt;&quot;\r\n          );\r\n        });\r\n  \r\n      \/\/ set a custom color palette\r\n      chart.palette(\r\n        anychart.palettes.distinctColors().items([&quot;#FDB827&quot;, &quot;#542583&quot;])\r\n      );\r\n\r\n      \/\/ set a container id for the chart\r\n      chart.container(&quot;container&quot;);\r\n  \r\n      \/\/ initiate chart drawing\r\n      chart.draw();\r\n\r\n    });\r\n\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, I have shown you how fast and easy it is to get a diverging bar chart up and running using JavaScript. We&#8217;ve also seen how a little bit of effort makes the graphic look really cool and lets you get more out of the underlying data. Please let me know if you have any questions.<\/p>\n<p>If you are feeling motivated to work more with interactive JS-based data visualization, go ahead and play around with the diverging bar charts on CodePen (I added links throughout the tutorial), check out <a href=\"https:\/\/www.anychart.com\/chartopedia\/\">other chart options<\/a>, or try <a href=\"https:\/\/en.wikipedia.org\/wiki\/Comparison_of_JavaScript_charting_libraries\" target=\"_blank\" rel=\"nofollow\">other JavaScript libraries<\/a>.<\/p>\n<p>Also, as we fondly look back at the statistics of the basketball legend\u2019s team here, remember to do more sports and create more visualizations!<\/p>\n<hr \/>\n<p><em>Published with permission of Shachee Swadia. Originally appeared on <a href=\"https:\/\/www.freecodecamp.org\/news\/diverging-bar-chart-javascript\/\" target=\"_blank\" rel=\"nofollow\">freeCodeCamp<\/a> under the title &#8220;How to Create a Diverging Bar Chart with a JavaScript Charting Library&#8221; on February 24, 2021.<\/em><\/p>\n<hr \/>\n<p><em>Check out other <a href=\"https:\/\/www.anychart.com\/blog\/category\/javascript-chart-tutorials\/\">JavaScript charting tutorials<\/a>.<\/em><\/p>\n<p><em>If you want to write a cool guest post for our blog, <a href=\"https:\/\/www.anychart.com\/support\/\">let us know<\/a>.<\/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>This article is a step-by-step guide that&#8217;ll show you how to build an interactive JavaScript range chart that visualizes 20 years of the LA Lakers\u2019 performance with Kobe Bryant. The year 2020 was pretty poignant for obvious reasons. But even before the pandemic, the year started on a sad note with the death of basketball [&hellip;]<!-- 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":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,23,13,279,4],"tags":[53,70,71,579,941,1202,284,127,258,471,266,620,54,1760,256,1111,744,844,2257,2260,1154,1762,2013,2014,32,55,144,36,907,141,249,81,57,1238,99,58,65,56,2254,1299,2255,577,578,1201,1721,2258,2259,218,87,581,459,30,172,2256],"class_list":["post-12275","post","type-post","status-publish","format-standard","hentry","category-anychart-charting-component","category-html5","category-javascript","category-javascript-chart-tutorials","category-tips-and-tricks","tag-anychart","tag-bar-chart","tag-bar-charts","tag-basketball","tag-basketball-data","tag-basketball-data-visualization","tag-chart-examples","tag-chart-types","tag-charts","tag-data-analysis","tag-data-analytics","tag-data-analytics-examples","tag-data-visualization","tag-data-visualization-design","tag-data-visualization-examples","tag-data-visualization-practice","tag-data-visualization-techniques","tag-data-visualization-tutorial","tag-diverging-bar-chart","tag-freecodecamp","tag-good-data-visualization","tag-guest-post","tag-html","tag-html-charts","tag-html5","tag-html5-charts","tag-infographics","tag-javascript","tag-javascript-chart-tutorial","tag-javascript-charting","tag-javascript-charting-api","tag-javascript-charting-library","tag-javascript-charts","tag-javascript-graph","tag-javascript-library","tag-js-chart","tag-js-charting","tag-js-charts","tag-kobe-bryant","tag-los-angeles","tag-los-angeles-lakers","tag-nba","tag-nba-data","tag-nba-data-visualization","tag-nba-statistics","tag-range-bar-chart","tag-range-bar-charts","tag-range-chart","tag-stacked-bar-chart","tag-stacked-bar-charts","tag-statistics","tag-tips-and-tricks","tag-tutorial","tag-win-loss-record","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 Diverging Bar Chart with JavaScript<\/title>\n<meta name=\"description\" content=\"This simple tutorial shows how to build an interactive Diverging Bar Chart using JS. Visualizing 20 years of the LA Lakers\u2019 win-loss data for illustration.\" \/>\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\/2021\/03\/02\/diverging-bar-chart-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Build Diverging Bar Chart with JavaScript\" \/>\n<meta property=\"og:description\" content=\"Learn to create interactive diverging bar charts using JS. Visualizing 20 years of the LA Lakers\u2019 win-loss data along the tutorial.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/\" \/>\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=\"2021-03-02T11:24:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-13T11:04:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1050\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Shachee Swadia\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"How to Build Diverging Bar Chart with JavaScript\" \/>\n<meta name=\"twitter:description\" content=\"Learn to create interactive diverging bar charts using JS. Visualizing 20 years of the LA Lakers\u2019 win-loss data along the tutorial.\" \/>\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=\"Shachee Swadia\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/\"},\"author\":{\"name\":\"Shachee Swadia\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/273255f98371177b5ac1f4775610bb51\"},\"headline\":\"How to Create Diverging Bar Chart with JavaScript\",\"datePublished\":\"2021-03-02T11:24:46+00:00\",\"dateModified\":\"2022-08-13T11:04:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/\"},\"wordCount\":1887,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png\",\"keywords\":[\"AnyChart\",\"bar chart\",\"bar charts\",\"basketball\",\"basketball data\",\"basketball data visualization\",\"chart examples\",\"chart types\",\"charts\",\"data analysis\",\"data analytics\",\"data analytics examples\",\"Data Visualization\",\"data visualization design\",\"data visualization examples\",\"data visualization practice\",\"data visualization techniques\",\"data visualization tutorial\",\"diverging bar chart\",\"freeCodeCamp\",\"good data visualization\",\"guest post\",\"HTML\",\"HTML charts\",\"HTML5\",\"html5 charts\",\"infographics\",\"JavaScript\",\"javascript chart tutorial\",\"javascript charting\",\"javascript charting api\",\"JavaScript charting library\",\"javascript charts\",\"javascript graph\",\"JavaScript library\",\"js chart\",\"js charting\",\"js charts\",\"Kobe Bryant\",\"Los Angeles\",\"Los Angeles Lakers\",\"NBA\",\"NBA data\",\"NBA data visualization\",\"NBA statistics\",\"range bar chart\",\"range bar charts\",\"range chart\",\"stacked bar chart\",\"stacked bar charts\",\"statistics\",\"Tips and tricks\",\"tutorial\",\"win-loss record\"],\"articleSection\":[\"AnyChart Charting Component\",\"HTML5\",\"JavaScript\",\"JavaScript Chart Tutorials\",\"Tips and Tricks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/\",\"url\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/\",\"name\":\"How to Create Diverging Bar Chart with JavaScript\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png\",\"datePublished\":\"2021-03-02T11:24:46+00:00\",\"dateModified\":\"2022-08-13T11:04:19+00:00\",\"author\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/273255f98371177b5ac1f4775610bb51\"},\"description\":\"This simple tutorial shows how to build an interactive Diverging Bar Chart using JS. Visualizing 20 years of the LA Lakers\u2019 win-loss data for illustration.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#primaryimage\",\"url\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png\",\"contentUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png\",\"width\":1920,\"height\":1050},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.anychart.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create Diverging Bar Chart with JavaScript\"}]},{\"@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\/273255f98371177b5ac1f4775610bb51\",\"name\":\"Shachee Swadia\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/50ebd5c61e4e46e052898a5ddbcef5996666132cf07c614be4725f028ec7eae3?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/50ebd5c61e4e46e052898a5ddbcef5996666132cf07c614be4725f028ec7eae3?s=96&r=g\",\"caption\":\"Shachee Swadia\"},\"url\":\"https:\/\/www.anychart.com\/blog\/author\/shachee-swadia\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create Diverging Bar Chart with JavaScript","description":"This simple tutorial shows how to build an interactive Diverging Bar Chart using JS. Visualizing 20 years of the LA Lakers\u2019 win-loss data for illustration.","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\/2021\/03\/02\/diverging-bar-chart-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How to Build Diverging Bar Chart with JavaScript","og_description":"Learn to create interactive diverging bar charts using JS. Visualizing 20 years of the LA Lakers\u2019 win-loss data along the tutorial.","og_url":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/","og_site_name":"AnyChart News","article_publisher":"https:\/\/www.facebook.com\/AnyCharts","article_published_time":"2021-03-02T11:24:46+00:00","article_modified_time":"2022-08-13T11:04:19+00:00","og_image":[{"width":1920,"height":1050,"url":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png","type":"image\/png"}],"author":"Shachee Swadia","twitter_card":"summary_large_image","twitter_title":"How to Build Diverging Bar Chart with JavaScript","twitter_description":"Learn to create interactive diverging bar charts using JS. Visualizing 20 years of the LA Lakers\u2019 win-loss data along the tutorial.","twitter_creator":"@AnyChart","twitter_site":"@AnyChart","twitter_misc":{"Written by":"Shachee Swadia","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#article","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/"},"author":{"name":"Shachee Swadia","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/273255f98371177b5ac1f4775610bb51"},"headline":"How to Create Diverging Bar Chart with JavaScript","datePublished":"2021-03-02T11:24:46+00:00","dateModified":"2022-08-13T11:04:19+00:00","mainEntityOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/"},"wordCount":1887,"commentCount":0,"image":{"@id":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png","keywords":["AnyChart","bar chart","bar charts","basketball","basketball data","basketball data visualization","chart examples","chart types","charts","data analysis","data analytics","data analytics examples","Data Visualization","data visualization design","data visualization examples","data visualization practice","data visualization techniques","data visualization tutorial","diverging bar chart","freeCodeCamp","good data visualization","guest post","HTML","HTML charts","HTML5","html5 charts","infographics","JavaScript","javascript chart tutorial","javascript charting","javascript charting api","JavaScript charting library","javascript charts","javascript graph","JavaScript library","js chart","js charting","js charts","Kobe Bryant","Los Angeles","Los Angeles Lakers","NBA","NBA data","NBA data visualization","NBA statistics","range bar chart","range bar charts","range chart","stacked bar chart","stacked bar charts","statistics","Tips and tricks","tutorial","win-loss record"],"articleSection":["AnyChart Charting Component","HTML5","JavaScript","JavaScript Chart Tutorials","Tips and Tricks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/","url":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/","name":"How to Create Diverging Bar Chart with JavaScript","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#primaryimage"},"image":{"@id":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png","datePublished":"2021-03-02T11:24:46+00:00","dateModified":"2022-08-13T11:04:19+00:00","author":{"@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/273255f98371177b5ac1f4775610bb51"},"description":"This simple tutorial shows how to build an interactive Diverging Bar Chart using JS. Visualizing 20 years of the LA Lakers\u2019 win-loss data for illustration.","breadcrumb":{"@id":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#primaryimage","url":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png","contentUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2021\/03\/diverging-bar-chart-javascript.png","width":1920,"height":1050},{"@type":"BreadcrumbList","@id":"https:\/\/www.anychart.com\/blog\/2021\/03\/02\/diverging-bar-chart-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.anychart.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Create Diverging Bar Chart with JavaScript"}]},{"@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\/273255f98371177b5ac1f4775610bb51","name":"Shachee Swadia","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/50ebd5c61e4e46e052898a5ddbcef5996666132cf07c614be4725f028ec7eae3?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/50ebd5c61e4e46e052898a5ddbcef5996666132cf07c614be4725f028ec7eae3?s=96&r=g","caption":"Shachee Swadia"},"url":"https:\/\/www.anychart.com\/blog\/author\/shachee-swadia\/"}]}},"_links":{"self":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/12275","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\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/comments?post=12275"}],"version-history":[{"count":14,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/12275\/revisions"}],"predecessor-version":[{"id":15506,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/12275\/revisions\/15506"}],"wp:attachment":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/media?parent=12275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/categories?post=12275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/tags?post=12275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}