跳至內容

順序尺度

順序尺度類似於 線性尺度,它們將連續的數值輸入域對應到連續的輸出範圍。與線性尺度不同的是,順序尺度的輸入域和輸出範圍總是恰好有兩個元素,而且輸出範圍通常指定為內插器,而不是值陣列。順序尺度通常用於顏色編碼;另請參閱 d3-scale-chromatic。這些尺度不公開 反轉內插 方法。順序尺度也有 對數對數對稱分位數 變體。

scaleSequential(domain, interpolator)

範例 · 原始碼 · 使用指定的 domaininterpolator 函數或陣列建構新的順序尺度。

js
const color = d3.scaleSequential([0, 100], d3.interpolateBlues);

如果未指定 domain,則預設為 [0, 1]。

js
const color = d3.scaleSequential(d3.interpolateBlues);

如果未指定 interpolator,則預設為恆等函數。

js
const identity = d3.scaleSequential();

套用比例尺時,interpolator 會呼叫一個值,通常在 [0, 1] 範圍內,其中 0 代表最小值,1 代表最大值。例如,要實作不建議使用的 angry rainbow 比例尺(請改用 interpolateRainbow

js
const rainbow = d3.scaleSequential((t) => d3.hsl(t * 360, 1, 0.5) + "");

如果 interpolator 是陣列,則代表比例尺的兩個元素輸出範圍,並使用 interpolate 轉換為 interpolator 函數。

js
const color = d3.scaleSequential(["red", "blue"]);

順序比例尺的 domain 必須是數字,且必須包含兩個值。

sequential.interpolator(interpolator)

如果指定 interpolator,則將比例尺的 interpolator 設定為指定的函數。

js
const color = d3.scaleSequential().interpolator(d3.interpolateBlues);

如果未指定 interpolator,則傳回比例尺目前的 interpolator。

js
color.interpolator() // d3.interpolateBlues

sequential.range(range)

請參閱 linear.range。如果指定 range,則使用 interpolate 將給定的兩個元素陣列轉換為 interpolator 函數。

js
const color = d3.scaleSequential().range(["red", "blue"]);

以上等同於

js
const color = d3.scaleSequential(d3.interpolate("red", "blue"));

sequential.rangeRound(range)

請參閱 linear.rangeRound。如果指定 range,則隱含地使用 interpolateRound 作為 interpolator。

scaleSequentialLog(domain, range)

傳回一個新的順序比例尺,具有對數轉換,類似於 對數比例尺

scaleSequentialPow(domain, range)

傳回一個新的順序比例尺,具有指數轉換,類似於 冪次比例尺

scaleSequentialSqrt(domain, range)

傳回一個新的順序比例尺,具有平方根轉換,類似於 平方根比例尺

scaleSequentialSymlog(domain, range)

傳回一個新的順序比例尺,具有對稱對數轉換,類似於 symlog 比例尺

scaleSequentialQuantile(domain, range)

原始碼 · 傳回一個新的順序比例尺,具有 p 分位數轉換,類似於 分位數比例尺

sequentialQuantile.quantiles(n)

原始碼 · 傳回一個包含 n + 1 個分位數的陣列。

js
const color = d3.scaleSequentialQuantile()
    .domain(penguins.map((d) => d.body_mass_g))
    .interpolator(d3.interpolateBlues);

color.quantiles(4); // [2700, 3550, 4050, 4750, 6300]

例如,如果 n = 4,則傳回一個包含五個數字的陣列:最小值、第一四分位數、中位數、第三四分位數和最大值。