Skip to content

top-level className #582

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

Merged
merged 4 commits into from
Oct 26, 2021
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ Plot.plot({
})
```

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

### Scale options

Plot passes data through [scales](https://observablehq.com/@observablehq/plot-scales) as needed before rendering marks. A scale maps abstract values such as time or temperature to visual values such as position or color. Within a given plot, marks share scales. For example, if a plot has two Plot.line marks, both share the same *x* and *y* scales for a consistent representation of data. (Plot does not currently support dual-axis charts, which are [not advised](https://blog.datawrapper.de/dualaxis/).)
Expand Down
14 changes: 7 additions & 7 deletions src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import {Axes, autoAxisTicks, autoScaleLabels} from "./axes.js";
import {facets} from "./facet.js";
import {markify} from "./mark.js";
import {Scales, autoScaleRange, applyScales, exposeScales, isOrdinalScale} from "./scales.js";
import {filterStyles, offset} from "./style.js";
import {filterStyles, maybeClassName, offset} from "./style.js";

export function plot(options = {}) {
const {facet, style, caption} = options;

// className for inline styles
const className = maybeClassName(options.className);

// When faceting, wrap all marks in a faceting mark.
if (facet !== undefined) {
const {marks} = options;
Expand Down Expand Up @@ -68,11 +71,8 @@ export function plot(options = {}) {

const {width, height} = dimensions;

// A unique identifier for this plot, for inline styles.
const uid = `plot-${Math.random().toString(16).slice(2)}`;

const svg = create("svg")
.attr("class", uid)
.attr("class", className)
.attr("fill", "currentColor")
.attr("font-family", "system-ui, sans-serif")
.attr("font-size", 10)
Expand All @@ -82,14 +82,14 @@ export function plot(options = {}) {
.attr("height", height)
.attr("viewBox", `0 0 ${width} ${height}`)
.call(svg => svg.append("style").text(`
.${uid} {
.${className} {
display: block;
background: white;
height: auto;
height: intrinsic;
max-width: 100%;
}
.${uid} text {
.${className} text {
white-space: pre;
}
`))
Expand Down
9 changes: 9 additions & 0 deletions src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,12 @@ export function filterStyles(index, {fill: F, fillOpacity: FO, stroke: S, stroke
function none(color) {
return color == null || color === "none";
}

const validClassName = /^-?([_a-z]|[\240-\377]|\\[0-9a-f]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-f])([_a-z0-9-]|[\240-\377]|\\[0-9a-f]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-f])*$/;

export function maybeClassName(name) {
if (name === undefined) return `plot-${Math.random().toString(16).slice(2)}`;
name = `${name}`;
if (!validClassName.test(name)) throw new Error(`invalid class name: ${name}`);
return name;
}
13 changes: 13 additions & 0 deletions test/output/aaplBollinger.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/aaplCandlestick.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/aaplChangeVolume.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/aaplClose.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/aaplCloseUntyped.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/aaplMonthly.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/aaplVolume.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/aaplVolumeRect.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/anscombeQuartet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/athletesHeightWeight.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/athletesHeightWeightBin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/athletesHeightWeightBinStroke.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/athletesHeightWeightSex.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/athletesHeightWeightSport.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/athletesNationality.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/athletesSexWeight.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/athletesSportSex.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/output/athletesSportWeight.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading