How to create bins using d3.js?

42 views Asked by At

Im trying to create a histogram that shows the number of matches held each season in the Premier League.

So far I have managed to create a bar chart that shows the total number of matches held yearly but it is not accurate as each season is from August to May of the following year.

<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src="//d3js.org/d3.v7.min.js"></script> 

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<!-- Create a div for the tooltip -->
<div id="tooltip" style="position: absolute; opacity: 0;"></div>

<script>
    var parseDate = d3.timeParse("%d/%m/%Y");
  // Load the data from CSV
    d3.csv("Final_dataset5.csv").then(function(data) {
    data.forEach(function(d) {
        d.Date = parseDate(d.Date);
    });
    var gamesByYear = d3.group(data, function(d) {
        return d.Date.getFullYear();
    });
    var gamesPerYear = Array.from(gamesByYear, ([key, value]) => ({
        year: key,
        games: value.length
    }));
    gamesPerYear.sort(function(a, b) {
        return a.year - b.year;
    });

Is there anyway I can bin my data for example from %d/%m/%Y 1/8/2000 to 30/5/2001 and so on for the following seasons

0

There are 0 answers