{"id":703,"date":"2013-08-15T18:41:19","date_gmt":"2013-08-15T18:41:19","guid":{"rendered":"http:\/\/billdwhite.com\/?p=703"},"modified":"2013-08-15T18:41:19","modified_gmt":"2013-08-15T18:41:19","slug":"d3-treemap-label-truncation","status":"publish","type":"post","link":"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/","title":{"rendered":"d3 Treemap Label Truncation"},"content":{"rendered":"<p>I received a comment asking how to truncate labels in my treemap example.  Here is a quick example showing how I handled it on a recent project.  I am sure there are other ways to do this and if anyone has a better alternative, please detail it in the comments.  Basically, when using labels in D3, I find it easier to work with HTML DIVs rather than the SVG Text nodes.  So, using the ForeignObject element within the SVG, I add a DIV containing the label and I can control the wrapping, truncation, etc using styling.  Internet Explorer, as usual, is years behind the other modern browsers so you have to &#8220;speak slowly&#8221; when IE is detected and dumb things down a bit so it can keep up. I basically have a little function that determines the length of the label text and if it exceeds the width of the treemap rectangle, I truncate it manually.  See the <a href=\"http:\/\/bl.ocks.org\/billdwhite\/6243279\" title=\"Treemap with truncating labels\">Gist<\/a> for the code and see the demo below.  (Keep in mind that I have not taken the time to deal with the parent group labels so you will see some over lap from those)<br \/>\nNOTE: The labels do not show up in IE10 even though they work in IE9. Big surprise. Maybe one day Microsoft will make a browser that works. I&#8217;m sure I could dig around and figure out why IE cannot handle these simple labels but until then, just pretend.<br \/>\n<center><\/p>\n<div><iframe loading=\"lazy\" src=\"\/wordpress\/wp-content\/js\/treemap_labels_01.html\" style=\"border: 2px solid #444444; border-radius: 3px;\" height=\"549\" width=\"549\" scrolling=\"no\"><\/iframe>\n<\/div>\n<p><\/center>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I received a comment asking how to truncate labels in my treemap example. Here is a quick example showing how I handled it on a recent project. I am sure there are other ways to do this and if anyone has a better alternative, please detail it in the comments. Basically, when using labels in\u2026 <span class=\"read-more\"><a href=\"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/\">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,1],"tags":[],"class_list":["post-703","post","type-post","status-publish","format-standard","hentry","category-d3","category-html5","category-javascript","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>d3 Treemap Label Truncation - 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\/2013\/08\/15\/d3-treemap-label-truncation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"d3 Treemap Label Truncation - Bill White\" \/>\n<meta property=\"og:description\" content=\"I received a comment asking how to truncate labels in my treemap example. Here is a quick example showing how I handled it on a recent project. I am sure there are other ways to do this and if anyone has a better alternative, please detail it in the comments. Basically, when using labels in\u2026 Read More &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/\" \/>\n<meta property=\"og:site_name\" content=\"Bill White\" \/>\n<meta property=\"article:published_time\" content=\"2013-08-15T18:41:19+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/\"},\"author\":{\"name\":\"Bill White\",\"@id\":\"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/ea6b87554d0eed13a0152765dd01d314\"},\"headline\":\"d3 Treemap Label Truncation\",\"datePublished\":\"2013-08-15T18:41:19+00:00\",\"dateModified\":\"2013-08-15T18:41:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/\"},\"wordCount\":246,\"commentCount\":0,\"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\/2013\/08\/15\/d3-treemap-label-truncation\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/\",\"url\":\"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/\",\"name\":\"d3 Treemap Label Truncation - Bill White\",\"isPartOf\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/#website\"},\"datePublished\":\"2013-08-15T18:41:19+00:00\",\"dateModified\":\"2013-08-15T18:41:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/billdwhite.com\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"d3 Treemap Label Truncation\"}]},{\"@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 Label Truncation - 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\/2013\/08\/15\/d3-treemap-label-truncation\/","og_locale":"en_US","og_type":"article","og_title":"d3 Treemap Label Truncation - Bill White","og_description":"I received a comment asking how to truncate labels in my treemap example. Here is a quick example showing how I handled it on a recent project. I am sure there are other ways to do this and if anyone has a better alternative, please detail it in the comments. Basically, when using labels in\u2026 Read More &raquo;","og_url":"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/","og_site_name":"Bill White","article_published_time":"2013-08-15T18:41:19+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":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/#article","isPartOf":{"@id":"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/"},"author":{"name":"Bill White","@id":"https:\/\/billdwhite.com\/wordpress\/#\/schema\/person\/ea6b87554d0eed13a0152765dd01d314"},"headline":"d3 Treemap Label Truncation","datePublished":"2013-08-15T18:41:19+00:00","dateModified":"2013-08-15T18:41:19+00:00","mainEntityOfPage":{"@id":"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/"},"wordCount":246,"commentCount":0,"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\/2013\/08\/15\/d3-treemap-label-truncation\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/","url":"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/","name":"d3 Treemap Label Truncation - Bill White","isPartOf":{"@id":"https:\/\/billdwhite.com\/wordpress\/#website"},"datePublished":"2013-08-15T18:41:19+00:00","dateModified":"2013-08-15T18:41:19+00:00","breadcrumb":{"@id":"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/billdwhite.com\/wordpress\/2013\/08\/15\/d3-treemap-label-truncation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/billdwhite.com\/wordpress\/"},{"@type":"ListItem","position":2,"name":"d3 Treemap Label Truncation"}]},{"@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\/703","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=703"}],"version-history":[{"count":0,"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/posts\/703\/revisions"}],"wp:attachment":[{"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/media?parent=703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/categories?post=703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/billdwhite.com\/wordpress\/wp-json\/wp\/v2\/tags?post=703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}