Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export default defineConfig({
{text: "Interval", link: "/transforms/interval"},
{text: "Map", link: "/transforms/map"},
{text: "Normalize", link: "/transforms/normalize"},
{text: "Occlusion", link: "/transforms/occlusion"},
{text: "Select", link: "/transforms/select"},
{text: "Shift", link: "/transforms/shift"},
{text: "Sort", link: "/transforms/sort"},
Expand Down
9 changes: 9 additions & 0 deletions docs/data/cancer.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import fs from "node:fs";
import {csvParse} from "d3";

export default {
watch: ["../public/data/cancer.csv"],
load([file]) {
return csvParse(fs.readFileSync(file, "utf-8"));
}
};
4 changes: 4 additions & 0 deletions docs/data/cancer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {data} from "./cancer.data";
import {autoType} from "d3";

export default data.map(({...d}) => autoType(d));
97 changes: 97 additions & 0 deletions docs/public/data/cancer.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name,year,survival
Prostate,5 Year,99
Thyroid,5 Year,96
Testis,5 Year,95
Melanomas,5 Year,89
Breast,5 Year,86
Hodgkin’s disease,5 Year,85
"Corpus uteri, uterus",5 Year,84
"Urinary, bladder",5 Year,82
"Cervix, uteri",5 Year,71
Larynx,5 Year,69
Rectum,5 Year,63
"Kidney, renal pelvis",5 Year,62
Colon,5 Year,62
Non-Hodgkin’s,5 Year,58
"Oral cavity, pharynx",5 Year,57
Ovary,5 Year,55
Leukemia,5 Year,43
"Brain, nervous system",5 Year,32
Multiple myeloma,5 Year,30
Stomach,5 Year,24
Lung and bronchus,5 Year,15
Esophagus,5 Year,14
"Liver, bile duct",5 Year,8
Pancreas,5 Year,4
Prostate,10 Year,95
Thyroid,10 Year,96
Testis,10 Year,94
Melanomas,10 Year,87
Breast,10 Year,78
Hodgkin’s disease,10 Year,80
"Corpus uteri, uterus",10 Year,83
"Urinary, bladder",10 Year,76
"Cervix, uteri",10 Year,64
Larynx,10 Year,57
Rectum,10 Year,55
"Kidney, renal pelvis",10 Year,54
Colon,10 Year,55
Non-Hodgkin’s,10 Year,46
"Oral cavity, pharynx",10 Year,44
Ovary,10 Year,49
Leukemia,10 Year,32
"Brain, nervous system",10 Year,29
Multiple myeloma,10 Year,13
Stomach,10 Year,19
Lung and bronchus,10 Year,11
Esophagus,10 Year,8
"Liver, bile duct",10 Year,6
Pancreas,10 Year,3
Prostate,15 Year,87
Thyroid,15 Year,94
Testis,15 Year,91
Melanomas,15 Year,84
Breast,15 Year,71
Hodgkin’s disease,15 Year,74
"Corpus uteri, uterus",15 Year,81
"Urinary, bladder",15 Year,70
"Cervix, uteri",15 Year,63
Larynx,15 Year,46
Rectum,15 Year,52
"Kidney, renal pelvis",15 Year,50
Colon,15 Year,54
Non-Hodgkin’s,15 Year,38
"Oral cavity, pharynx",15 Year,38
Ovary,15 Year,50
Leukemia,15 Year,30
"Brain, nervous system",15 Year,28
Multiple myeloma,15 Year,7
Stomach,15 Year,19
Lung and bronchus,15 Year,8
Esophagus,15 Year,8
"Liver, bile duct",15 Year,6
Pancreas,15 Year,3
Prostate,20 Year,81
Thyroid,20 Year,95
Testis,20 Year,88
Melanomas,20 Year,83
Breast,20 Year,65
Hodgkin’s disease,20 Year,67
"Corpus uteri, uterus",20 Year,79
"Urinary, bladder",20 Year,68
"Cervix, uteri",20 Year,60
Larynx,20 Year,38
Rectum,20 Year,49
"Kidney, renal pelvis",20 Year,47
Colon,20 Year,52
Non-Hodgkin’s,20 Year,34
"Oral cavity, pharynx",20 Year,33
Ovary,20 Year,50
Leukemia,20 Year,26
"Brain, nervous system",20 Year,26
Multiple myeloma,20 Year,5
Stomach,20 Year,15
Lung and bronchus,20 Year,6
Esophagus,20 Year,5
"Liver, bile duct",20 Year,8
Pancreas,20 Year,3
133 changes: 133 additions & 0 deletions docs/transforms/occlusion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<script setup>

import * as Plot from "@observablehq/plot";
import * as d3 from "d3";
import {ref} from "vue";
import cancer from "../data/cancer.ts";

const minDistance = ref(11);

const points = (() => {
const random = d3.randomLcg(42);
const data = [];
const points = [];
let step;
for (step = 0; step < 31; ++step) {
data.push(random());
points.push(...data.map((y, node) => ({step, y, node})));
}
points.push(...data.map((y, node) => ({step, y, node})));
return points;
})();
</script>

# Occlusion transform <VersionBadge pr="1957" />

Given a position dimension (either **x** or **y**), the **occlusion** transform rearranges the values along that dimension in such a way that the distance between nodes is greater than or equal to the minimum distance, and their visual order preserved. The [occlusionX transform](#occlusionX) rearranges the **x** (horizontal) position of each series of nodes sharing a common **y** (vertical) position; likewise the [occlusionY transform](#occlusionY) rearranges nodes vertically.

The occlusion transform is commonly used to prevent superposition of labels on line charts. The example below, per [Edward Tufte](https://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0003nk), represents estimates of survival rates per type of cancer after 5, 10, 15 and 20 years. Each data point is labelled with its actual value (rounded to the unit). Labels in the last column indicate the type.

:::plot
```js
Plot.plot({
width: 400,
height: 600,
marginRight: 60,
marginBottom: 20,
x: {
axis: "top",
domain: ["5 Year", "10 Year", "15 Year", "20 Year"],
label: null,
padding: 0
},
y: { axis: null, insetTop: 20 },
marks: [
Plot.line(cancer, {x: "year", y: "survival", z: "name", strokeWidth: 1}),
Plot.text(cancer, Plot.occlusionY({
text: "survival",
x: "year",
y: "survival",
textAnchor: "end",
dx: 5,
fontVariant: "tabular-nums",
stroke: "var(--plot-background)",
strokeWidth: 7,
fill: "currentColor",
tip: true
})),
Plot.text(cancer, Plot.occlusionY({
filter: d => d.year === "20 Year",
text: "name",
textAnchor: "start",
frameAnchor: "right",
dx: 10,
y: "survival"
}))
],
caption: "Estimates of survival rate (%), per type of cancer"
})
```

Without this transform, some of these labels would otherwise be masking each other. Note that when several labels share an identical position and text contents, only the first one is retained—and the others are filtered out (for example, check the value 62 in the first column).

The **minDistance** option is a constant indicating the minimum distance between nodes, in pixels. It defaults to 11, about the height of a line of text with the default font size. (If zero, the transform is not applied.)

The chart below shows how the positions are transformed as we repeatedly inject nodes into a collection, at a random vertical position, and apply the occlusionY transform at each step (horizontal axis). Adjust the range slider below to see how the positions change with the mininmum distance option:

<p>
<label class="label-input">
minDistance:
<input type="range" v-model.number="minDistance" min="0" max="30" step="0.1">
<span style="font-variant-numeric: tabular-nums;">{{minDistance.toLocaleString("en-US")}}</span>
</label>
</p>

:::plot
```js
Plot.plot({
y: {axis: null, inset: 25},
color: {type: "categorical"},
marks: [
Plot.line(points, Plot.occlusionY(minDistance, {
x: "step",
stroke: "node",
y: "y",
curve: "basis",
strokeWidth: 1
})),
Plot.dot(points, Plot.occlusionY(minDistance, {
x: "step",
fill: "node",
r: (d) => d.step === d.node,
y: "y"
})),
]
})
```
:::

The occlusion transform differs from the [dodge transform](./dodge.md) in that it only adjusts the nodes’ existing positions.

The occlusion transform can be used with any mark that supports **x** and **y** position.

## Occlusion options

The occlusion transforms accept the following option:

* **minDistance** — the number of pixels separating the nodes’ positions

## occlusionY(*occlusionOptions*, *options*) {#occlusionY}

```js
Plot.occlusionY(minDistance, {x: "date", y: "value"})
```

Given marks arranged along the *y* axis, the occlusionY transform adjusts their vertical positions in such a way that two nodes are separated by at least *minDistance* pixels, avoiding overlapping. The order of the nodes is preserved. The *x* position channel, if present, is used to determine series on which the transform is applied, and left unchanged.

## occlusionX(*occlusionOptions*, *options*) {#occlusionX}

```js
Plot.occlusionX({x: "value"})
```

Equivalent to Plot.occlusionY, but arranging the marks horizontally by returning an updated *x* position channel that avoids overlapping. The *y* position channel, if present, is used to determine series and left unchanged.
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export * from "./transforms/group.js";
export * from "./transforms/hexbin.js";
export * from "./transforms/map.js";
export * from "./transforms/normalize.js";
export * from "./transforms/occlusion.js";
export * from "./transforms/select.js";
export * from "./transforms/shift.js";
export * from "./transforms/stack.js";
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export {find, group, groupX, groupY, groupZ} from "./transforms/group.js";
export {hexbin} from "./transforms/hexbin.js";
export {normalize, normalizeX, normalizeY} from "./transforms/normalize.js";
export {map, mapX, mapY} from "./transforms/map.js";
export {occlusionX, occlusionY} from "./transforms/occlusion.js";
export {shiftX} from "./transforms/shift.js";
export {window, windowX, windowY} from "./transforms/window.js";
export {select, selectFirst, selectLast, selectMaxX, selectMaxY, selectMinX, selectMinY} from "./transforms/select.js";
Expand Down
59 changes: 59 additions & 0 deletions src/transforms/occlusion.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type {ChannelValueSpec} from "../channel.js";
import type {Initialized} from "./basic.js";

/** Options for the occlusion transform. */
export interface OcclusionOptions {
/**
* A constant in pixels describing the minimum distance between two nodes.
* Defaults to 11.
*/
minDistance?: number;
}

/** Options for the occlusionX transform. */
export interface OcclusionXOptions extends OcclusionOptions {
/**
* The vertical position. Nodes sharing the same vertical position will be
* rearranged horizontally together.
*/
y?: ChannelValueSpec;
}

/** Options for the occlusionY transform. */
export interface OcclusionYOptions extends OcclusionOptions {
/**
* The horizontal position. Nodes sharing the same horizontal position will be
* rearranged vertically together.
*/
x?: ChannelValueSpec;
}

/**
* Given an **x** position channel, rearranges the values in such a way that the
* horizontal distance between nodes is greater than or equal to the minimum
* distance, and their visual order preserved. Nodes that share the same
* position and text are fused together.
*
* If *occlusionOptions* is a number, it is shorthand for the occlusion
* **minDistance**.
*/
export function occlusionX<T>(options?: T & OcclusionXOptions): Initialized<T>;
export function occlusionX<T>(
occlusionOptions?: OcclusionXOptions | OcclusionXOptions["minDistance"],
options?: T
): Initialized<T>;

/**
* Given a **y** position channel, rearranges the values in such a way that the
* vertical distance between nodes is greater than or equal to the minimum
* distance, and their visual order preserved. Nodes that share the same
* position and text are fused together.
*
* If *occlusionOptions* is a number, it is shorthand for the occlusion
* **minDistance**.
*/
export function occlusionY<T>(options?: T & OcclusionYOptions): Initialized<T>;
export function occlusionY<T>(
dodgeOptions?: OcclusionYOptions | OcclusionYOptions["minDistance"],
options?: T
): Initialized<T>;
Loading