Skip to content

Commit f5a99d5

Browse files
authored
top-level clip option (#1792)
1 parent 0de63df commit f5a99d5

File tree

9 files changed

+27
-28
lines changed

9 files changed

+27
-28
lines changed

docs/features/plots.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ Unitless numbers ([quirky lengths](https://www.w3.org/TR/css-values-4/#deprecate
270270

271271
The generated SVG element has a class name which applies a default stylesheet. Use the top-level **className** option to specify that class name.
272272

273+
The **clip** option <VersionBadge pr="1792" /> determines the default clipping behavior if the [mark **clip** option](./marks.md#mark-options) is not specified; set it to true to enable clipping. This option does not affect [axis](../marks/axis.md), [grid](../marks/grid.md), and [frame](../marks/frame.md) marks, whose **clip** option defaults to false.
274+
273275
The **document** option specifies the [document](https://developer.mozilla.org/en-US/docs/Web/API/Document) used to create plot elements. It defaults to window.document, but can be changed to another document, say when using a virtual DOM implementation for server-side rendering in Node.
274276

275277
## plot(*options*) {#plot}

src/context.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {GeoStreamWrapper} from "d3";
2+
import type {MarkOptions} from "./mark.js";
23

34
/** Additional rendering context provided to marks and initializers. */
45
export interface Context {
@@ -16,4 +17,7 @@ export interface Context {
1617

1718
/** The current projection, if any. */
1819
projection?: GeoStreamWrapper;
20+
21+
/** The default clip for all marks. */
22+
clip?: MarkOptions["clip"];
1923
}

src/context.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {creator, select} from "d3";
2+
import {maybeClip} from "./style.js";
23

34
export function createContext(options = {}) {
4-
const {document = typeof window !== "undefined" ? window.document : undefined} = options;
5-
return {document};
5+
const {document = typeof window !== "undefined" ? window.document : undefined, clip} = options;
6+
return {document, clip: maybeClip(clip)};
67
}
78

89
export function create(name, {document}) {

src/mark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class Mark {
2222
marginRight = margin,
2323
marginBottom = margin,
2424
marginLeft = margin,
25-
clip,
25+
clip = defaults?.clip,
2626
channels: extraChannels,
2727
tip,
2828
render

src/marks/axis.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ function axisMark(mark, k, ariaLabel, data, options, initialize) {
564564
channels = {};
565565
}
566566
m.ariaLabel = ariaLabel;
567+
if (m.clip === undefined) m.clip = false; // don’t clip axes by default
567568
return m;
568569
}
569570

src/marks/frame.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransfo
66
const defaults = {
77
ariaLabel: "frame",
88
fill: "none",
9-
stroke: "currentColor"
9+
stroke: "currentColor",
10+
clip: false
1011
};
1112

1213
const lineDefaults = {
1314
ariaLabel: "frame",
1415
fill: null,
1516
stroke: "currentColor",
16-
strokeLinecap: "square"
17+
strokeLinecap: "square",
18+
clip: false
1719
};
1820

1921
export class Frame extends Mark {

src/plot.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {ChannelValue} from "./channel.js";
22
import type {LegendOptions} from "./legends.js";
3-
import type {Data, Markish} from "./mark.js";
3+
import type {Data, MarkOptions, Markish} from "./mark.js";
44
import type {ProjectionFactory, ProjectionImplementation, ProjectionName, ProjectionOptions} from "./projection.js";
55
import type {Scale, ScaleDefaults, ScaleName, ScaleOptions} from "./scales.js";
66

@@ -146,6 +146,9 @@ export interface PlotOptions extends ScaleDefaults {
146146
*/
147147
document?: Document;
148148

149+
/** The default clip for all marks. */
150+
clip?: MarkOptions["clip"];
151+
149152
// scale, axis, and legend definitions
150153

151154
/**

src/style.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,8 @@ import {geoPath, group, namespaces} from "d3";
22
import {create} from "./context.js";
33
import {defined, nonempty} from "./defined.js";
44
import {formatDefault} from "./format.js";
5-
import {
6-
string,
7-
number,
8-
maybeColorChannel,
9-
maybeNumberChannel,
10-
maybeKeyword,
11-
isNoneish,
12-
isNone,
13-
isRound,
14-
keyof
15-
} from "./options.js";
5+
import {isNone, isNoneish, isRound, maybeColorChannel, maybeNumberChannel} from "./options.js";
6+
import {keyof, keyword, number, string} from "./options.js";
167
import {warn} from "./warnings.js";
178

189
export const offset = (typeof window !== "undefined" ? window.devicePixelRatio > 1 : typeof it === "undefined") ? 0 : 0.5; // prettier-ignore
@@ -311,13 +302,15 @@ export function* groupIndex(I, position, mark, channels) {
311302
export function maybeClip(clip) {
312303
if (clip === true) clip = "frame";
313304
else if (clip === false) clip = null;
314-
return maybeKeyword(clip, "clip", ["frame", "sphere"]);
305+
else if (clip != null) clip = keyword(clip, "clip", ["frame", "sphere"]);
306+
return clip;
315307
}
316308

317309
// Note: may mutate selection.node!
318310
function applyClip(selection, mark, dimensions, context) {
319311
let clipUrl;
320-
switch (mark.clip) {
312+
const {clip = context.clip} = mark;
313+
switch (clip) {
321314
case "frame": {
322315
const {width, height, marginLeft, marginRight, marginTop, marginBottom} = dimensions;
323316
const id = getClipId();

test/plots/band-clip.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@ import * as d3 from "d3";
44
export async function bandClip() {
55
return Plot.plot({
66
y: {type: "band"},
7-
marks: [
8-
Plot.frame(),
9-
Plot.text(["A", "B", "C"], {
10-
x: (d) => d,
11-
y: (d) => d,
12-
clip: true,
13-
fontSize: 50
14-
})
15-
]
7+
clip: true,
8+
marks: [Plot.frame(), Plot.text(["A", "B", "C"], {x: (d) => d, y: (d) => d, fontSize: 50})]
169
});
1710
}
1811

0 commit comments

Comments
 (0)