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 = 'https://cdn.rawgit.com/freeCodeCamp/testable-projects-fcc/a80ce8f9/src/data/tree_map/video-game-sales-data.json'

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

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

// set up chart subtitle
svg.append('text')
   .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 = d3.select('body')
                  .append('div')
                  .attr('class', 'tooltip')
                  .attr('id', 'tooltip')
                  .style('opacity', 0)

// set up color scale
const fade = color => d3.interpolateRgb(color, '#fff')(0.4)
// colors from https://goo.gl/iiLvva
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])
             .paddingInner(1)

// 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)
  treemapLayout(root)
  
  
  // set up each 'rect' (node)
  const nodes = svg.selectAll('g')
                   .data(root.leaves())
                   .enter()
                   .append('g')
                   .attr('transform', d => `translate(${d.x0}, ${d.y0})`)
  
  nodes.append('rect')
       .attr('width', d => d.x1 - d.x0)
       .attr('height', d => d.y1 - d.y0)
       .attr('fill', d => color(d.data.category))
       .attr('class', 'tile')
       .attr('data-name', d => d.data.name)
       .attr('data-category', d => d.data.category)
       .attr('data-value', d => d.data.value)
       // set up mouseover to modify tooltip div
       .on('mouseover', (d, i) => {
         tooltip.transition()
                .duration(200)
                .style('opacity', 0.9)
         tooltip.html(`
                 Name: ${d.data.name}<br />
                 Category: ${d.data.category}<br />
                 Value: ${d.data.value}
                `)
                .attr('data-value', d.data.value)
                .style('left', (d3.event.pageX) + 20 + 'px')
                .style('top', (d3.event.pageY) + 'px')
       })
       // set up mouseout to modify tooltip div
       .on('mouseout', d => {
         tooltip.transition()
                .duration(200)
                .style('opacity', 0)
       })
  
  // set up text labels for each 'rect'
  nodes.append('text')
       .attr('class', (d, i) => {
         // if (i <= 0) {
         //   return 'label-hide' // hide lables for parent nodes
         // }
         return 'label'
       })
       .selectAll('tspan')
       .data(d => d.data.name.split(' ')) // split text on spaces for word wrap and add tspan for each word
       .enter()
       .append('tspan')
       .attr('x', 4)
       .attr('y', (d, i) => 13 + i * 10)
       .text(d => d)
  
  // get all gaming platforms
  let platforms = root.leaves().map(node => node.data.category) // 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
  
  legend.selectAll('rect')
        .data(platforms)
        .enter()
        .append('rect')
        .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))
  
  legend.selectAll('text')
        .data(platforms)
        .enter()
        .append('text')
        .attr('x', (d, i) => i * (sqWidth + 15) + 35)
        .attr('y', h + 100)
        .style('text-anchor', 'middle')
        .text(d => d)
  
})
            
          
!
999px
🕑 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.

Console