Skip to main content

Perspective's Datagrid: the 2024 NFL offences by EPA per play, each team's cell in its own colour and its logo as a link

With JavaScript on, Perspective draws this figure here.

{
  "plugin": "Datagrid",
  "theme": "Pro Light",
  "columns_config": {
    "team": {
      "bg_mode": "series",
      "bg_color": "linear-gradient(to right, #241773, #c60c30, #8d9296, #5a1414, #b85149, #53575f, #ff4a41, #203731, #bf880e, #97233f, #a71600, #006fce, #000000, #6d2482, #0043aa, #004c6e, #00472c, #59a222, #0d6f7f, #1e2d00, #32202f, #dc751d, #0085ca, #003876, #82703f, #e64100, #000940, #002244, #492e5b, #6d92b8, #2d0000, #311d00)"
    },
    "plays": {
      "number_format": {
        "useGrouping": false,
        "maximumFractionDigits": 0
      }
    },
    "epa_per_play": {
      "number_format": {
        "minimumFractionDigits": 3,
        "maximumFractionDigits": 3
      }
    },
    "def_epa_per_play": {
      "number_format": {
        "minimumFractionDigits": 3,
        "maximumFractionDigits": 3
      }
    },
    "team_logo": {
      "link": true
    }
  }
}
import { NFL_TEAM_EPA_2024 } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { perspectiveConfig, perspectiveLogoColumn } from "@sportsdataverse/sdvplot/perspective";

await loadLeague("nfl");
// nflverse's 2024 regular season, best offence first: the grid hands each team its colour in this order
const offence = NFL_TEAM_EPA_2024.map((t) => ({
team: t.team,
plays: t.off_plays,
epa_per_play: t.off_epa / t.off_plays,
def_epa_per_play: t.def_epa / t.def_plays,
})).sort((a, b) => b.epa_per_play - a.epa_per_play);
// a logo URL column right after the team (the Datagrid draws no images: it shows a URL as a link)
const rows = perspectiveLogoColumn(offence, "team", { league: "nfl" });
const config = perspectiveConfig(rows, { teamCol: "team", league: "nfl", logoCol: "team_logo" });

// In the browser: the rows as a Perspective table, the viewer bound to it, and sdvplot's config restored
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(rows.map((r) => ({ ...r })));
await viewer.load(worker);
// the logo links last: a URL is wide, and the numbers stay in view
await viewer.restore({
table: await table.get_name(),
...config,
columns: ["team", "plays", "epa_per_play", "def_epa_per_play", "team_logo"],
});
return () => void table.delete();
},
height: 520,
label: `The 2024 NFL offences by EPA per play in Perspective's Datagrid, best first, each team's cell in its own colour: ${rows
.map((r) => `${r.team} ${r.epa_per_play.toFixed(3)}`)
.join(", ")}`,
} as const;

// Perspective draws only in a browser; without JavaScript the page shows the config sdvplot hands it
config;

Tags: perspective · datagrid · perspectiveConfig · perspectiveLogoColumn · table · nfl. All sdvplot examples.