Skip to content

Commit a06b6bf

Browse files
committed
Plot.plot defines update, not Plot.mark
1 parent 7044c13 commit a06b6bf

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

src/facet.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Facet extends Mark {
6767
}
6868
return {index, channels: [...channels, ...subchannels]};
6969
}
70-
render(index, scales, channels, dimensions, axes) {
70+
render(index, scales, channels, dimensions, axes, update) {
7171
const {marks, marksChannels, marksIndex, marksIndexByFacet} = this;
7272
const {fx, fy} = scales;
7373
const fyMargins = fy && {marginTop: 0, marginBottom: 0, height: fy.bandwidth()};
@@ -84,7 +84,7 @@ class Facet extends Mark {
8484
.data(domain)
8585
.join("g")
8686
.attr("transform", ky => `translate(0,${fy(ky)})`)
87-
.append((_, i) => (i === j ? axis1 : axis2).render(null, scales, null, fyDimensions));
87+
.append((_, i) => (i === j ? axis1 : axis2).render(null, scales, null, fyDimensions, null, update));
8888
}
8989
if (fx && axes.x) {
9090
const domain = fx.domain();
@@ -96,7 +96,7 @@ class Facet extends Mark {
9696
.data(domain)
9797
.join("g")
9898
.attr("transform", kx => `translate(${fx(kx)},0)`)
99-
.append((_, i) => (i === j ? axis1 : axis2).render(null, scales, null, fxDimensions));
99+
.append((_, i) => (i === j ? axis1 : axis2).render(null, scales, null, fxDimensions, null, update));
100100
}
101101
})
102102
.call(g => g.selectAll()
@@ -110,7 +110,9 @@ class Facet extends Mark {
110110
marksFacetIndex[i],
111111
scales,
112112
marksChannels[i],
113-
subdimensions
113+
subdimensions,
114+
null,
115+
update
114116
);
115117
if (node != null) this.appendChild(node);
116118
}

src/mark.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,6 @@ export class Mark {
4545
})
4646
};
4747
}
48-
update(g, values) {
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);
61-
}
6248
}
6349

6450
// TODO Type coercion?

src/marks/bar.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,21 @@ export class AbstractBar extends Mark {
4646
this.rx = number(rx);
4747
this.ry = number(ry);
4848
}
49-
render(I, scales, channels, dimensions) {
49+
render(I, scales, channels, dimensions, axes, update) {
5050
const {rx, ry} = this;
5151
const {color} = scales;
5252
const {z: Z, title: L, fill: F, stroke: S, picker: J} = channels;
5353
const index = filter(I, ...this._positions(channels), F, S);
5454
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
55+
const values = J && J.flat();
5556
const node = create("svg:g")
5657
.call(applyIndirectStyles, this)
5758
.call(this._transform, scales)
5859
.call(g => g.selectAll()
5960
.data(index)
6061
.join("rect")
6162
.call(J ? rect => rect
62-
.on("click", (event, i) => super.update(event.currentTarget, J[i]))
63+
.on("click", (event, i) => update(values, J[i]))
6364
: () => {}
6465
)
6566
.call(applyDirectStyles, this)
@@ -73,8 +74,7 @@ export class AbstractBar extends Mark {
7374
.call(ry != null ? rect => rect.attr("ry", ry) : () => {})
7475
.call(title(L)))
7576
.node();
76-
console.log({J});
77-
if (J) this.update(node, J.flat());
77+
if (values) update(values, values);
7878
return node;
7979
}
8080
_x({x}, {x: X}, {marginLeft}) {

src/marks/rect.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,23 @@ export class Rect extends Mark {
5555
render(
5656
I,
5757
{x, y, color},
58-
{x1: X1, y1: Y1, x2: X2, y2: Y2, z: Z, title: L, fill: F, stroke: S, picker: J}
58+
{x1: X1, y1: Y1, x2: X2, y2: Y2, z: Z, title: L, fill: F, stroke: S, picker: J},
59+
dimensions,
60+
axes,
61+
update
5962
) {
6063
const {rx, ry} = this;
6164
const index = filter(I, X1, Y2, X2, Y2, F, S);
6265
if (Z) index.sort((i, j) => ascending(Z[i], Z[j]));
66+
const values = J && J.flat();
6367
const node = create("svg:g")
6468
.call(applyIndirectStyles, this)
6569
.call(applyTransform, x, y)
6670
.call(g => g.selectAll()
6771
.data(index)
6872
.join("rect")
6973
.call(J ? rect => rect
70-
.on("click", (event, i) => super.update(event.currentTarget, J[i]))
74+
.on("click", (event, i) => update(values, J[i]))
7175
: () => {}
7276
)
7377
.call(applyDirectStyles, this)
@@ -81,7 +85,7 @@ export class Rect extends Mark {
8185
.attr("stroke", S && (i => color(S[i])))
8286
.call(title(L)))
8387
.node();
84-
if (J) this.update(node, J.flat());
88+
if (values) update(values, values);
8589
return node;
8690
}
8791
}

src/plot.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,19 @@ export function plot(options = {}) {
8282
if (typeof style === "string") this.style = style;
8383
else Object.assign(this.style, style);
8484
});
85+
86+
function update(values, sel) {
87+
const node = svg.node();
88+
node.value = (node.value === sel) ? values : sel;
89+
if (typeof window === 'undefined') return;
90+
console.log(node.value);
91+
node.dispatchEvent(new CustomEvent('input'));
92+
}
8593

8694
for (const mark of marks) {
8795
const channels = markChannels.get(mark);
8896
const index = markIndex.get(mark);
89-
const node = mark.render(index, scales, channels, dimensions, axes);
97+
const node = mark.render(index, scales, channels, dimensions, axes, update);
9098
if (node != null) svg.append(() => node);
9199
}
92100

0 commit comments

Comments
 (0)