Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add className option for marks #1098

Merged
merged 9 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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/features/marks.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ All marks support the following style options:
* **dx** - horizontal offset (in pixels; defaults to 0)
* **dy** - vertical offset (in pixels; defaults to 0)
* **target** - link target (e.g., “_blank” for a new window); for use with the **href** channel
* **className** - the [class attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class), if any (defaults to null) <VersionBadge pr="1098" />
* **ariaDescription** - a textual description of the mark’s contents
* **ariaHidden** - if true, hide this content from the accessibility tree
* **pointerEvents** - the [pointer events](https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events) (*e.g.*, *none*)
Expand Down
7 changes: 7 additions & 0 deletions src/mark.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ export interface MarkOptions {
*/
marginLeft?: number;

/**
* The [class attribute][1]; a constant string.
*
* [1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class
*/
className?: string;

/**
* The [aria-description][1]; a constant textual description.
*
Expand Down
4 changes: 3 additions & 1 deletion src/mark.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {maybeFacetAnchor} from "./facet.js";
import {maybeClip, maybeNamed, maybeValue} from "./options.js";
import {arrayify, isDomainSort, isObject, isOptions, keyword, range, singleton} from "./options.js";
import {project} from "./projection.js";
import {styles} from "./style.js";
import {maybeClassName, styles} from "./style.js";
import {basic, initializer} from "./transforms/basic.js";

export class Mark {
Expand All @@ -22,6 +22,7 @@ export class Mark {
marginRight = margin,
marginBottom = margin,
marginLeft = margin,
className,
clip = defaults?.clip,
channels: extraChannels,
tip,
Expand Down Expand Up @@ -71,6 +72,7 @@ export class Mark {
this.marginLeft = +marginLeft;
this.clip = maybeClip(clip);
this.tip = maybeTip(tip);
this.className = className ? maybeClassName(className) : null;
// Super-faceting currently disallow position channels; in the future, we
// could allow position to be specified in fx and fy in addition to (or
// instead of) x and y.
Expand Down
1 change: 1 addition & 0 deletions src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ function applyClip(selection, mark, dimensions, context) {
// Note: may mutate selection.node!
export function applyIndirectStyles(selection, mark, dimensions, context) {
applyClip(selection, mark, dimensions, context);
applyAttr(selection, "class", mark.className);
applyAttr(selection, "fill", mark.fill);
applyAttr(selection, "fill-opacity", mark.fillOpacity);
applyAttr(selection, "stroke", mark.stroke);
Expand Down
66 changes: 66 additions & 0 deletions test/output/classNameOnMarks.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions test/plots/class-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export async function classNameOnMarks() {
const sales = await d3.csv("data/fruit-sales.csv", d3.autoType);
return Plot.plot({
marginLeft: 50,
y: {
label: null,
reverse: true
},
marks: [
Plot.barX(
sales,
Plot.groupY({x: "sum"}, {x: "units", y: "fruit", sort: {y: "x", reverse: true}, className: "fruitbars"})
),
Plot.ruleX([0])
]
});
}
1 change: 1 addition & 0 deletions test/plots/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export * from "./cars-mpg.js";
export * from "./cars-parcoords.js";
export * from "./channel-domain.js";
export * from "./clamp.js";
export * from "./class-name.js";
export * from "./collapsed-histogram.js";
export * from "./color-piecewise.js";
export * from "./country-centroids.js";
Expand Down