Skip to main content

uPlot: the 2023-24 Pacific's series colours on a light and a dark chart, each at 3:1 contrast or better and distinct

[
  {
    "team": "Clippers",
    "light": "#12173f",
    "dark": "#656882",
    "darkContrast": 3.2
  },
  {
    "team": "Suns",
    "light": "#29127a",
    "dark": "#e56020",
    "darkContrast": 5
  },
  {
    "team": "Lakers",
    "light": "#be8b1d",
    "dark": "#fdb927",
    "darkContrast": 10.1
  },
  {
    "team": "Kings",
    "light": "#6a7a82",
    "dark": "#9056a9",
    "darkContrast": 3.4
  },
  {
    "team": "Warriors",
    "light": "#1d428a",
    "dark": "#ff9600",
    "darkContrast": 8
  }
]
import { NBA_STANDINGS } from "@sportsdataverse/examples/data";
import { contrast, loadLeague } from "@sportsdataverse/sdvplot";
import { withTeamSeries } from "@sportsdataverse/sdvplot/uplot";

await loadLeague("nba");
type Series = { label?: string; stroke?: string };
// one series per team (series[0] is uPlot's x), coloured from one row per team
const series: Series[] = [{ label: "Game" }, ...NBA_STANDINGS.map((s) => ({ label: `${s.city} ${s.team}` }))];
const rows = NBA_STANDINGS.map((s) => ({ team: s.team }));
const strokes = (background: string): string[] =>
withTeamSeries({ series }, rows, { team: "team", league: "nba", background })
.series.slice(1)
.map((s) => s.stroke ?? "");

// uPlot draws on a transparent canvas: give withTeamSeries the colour behind it, and a team colour that would not
// read there gives way to the team's other colour, or is lightened (dark) or darkened (light) until it does; one that
// would land too close to an earlier team's is moved apart (on the dark chart the Lakers and Warriors keep two colours).
const light = strokes("#ffffff");
const dark = strokes("#181a1b");
NBA_STANDINGS.map((s, i) => ({
team: s.team,
light: light[i],
dark: dark[i],
darkContrast: Math.round(contrast(dark[i] ?? "", "#181a1b") * 10) / 10,
}));

Tags: uplot · withTeamSeries · contrast · dark mode · nba. All sdvplot examples.