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

              
                <div id='header'>
  <h1 id="title">A big ol Graph</h1>
  <p id='description'>A big looooooooooooooonnnnnggg description</p>
  <div class='data_select'>
    <button class='data_select-button data_select_0 active' onClick='changeData(0)'>
      Kickstarter Data
    </button>
    <button class='data_select-button data_select_1' onClick='changeData(1)'>
      Movie Data
    </button>
    <button class='data_select-button data_select_2' onClick='changeData(2)'>
      Video Game Data
    </button>
  </div>
</div>
<div id='root'></div>
<div id='source'></div>
<div id='legend'></div>

<!-- <script src='https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js'></script> -->
              
            
!

CSS

              
                * {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

#header {
  text-align: center;
}

#root {
  margin: auto;
  position: relative;
  width: 960px;
/*   border: 3px dashed tomato; */
}

#legend {
  text-align: center;
  margin-top: 20px;
}

g {
  overflow: hidden;
}

.tile {
  border: 1px solid white;
  font: 10px sans-serif;
  line-height: 12px;
  overflow: hidden;
  position: absolute;
  text-indent: 2px;
  background: steelblue;
}

text {
  cursor: default;
}

.tooltip_text {
  fill: black;
  font-size: 0.8em;
}

button {
  margin: 0px 5px 20px 5px;
  font-size: 1.1em;
  padding: 5px 10px;
  cursor: pointer;
  background-color: #fff;
  border: 2px solid #29B6F6;
  border-radius: 10px;
  transition: 0.2s linear;
  box-shadow: 5px 5px 10px rgba(0,0,0,0.1);
}

button:hover {
  background-color: #B2E8F7;
}

button:active, button.active:active {
  background-color: #42A5F5;
  color: #fff;
  box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
}

button.active {
  background-color: #64B5F6;
  color: #ecf0f1;
  box-shadow: 3px 3px 10px rgba(0,0,0,0.1);
}

#source {
  font-size: 1em;
  text-align: center;
  color: #bec3c7;
  padding: 10px;
}
              
            
!

JS

              
                console.clear();

const kickstarter_url = "https://cdn.rawgit.com/freeCodeCamp/testable-projects-fcc/a80ce8f9/src/data/tree_map/kickstarter-funding-data.json"
const movie_url = "https://cdn.rawgit.com/freeCodeCamp/testable-projects-fcc/a80ce8f9/src/data/tree_map/movie-data.json"
const games_url = "https://cdn.rawgit.com/freeCodeCamp/testable-projects-fcc/a80ce8f9/src/data/tree_map/video-game-sales-data.json"

const data_arr = [kickstarter_url, movie_url, games_url];
var data_selected = 0
const headers = [
  {name: 'Kickstarter Projects', desc: 'The top 100 pledged Kickstarter projects'},
  {name: 'Movie Sales', desc: 'The top videogames by sales, aranged by genre'},
  {name: 'Video Game Sales', desc: 'The most popular movies by sales, aranged by genre'}
]

