Function: perspectiveConfig()
perspectiveConfig(
rows,o):PerspectiveConfig
Defined in: packages/sdvplot/src/perspective.ts:251
A Perspective viewer config that colours each team by its own colours, for the Datagrid or a chart: the argument of
viewer.restore(), as Python's perspective_sdv_config builds it (same keys, same colours, same order).
The colours are the shared team palette (every adapter's, and Python's), taken over the teams in sorted order: each team's colour, made 3:1 against the background and ΔE 20 from every earlier team's, so a team keeps one colour whatever the rows' order. Perspective takes a palette, not a colour per value, so each palette lists those colours in the order Perspective hands colours out (checked in Chromium on Perspective 5.5.1):
plugin: "Datagrid": the team column's cells are filled (bg_mode"series"), in the order the grid first draws each value: the rows' order. Keep the grid in the rows' order (nosort) and to about one row per team. Null cells take no colour and no slot. Only a text team column is filled: Perspective fills no number column, so numeric team ids get none, with one warning.- a chart: split a bar, line or area chart by the team (
split_by: [teamCol]) and each series takes its colour from every number column'spalette, in sorted order (Perspective sorts split values; a null series comes first, so the palette then starts with the fallback grey). Put the team column in an "X/Y Scatter"'s Color slot (its third column) and each team's points take theirs from the team column'spalette, in the rows' order.
Every number column also gets a number_format (Intl.NumberFormat options): 3 decimals for a float, 1 for a float
named as a percentage (pct, percent), and an integer plain (2024, not 2,024). formats overrides them.
Parameters
rows
readonly Readonly<Record<string, unknown>>[]
the rows the viewer shows (what perspective.table(rows) loads). With no rows and no schema no column is
known: the config has plugin and theme and an empty columns_config, without an error (pass the table's
schema for its number formats, as Python's empty frame keeps its columns).
o
Returns
plugin, theme and columns_config; merge your own group_by, split_by, columns and the rest in.
Throws
InputError for a column the rows do not have (teamCol, logoCol or a formats key), a team column that
mixes numbers and strings, an unknown mode, a theme that is not a spec (or a function returning one), or a
season outside the league's. UnresolvedTeamError with strict: true. A theme function's own errors pass through
(sdvtables' TableSpecError for a light-only theme in dark mode).
Example
import { loadLeague } from "@sportsdataverse/sdvplot";
import { perspectiveConfig } from "@sportsdataverse/sdvplot/perspective";
await loadLeague("nfl");
// 2024 regular season, offence: EPA per play (nflverse play-by-play)
const epa = [
{ posteam: "BAL", epa_per_play: 0.218 },
{ posteam: "BUF", epa_per_play: 0.1885 },
{ posteam: "DET", epa_per_play: 0.1592 },
];
const grid = perspectiveConfig(epa, { teamCol: "posteam", league: "nfl" });
const bars = {
...perspectiveConfig(epa, { teamCol: "posteam", league: "nfl", plugin: "Y Bar" }),
group_by: ["posteam"],
split_by: ["posteam"],
columns: ["epa_per_play"],
};
// in the browser: await viewer.load(await worker.table(epa)); await viewer.restore(grid)
[grid.columns_config.posteam?.bg_color, bars.columns_config.epa_per_play?.palette];