Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>d3-voronoi-treemap usage</title>
    <meta name="description" content="d3-voronoi-treemap plugin to remake 'The Costs of Being Fat, in Actual Dollars'">
    <script src="//d3js.org/d3.v4.min.js" charset="utf-8"></script>
    <script src="https://raw.githack.com/Kcnarf/d3-weighted-voronoi/master/build/d3-weighted-voronoi.js"></script>
    <script src="https://rawcdn.githack.com/Kcnarf/d3-voronoi-map/v1.2.0/build/d3-voronoi-map.js"></script>
    <script src="https://rawcdn.githack.com/Kcnarf/d3-voronoi-treemap/v1.1.0/build/d3-voronoi-treemap.js"></script>
    <style>
      svg {
        background-color: #FFFFFF;
      }

      #title {
        letter-spacing: 4px;
        font-weight: 700;
        font-size: x-large;
      }

      text.tiny {
        font-size: 10pt;
      }
      text.light {
        fill: lightgrey
      }

      .world {
        stroke: lightgrey;
        stroke-width: 4px;
      }

      .cell {
        stroke: white;
        stroke-width: 1px;
      }

      .label {
        text-anchor: middle;
        fill: white;
      }

      .label>.name {
        dominant-baseline: text-after-edge;
      }

      .label>.value {
        dominant-baseline: text-before-edge;
      }

      .hoverer {
        fill: transparent;
        stroke: white;
        stroke-width:0px;
      }

      .hoverer:hover {
        stroke-width: 3px;
      }

      .legend-color {
        stroke-width: 1px;
        stroke:darkgrey;
      }
    </style>
  </head>
  <body>
    <svg></svg>

    <script>
      //begin: constants
      var _2PI = 2*Math.PI;
      //end: constants

      //begin: layout conf.
      var svgWidth = 600,
          svgHeight = 600,
          margin = {top: 10, right: 10, bottom: 10, left: 10},
          height = svgHeight - margin.top - margin.bottom,
          width = svgWidth - margin.left - margin.right,
          halfWidth = width/2,
          halfHeight = height/2,
          quarterWidth = width/4,
          quarterHeight = height/4,
          titleY = 20,
          legendsMinY = height - 20,
          treemapRadius = 290,
          treemapCenter = [halfWidth, halfHeight+5];
      //end: layout conf.

      //begin: treemap conf.
      var _voronoiTreemap = d3.voronoiTreemap();
      var hierarchy, circlingPolygon;
      //end: treemap conf.

      //begin: drawing conf.
      var fontScale = d3.scaleLinear();
      //end: drawing conf.

      //begin: reusable d3Selection
      var svg, drawingArea, treemapContainer;
      //end: reusable d3Selection

      var data = {"name": "world",
                  "children": [
                    {
                      "name": "Asia",
                      "color": "#f58321",
                      "children": [
                        {"name": "China", "weight": 30.84, "code": "CN"},
                        {"name": "Japan", "weight": 5.91, "code": "JP"},
                        {"name": "India", "weight": 2.83, "code": "IN"},
                        {"name": "South Korea", "weight": 1.86, "code": "KR"},
                        {"name": "Russia", "weight": 1.8, "code": "RU"},
                        {"name": "Indonesia", "weight": 1.16, "code": "ID"},
                        {"name": "Turkey", "weight": 0.97, "code": "TR"},
                        {"name": "Saudi Arabia", "weight": 0.87, "code": "SA"},
                        {"name": "Iran", "weight": 0.57, "code": "IR"},
                        {"name": "Thaïland", "weight": 0.53, "code": "TH"},
                        {"name": "United Arab Emirates", "weight": 0.5, "code": "AE"},
                        {"name": "Hong Kong", "weight": 0.42, "code": "HK"},
                        {"name": "Israel", "weight": 0.4, "code": "IL"},
                        {"name": "Malasya", "weight": 0.4, "code": "MY"},
                        {"name": "Singapore", "weight": 0.39, "code": "SG"},
                        {"name": "Philippines", "weight": 0.39, "code": "PH"}
                      ]
                    },
                    {
                      "name": "North America",
                      "color": "#ef1621",
                      "children": [
                        {"name": "United States", "weight": 24.32, "code": "US"},
                        {"name": "Canada", "weight": 2.09, "code": "CA"},
                        {"name": "Mexico", "weight": 1.54, "code": "MX"}
                      ]
                    },
                    {
                      "name": "Europe",
                      "color": "#77bc45",
                      "children": [
                        {"name": "Germany", "weight": 4.54, "code": "DE"},
                        {"name": "United Kingdom", "weight": 3.85, "code": "UK"},
                        {"name": "France", "weight": 3.26, "code": "FR"},
                        {"name": "Italy", "weight": 2.46, "code": "IT"},
                        {"name": "Spain", "weight": 1.62, "code": "ES"},
                        {"name": "Netherlands", "weight": 1.01, "code": "NL"},
                        {"name": "Switzerland", "weight": 0.9, "code": "CH"},
                        {"name": "Sweden", "weight": 0.67, "code": "SE"},
                        {"name": "Poland", "weight": 0.64, "code": "PL"},
                        {"name": "Belgium", "weight": 0.61, "code": "BE"},
                        {"name": "Norway", "weight": 0.52, "code": "NO"},
                        {"name": "Austria", "weight": 0.51, "code": "AT"},
                        {"name": "Denmark", "weight": 0.4, "code": "DK"},
                        {"name": "Ireland", "weight": 0.38, "code": "IE"}
                      ]
                    },
                    {
                      "name": "South America",
                      "color": "#4aaaea",
                      "children": [
                        {"name": "Brazil", "weight": 2.39, "code": "BR"},
                        {"name": "Argentina", "weight": 0.79, "code": "AR"},
                        {"name": "Venezuela", "weight": 0.5, "code": "VE"},
                        {"name": "Colombia", "weight": 0.39, "code": "CO"}
                      ]
                    },
                    {
                      "name": "Australia",
                      "color": "#00acad",
                      "children": [
                        {"name": "Australia", "weight": 1.81, "code": "AU"}
                      ]
                    },
                    {
                      "name": "Africa",
                      "color": "#f575a3",
                      "children": [
                        {"name": "Nigeria", "weight": 0.65, "code": "NG"},
                        {"name": "Egypt", "weight": 0.45, "code": "EG"},
                        {"name": "South Africa", "weight": 0.42, "code": "ZA"}
                      ]
                    },
                    {
                      "name": "Rest of the World",
                      "color": "#592c94",
                      "children": [
                        {"name": "Rest of the World", "weight": 9.41, "code": "RotW"}
                      ]
                    }
                  ]
                 };

      var json = JSON.stringify(data);

      initData();
      initLayout(data);

      hierarchy = d3.hierarchy(data).sum(function(d){ return d.weight; });
      _voronoiTreemap
        .clip(circlingPolygon)
      (hierarchy);

      drawTreemap(hierarchy);

      function initData(rootData) {
        circlingPolygon = computeCirclingPolygon(treemapRadius);
        fontScale.domain([3, 20]).range([8, 20]).clamp(true);
      }

      function computeCirclingPolygon(radius) {
        var points = 60,
            increment = _2PI/points,
            circlingPolygon = [];

        for (var a=0, i=0; i<points; i++, a+=increment) {
          circlingPolygon.push(
            [radius + radius*Math.cos(a), radius + radius*Math.sin(a)]
          )
        }

        return circlingPolygon;
      };

      function initLayout(rootData) {
        svg = d3.select("svg")
          .attr("width", svgWidth)
          .attr("height", svgHeight);

        drawingArea = svg.append("g")
          .classed("drawingArea", true)
          .attr("transform", "translate("+[margin.left,margin.top]+")");

        treemapContainer = drawingArea.append("g")
          .classed("treemap-container", true)
          .attr("transform", "translate("+treemapCenter+")");

        treemapContainer.append("path")
          .classed("world", true)
          .attr("transform", "translate("+[-treemapRadius,-treemapRadius]+")")
          .attr("d", "M"+circlingPolygon.join(",")+"Z");
      }

      function drawTreemap(hierarchy) {
        var leaves=hierarchy.leaves();

        var cells = treemapContainer.append("g")
        .classed('cells', true)
        .attr("transform", "translate("+[-treemapRadius,-treemapRadius]+")")
        .selectAll(".cell")
        .data(leaves)
        .enter()
        .append("path")
        .classed("cell", true)
        .attr("d", function(d){ return "M"+d.polygon.join(",")+"z"; })
        .style("fill", function(d){
          return d.parent.data.color;
        });

        var labels = treemapContainer.append("g")
        .classed('labels', true)
        .attr("transform", "translate("+[-treemapRadius,-treemapRadius]+")")
        .selectAll(".label")
        .data(leaves)
        .enter()
        .append("g")
        .classed("label", true)
        .attr("transform", function(d){
          return "translate("+[d.polygon.site.x, d.polygon.site.y]+")";
        })
        .style("font-size", function(d){ return fontScale(d.data.weight); });

        labels.append("text")
          .classed("name", true)
          .html(function(d){
          return (d.data.weight<1)? d.data.code : d.data.name;
        });
        labels.append("text")
          .classed("value", true)
          .text(function(d){ return d.data.weight; });

        var hoverers = treemapContainer.append("g")
        .classed('hoverers', true)
        .attr("transform", "translate("+[-treemapRadius,-treemapRadius]+")")
        .selectAll(".hoverer")
        .data(leaves)
        .enter()
        .append("path")
        .classed("hoverer", true)
        .attr("d", function(d){ return "M"+d.polygon.join(",")+"z"; });

        hoverers.append("title")
          .text(function(d) { return d.data.name + "\n" + d.value; });
      }
    </script>
  </body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console