diff --git a/src/index.md b/src/index.md index 7273b92..3a619bf 100644 --- a/src/index.md +++ b/src/index.md @@ -1,11 +1,15 @@ ```js const parseTime = d3.timeParse("%b %d, %Y"); -const coerceGameData = (d,i) => ({date: parseTime(d.date), spread: Number(d.spread)}); -const oddsfile = FileAttachment("./data/odds_data_abbr.json").json().then((D) => D.map(coerceGameData)); +const formatYear = d3.utcFormat("%Y"); +const coerceGameData = (d,i) => ({date: parseTime(d.date), year: parseInt(formatYear(parseTime(d.date))), spread: Number(d.spread)}); +const oddsfile = FileAttachment("./data/odds_data.json").json().then((D) => D.map(coerceGameData)); +``` +```js +const oddsByYear = d3.group(oddsfile, d => d.year) ``` ```js -Inputs.table(oddsfile); +display(oddsByYear); ``` ```js @@ -18,15 +22,16 @@ function oddsPlot(data, {width} = {}) { const marginLeft = 40; // Declare the x (horizontal position) scale. - const x = d3.scaleUtc(d3.extent(data, d => d.date), [marginLeft, width - marginRight]); + const x = d3.scaleTime([1952,2025], [marginLeft, width-marginRight]); // Declare the y (vertical position) scale. - const y = d3.scaleLinear([0, d3.max(data, d => d.spread)], [height - marginBottom, marginTop]); + const y = d3.scaleLinear([0, 600], [height - marginBottom, marginTop]); - // Declare the line generator. +// Declare the line generator. const line = d3.line() - .x(d => x(d.date)) - .y(d => y(d.spread)); + .x(d => x(d.year)) + .y(d => y(d.count())) + // .y(d => y(total(d, "year", d.year))); // Create the SVG container. const svg = d3.create("svg") @@ -48,12 +53,6 @@ function oddsPlot(data, {width} = {}) { .call(g => g.selectAll(".tick line").clone() .attr("x2", width - marginLeft - marginRight) .attr("stroke-opacity", 0.1)) - .call(g => g.append("text") - .attr("x", -marginLeft) - .attr("y", 10) - .attr("fill", "currentColor") - .attr("text-anchor", "start") - .text("↑ Daily close ($)")); // Append a path for the line. svg.append("path") @@ -66,6 +65,15 @@ function oddsPlot(data, {width} = {}) { } ``` +```js +function total(data, label, find) +{ + return data.reduce(function(count, entry) { + return count + (entry[label] === find ? 1 : 0); + }, 0); +} +``` +