Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <h1 id="title">Top selling video games</h1>
<h2 id="description">Top 100 grouped by platform</h2>
<svg id="treemap"></svg>
<svg id="legend"></svg>
              
            
!

CSS

              
                body {
  text-align: center;
  font-family: monospace
}

svg {
  display: block;
  margin: 20px auto;
}

.tooltip {
  background: #e6e5e0;
  color: #000000;
  min-width: 200px;
  height: 40px;
  padding: 10px 20px;
  position: absolute;
  display: flex;
  align-items: center;
}
              
            
!

JS

              
                const GAME_DATA = "https://cdn.freecodecamp.org/testable-projects-fcc/data/tree_map/video-game-sales-data.json"

const HEIGHT = 720
const WIDTH = 1200

fetch(GAME_DATA)
  .then(res => res.json())
  .then(data => drawGraph(data))

function drawGraph(data) {    
  let body = d3.select("body")
  
  let tooltip = body.append("div")
    .attr("class", "tooltip")
    .attr("id", "tooltip")
    .style("opacity", 0)
  
  let svg = d3.select("#treemap")
    .attr("height", HEIGHT)
    .attr("width", WIDTH)
  
  let hierarchy = d3.hierarchy(data, node => {
    return node['children']
  }).sum(node => {
    return node['value']
  }).sort((node1, node2) => {
    return node2['value'] - node1['value']
  })
  
  let createTreemap = d3.treemap()
    .size([WIDTH, HEIGHT])
  
  createTreemap(hierarchy)
  
  let gameData = hierarchy.leaves()
  // console.log(gameData)
  
  let cell = svg.selectAll('g')
    .data(gameData)
    .enter()
    .append('g')
    .attr('transform', game => `translate(${game['x0']}, ${game['y0']})`)
  
  cell.append('rect')
    .attr('class', 'tile')
    .attr('width', game => game['x1'] - game['x0'])
    .attr('height', game => game['y1'] - game['y0'])
    .attr('fill', game => {
      let category = game['data']['category']
      return colourSelect(category)
    })
    .attr("data-name", game => game['data']['name'])
    .attr("data-category", game => game['data']['category'])
    .attr("data-value", game => game['data']['value'])
    .attr('stroke', "#fff")
    .on('mousemove', (event, game) => {
      tooltip.style('opacity', 0.9)
        .attr('data-value', game['data']['value'])
        .text(`${game['data']['name']}: ${game['data']['value']}m units`)
        .style('left', event.pageX + 10 + 'px')
        .style('top', event.pageY - 28 + 'px')
    })
    .on('mouseout', (event, data) => {
      tooltip.style('opacity', 0)
        .style('left', "-200px")
        .style('top', "-200px")
    })
  
    cell.append('text')
      .text(game => game['data']['name'])
      .attr('x', 5)
      .attr('y', 10)
      .style('font-size', '8px')
      .style('fill', "#fff") 
  
  let legend = d3.select("#legend")
    .attr('width', 1200)
    .attr('height', 300)
  
  let legendCounter = 0
  
  data.children.forEach(child => {
    let legendItem = legend.append('g')
      .attr('transform', `translate(${legendCounter}, 0)`)
    
    legendCounter += 62
    
    legendItem.append('rect')
      .attr('width', 15)
      .attr('height', 15)
      .attr('fill', colourSelect(child.name))
      .attr('class', 'legend-item')
    
    legendItem.append('text').text(child.name)
      .attr('x', 20)
      .attr('y', 12)
      .attr('width', 200)
      .attr('height', 50)
      .style('font-size', '10px')
  })
}

function colourSelect(consoleName) {
  switch(consoleName) {
    case "Wii":
      return "#6e3b6a"
      break
    case "GB":
      return "#e8cb8e"
      break
    case "SNES":
      return "#ba783d"
      break
    case "PS":
      return "#4b5461"
      break
    case "2600":
      return "#000000"
      break
    case "PS2":
      return "#823b3b"
      break
    case "PS3":
      return "#40263c"
      break
    case "PS4":
      return "#7d5540"
      break
    case "PSP":
      return "#7d5540"
      break
    case "GBA":
      return "#e2e697"
      break
    case "XB":
      return "#3c6347"
      break
    case "XOne":
      return "#3c3045"
      break
    case "X360":
      return "#9abd31"
      break
    case "NES":
      return "#355a7a"
      break
    case "DS":
      return "#97a9c4"
      break
    case "3DS":
      return "#8db3b2"
      break
    case "N64":
      return "#e3deac"
      break
    case "PC":
      return "#a17886"
      break
  }
}
              
            
!
999px

Console