Skip to content

Commit 7044c13

Browse files
committed
- default picker on bin and group transforms
- possible picker on rect and bar marks - sets the value on init — though it needs async again :( - click a second time to retrieve the full data - the picker is not tested, but it doesn't break tests anymore
1 parent 2b63994 commit 7044c13

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

src/mark.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@ export class Mark {
4646
};
4747
}
4848
update(g, values) {
49-
const svg = g.ownerSVGElement;
50-
console.log(values);
51-
svg.value = values;
52-
svg.dispatchEvent(new CustomEvent('input'));
49+
// 🌶 this doesn't work in node
50+
if (typeof window === 'undefined') return;
51+
// 🌶 async (to allow setting up the initial values)
52+
// todo: let the plot inform of the update method
53+
const all = (this.values || (this.values = values)); // keep initial values
54+
setTimeout(() => {
55+
const svg = g.ownerSVGElement;
56+
if (svg.value === values) values = all; // reset
57+
console.log(values);
58+
svg.value = values;
59+
svg.dispatchEvent(new CustomEvent('input'));
60+
}, 1);
5361
}
5462
}
5563

src/marks/bar.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class AbstractBar extends Mark {
1313
title,
1414
fill,
1515
stroke,
16+
picker,
1617
inset = 0,
1718
insetTop = inset,
1819
insetRight = inset,
@@ -32,7 +33,8 @@ export class AbstractBar extends Mark {
3233
{name: "z", value: z, optional: true},
3334
{name: "title", value: title, optional: true},
3435
{name: "fill", value: vfill, scale: "color", optional: true},
35-
{name: "stroke", value: vstroke, scale: "color", optional: true}
36+
{name: "stroke", value: vstroke, scale: "color", optional: true},
37+
{name: "picker", value: picker === true ? d => d : picker, optional: true}
3638
],
3739
options
3840
);
@@ -47,15 +49,19 @@ export class AbstractBar extends Mark {
4749
render(I, scales, channels, dimensions) {
4850
const {rx, ry} = this;
4951
const {color} = scales;
50-
const {z: Z, title: L, fill: F, stroke: S} = channels;
52+
const {z: Z, title: L, fill: F, stroke: S, picker: J} = channels;
5153
const index = filter(I, ...this._positions(channels), F, S);
5254
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
53-
return create("svg:g")
55+
const node = create("svg:g")
5456
.call(applyIndirectStyles, this)
5557
.call(this._transform, scales)
5658
.call(g => g.selectAll()
5759
.data(index)
5860
.join("rect")
61+
.call(J ? rect => rect
62+
.on("click", (event, i) => super.update(event.currentTarget, J[i]))
63+
: () => {}
64+
)
5965
.call(applyDirectStyles, this)
6066
.attr("x", this._x(scales, channels, dimensions))
6167
.attr("width", this._width(scales, channels, dimensions))
@@ -67,6 +73,9 @@ export class AbstractBar extends Mark {
6773
.call(ry != null ? rect => rect.attr("ry", ry) : () => {})
6874
.call(title(L)))
6975
.node();
76+
console.log({J});
77+
if (J) this.update(node, J.flat());
78+
return node;
7079
}
7180
_x({x}, {x: X}, {marginLeft}) {
7281
const {insetLeft} = this;

src/marks/rect.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class Rect extends Mark {
1616
title,
1717
fill,
1818
stroke,
19-
picker = d => d,
19+
picker,
2020
inset = 0,
2121
insetTop = inset,
2222
insetRight = inset,
@@ -40,7 +40,7 @@ export class Rect extends Mark {
4040
{name: "title", value: title, optional: true},
4141
{name: "fill", value: vfill, scale: "color", optional: true},
4242
{name: "stroke", value: vstroke, scale: "color", optional: true},
43-
{name: "picker", value: picker, optional: true}
43+
{name: "picker", value: picker === true ? d => d : picker, optional: true}
4444
],
4545
options
4646
);
@@ -60,7 +60,7 @@ export class Rect extends Mark {
6060
const {rx, ry} = this;
6161
const index = filter(I, X1, Y2, X2, Y2, F, S);
6262
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
63-
return create("svg:g")
63+
const node = create("svg:g")
6464
.call(applyIndirectStyles, this)
6565
.call(applyTransform, x, y)
6666
.call(g => g.selectAll()
@@ -81,6 +81,8 @@ export class Rect extends Mark {
8181
.attr("stroke", S && (i => color(S[i])))
8282
.call(title(L)))
8383
.node();
84+
if (J) this.update(node, J.flat());
85+
return node;
8486
}
8587
}
8688

src/transforms/group.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import {defined} from "../defined.js";
33
import {valueof, maybeColor, maybeTransform, maybeValue, maybeLazyChannel, lazyChannel, first, identity, take, maybeTuple} from "../mark.js";
44

55
export function groupX({x, ...options} = {}) {
6-
const [transform, X, y, z, fill, stroke] = group1(x, options);
7-
return {x: X, y, ...transform, z, fill, stroke};
6+
const [transform, X, y, z, fill, stroke, picker] = group1(x, options);
7+
return {x: X, y, ...transform, picker, z, fill, stroke};
88
}
99

1010
export function groupY({y, ...options} = {}) {
11-
const [transform, Y, x, z, fill, stroke] = group1(y, options);
12-
return {y: Y, x, ...transform, z, fill, stroke};
11+
const [transform, Y, x, z, fill, stroke, picker] = group1(y, options);
12+
return {y: Y, x, ...transform, picker, z, fill, stroke};
1313
}
1414

1515
export function groupR(options) {
@@ -21,8 +21,8 @@ export function groupFill(options) {
2121
}
2222

2323
export function group({x, y, out, ...options} = {}) {
24-
const [transform, X, Y, L, z, fill, stroke] = group2(x, y, options);
25-
return {x: X, y: Y, ...transform, z, fill, stroke, [out]: L};
24+
const [transform, X, Y, L, z, fill, stroke, picker] = group2(x, y, options);
25+
return {x: X, y: Y, ...transform, picker, z, fill, stroke, [out]: L};
2626
}
2727

2828
function group1(x = identity, {domain, normalize, ...options} = {}) {
@@ -35,6 +35,7 @@ function group1(x = identity, {domain, normalize, ...options} = {}) {
3535
const [vstroke] = maybeColor(stroke);
3636
const [F = fill, setF] = maybeLazyChannel(vfill);
3737
const [S = stroke, setS] = maybeLazyChannel(vstroke);
38+
const [J, setJ] = lazyChannel();
3839
const defined = maybeDomain(domain);
3940
return [
4041
{
@@ -71,14 +72,16 @@ function group1(x = identity, {domain, normalize, ...options} = {}) {
7172
}
7273
groupIndex.push(groupFacet);
7374
}
75+
setJ(groupData);
7476
return {data: groupData, index: groupIndex};
7577
})
7678
},
7779
X,
7880
Y,
7981
Z,
8082
F,
81-
S
83+
S,
84+
J
8285
];
8386
}
8487

@@ -96,6 +99,7 @@ function group2(xv, yv, {domain, normalize, ...options} = {}) {
9699
const [vstroke] = maybeColor(stroke);
97100
const [F = fill, setF] = maybeLazyChannel(vfill);
98101
const [S = stroke, setS] = maybeLazyChannel(vstroke);
102+
const [J, setJ] = lazyChannel();
99103
const xdefined = maybeDomain(xdomain);
100104
const ydefined = maybeDomain(ydomain);
101105
return [
@@ -139,6 +143,7 @@ function group2(xv, yv, {domain, normalize, ...options} = {}) {
139143
}
140144
groupIndex.push(groupFacet);
141145
}
146+
setJ(groupData);
142147
return {data: groupData, index: groupIndex};
143148
})
144149
},
@@ -147,7 +152,8 @@ function group2(xv, yv, {domain, normalize, ...options} = {}) {
147152
L,
148153
Z,
149154
F,
150-
S
155+
S,
156+
J
151157
];
152158
}
153159

0 commit comments

Comments
 (0)