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

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

              
                <div class="container">
  <div class="" id="grid">
  </div>
</div>

              
            
!

CSS

              
                body
  background-color: #fff

#grid
  display: grid
  grid-gap: 4px
  grid-template-columns: repeat(18,1fr)
  .card
    transition: opacity 0.3s
    cursor: pointer

  &.selected
    .card
      opacity: 0.5
    &.selected--reit
      .card-reit
        opacity: 1
    &.selected--intl
      .card-intl
        opacity: 1
    &.selected--em
      .card-em
        opacity: 1
    &.selected--lgcap
      .card-lgcap
        opacity: 1
    &.selected--smcap
      .card-smcap
        opacity: 1
    &.selected--cash
      .card-cash
        opacity: 1
    &.selected--hgbnd
      .card-hgbnd
        opacity: 1
    &.selected--hybnd
      .card-hybnd
        opacity: 1
    &.selected--div
      .card-div
        opacity: 1

.year   
  display: grid
  grid-gap: 4px
  grid-template-columns: 60px
  grid-template-rows: 40px repeat(9,1fr)
  
  .year-title
    grid-area: header
  
.card-fund
  color: #fff
  width: 70px
  text-align: center

.card-year
  background-color: #ddd
  font-weight: bold
  text-align: center

.card-intl
  background-color: #546E7A
.card-em
  background-color: #8E24AA
.card-lgcap
  background-color: #FB8C00
.card-smcap
  background-color: #1E88E5
.card-reit
  background-color: #e53935
.card-hybnd
  background-color: #6D4C41
.card-hgbnd
  background-color: #7CB342
.card-cash
  background-color: #FDD835
.card-div
  background-color: steelblue
              
            
!

JS

              
                var titles = {
  'reit': 'REIT',
  'em': 'EM',
  'smcap': 'Sm Cap',
  'intl': 'Intl',
  'lgcap': 'Lg Cap',
  'hybnd': 'HY Bnd',
  'hgbnd': 'HG Bnd',
  'cash': 'Cash',
  'div': 'Div'
};

var div = {
  em: 0,
  smcap: 0,
  intl: .25,
  reit: .05,
  lgcap: .50,
  hybnd: 0,
  hgbnd: .20,
  cash: 0
}

