Skip to main content

Streaming into Perspective: 11,778 plays of the 2024 NFL season replayed a week at a time with table.update, each offence's total EPA growing in its colour

With JavaScript on, Perspective draws this figure here.

{
  "batches": [
    {
      "week": 1,
      "plays": 2007
    },
    {
      "week": 2,
      "plays": 2009
    },
    {
      "week": 3,
      "plays": 2043
    },
    {
      "week": 4,
      "plays": 2045
    },
    {
      "week": 5,
      "plays": 1854
    },
    {
      "week": 6,
      "plays": 1820
    }
  ],
  "config": {
    "plugin": "Y Bar",
    "theme": "Pro Light",
    "columns_config": {
      "posteam": {
        "palette": "linear-gradient(to right, #97233f, #c60c30, #241773, #bf880e, #82703f, #0085ca, #002244, #311d00, #59a222, #004c6e, #203731, #53575f, #003876, #32202f, #dc751d, #0d6f7f, #0043aa, #8d9296, #006fce, #2d0000, #492e5b, #6d2482, #ff4a41, #000940, #00472c, #a71600, #000000, #1e2d00, #6d92b8, #e64100, #b85149, #5a1414)"
      },
      "week": {
        "number_format": {
          "useGrouping": false,
          "maximumFractionDigits": 0
        },
        "palette": "linear-gradient(to right, #97233f, #000000, #241773, #c60c30, #0085ca, #e64100, #ff4a41, #311d00, #002244, #004c6e, #8d9296, #203731, #32202f, #003876, #0d6f7f, #bf880e, #0043aa, #006fce, #2d0000, #dc751d, #6d2482, #000940, #82703f, #492e5b, #00472c, #53575f, #1e2d00, #59a222, #a71600, #b85149, #6d92b8, #5a1414)"
      },
      "epa": {
        "number_format": {
          "minimumFractionDigits": 3,
          "maximumFractionDigits": 3
        },
        "palette": "linear-gradient(to right, #97233f, #000000, #241773, #c60c30, #0085ca, #e64100, #ff4a41, #311d00, #002244, #004c6e, #8d9296, #203731, #32202f, #003876, #0d6f7f, #bf880e, #0043aa, #006fce, #2d0000, #dc751d, #6d2482, #000940, #82703f, #492e5b, #00472c, #53575f, #1e2d00, #59a222, #a71600, #b85149, #6d92b8, #5a1414)"
      }
    },
    "group_by": [
      "posteam"
    ],
    "split_by": [
      "posteam"
    ],
    "columns": [
      "epa"
    ],
    "aggregates": {
      "epa": "sum"
    }
  }
}
import { nflPlays2024 } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { type PerspectiveSchema, perspectiveConfig } from "@sportsdataverse/sdvplot/perspective";

await loadLeague("nfl");
// nflverse's 2024 play-by-play, weeks 1 to 6: the rush and pass plays, a batch per week
const plays = (await nflPlays2024()).map((p) => ({
play: p.play,
week: p.week,
posteam: p.posteam,
defteam: p.defteam,
epa: p.epa,
}));
const weeks = [...new Set(plays.map((p) => p.week))].sort((a, b) => a - b);
const batches = weeks.map((w) => plays.filter((p) => p.week === w));
// the table starts empty, so its types are given: the same schema goes to Perspective and to sdvplot
const schema: PerspectiveSchema = {
play: "string",
week: "integer",
posteam: "string",
defteam: "string",
epa: "float",
};
const config = {
...perspectiveConfig(plays, { teamCol: "posteam", league: "nfl", plugin: "Y Bar", schema }),
group_by: ["posteam"],
split_by: ["posteam"],
columns: ["epa"],
aggregates: { epa: "sum" as const },
};

export const browser = {
lib: "perspective",
mount: async ({
worker,
viewer,
}: {
worker: import("@perspective-dev/client").Client;
viewer: import("@perspective-dev/viewer").HTMLPerspectiveViewerElement;
}) => {
const table = await worker.table(schema, { index: "play" });
// the first week before the config: Perspective 5.5 drops a columns_config restored onto an empty table (the
// team colours and number formats would be lost)
await table.update((batches[0] ?? []).map((p) => ({ ...p })));
await viewer.load(worker);
await viewer.restore({ table: await table.get_name(), ...config });
// then a week's plays every 0.8 s, as a live feed would send them: the bars grow as each batch lands
let next = 1;
const timer = setInterval(() => {
const batch = batches[next++];
if (batch === undefined) return clearInterval(timer);
void table.update(batch.map((p) => ({ ...p })));
}, 800);
return () => {
clearInterval(timer);
void table.delete();
};
},
height: 480,
label: `The 2024 NFL season's first ${weeks.length} weeks replayed into Perspective a week at a time: each offence's total EPA as a bar in its colour, growing with each batch of plays (${batches
.map((b, i) => `week ${weeks[i]}: ${b.length} plays`)
.join(", ")})`,
} as const;

// Perspective draws only in a browser; without JavaScript the page shows each batch and the config
{ batches: batches.map((b, i) => ({ week: weeks[i], plays: b.length })), config };

Tags: perspective · streaming · table.update · play-by-play · perspectiveConfig · nfl. All sdvplot examples.