My Original dataset has 4 columns name, average_rating, year and rating. I have to create linechart for count(a_rating), where 0<rating<9 vs rating. However if for a particular year rating doesn't exist, I need to replace he count with zero. e.g. if no rating 2 exists for year 2015, then year: 2015, rating: 2, count:0. I am not able to generate the zeros.
few rows of rating.csv->
name,year,average_rating,users_rated Codenames,2015,7.71148,51209 King of Tokyo,2011,7.23048,48611 Love Letter,2012,7.25253,47014 Splendor,2014,7.47331,45239 7 Wonders Duel,2015,8.12219,40971 Scythe,2016,8.2841,39962 Lords of Waterdeep,2012,7.76601,38696 Terraforming Mars,2016,8.40331,39327 The Castles of Burgundy,2011,8.12835,36901 Patchwork,2014,7.7122,33721 Dead of Winter: A Crossroads Game,2014,7.67747,32915 Terra Mystica,2012,8.19293,32719 Pandemic Legacy: Season 1,2015,8.64165,31326 Coup,2012,7.03729,29287
d3.dsv(",", rating.csv, function (d) {
return {
// format data attributes if required
name:d.name,
year:d3.timeFormat("%Y")(new Date(d.year)),
avgRating:Math.floor(+d["average_rating"]),
users:+d['users_rated']
}
}).then(function (data) {
const m = new Map();
data.forEach(({year, avgRating}) => {
// Create a key with values that we want to group by
// A list of key-value pairs is chosen to make use of `Object.fromEntries` later
if (year>2014){
const hash = JSON.stringify([['year', year], ['rating', +avgRating]]);
m.set(hash, (m.get(hash) || 0) + 1);}
});
const grouped = [...m].map(([rec, count]) => ({
...Object.fromEntries(JSON.parse(rec)),
count,
}))
var sumstat = d3.nest() // nest function allows to group the calculation per level of a factor
.key(function(d) { return d.year;})
.entries(grouped);
console.log(sumstat)