function render() {
  console.log('rendering...')
  
$.getJSON(data_arr[data_selected], data => {
  console.log("=========================")
  
  const width   = 960,
        height  = 500;

  const color = d3.scaleOrdinal().range(d3.schemeCategory20c)

  const treemap = d3.treemap().size([width, height])//.paddingOuter(5).paddingInner(3);
  
  d3.select('#title').text(headers[data_selected].name)
  d3.select('#description').text(headers[data_selected].desc)
  d3.select('#source').text('Source: ' + data_arr[data_selected])
  
  d3.select('#root')
    .selectAll('*')
    .remove()

  const svg = d3.select('#root')
                 .append('svg')
                 .style('position', 'relative')
                 .style('width', width + 'px')
                 .style('height', height + 'px')

  const root = d3.hierarchy(data, d => d.children)
                 .sum(d => d.value);

  const tree = treemap(root)

  const node = svg.datum(root)
                  .selectAll('.node')
                  .data(tree.leaves())
                  .enter()
                  .append('g')
  
                  .on('mouseover', function (d) {
                    tooltip
                     .style('opacity', 1)//.attr('display', null)
                    tooltip.attr('data-value', d.data.value)
                    var tooltip_width = Math.max(d.data.name.length * 8, `Value: ${d.data.value}`.length * 8)
                    
                    var x = d3.mouse(this)[0] > width - 200 ? 
                                d3.mouse(this)[0] - 25 - tooltip_width : 
                                d3.mouse(this)[0] + 25;
                    
                    var y =  d3.mouse(this)[1] > height - 200 ? 
                                d3.mouse(this)[1] - 100 : 
                                d3.mouse(this)[1];
                    
                    tooltip.attr('transform', `translate(${x}, ${y})`)
                    tooltip_name.text(d.data.name)
                    tooltip_catt.text(`Category: ${d.data.category}`)
                    tooltip_value.text(`Value: ${d.data.value}`)
                    tooltip_back.attr('width', tooltip_width)
                  })
                  .on('mousemove', function (d) {
                    var tooltip_width = Math.max(d.data.name.length * 8, `Value: ${d.data.value}`.length * 8)
                    
                    var x = d3.mouse(this)[0] > width - 200 ? 
                                d3.mouse(this)[0] - 25 - tooltip_width : 
                                d3.mouse(this)[0] + 25;
                    
                    var y =  d3.mouse(this)[1] > height - 70 ? 
                                d3.mouse(this)[1] - 50 : 
                                d3.mouse(this)[1];
                    
                    tooltip.attr('transform', `translate(${x}, ${y})`)
                  })
                  .on('mouseout', function () {
                    tooltip//.attr('display', 'none')
                     .style('opacity', 0)
                  })
  
const node_rect =  node.append('rect')
                        .attr('class', 'tile')
                        .style('x', d => d.x0)
                        .style('y', d => d.y0)
                        .style('width', d => Math.max(0, d.x1 - d.x0 - 1) + 'px')
                        .style('height', d => Math.max(0, d.y1 - d.y0 - 1) + 'px')
                        .style('fill', d => color(d.parent.data.name))
                        // .text(d => d.data.name);
                        .attr('data-name', d => d.data.name)
                        .attr('data-category', d => d.data.category)
                        .attr('data-value', d => d.data.value)
  
  const node_text = node.append('text')
                        // .text(d => d.data.name)
                        .attr('x', d => d.x0)
                        .attr('y', d => d.y0)
                        .attr("fill", "black")
                        .attr("font-size", "0.6em")
                        .attr("transform", "translate(4, 10)")
                        
  const node_text_tspan = node_text.selectAll('tspan')
                                   .data(d => d.data.name.split(' ').map(each => ({ name: each, x0: d.x0, y0: d.y0 })))
                                   .enter()
                                   .append('tspan')
                                   .text(d => d.name)
                                   .attr('x', d => d.x0)
                                   .attr('y', (d, i) => d.y0 + (i * 10))
                                   // .style('transform', (d, i) => `translate(0px, ${i * 4}px)`)
  
  // translate inside a tspan does nothing
  // cannot put a 'text' inside a 'rect'
  
  const tooltip = svg.append('g')
                     .style('opacity', 0)
                     // .attr('display', 'none')
                     .attr('transform', 'translate(50, 50)')
                     .attr('id', 'tooltip')
  
  const tooltip_back = tooltip.append('rect')
                              .attr('stroke', '#000000')
                              .attr('fill', '#fff')
                              .attr('width', '150px')
                              .attr('height', '60px')
  
  const tooltip_name = tooltip.append('text')
                              .text('Wii Sports Resort')
                              .attr('class', 'tooltip_text')
                              .style('transform', 'translate(4px, 15px)')
                                    // .attr("text-anchor", "middle");
  
  const tooltip_catt = tooltip.append('text')
                              .text('Cattegory: Wii')
                              .attr('class', 'tooltip_text')
                              .style('transform', 'translate(4px, 30px)')
                                    // .attr("text-anchor", "middle");
  
  const tooltip_value = tooltip.append('text')
                              .text('Value: 5.28')
                              .attr('class', 'tooltip_text')
                              .style('transform', 'translate(4px, 45px)')
                                    // .attr("text-anchor", "middle");
  
  const key_data = d3.set(data.children.map(each => each.name))
  
  d3.select('#legend').selectAll('*').remove()
  
  var legend_width = 700
  
  const legend_svg = d3.select('#legend')
                        .append('svg')
                        .attr('width', legend_width)
                        // .style('border', '1px dashed tomato')
  
  const legend_item = legend_svg.append('g')
                                .selectAll('g')
                                .data(root.children)
                                .enter()
                                .append('g')
                                .attr('transform', (d, i) => {
                                  var col = i % 3 == 0 ? 0 : (i+2) % 3 == 0 ? 1 : 2;
                                  return `translate(${(col * (legend_width/3)) + 50}, ${(Math.floor(i/3) * 20) + 20})`
                                })
                                
                     legend_item.append('text')
                                .text(d => d.data.name)
                                // .attr('text-anchor', 'middle')
  
  const legend_icon = legend_item.append('rect')
                                .attr('class', 'legend-item')
                                  .attr('width', 15)
                                  .attr('height', 15)
                                  .attr('fill', d => color(d.data.name))
                                  .attr('transform', 'translate(-25, -13)')
  
});
  
  
}

render()

function changeData (num) {
  data_selected = num
  render()
  $('button').removeClass('active')
  $(`.data_select_${num}`).addClass('active')
}
              
            
!
999px

Console