//read in gameplay data var title = "FIFA 17"; d3.json("gameplay-data.json", function(data) { //top played games var games = data.report; //find rank of chosen game games.forEach( function(d,i) { d.rank = i+1; } ) var rank = games.filter( function(d){return d.label === title})[0].rank; games.forEach( function(d,i) { d.relative = d.rank-rank; } ) if(rank<5) { var games_7 = games.filter( function(d){return d.rank < 8});} if(rank>4) { var games_7 = games.filter( function(d){return d.relative > -4 && d.relative < 4});} games_7.forEach( function(d,i) { if(d.relative===0) { d.class = 'table-info'} else {d.class = ' '} } ) //populate table var gameplay_rows = d3.select('#broadcast').selectAll('.broadcast_rows').data(games_7).enter().append('tr').attr('class' , 'broadcast_rows'); gameplay_rows.append('td').html(function(d) {return d.rank}).attr('class',function(d) {return d.class + ' rank '}); gameplay_rows.append('td').html(function(d) {return d.label}).attr('class',function(d) {return d.class + ' label '}); gameplay_rows.append('td').html(function(d) {return numberWithCommas(d.total)}).attr('class',function(d) {return d.class + ' total '}); }); //now update function update() { d3.json("gameplay-data.json", function(data) { //top played games var games = data.report; games.forEach( function(d,i) { d.total = Math.round(d.total * ( 1 - Math.random() / 10)); } ) games.sort(function(x, y){ return d3.descending(x.total, y.total); }) //find rank of chosen game games.forEach( function(d,i) { d.rank = i+1; } ) var rank = games.filter( function(d){return d.label === title})[0].rank; games.forEach( function(d,i) { d.relative = d.rank-rank; } ) if(rank<5) { var games_7_new = games.filter( function(d){return d.rank < 8});} if(rank>4) { var games_7_new = games.filter( function(d){return d.relative > -4 && d.relative < 4});} games_7_new.forEach( function(d,i) { if(d.relative===0) { d.class = 'table-info'} else {d.class = ' '} } ) //populate table d3.selectAll('.broadcast_rows').remove(); var gameplay_rows = d3.select('#broadcast').selectAll('.broadcast_rows').data(games_7_new).enter().append('tr').attr('class' , 'broadcast_rows'); gameplay_rows.append('td').html(function(d) {return d.rank}).attr('class',function(d) {return d.class + ' rank '}); gameplay_rows.append('td').html(function(d) {return d.label}).attr('class',function(d) {return d.class + ' label '}); gameplay_rows.append('td').html(function(d) {return numberWithCommas(d.total)}).attr('class',function(d) {return d.class + ' total '}); function nth(d) { if(d>3 && d<21) return 'th'; switch (d % 10) { case 1: return "st"; case 2: return "nd"; case 3: return "rd"; default: return "th"; } } d3.select('#broadcast_rank').html(rank + nth(rank)+' most broadcasted game today'); }); }; setInterval(update, 2000); function numberWithCommas(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); }