var data = [
  {
    year: 2003,
    results: {
      em: 56.3,
      smcap: 47.3,
      intl: 39.2,
      reit: 37.1,
      lgcap: 28.7,
      hybnd: 28.2,
      hgbnd: 4.1,
      cash: 1,
      div: 10
    }
  },
  {  
	  year: 2004,
    results: {
      em: 26,
      smcap: 18.3,
      intl: 20.7,
      reit: 31.6,
      lgcap: 10.9,
      hybnd: 10.9,
      hgbnd: 4.3,
      cash: 1.4,
      div: 10
    }
  },
  {
	  year: 2005,
    results: {
      em: 34.5,
      smcap: 4.6,
      intl: 14,
      reit: 12.2,
      lgcap: 4.9,
      hybnd: 2.7,
      hgbnd: 2.4,
      cash: 3.2,
      div: 10  
    }
	},
  {
    year: 2006,
    results: {
      em: 32.6,
      smcap: 18.4,
      intl: 26.9,
      reit: 35.1,
      lgcap: 15.8,
      hybnd: 11.8,
      hgbnd: 4.3,
      cash: 4.7,
      div: 10
    },
  },
  {
    year: 2007,
    results: {
      em: 39.8,
      smcap: -1.6,
      intl: 11.6,
      reit: -15.7,
      lgcap: 5.5,
      hybnd: 2.2,
      hgbnd: 7,
      cash: 4.4,
      div: 10
    }
	},
  {
    year: 2008,
    results: {
      em: -53.2,
      smcap: -33.8,
      intl: -43.1,
      reit: -37.7,
      lgcap: -37,
      hybnd: -26.4,
      hgbnd: 5.2,
      cash: 1.4,
      div: 10
    }
	},
  {
    year: 2009,
    results: {
      em: 79,
      smcap: 27.2,
      intl: 32.5,
      reit: 28,
      lgcap: 26.5,
      hybnd: 57.5,
      hgbnd: 5.9,
      cash: 0.2,
      div: 10
    }
	},
  {
	  year: 2010,
    results: {
      em: 19.2,
      smcap: 26.9,
      intl: 8.2,
      reit: 28,
      lgcap: 15.1,
      hybnd: 15.2,
      hgbnd: 6.5,
      cash: 0.2,
      div: 10
    }
	},
  {
    year: 2011,
    results: {
      em: -18.2,
      smcap: -4.2,
      intl: 11.7,
      reit: 8.3,
      lgcap: 2.1,
      hybnd: 4.4,
      hgbnd: 7.8,
      cash: 0.1,
      div: 10
    }
	},
  {
    year: 2012,
    results: {
      em: 18.6,
      smcap: 16.4,
      intl: 17.9,
      reit: 19.7,
      lgcap: 16,
      hybnd: 15.6,
      hgbnd: 4.2,
      cash: 0.1,
      div: 10
    },
  },
  {
  	year: 2013, 
    results: {
      em: -2.3,
      smcap: 38.8,
      intl: 23.3,
      reit: 2.9,
      lgcap: 32.4,
      hybnd: 7.4,
      hgbnd: -2,
      cash: 0.1,
      div: 10
    }
	},
  {
    year: 2014,
    results: {
      em: -1.8,
      smcap: 4.9,
      intl: -4.5,
      reit: 28,
      lgcap: 13.7,
      hybnd: 2.5,
      hgbnd: 6,
      cash: 1.4,
      div: 10
    }
	},
  {
	  year: 2015,
    results: {
      em: -14.6,
      smcap: -4.4,
      intl: -0.4,
      reit: 2.8,
      lgcap: 1.4,
      hybnd: -4.6,
      hgbnd: 0.6,
      cash: 0.1,
      div: 10
    }
	},
  {
    year: 2016,
    results: {
      em: 11.6,
      smcap: 21.3,
      intl: 1.5,
      reit: 8.6,
      lgcap: 12,
      hybnd: 17.5,
      hgbnd: 2.7,
      cash: 0.3,
      div: 10  
    }
	},
  {
    year: 2017,
		results: {
      em: 37.8,
      smcap: 14.7,
      intl: 25.6,
      reit: 8.7,
      lgcap: 21.8,
      hybnd: 7.5,
      hgbnd: 3.5,
      cash: 1,
      div: 10
    }
	},
  {
    year: 2018,
		results: {
      em: -14.3,
      smcap: -11,
      intl: -13.4,
      reit: -4,
      lgcap: -4.4,
      hybnd: -2.3,
      hgbnd: 0,
      cash: 2,
      div: 10
    }
	}
];

function sortData(yearData) {
  return d3.entries(yearData.results).sort(function(x, y){
    return d3.descending(x.value, y.value);
  });
}

data.forEach(function(yearData) {
  yearData.sortedData = sortData(yearData)
})

function setAllocation(data) {
  data.forEach(function(yearData) {
    yearData.results.div = computeReturn(yearData.results, div)
  });
  
  return data;
}

data = setAllocation(data)

function computeReturn(returns, aa) {
  var totalReturns = 0;
  var keys = Object.keys(aa);
  keys.forEach(function(key) {
    console.log('totalReturns', totalReturns)
    totalReturns = totalReturns + (returns[key] * aa[key]);
  });
  console.log('totalReturns', totalReturns)
  return totalReturns;
}

$(function() {
  var grid = d3.select('#grid');
  
  // append the header row
  var years = grid.selectAll('div')
    .data(data).enter()
    .append('div')
    .attr('class', 'year')
    .style('grid-row', '1 / 9')
    .style('grid-column', function(d, index) {
      return index+1;
    })
    .html(function(column, i) {
      return "<header class='year-title bg-grey-500 text-grey-700 text-lg text-center p-1 font-semibold' style='grid-row:1;grid-column:1'>"+column.year+"</header>";
    })
  
 
  // create a cell in each row for each column
  years.selectAll('div')
     .data(function(d) { return sortData(d) })
     .enter()
     .append('div')
     .attr('class', function(d, index) {
       return "card p-1 text-white card-" + d.key;
     })
     .style('grid-column', 1)
     .style('grid-row', function(d, index) {
       return index+2;
     })
     .on('mouseover', function(d) {
       grid.classed('selected', true)
           .classed("selected--"+d.key, true)
           .attr('data-selected-sector', d.key);
     })
     .on('mouseout', function(d) {
       var sector = grid.attr('data-selected-sector');
       grid.classed('selected', false)
           .classed('selected--'+sector, false);
     })
     .html(function (d) {
       return "<span class='font-bold text-lg'>"+titles[d.key] + "</span><br/><span class='font-normal text-md'>" + d.value + "%</span>";
     })
});
              
            
!
999px

Console