Skip to content

Commit 8e52154

Browse files
RLessermbostock
andauthored
Add className option for marks (#1098)
* add className to style options * new test for className option * Removed redundant function, added test output Removed the maybeClassNameOptional function, and fixed the existing maybeClassName function calls to take in a "provide" boolean. Added the test output file as well. * push ifs up * documentation --------- Co-authored-by: Mike Bostock <mbostock@gmail.com>
1 parent 0e5c684 commit 8e52154

File tree

7 files changed

+99
-1
lines changed

7 files changed

+99
-1
lines changed

docs/features/marks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ All marks support the following style options:
482482
* **dx** - horizontal offset (in pixels; defaults to 0)
483483
* **dy** - vertical offset (in pixels; defaults to 0)
484484
* **target** - link target (e.g., “_blank” for a new window); for use with the **href** channel
485+
* **className** - the [class attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class), if any (defaults to null) <VersionBadge pr="1098" />
485486
* **ariaDescription** - a textual description of the mark’s contents
486487
* **ariaHidden** - if true, hide this content from the accessibility tree
487488
* **pointerEvents** - the [pointer events](https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events) (*e.g.*, *none*)

src/mark.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ export interface MarkOptions {
239239
*/
240240
marginLeft?: number;
241241

242+
/**
243+
* The [class attribute][1]; a constant string.
244+
*
245+
* [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class
246+
*/
247+
className?: string;
248+
242249
/**
243250
* The [aria-description][1]; a constant textual description.
244251
*

src/mark.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {maybeFacetAnchor} from "./facet.js";
44
import {maybeClip, maybeNamed, maybeValue} from "./options.js";
55
import {arrayify, isDomainSort, isObject, isOptions, keyword, range, singleton} from "./options.js";
66
import {project} from "./projection.js";
7-
import {styles} from "./style.js";
7+
import {maybeClassName, styles} from "./style.js";
88
import {basic, initializer} from "./transforms/basic.js";
99

1010
export class Mark {
@@ -22,6 +22,7 @@ export class Mark {
2222
marginRight = margin,
2323
marginBottom = margin,
2424
marginLeft = margin,
25+
className,
2526
clip = defaults?.clip,
2627
channels: extraChannels,
2728
tip,
@@ -71,6 +72,7 @@ export class Mark {
7172
this.marginLeft = +marginLeft;
7273
this.clip = maybeClip(clip);
7374
this.tip = maybeTip(tip);
75+
this.className = className ? maybeClassName(className) : null;
7476
// Super-faceting currently disallow position channels; in the future, we
7577
// could allow position to be specified in fx and fy in addition to (or
7678
// instead of) x and y.

src/style.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ function applyClip(selection, mark, dimensions, context) {
348348
// Note: may mutate selection.node!
349349
export function applyIndirectStyles(selection, mark, dimensions, context) {
350350
applyClip(selection, mark, dimensions, context);
351+
applyAttr(selection, "class", mark.className);
351352
applyAttr(selection, "fill", mark.fill);
352353
applyAttr(selection, "fill-opacity", mark.fillOpacity);
353354
applyAttr(selection, "stroke", mark.stroke);

test/output/classNameOnMarks.svg

Lines changed: 66 additions & 0 deletions
Loading

test/plots/class-name.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
export async function classNameOnMarks() {
5+
const sales = await d3.csv("data/fruit-sales.csv", d3.autoType);
6+
return Plot.plot({
7+
marginLeft: 50,
8+
y: {
9+
label: null,
10+
reverse: true
11+
},
12+
marks: [
13+
Plot.barX(
14+
sales,
15+
Plot.groupY({x: "sum"}, {x: "units", y: "fruit", sort: {y: "x", reverse: true}, className: "fruitbars"})
16+
),
17+
Plot.ruleX([0])
18+
]
19+
});
20+
}

test/plots/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export * from "./cars-mpg.js";
5151
export * from "./cars-parcoords.js";
5252
export * from "./channel-domain.js";
5353
export * from "./clamp.js";
54+
export * from "./class-name.js";
5455
export * from "./collapsed-histogram.js";
5556
export * from "./color-piecewise.js";
5657
export * from "./country-centroids.js";

0 commit comments

Comments
 (0)