Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              html { height: 100%; }
body {
  align-items: center;
  background-color: #1D1E22;
  display: flex;
  justify-content: center;
  height: 100%;

svg {
  background-color: #fff;
  margin: 20px;
  padding: 150px 20px 150px 20px;

div.tooltip {
  background: rgba(112, 127, 140, .85);
  border: none;
  border-radius: 2px;
  color: #fff;
  font-size: 18px;
  padding: 10px 20px;
  pointer-events: none;
  position: absolute;
  text-align: center;

rect {
  stroke: white;

#title {
  font-size: 24px;

.label-hide {
  display: none;

.label {
  font-size: 10px;
              const salesData = ''

// set up svg
const w = 1000
const h = 1000
const svg ='body')
              .attr('width', w)
              .attr('height', h)

// set up chart title
   .attr('transform', `translate(${w/2}, -60)`)
   .attr('class', 'title')
   .attr('id', 'title')
   .style('text-anchor', 'middle')
   .text('Video Game Sales')

// set up chart subtitle
   .attr('transform', `translate(${w/2}, -30)`)
   .attr('class', 'description')
   .attr('id', 'description')
   .style('text-anchor', 'middle')
   .text('Top 100 Most Sold Video Games Grouped by Platform')

// append div for use as tooltip
const tooltip ='body')
                  .attr('class', 'tooltip')
                  .attr('id', 'tooltip')
                  .style('opacity', 0)

// set up color scale
const fade = color => d3.interpolateRgb(color, '#fff')(0.4)
// colors from
const color = d3.scaleOrdinal(['#e6194B', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#42d4f4', '#f032e6', '#bfef45', '#fabebe', '#469990', '#e6beff', '#9A6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#a9a9a9', '#ffffff', '#000000'].map(fade))

// set layout to treemap
const treemapLayout = d3.treemap()

// configure treemap layout
treemapLayout.size([w, h])

// fetch data
d3.json(salesData, { crossOrigin: 'anonymous' }).then(data => {
  // construct root node from hierarchical data 
  const root = d3.hierarchy(data)
  // before applying layout to hierarchy, run .sum() on hierarchy
  // (traverses tree to set .value of each node to sum of children)
  root.sum(d => d.value)
  // sort heierarchy from largest to smallest
  root.sort((a, b) => b.height - a.height || b.value - a.value)
  // call treemapLayout and pass in hierarchy object
  // (layout adds 4 properties: x0, x1, y0, y1)
  // set up each 'rect' (node)
  const nodes = svg.selectAll('g')
                   .attr('transform', d => `translate(${d.x0}, ${d.y0})`)
       .attr('width', d => d.x1 - d.x0)
       .attr('height', d => d.y1 - d.y0)
       .attr('fill', d => color(
       .attr('class', 'tile')
       .attr('data-name', d =>
       .attr('data-category', d =>
       .attr('data-value', d =>
       // set up mouseover to modify tooltip div
       .on('mouseover', (d, i) => {
                .style('opacity', 0.9)
                 Name: ${}<br />
                 Category: ${}<br />
                 Value: ${}
                .style('left', (d3.event.pageX) + 20 + 'px')
                .style('top', (d3.event.pageY) + 'px')
       // set up mouseout to modify tooltip div
       .on('mouseout', d => {
                .style('opacity', 0)
  // set up text labels for each 'rect'
       .attr('class', (d, i) => {
         // if (i <= 0) {
         //   return 'label-hide' // hide lables for parent nodes
         // }
         return 'label'
       .data(d =>' ')) // split text on spaces for word wrap and add tspan for each word
       .attr('x', 4)
       .attr('y', (d, i) => 13 + i * 10)
       .text(d => d)
  // get all gaming platforms
  let platforms = root.leaves().map(node => // use .leaves() to reaturn array of leaf nodes in traversal order (leaves are nodes with no children)
  platforms = platforms.filter((platform, index, platforms) => platforms.indexOf(platform) === index)
  // set up legend
  const legend = svg.append('g')
                    .attr('id', 'legend')
                    .attr('class', 'legend')
                    .attr('transform', `translate(0, 0)`)
  // add legend squares
  const sqWidth = 40
  const sqHeight = 40
        .attr('width', sqWidth)
        .attr('height', sqHeight)
        .attr('x', (d, i) => i * (sqWidth + 15) + 13)
        .attr('y', h + 40)
        .attr('class', 'legend-item')
        .style('fill', (d, i) => color(d))
        .attr('x', (d, i) => i * (sqWidth + 15) + 35)
        .attr('y', h + 100)
        .style('text-anchor', 'middle')
        .text(d => d)
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.