Skip to content

Commit 6219edf

Browse files
committed
strokeWidth as a common style; move the filterDefined definition to style.js
1 parent 3d0d16c commit 6219edf

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,11 @@ All marks support the following optional channels:
473473
* **fill** - a fill color; bound to the *color* scale
474474
* **fillOpacity** - a fill opacity; bound to the *opacity* scale
475475
* **stroke** - a stroke color; bound to the *color* scale
476+
* **strokeWidth** - stroke width (in pixels)
476477
* **strokeOpacity** - a stroke opacity; bound to the *opacity* scale
477478
* **title** - a tooltip (a string of text, possibly with newlines)
478479

479-
The **fill**, **fillOpacity**, **stroke**, and **strokeOpacity** options can be specified as either channels or constants. When the fill or stroke is specified as a function or array, it is interpreted as a channel; when the fill or stroke is specified as a string, it is interpreted as a constant if a valid CSS color and otherwise it is interpreted as a column name for a channel. Similarly when the fill or stroke opacity is specified as a number, it is interpreted as a constant; otherwise it is interpeted as a channel. When the radius is specified as a number, it is interpreted as a constant; otherwise it is interpreted as a channel.
480+
The **fill**, **fillOpacity**, **stroke**, **strokeWidth**, and **strokeOpacity** options can be specified as either channels or constants. When the fill or stroke is specified as a function or array, it is interpreted as a channel; when the fill or stroke is specified as a string, it is interpreted as a constant if a valid CSS color and otherwise it is interpreted as a column name for a channel. Similarly when the fill or stroke opacity or the stroke width is specified as a number, it is interpreted as a constant; otherwise it is interpreted as a channel. When the radius is specified as a number, it is interpreted as a constant; otherwise it is interpreted as a channel.
480481

481482
The rectangular marks ([bar](#bar), [cell](#cell), and [rect](#rect)) support insets and rounded corner constant options:
482483

src/plot.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import {create} from "d3";
22
import {Axes, autoAxisTicks, autoAxisLabels} from "./axes.js";
3-
import {filter} from "./defined.js";
43
import {facets} from "./facet.js";
54
import {values} from "./mark.js";
65
import {Scales, autoScaleRange} from "./scales.js";
7-
import {offset} from "./style.js";
6+
import {offset, filterDefined} from "./style.js";
87

98
export function plot(options = {}) {
109
const {facet, style, caption} = options;
@@ -85,8 +84,7 @@ export function plot(options = {}) {
8584
for (const mark of marks) {
8685
const channels = markChannels.get(mark);
8786
const scaled = values(channels, scales);
88-
const index = filter(markIndex.get(mark), scaled.fill, scaled.fillOpacity, scaled.stroke, scaled.strokeOpacity);
89-
const node = mark.render(index, scales, scaled, dimensions, axes);
87+
const node = mark.render(filterDefined(markIndex.get(mark), scaled), scales, scaled, dimensions, axes);
9088
if (node != null) svg.appendChild(node);
9189
}
9290

src/style.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {string, number, maybeColor, maybeNumber, title, titleGroup} from "./mark.js";
2+
import {filter} from "./defined.js";
23

34
export const offset = typeof window !== "undefined" && window.devicePixelRatio > 1 ? 0 : 0.5;
45

@@ -49,12 +50,13 @@ export function styles(
4950
const [vfill, cfill] = maybeColor(fill, defaultFill);
5051
const [vfillOpacity, cfillOpacity] = maybeNumber(fillOpacity);
5152
const [vstrokeOpacity, cstrokeOpacity] = maybeNumber(strokeOpacity);
53+
const [vstrokeWidth, cstrokeWidth] = maybeNumber(strokeWidth);
5254
if (defaultFill !== null) {
5355
mark.fill = impliedString(cfill, "currentColor");
5456
mark.fillOpacity = impliedNumber(cfillOpacity, 1);
5557
}
5658
mark.stroke = impliedString(cstroke, "none");
57-
mark.strokeWidth = impliedNumber(strokeWidth, 1);
59+
mark.strokeWidth = impliedNumber(cstrokeWidth, 1);
5860
mark.strokeOpacity = impliedNumber(cstrokeOpacity, 1);
5961
mark.strokeLinejoin = impliedString(strokeLinejoin, "miter");
6062
mark.strokeLinecap = impliedString(strokeLinecap, "butt");
@@ -68,23 +70,26 @@ export function styles(
6870
{name: "fill", value: vfill, scale: "color", optional: true},
6971
{name: "fillOpacity", value: vfillOpacity, scale: "opacity", optional: true},
7072
{name: "stroke", value: vstroke, scale: "color", optional: true},
71-
{name: "strokeOpacity", value: vstrokeOpacity, scale: "opacity", optional: true}
73+
{name: "strokeOpacity", value: vstrokeOpacity, scale: "opacity", optional: true},
74+
{name: "strokeWidth", value: vstrokeWidth, optional: true}
7275
];
7376
}
7477

75-
export function applyChannelStyles(selection, {title: L, fill: F, fillOpacity: FO, stroke: S, strokeOpacity: SO}) {
78+
export function applyChannelStyles(selection, {title: L, fill: F, fillOpacity: FO, stroke: S, strokeOpacity: SO, strokeWidth: SW}) {
7679
applyAttr(selection, "fill", F && (i => F[i]));
7780
applyAttr(selection, "fill-opacity", FO && (i => FO[i]));
7881
applyAttr(selection, "stroke", S && (i => S[i]));
7982
applyAttr(selection, "stroke-opacity", SO && (i => SO[i]));
83+
applyAttr(selection, "stroke-width", SW && (i => SW[i]));
8084
title(L)(selection);
8185
}
8286

83-
export function applyGroupedChannelStyles(selection, {title: L, fill: F, fillOpacity: FO, stroke: S, strokeOpacity: SO}) {
87+
export function applyGroupedChannelStyles(selection, {title: L, fill: F, fillOpacity: FO, stroke: S, strokeOpacity: SO, strokeWidth: SW}) {
8488
applyAttr(selection, "fill", F && (([i]) => F[i]));
8589
applyAttr(selection, "fill-opacity", FO && (([i]) => FO[i]));
8690
applyAttr(selection, "stroke", S && (([i]) => S[i]));
8791
applyAttr(selection, "stroke-opacity", SO && (([i]) => SO[i]));
92+
applyAttr(selection, "stroke-width", SW && (([i]) => SW[i]));
8893
titleGroup(L)(selection);
8994
}
9095

@@ -128,3 +133,7 @@ export function impliedString(value, impliedValue) {
128133
export function impliedNumber(value, impliedValue) {
129134
if ((value = number(value)) !== impliedValue) return value;
130135
}
136+
137+
export function filterDefined(index, {fill, fillOpacity, stroke, strokeOpacity, strokeWidth}) {
138+
return filter(index, fill, fillOpacity, stroke, strokeOpacity, strokeWidth);
139+
}

0 commit comments

Comments
 (0)