{"id":15051,"date":"2022-06-08T09:02:16","date_gmt":"2022-06-08T09:02:16","guid":{"rendered":"https:\/\/www.anychart.com\/blog\/?p=15051"},"modified":"2022-08-13T11:11:50","modified_gmt":"2022-08-13T11:11:50","slug":"treemap-js","status":"publish","type":"post","link":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/","title":{"rendered":"How to Make Treemap Using JS"},"content":{"rendered":"<p><a href=\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/\"><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart.png\" alt=\"A JavaScript Treemap chart on a website or in an app\" width=\"100%\" class=\"alignnone size-full wp-image-15067\" srcset=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart.png 1200w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart-300x173.png 300w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart-768x442.png 768w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart-1024x589.png 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a>Treemap visualizations are widely used in hierarchical data analysis. If you need to build one but have never done that before, you might think the process is somewhat complicated. Well, not necessarily. I decided to make a step-by-step tutorial explaining how to create awesome interactive treemap charts with ease using JavaScript. And you\u2019re gonna love the illustrations!<\/p>\n<p>Are we alone in the universe? A question every one of us has asked ourselves at some point. While we are thinking about the odds of the Earth being the only habitable planet in the universe, or not, one of the things we might consider is how big the universe is. Let\u2019s look at that with the help of treemaps! In this tutorial, we will be visualizing the scale of the 10 largest galaxies in the known universe using the treemapping technique.<\/p>\n<p>So, would you like to know how to quickly build a JS-based treemap chart? Follow me in this stepwise tutorial and learn in an easy, fun way!<\/p>\n<p><!--more Read the JS charting tutorial \u00bb--><\/p>\n<h2>What Is Treemap?<\/h2>\n<p>Before we get down to the tutorial itself, let\u2019s look at the concept of treemaps. A <a href=\"https:\/\/www.anychart.com\/chartopedia\/chart-type\/treemap\/\">treemap chart<\/a> is a popular technique for visualizing hierarchically organized, tree-structured data. Designed to show at a glance the structure of hierarchy along with the value of individual data points, it makes use of nested rectangles whose size is proportional to the corresponding quantities.<\/p>\n<p>Each branch of the tree is a rectangle, and for sub-branches, there are smaller rectangles nested inside them. Displaying data by color and proximity, treemaps can easily represent lots of data while making efficient use of space and are great for comparing proportions within hierarchies.<\/p>\n<p>The treemap chart type was invented by professor <a href=\"https:\/\/en.wikipedia.org\/wiki\/Ben_Shneiderman\" target=\"_blank\" rel=\"nofollow\">Ben Shneiderman<\/a> who has given significant contributions in the field of information design and human-computer interaction. Treemaps are used in many data visualization areas and can be found applied in the analysis of stock market data, census systems, and election statistics, as well as in data journalism, hard drive exploration tools, etc.<\/p>\n<h2>A Peek at Our JS Treemap Chart<\/h2>\n<p>So now, we will build a treemap using JavaScript to compare the sizes of the top 10 galaxies in the known universe. Take a look at exactly what we are going to create. This is what our JS treemap chart will look like by the end of the tutorial.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/0-treemap-chart-preview.gif\" alt=\"Treemap Chart Preview\" width=\"100%\" class=\"alignnone size-full wp-image-15056\" \/><\/p>\n<p>Let\u2019s start our interstellar journey!<\/p>\n<h2>Create a Basic JS Treemap Chart<\/h2>\n<p>Creating a JavaScript-based treemap chart generally takes four basic steps that are as follows:<\/p>\n<ol>\n<li>Create an HTML page<\/li>\n<li>Reference JavaScript files<\/li>\n<li>Set data<\/li>\n<li>Write some JS treemapping code<\/li>\n<\/ol>\n<p>And don\u2019t worry if you are a novice in HTML, CSS, and JavaScript. We will go through each and every step in detail, and by the end of this tutorial, you will have your own JS treemap ready.<\/p>\n<p>So, the countdown has begun, let\u2019s get our chart ready for launch.<\/p>\n<h3>1. Create an HTML Page<\/h3>\n<p>The first thing we do here is to create a basic HTML page. There, we add an HTML block element (<code>&lt;div&gt;<\/code>) \u2014 that\u2019s where our treemap chart will be placed \u2014 and assign it an ID attribute (for example, let it be \u201ccontainer\u201d) to reference it later in the code.<\/p>\n<p>We should also set some styling for the\u00a0<code>&lt;div&gt;<\/code>. Let\u2019s define the <code>width<\/code> and <code>height<\/code> properties as 100% and margin and padding as 0. You may change this to your liking.<\/p>\n<pre><code class=\"html\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&nbsp;&nbsp;&lt;head&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta charset=\"utf-8\"&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;JavaScript Treemap Chart&lt;\/title&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;style type=\"text\/css\"&gt;  \t\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html, body, #container {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width: 100%;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height: 100%;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;margin: 0;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;padding: 0;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/style&gt;\r\n&nbsp;&nbsp;&lt;\/head&gt;\r\n&nbsp;&nbsp;&lt;body&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n&nbsp;&nbsp;&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>2. Reference JavaScript Files<\/h3>\n<p>Next, we need to reference all the required scripts that will be used to create a treemap chart.<\/p>\n<p>There are <a href=\"https:\/\/en.wikipedia.org\/wiki\/Comparison_of_JavaScript_charting_libraries\" target=\"_blank\" rel=\"nofollow\">multiple<\/a> JavaScript charting libraries out there to choose from. The fundamental steps of creating interactive data visualizations remain more or less the same with any of them. Here, for illustration, I am going to use <a href=\"https:\/\/www.anychart.com\">AnyChart<\/a> \u2014 it supports treemaps and has a free version, with the source open on <a href=\"https:\/\/github.com\/AnyChart\/AnyChart\" target=\"_blank\" rel=\"nofollow\">GitHub<\/a>.<\/p>\n<p>So, to build a treemap chart, we need the &#8216;core&#8217; and &#8216;treemap&#8217; <a href=\"https:\/\/docs.anychart.com\/Quick_Start\/Modules\" target=\"_blank\" rel=\"nofollow\">modules<\/a>. Let\u2019s reference both in the head section of the HTML page created in the first step. We will obtain them from the CDN (alternatively, the scripts can be downloaded).<\/p>\n<pre><code class=\"html\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&nbsp;&nbsp;&lt;head&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta charset=\"utf-8\"&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;JavaScript Treemap Chart&lt;\/title&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;script src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-treemap.min.js\"&gt;&lt;\/script&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;style type=\"text\/css\"&gt;  \t\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html, body, #container {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width: 100%;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height: 100%;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;margin: 0;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;padding: 0;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/style&gt;\r\n&nbsp;&nbsp;&lt;\/head&gt;\r\n&nbsp;&nbsp;&lt;body&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n&nbsp;&nbsp;&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>3. Set Data<\/h3>\n<p>Here comes the data. We will visualize the scale of the top 10 largest galaxies in the known universe. The galaxies are so incredibly vast that they\u2019re measured by how many light-years across they are. A light-year is the distance a beam of light travels in a single Earth year, which equates to approximately 6 trillion miles.<\/p>\n<p>I have already taken the galaxy scale data from <a href=\"https:\/\/largest.org\/geography\/galaxies\/\" target=\"_blank\" rel=\"nofollow\">Largest.org<\/a>.<\/p>\n<p>For our chart, the tree structure data will look as follows. The root-level element is \u2018Galaxies,\u2019 divided (by galaxy type) into &#8216;Elliptical&#8217; and &#8216;Spiral&#8217; as its children, which further have arrays of individual galaxy objects as their own children.<\/p>\n<p>Each galaxy object is provided with key-value properties of name and scale in light-years. For example, <code>{name: \"IC 1101\", value: 4000000}<\/code> means the IC 1101 galaxy with its scale being 4,000,000 light-years. Honestly, it&#8217;s very hard to comprehend how massive it is.<\/p>\n<pre><code class=\"javascript\">var dataSet = [\r\n  {name: \"Galaxies\", children: [\r\n    {name: \"Elliptical\", children: [\r\n      {name: \"IC 1101\", value: 4000000},\r\n      {name: \"Hercules A\", value: 1500000},\r\n      {name: \"A2261-BCG\", value: 1000000},\r\n      {name: \"ESO 306-17\", value: 1000000},\r\n      {name: \"ESO 444-46\", value: 402200},\r\n    ]},\r\n    {name: \"Spiral\", children: [\t\r\n      {name: \"Rubin's Galaxy\", value: 832000},\r\n      {name: \"Comet Galaxy\", value: 600000},\r\n      {name: \"Condor Galaxy\", value: 522000},\r\n      {name: \"Tadpole Galaxy\", value: 280000},\r\n      {name: \"Andromeda Galaxy\", value: 220000} \r\n    ]}\r\n  ]}\r\n];<\/code><\/pre>\n<h3>4. Write Some JS Treemapping Code<\/h3>\n<p>Now, just a few lines of JavaScript code to fuel up our treemap chart.<\/p>\n<p>First, we include the <code>anychart.onDocumentReady()<\/code> function, which will enclose all the JavaScript code of the treemap ensuring it will execute when the web page is fully loaded and ready.<\/p>\n<pre><code class=\"javascript\">&lt;script&gt;\r\n&nbsp;&nbsp;anychart.onDocumentReady(function () {\r\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ The JS treemapping code will be written here.\r\n&nbsp;&nbsp;});\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>Second, we add the data we want to visualize in a treemap chart, from step 3.<\/p>\n<pre><code class=\"html\">&lt;script&gt;\r\n \r\n  anychart.onDocumentReady(function () {\r\n \r\n    var dataSet = [\r\n      {name: \"Galaxies\", children: [\r\n        {name: \"Elliptical\", children: [\r\n          {name: \"IC 1101\", value: 4000000},\r\n          {name: \"Hercules A\", value: 1500000},\r\n          {name: \"A2261-BCG\", value: 1000000},\r\n          {name: \"ESO 306-17\", value: 1000000},\r\n          {name: \"ESO 444-46\", value: 402200},\r\n        ]},\r\n        {name: \"Spiral\", children: [\t\r\n          {name: \"Rubin's Galaxy\", value: 832000},\r\n          {name: \"Comet Galaxy\", value: 600000},\r\n          {name: \"Condor Galaxy\", value: 522000},\r\n          {name: \"Tadpole Galaxy\", value: 280000},\r\n          {name: \"Andromeda Galaxy\", value: 220000} \r\n        ]}\r\n      ]}\r\n    ];\r\n \r\n  });\r\n \r\n&lt;\/script&gt<\/code><\/pre>\n<p>Third, we add the following line to create a treemap from the tree data.<\/p>\n<pre><code class=\"javascript\">var chart = anychart.treeMap(dataSet, \"as-tree\");<\/code><\/pre>\n<p>Finally, we add a title, put the chart in the previously defined\u00a0<code>&lt;div&gt;<\/code>\u00a0container, and display it using the <code>draw<\/code> command.<\/p>\n<pre><code class=\"javascript\">chart.title(\"The 10 Largest Galaxies in the Known Universe\");\r\nchart.container(\"container\");\r\nchart.draw();<\/code><\/pre>\n<p>Now our JS treemap is basically ready, and if we stopped at that, it would look something like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/1-treemap-chart-js-initial.png\" alt=\"Initial JS Treemap\" width=\"100%\" class=\"alignnone size-full wp-image-15061\" srcset=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/1-treemap-chart-js-initial.png 1100w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/1-treemap-chart-js-initial-300x180.png 300w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/1-treemap-chart-js-initial-768x461.png 768w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/1-treemap-chart-js-initial-1024x614.png 1024w\" sizes=\"(max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>There are only two tiles visible immediately as the treemap chart is loaded, \u2018Elliptical\u2019 and \u2018Spiral.\u2019 We can click on those, and they will expand to show their respective children galaxies \u2014 that\u2019s what is called a <a href=\"https:\/\/docs.anychart.com\/Drilldown\" target=\"_blank\" rel=\"nofollow\">drill-down<\/a> action.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/2-treemap-chart-initial.gif\" alt=\"Initial JS Treemap in Animated Demonstration\" width=\"100%\" class=\"alignnone size-full wp-image-15064\" \/><\/p>\n<p>Why is this happening, just two tiles? Because by default, the maximum depth value is set as 1. It means we can see only one level with its parent at a time. The lower levels are hidden. On the first level, we have \u2018Galaxies\u2019 being divided into \u2018Elliptical\u2019 and \u2018Spiral,\u2019 so we only see that by default.<\/p>\n<p>Now, what do we do to display all the galaxy tiles at once? It&#8217;s very simple. We just need to change the maximum depth value using the <code>maxDepth()<\/code> function.<\/p>\n<pre><code class=\"javascript\">chart.maxDepth(2);<\/code><\/pre>\n<p>The countdown is over, and our treemap chart is already now!<\/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-zUBT93xw\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/zUBT93xw\/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-zUBT93xw{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<p>In this chart, we can see how the galaxies are grouped based on hierarchy, and we can also click on the \u2018Elliptical\u2019 or \u2018Spiral\u2019 headers at the top to zoom in on their children&#8217;s galaxies.<\/p>\n<p>Look at the interactive version of this basic JavaScript treemap chart on <a href=\"https:\/\/jsfiddle.net\/awanshrestha\/zL6gbem5\/\" target=\"_blank\" rel=\"nofollow\">JSFiddle<\/a> [or <a href=\"https:\/\/playground.anychart.com\/zUBT93xw\/\" target=\"_blank\" rel=\"nofollow\">AnyChart Playground<\/a>]. Feel free to play around with it. You can also check out the full code below.<\/p>\n<pre><code class=\"html\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&nbsp;&nbsp;&lt;head&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta charset=\"utf-8\"&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;JavaScript Treemap Chart&lt;\/title&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;script data-fr-src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;script data-fr-src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-treemap.min.js\"&gt;&lt;\/script&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;style type=\"text\/css\"&gt;  \t\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html, body, #container {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width: 100%;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height: 100%;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;margin: 0;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;padding: 0;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/style&gt;\r\n&nbsp;&nbsp;&lt;\/head&gt;\r\n&nbsp;&nbsp;&lt;body&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;script&gt;\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;anychart.onDocumentReady(function () {\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create the data\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var dataSet = [\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Galaxies\", children: [\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Elliptical\", children: [\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"IC 1101\", value: 4000000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Hercules A\", value: 1500000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"A2261-BCG\", value: 1000000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"ESO 306-17\", value: 1000000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"ESO 444-46\", value: 402200},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Spiral\", children: [\t\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Rubin's Galaxy\", value: 832000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Comet Galaxy\", value: 600000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Condor Galaxy\", value: 522000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Tadpole Galaxy\", value: 280000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Andromeda Galaxy\", value: 220000} \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;];\r\n        \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/  create the treemap chart and set the data\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var chart = anychart.treeMap(dataSet, \"as-tree\");\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ set the chart title\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.title(\"The 10 Largest Galaxies in the Known Universe\");\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ set the container id for the chart\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.container(\"container\");\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ initiate drawing the chart\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.draw();\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/script&gt;\r\n&nbsp;&nbsp;&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<p>It was quite effortless to create a beautiful JavaScript treemap chart, wasn\u2019t it? Now, you can see at a glance how the scale of the 10 largest galaxies looks and compare them.<\/p>\n<p>The resulting chart already looks very good to me. But, let me show you how you can customize JavaScript treemaps whenever you want more.<\/p>\n<h2>Customize the JS Treemap Chart<\/h2>\n<p>Now, let&#8217;s add some aesthetic and functional changes to make our interactive treemap chart even better and more insightful.<\/p>\n<h3>A. Change Colors<\/h3>\n<p>An easy way to change the look and feel of any chart is to change the color to match the data we are representing. Here we are showing galaxies in the universe. When I think about the universe, I imagine the dark blue and purple clouds of stars in the dark space.<\/p>\n<p>So, let&#8217;s change the color of the tiles. And let\u2019s paint them purple. Also, let\u2019s make them dark blue when selected.<\/p>\n<pre><code class=\"javascript\">chart.normal().fill('#B46FC2');\r\nchart.hovered().fill('#44008B', 0.8);\r\nchart.selected().fill('#0A0068', 0.8);\r\nchart.selected().hatchFill(\"forward-diagonal\", '#282147', 2, 20);<\/code><\/pre>\n<p>Here, we\u2019ve added the <code>fill()<\/code> and <code>hatchFill()<\/code> methods to change the colors of our treemap chart.<\/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-v86V9biQ\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/v86V9biQ\/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-v86V9biQ{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<h3>B. Apply a Linear Color Scale<\/h3>\n<p>In treemaps, not only the size but also the color of the tiles can be helpful in highlighting the proportions. Let\u2019s see a cool way to have the tiles automatically colored based on the corresponding data dimensions with the help of a linear color scale.<\/p>\n<p>We create a linear color scale, provide it with two values, one for the lowest range value and the other for the highest, and finally, enable the color range.<\/p>\n<pre><code class=\"javascript\">var customColorScale = anychart.scales.linearColor();\r\ncustomColorScale.colors(['#37B8F7', '#ffcc00']);\r\nchart.colorScale(customColorScale);\r\nchart.colorRange().enabled(true);\r\nchart.colorRange().length('90%');<\/code><\/pre>\n<p>For these implementations to take place, we need to get rid of the code written in the previous section.<\/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-B0QXYzJg\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/B0QXYzJg\/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-B0QXYzJg{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<p>Here we have light blue for the lowest and yellow for the highest. Play along with the colors.<\/p>\n<h3>C. Format Labels and Tooltips<\/h3>\n<p>We can format our labels with the use of HTML. For that, we need to enable HTML for labels. Then there is no limit to how you can style them using HTML.<\/p>\n<p>To keep it simple, we will format the labels as\u00a0<code>&lt;span&gt;<\/code> HTML elements and style them to increase the font size and change the color.<\/p>\n<pre><code class=\"javascript\">chart.labels().useHtml(true);\r\nchart.labels().format(\r\n  \"&lt;span style='font-size: 24px; color: #00076f'&gt;{%name}&lt;\/span&gt;&lt;br&gt;{%value}\"\r\n);<\/code><\/pre>\n<p>As you can see in the code snippet above, we have also used the <code>{%name}<\/code> and <code>{%value}<\/code> tokens to change the text of treemap labels and tooltips. In this way, the name and the scale value will be output for each galaxy while creating the visualization.<\/p>\n<p>In addition, let\u2019s customize the text of the tooltip using the <code>format()<\/code> method. An informative tooltip can greatly help to understand the data better.<\/p>\n<pre><code class=\"javascript\">chart.tooltip().format(\r\n  \t\"Scale: {%value} light-years\"\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-SaVuNF81\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/SaVuNF81\/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-SaVuNF81{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<p>Our JS-based treemap chart looks much enhanced with the label and tooltip formatting.<\/p>\n<h3>D. Sort Tiles in Ascending Order<\/h3>\n<p>By default, the treemap tiles are in descending order. We can see that the galaxies are sorted from highest to lowest, and the IC 1101 galaxy with the largest scale is the first from the left.<\/p>\n<p>But that does not have to always be the case. Sometimes, we might need to show data in ascending order. Prepare yourself to be amazed to see how easy that is! We do that with one line of code.<\/p>\n<pre><code class=\"javascript\">chart.sort(\"asc\");<\/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-5ZzVNs9c\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/5ZzVNs9c\/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-5ZzVNs9c{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<p>Check out the whole code of this final interactive JavaScript treemap below [and also on <a href=\"https:\/\/playground.anychart.com\/5ZzVNs9c\" target=\"_blank\" rel=\"nofollow\">AnyChart Playground<\/a>]. Feel free to try some experimentation with it.<\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&nbsp;&nbsp;&lt;head&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta charset=\"utf-8\"&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;JavaScript Treemap Chart&lt;\/title&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;script data-fr-src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-core.min.js\"&gt;&lt;\/script&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;script data-fr-src=\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-treemap.min.js\"&gt;&lt;\/script&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;style type=\"text\/css\"&gt;  \t\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;html, body, #container {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width: 100%;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height: 100%;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;margin: 0;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;padding: 0;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/style&gt;\r\n&nbsp;&nbsp;&lt;\/head&gt;\r\n&nbsp;&nbsp;&lt;body&gt;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=\"container\"&gt;&lt;\/div&gt;\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;script&gt;\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;anychart.onDocumentReady(function () {\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create the data\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var dataSet = [\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Galaxies\", children: [\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Elliptical\", children: [\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"IC 1101\", value: 4000000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Hercules A\", value: 1500000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"A2261-BCG\", value: 1000000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"ESO 306-17\", value: 1000000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"ESO 444-46\", value: 402200},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Spiral\", children: [\t\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Rubin's Galaxy\", value: 832000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Comet Galaxy\", value: 600000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Condor Galaxy\", value: 522000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Tadpole Galaxy\", value: 280000},\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{name: \"Andromeda Galaxy\", value: 220000} \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;];\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ create the treemap chart and set the data\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var chart = anychart.treeMap(dataSet, \"as-tree\");\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ set the chart title\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.title(\"The 10 Largest Galaxies in the Known Universe\");\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ set a custom color scale\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var customColorScale = anychart.scales.linearColor();\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;customColorScale.colors(['#37B8F7', '#ffcc00']);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.colorScale(customColorScale);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.colorRange().enabled(true);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.colorRange().length('90%');\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ format the labels\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.labels().useHtml(true);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.labels().format(\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"&lt;span style='font-size: 24px; color: #00076f'&gt;{%name}&lt;\/span&gt;&lt;br&gt;{%value}\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);\r\n  \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ format the tooltips\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.tooltip().format(\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"Scale: {%value} light years\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);\r\n\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ sort in ascending order\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.sort(\"asc\");\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ set the container id for the chart\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.container(\"container\");\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ initiate drawing the chart\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chart.draw();\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});\r\n \r\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/script&gt;\r\n&nbsp;&nbsp;&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>You might not have got the answer to the <em>\u201cAre we alone in the universe?\u201d<\/em> question. But you have learned to create awesome interactive JavaScript treemap charts effortlessly!<\/p>\n<p>Now it&#8217;s your turn to build your own JS-based treemap chart visualization. See the <a href=\"https:\/\/docs.anychart.com\/Basic_Charts\/Treemap_Chart\" target=\"_blank\" rel=\"nofollow\">treemap documentation<\/a> to find out how anything else could be done in addition to what we\u2019ve discussed in this tutorial, or apply the same logic with a different charting library, and certainly don\u2019t hesitate to reach out to me if you need any further help (I\u2019ll do my best).<\/p>\n<p>The universe keeps expanding, and let it also be so with our learnings, knowledge, and creativity.<\/p>\n<hr \/>\n<p><em>Published with the permission of Awan Shrestha. Originally appeared on <a href=\"https:\/\/dzone.com\/articles\/treemap-chart-javascript\" target=\"_blank\" rel=\"nofollow\">DZone<\/a> with the title <em>&#8220;How to Build a Treemap Using JavaScript&#8221;<\/em> on June 1, 2022.<\/p>\n<p>You may also want to see the basic JavaScript <a href=\"https:\/\/www.anychart.com\/blog\/2019\/08\/01\/treemap-chart-create-javascript\/\">Treemap Chart Tutorial<\/a> originally published on our blog.<\/p>\n<p>Check out other <a href=\"https:\/\/www.anychart.com\/blog\/category\/javascript-chart-tutorials\/\">JavaScript charting tutorials<\/a>.<\/p>\n<hr \/>\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>Treemap visualizations are widely used in hierarchical data analysis. If you need to build one but have never done that before, you might think the process is somewhat complicated. Well, not necessarily. I decided to make a step-by-step tutorial explaining how to create awesome interactive treemap charts with ease using JavaScript. And you\u2019re gonna love [&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":22,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,23,13,279,4],"tags":[619,618,53,3173,260,3149,284,127,166,258,471,266,620,1292,880,806,3352,509,2220,2838,54,461,1389,2757,256,1111,744,844,130,165,313,1370,133,774,3369,803,1498,805,3382,2884,2885,2013,2014,32,55,144,167,146,433,152,2949,151,36,907,141,249,81,57,169,170,1238,142,96,99,58,65,56,101,459,30,2224,1221,190,172,3383,807,808,954,293,899,2816,1763,804],"class_list":["post-15051","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-analysis","tag-analytics","tag-anychart","tag-app-development","tag-best-data-visualization-examples","tag-chart-development","tag-chart-examples","tag-chart-types","tag-charting","tag-charts","tag-data-analysis","tag-data-analytics","tag-data-analytics-examples","tag-data-chart","tag-data-charting","tag-data-charts","tag-data-graphic","tag-data-graphics","tag-data-visual","tag-data-visualisation","tag-data-visualization","tag-data-visualization-101","tag-data-visualization-best-pracices","tag-data-visualization-development","tag-data-visualization-examples","tag-data-visualization-practice","tag-data-visualization-techniques","tag-data-visualization-tutorial","tag-data-visualization-weekly","tag-data-visualizations","tag-data-visuals","tag-data-viz-examples","tag-dataviz","tag-dataviz-examples","tag-developers","tag-dzone","tag-example","tag-front-end-development","tag-galaxies","tag-hierarchical-data","tag-hierarchical-data-visualization","tag-html","tag-html-charts","tag-html5","tag-html5-charts","tag-infographics","tag-interactive-charts","tag-interactive-data-visualization","tag-interactive-graphics","tag-interactive-infographic","tag-interactive-infographics","tag-interactive-visualizations","tag-javascript","tag-javascript-chart-tutorial","tag-javascript-charting","tag-javascript-charting-api","tag-javascript-charting-library","tag-javascript-charts","tag-javascript-drawing","tag-javascript-drawing-library","tag-javascript-graph","tag-javascript-graphics","tag-javascript-graphics-library","tag-javascript-library","tag-js-chart","tag-js-charting","tag-js-charts","tag-js-graphics","tag-statistics","tag-tips-and-tricks","tag-tree-data","tag-treemap","tag-treemap-chart","tag-tutorial","tag-universe","tag-visual-analysis","tag-visual-analytics","tag-visual-data-analytics","tag-visualization","tag-visualizations","tag-web-design","tag-web-developers","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>Treemap with JavaScript: Tutorial for Web Developers &amp; Data Enthusiasts<\/title>\n<meta name=\"description\" content=\"Learn what a treemap is and how to easily make it using JavaScript while treemapping the scale of the 10 largest galaxies step by step.\" \/>\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\/2022\/06\/08\/treemap-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Treemap with JavaScript\" \/>\n<meta property=\"og:description\" content=\"Learn what a treemap is &amp; how to easily make it using JS while treemapping the scale of the 10 largest galaxies step by step.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/\" \/>\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=\"2022-06-08T09:02:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-13T11:11:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-char-social.png\" \/>\n<meta name=\"author\" content=\"Awan Shrestha\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Treemap with JavaScript\" \/>\n<meta name=\"twitter:description\" content=\"Learn what a treemap is &amp; how to easily make it using JS while treemapping the scale of the 10 largest galaxies step by step.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-char-social.png\" \/>\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=\"Awan Shrestha\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/\"},\"author\":{\"name\":\"Awan Shrestha\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1\"},\"headline\":\"How to Make Treemap Using JS\",\"datePublished\":\"2022-06-08T09:02:16+00:00\",\"dateModified\":\"2022-08-13T11:11:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/\"},\"wordCount\":1843,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart.png\",\"keywords\":[\"analysis\",\"analytics\",\"AnyChart\",\"app development\",\"best data visualization examples\",\"chart development\",\"chart examples\",\"chart types\",\"charting\",\"charts\",\"data analysis\",\"data analytics\",\"data analytics examples\",\"data chart\",\"data charting\",\"data charts\",\"data graphic\",\"data graphics\",\"data visual\",\"data visualisation\",\"Data Visualization\",\"data visualization 101\",\"data visualization best practices\",\"data visualization development\",\"data visualization examples\",\"data visualization practice\",\"data visualization techniques\",\"data visualization tutorial\",\"data visualization weekly\",\"data visualizations\",\"data visuals\",\"data viz examples\",\"dataviz\",\"dataviz examples\",\"developers\",\"DZone\",\"example\",\"front-end development\",\"galaxies\",\"hierarchical data\",\"hierarchical data visualization\",\"HTML\",\"HTML charts\",\"HTML5\",\"html5 charts\",\"infographics\",\"interactive charts\",\"interactive data visualization\",\"interactive graphics\",\"interactive infographic\",\"interactive infographics\",\"interactive visualizations\",\"JavaScript\",\"javascript chart tutorial\",\"javascript charting\",\"javascript charting api\",\"JavaScript charting library\",\"javascript charts\",\"javascript drawing\",\"javascript drawing library\",\"javascript graph\",\"javascript graphics\",\"JavaScript graphics library\",\"JavaScript library\",\"js chart\",\"js charting\",\"js charts\",\"JS graphics\",\"statistics\",\"Tips and tricks\",\"tree data\",\"treemap\",\"treemap chart\",\"tutorial\",\"universe\",\"visual analysis\",\"visual analytics\",\"visual data analytics\",\"visualization\",\"visualizations\",\"web design\",\"web developers\",\"web development\"],\"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\/2022\/06\/08\/treemap-js\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/\",\"url\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/\",\"name\":\"Treemap with JavaScript: Tutorial for Web Developers & Data Enthusiasts\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart.png\",\"datePublished\":\"2022-06-08T09:02:16+00:00\",\"dateModified\":\"2022-08-13T11:11:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1\"},\"description\":\"Learn what a treemap is and how to easily make it using JavaScript while treemapping the scale of the 10 largest galaxies step by step.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#primaryimage\",\"url\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart.png\",\"contentUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart.png\",\"width\":1200,\"height\":690},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.anychart.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Make Treemap Using JS\"}]},{\"@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\/3f107fdcb3fe326a63cd52812f5287c1\",\"name\":\"Awan Shrestha\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g\",\"caption\":\"Awan Shrestha\"},\"url\":\"https:\/\/www.anychart.com\/blog\/author\/awan-shrestha\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Treemap with JavaScript: Tutorial for Web Developers & Data Enthusiasts","description":"Learn what a treemap is and how to easily make it using JavaScript while treemapping the scale of the 10 largest galaxies step by step.","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\/2022\/06\/08\/treemap-js\/","og_locale":"en_US","og_type":"article","og_title":"Treemap with JavaScript","og_description":"Learn what a treemap is & how to easily make it using JS while treemapping the scale of the 10 largest galaxies step by step.","og_url":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/","og_site_name":"AnyChart News","article_publisher":"https:\/\/www.facebook.com\/AnyCharts","article_published_time":"2022-06-08T09:02:16+00:00","article_modified_time":"2022-08-13T11:11:50+00:00","og_image":[{"url":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-char-social.png","type":"","width":"","height":""}],"author":"Awan Shrestha","twitter_card":"summary_large_image","twitter_title":"Treemap with JavaScript","twitter_description":"Learn what a treemap is & how to easily make it using JS while treemapping the scale of the 10 largest galaxies step by step.","twitter_image":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-char-social.png","twitter_creator":"@AnyChart","twitter_site":"@AnyChart","twitter_misc":{"Written by":"Awan Shrestha","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#article","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/"},"author":{"name":"Awan Shrestha","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1"},"headline":"How to Make Treemap Using JS","datePublished":"2022-06-08T09:02:16+00:00","dateModified":"2022-08-13T11:11:50+00:00","mainEntityOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/"},"wordCount":1843,"commentCount":0,"image":{"@id":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#primaryimage"},"thumbnailUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart.png","keywords":["analysis","analytics","AnyChart","app development","best data visualization examples","chart development","chart examples","chart types","charting","charts","data analysis","data analytics","data analytics examples","data chart","data charting","data charts","data graphic","data graphics","data visual","data visualisation","Data Visualization","data visualization 101","data visualization best practices","data visualization development","data visualization examples","data visualization practice","data visualization techniques","data visualization tutorial","data visualization weekly","data visualizations","data visuals","data viz examples","dataviz","dataviz examples","developers","DZone","example","front-end development","galaxies","hierarchical data","hierarchical data visualization","HTML","HTML charts","HTML5","html5 charts","infographics","interactive charts","interactive data visualization","interactive graphics","interactive infographic","interactive infographics","interactive visualizations","JavaScript","javascript chart tutorial","javascript charting","javascript charting api","JavaScript charting library","javascript charts","javascript drawing","javascript drawing library","javascript graph","javascript graphics","JavaScript graphics library","JavaScript library","js chart","js charting","js charts","JS graphics","statistics","Tips and tricks","tree data","treemap","treemap chart","tutorial","universe","visual analysis","visual analytics","visual data analytics","visualization","visualizations","web design","web developers","web development"],"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\/2022\/06\/08\/treemap-js\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/","url":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/","name":"Treemap with JavaScript: Tutorial for Web Developers & Data Enthusiasts","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#primaryimage"},"image":{"@id":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#primaryimage"},"thumbnailUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart.png","datePublished":"2022-06-08T09:02:16+00:00","dateModified":"2022-08-13T11:11:50+00:00","author":{"@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1"},"description":"Learn what a treemap is and how to easily make it using JavaScript while treemapping the scale of the 10 largest galaxies step by step.","breadcrumb":{"@id":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#primaryimage","url":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart.png","contentUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2022\/06\/treemap-javascript-chart.png","width":1200,"height":690},{"@type":"BreadcrumbList","@id":"https:\/\/www.anychart.com\/blog\/2022\/06\/08\/treemap-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.anychart.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Make Treemap Using JS"}]},{"@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\/3f107fdcb3fe326a63cd52812f5287c1","name":"Awan Shrestha","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g","caption":"Awan Shrestha"},"url":"https:\/\/www.anychart.com\/blog\/author\/awan-shrestha\/"}]}},"_links":{"self":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/15051","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\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/comments?post=15051"}],"version-history":[{"count":17,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/15051\/revisions"}],"predecessor-version":[{"id":15530,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/15051\/revisions\/15530"}],"wp:attachment":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/media?parent=15051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/categories?post=15051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/tags?post=15051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}