{"id":496,"date":"2012-12-16T09:26:27","date_gmt":"2012-12-16T14:26:27","guid":{"rendered":"http:\/\/billdwhite.com\/?p=496"},"modified":"2012-12-16T09:26:27","modified_gmt":"2012-12-16T14:26:27","slug":"d3-treemap-with-title-headers","status":"publish","type":"post","link":"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/","title":{"rendered":"d3 Treemap with Title Headers"},"content":{"rendered":"<p>The <a href=\"https:\/\/github.com\/mbostock\/d3\">D3 library<\/a> is really a great way to create data visualizations in HTML5. I absolutely love it and its <a href=\"https:\/\/github.com\/mbostock\/d3\/wiki\/Treemap-Layout\">treemap<\/a> is especially powerful. I wanted to create a treemap that showed grouping headers like the JIT version that has been around for a while, but it took a little more work to get D3 to do the same thing. I <a href=\"https:\/\/groups.google.com\/forum\/#!msg\/d3-js\/TO5xjTDgjBY\/x3JYN2yHWJkJ\">came across a post<\/a> that showed the attempts of others to create the same thing and I wanted to show the code of how I did it in case others are interested.<\/p>\n<p>I created two versions: the first uses SVG labels which are nice because you can measure them to decide what room is available for displaying a label in a given cell. However, wrapping is much more involved and ended up creating a second version which uses embedded HTML divs to create self wrapping labels which seem to look a little better. I also modified the examples of others in the thread so instead of showing labels where there is enough room to see them, I instead took the approach of showing labels for given group when that group was selected.<\/p>\n<p>(NOTE: The labels do not work in IE in the first and third demos because IE is a horrible browser. I could write checks to use svg elements instead of foreignobjects for the labels but you get what you need from the second demo if you want IE compatibility.)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>So here is the first example which uses divs for the labels: (<a href=\"\/wordpress\/wp-content\/js\/treemap_headers_01.html\">link to code<\/a>) (<a href=\"http:\/\/bl.ocks.org\/4325239\">bl.ocks.org<\/a>)<\/p>\n<p>&nbsp;<\/p>\n<div><iframe loading=\"lazy\" style=\"border: 2px solid #444444; border-radius: 3px;\" src=\"\/wordpress\/wp-content\/js\/treemap_headers_01.html\" height=\"550\" width=\"550\" scrolling=\"no\"><\/iframe><\/div>\n<p>\n&nbsp;<\/p>\n<p>&#8230;and here is the second version which uses SVG text elements: (<a href=\"\/wordpress\/wp-content\/js\/treemap_headers_02.html\">link to code<\/a>) (<a href=\"http:\/\/bl.ocks.org\/4325246\">bl.ocks.org<\/a>)<\/p>\n<p><iframe loading=\"lazy\" style=\"border: 2px solid #444444; border-radius: 3px;\" src=\"\/wordpress\/wp-content\/js\/treemap_headers_02.html\" height=\"550\" width=\"550\" scrolling=\"no\"><\/iframe>&nbsp;<br \/>\n<br \/>\nUPDATE:<br \/>\nHere is the third version which uses IDs instead of the name so duplicates titles will work (notice the two &#8216;display&#8217; groups on the left side): (<a href=\"\/wordpress\/wp-content\/js\/treemap_headers_03.html\">link to code<\/a>)<\/p>\n<p><iframe loading=\"lazy\" style=\"border: 2px solid #444444; border-radius: 3px;\" src=\"\/wordpress\/wp-content\/js\/treemap_headers_03.html\" height=\"550\" width=\"550\" scrolling=\"no\"><\/iframe>&nbsp;<br \/>\n<br \/>\nUPDATE #2:<br \/>\nIn the comments for this post I received a request to show how one might highlight the children on mouseover. I&#8217;ve modified one of the above examples to demonstrate this:<\/p>\n<p><iframe loading=\"lazy\" style=\"border: 2px solid #444444; border-radius: 3px;\" src=\"\/wordpress\/wp-content\/js\/treemap_headers_04.html\" height=\"550\" width=\"550\" scrolling=\"no\"><\/iframe><\/p>\n<p>Basically, I just added code to modify the child blocks on mouseover like this:<\/p>\n<pre lang=\"javascript\">    var childEnterTransition = childrenCells.enter()\n        .append(\"g\")\n        .attr(\"class\", \"cell child\")\n        .on(\"click\", function(d) {\n            zoom(node === d.parent ? root : d.parent);\n        })\n        .on(\"mouseover\", function() {\n            this.parentNode.appendChild(this); \/\/ workaround for bringing elements to the front (ie z-index)\n            d3.select(this)\n                .attr(\"filter\", \"url(#outerDropShadow)\")\n                .select(\".background\")\n                .style(\"stroke\", \"#000000\");\n        })\n        .on(\"mouseout\", function() {\n            d3.select(this)\n                .attr(\"filter\", \"\")\n                .select(\".background\")\n                .style(\"stroke\", \"#FFFFFF\");\n        });<\/pre>\n<p>I added an SVG filter (in the section) and just modified the background border stroke of each child on mouseover. I also called appendChild on the parent container to make sure the moused-over child had the highest z-index within the groups of child nodes, otherwise the filtering gets clipped by any child added after the current child. <a title=\"Gist showing treemap with child hovering\" href=\"https:\/\/gist.github.com\/billdwhite\/7028521\" target=\"_blank\">Here is a Gist showing the code.<\/a><\/p>\n<p>UPDATE: #3:<br \/>\nI received a question about how to space out the parent\/child groups so I put this demo together to show one possible avenue to implement this.  I essentially increased the treemap layout padding and then used the d.dy value to size the parent background to occupy the container&#8217;s full height rather than just the header height.  (It is not necessary for this demo but you might need to sort your nodes to make sure the parent nodes are added before the child nodes and in the correct order to avoid having larger squares covering smaller ones.).  To see the changes you can pull the source code for all of these treemap demos <a href=\"https:\/\/gist.github.com\/billdwhite\/605b0178d301e0e8b2e3\">here<\/a> and then do a diff between treemap_header_01.html and treemap_header_05.html to see the changes made to implement this. (the gist shows the treemap_header_02.html demo first so scroll down to find the source for the others)<\/p>\n<p><iframe loading=\"lazy\" style=\"border: 2px solid #444444; border-radius: 3px;\" src=\"\/wordpress\/wp-content\/js\/treemap_headers_05.html\" height=\"550\" width=\"550\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The D3 library is really a great way to create data visualizations in HTML5. I absolutely love it and its treemap is especially powerful. I wanted to create a treemap that showed grouping headers like the JIT version that has been around for a while, but it took a little more work to get D3\u2026 <span class=\"read-more\"><a href=\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,7,9],"tags":[],"class_list":["post-496","post","type-post","status-publish","format-standard","hentry","category-d3","category-html5","category-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>d3 Treemap with Title Headers - Bill White<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"d3 Treemap with Title Headers - Bill White\" \/>\n<meta property=\"og:description\" content=\"The D3 library is really a great way to create data visualizations in HTML5. I absolutely love it and its treemap is especially powerful. I wanted to create a treemap that showed grouping headers like the JIT version that has been around for a while, but it took a little more work to get D3\u2026 Read More &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/\" \/>\n<meta property=\"og:site_name\" content=\"Bill White\" \/>\n<meta property=\"article:published_time\" content=\"2012-12-16T14:26:27+00:00\" \/>\n<meta name=\"author\" content=\"Bill White\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bill_d_white\" \/>\n<meta name=\"twitter:site\" content=\"@bill_d_white\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bill White\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/\"},\"author\":{\"name\":\"Bill White\",\"@id\":\"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/ea6b87554d0eed13a0152765dd01d314\"},\"headline\":\"d3 Treemap with Title Headers\",\"datePublished\":\"2012-12-16T14:26:27+00:00\",\"dateModified\":\"2012-12-16T14:26:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/\"},\"wordCount\":583,\"commentCount\":101,\"publisher\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/ea6b87554d0eed13a0152765dd01d314\"},\"articleSection\":[\"d3\",\"HTML5\",\"Javascript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/\",\"url\":\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/\",\"name\":\"d3 Treemap with Title Headers - Bill White\",\"isPartOf\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/#website\"},\"datePublished\":\"2012-12-16T14:26:27+00:00\",\"dateModified\":\"2012-12-16T14:26:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/billdwhite.com\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"d3 Treemap with Title Headers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/billdwhite.com\/wordpress\/#website\",\"url\":\"https:\/\/billdwhite.com\/wordpress\/\",\"name\":\"Bill White's Blog\",\"description\":\"UI Development and Data Visualization:  Angular \/ React \/ D3 \/ Typescript \/ Javascript \/ UI \/ UX \/ Etc\",\"publisher\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/ea6b87554d0eed13a0152765dd01d314\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/billdwhite.com\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/ea6b87554d0eed13a0152765dd01d314\",\"name\":\"Bill White\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3c3595ee8305a186eea4ea5286143893?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3c3595ee8305a186eea4ea5286143893?s=96&d=mm&r=g\",\"caption\":\"Bill White\"},\"logo\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/www.billdwhite.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"d3 Treemap with Title Headers - Bill White","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:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/","og_locale":"en_US","og_type":"article","og_title":"d3 Treemap with Title Headers - Bill White","og_description":"The D3 library is really a great way to create data visualizations in HTML5. I absolutely love it and its treemap is especially powerful. I wanted to create a treemap that showed grouping headers like the JIT version that has been around for a while, but it took a little more work to get D3\u2026 Read More &raquo;","og_url":"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/","og_site_name":"Bill White","article_published_time":"2012-12-16T14:26:27+00:00","author":"Bill White","twitter_card":"summary_large_image","twitter_creator":"@bill_d_white","twitter_site":"@bill_d_white","twitter_misc":{"Written by":"Bill White","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/#article","isPartOf":{"@id":"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/"},"author":{"name":"Bill White","@id":"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/ea6b87554d0eed13a0152765dd01d314"},"headline":"d3 Treemap with Title Headers","datePublished":"2012-12-16T14:26:27+00:00","dateModified":"2012-12-16T14:26:27+00:00","mainEntityOfPage":{"@id":"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/"},"wordCount":583,"commentCount":101,"publisher":{"@id":"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/ea6b87554d0eed13a0152765dd01d314"},"articleSection":["d3","HTML5","Javascript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/","url":"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/","name":"d3 Treemap with Title Headers - Bill White","isPartOf":{"@id":"https:\/\/billdwhite.com\/wordpress\/#website"},"datePublished":"2012-12-16T14:26:27+00:00","dateModified":"2012-12-16T14:26:27+00:00","breadcrumb":{"@id":"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/billdwhite.com\/wordpress\/2012\/12\/16\/d3-treemap-with-title-headers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/billdwhite.com\/wordpress\/"},{"@type":"ListItem","position":2,"name":"d3 Treemap with Title Headers"}]},{"@type":"WebSite","@id":"https:\/\/billdwhite.com\/wordpress\/#website","url":"https:\/\/billdwhite.com\/wordpress\/","name":"Bill White's Blog","description":"UI Development and Data Visualization:  Angular \/ React \/ D3 \/ Typescript \/ Javascript \/ UI \/ UX \/ Etc","publisher":{"@id":"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/ea6b87554d0eed13a0152765dd01d314"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/billdwhite.com\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/ea6b87554d0eed13a0152765dd01d314","name":"Bill White","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3c3595ee8305a186eea4ea5286143893?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3c3595ee8305a186eea4ea5286143893?s=96&d=mm&r=g","caption":"Bill White"},"logo":{"@id":"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/www.billdwhite.com"]}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/posts\/496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/comments?post=496"}],"version-history":[{"count":0,"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/posts\/496\/revisions"}],"wp:attachment":[{"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/media?parent=496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/categories?post=496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/tags?post=496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}