chartcn

useUnitMap

Builds a key-to-unit lookup map from series config arrays.

A simple memoised hook that builds a Record<string, string> from any array of series config objects that have key and unit fields.

Usage

import { useUnitMap } from "@exhibit/charts";

const series = [
  { key: "evaporation", label: "Evaporation", unit: "ml" },
  { key: "temperature", label: "Temperature", unit: "°C" },
  { key: "humidity", label: "Humidity" }, // no unit
];

const unitMap = useUnitMap(series);
// { evaporation: "ml", temperature: "°C" }

Parameters

ParameterTypeDescription
seriesArray<{ key: string; unit?: string }>Series config objects

Return value

Record<string, string> mapping data keys to their unit strings. Keys without a unit are omitted.

Memoisation

The hook uses JSON.stringify on the [key, unit] pairs to create a stable dependency, so the map is only recomputed when the series configuration actually changes.

On this page