Skip to content

Commit 9339c24

Browse files
Filmbostock
andauthored
fix: avoid double application of a scale transform (#1645)
* avoid double application of a scale transform when using a derived mark (such as a tip) * transform instead of transformed --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 20eeb56 commit 9339c24

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

src/channel.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export interface Channel {
109109
*/
110110
filter?: ((value: any) => boolean) | null;
111111

112+
/** Whether to apply the scale’s transform, if any; defaults to true. */
113+
transform?: boolean;
114+
112115
/**
113116
* An internal hint to affect the default construction of scales. For example,
114117
* the dot mark uses a channel hint to affect the default range of the

src/plot.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,20 @@ function applyScaleTransforms(channels, options) {
385385
return channels;
386386
}
387387

388-
// Note: mutates channel.value to apply the scale transform, if any.
388+
// Note: mutates channel.value to apply the scale transform, if any. Also sets
389+
// channel.transform to false to prevent duplicate transform application.
389390
function applyScaleTransform(channel, options) {
390-
const {scale} = channel;
391-
if (scale == null) return;
391+
const {scale, transform: t = true} = channel;
392+
if (scale == null || !t) return;
392393
const {
393394
type,
394395
percent,
395396
interval,
396397
transform = percent ? (x) => x * 100 : maybeIntervalTransform(interval, type)
397398
} = options[scale] ?? {};
398-
if (transform != null) channel.value = map(channel.value, transform);
399+
if (transform == null) return;
400+
channel.value = map(channel.value, transform);
401+
channel.transform = false;
399402
}
400403

401404
// An initializer may generate channels without knowing how the downstream mark

test/output/tipTransform.html

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<figure style="max-width: initial;"><svg class="plot-ramp" font-family="system-ui, sans-serif" font-size="10" width="240" height="50" viewBox="0 0 240 50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
2+
<style>
3+
.plot-ramp {
4+
display: block;
5+
background: white;
6+
height: auto;
7+
height: intrinsic;
8+
max-width: 100%;
9+
overflow: visible;
10+
}
11+
12+
.plot-ramp text {
13+
white-space: pre;
14+
}
15+
</style>
16+
<image x="0" y="18" width="240" height="10" preserveAspectRatio="none" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAABCAYAAAAxWXB3AAAABmJLR0QA/wD/AP+gvaeTAAABH0lEQVQ4jX2QQW4bUQxDn6hJ6sTtuosse/8jSuzi64/HblADAvlIDjDj+PP7y0f+4tCdQz9J3Tnyk9Qd6YPMT6QPlDekG5E3QjeUP4h8B43mO6E3yDm9QR44D9CBM4cTS0tTOAUpWsIZkwVW4ARn0BlYDDPedILlyT1+a5+MevKG7KWzQeusWv7M6uziGx9RxOxCRUQ/qU4uNHtFo9lo83dKk5csWT53TpPDYvW7S17OTeJ/2Zv90OkOb57s9Bduo531o1c/WBddf+32Rn7mKM6d2kQbFUsbokw00Ib6j5ahrzr+dfPEL88Nd4F79GTo8rrTQ8/umtepq6teXZXpnr5N1cW316uM757nPBt7vfZk7Z0t7/mM7c3lDMTy+/cXwM9vrI9SuXIAAAAASUVORK5CYII="></image>
17+
<g transform="translate(0,28)" fill="none" text-anchor="middle" font-variant="tabular-nums">
18+
<g class="tick" opacity="1" transform="translate(0.5,0)">
19+
<line stroke="currentColor" y2="6" y1="-10"></line>
20+
<text fill="currentColor" y="9" dy="0.71em">0</text>
21+
</g>
22+
<g class="tick" opacity="1" transform="translate(48.5,0)">
23+
<line stroke="currentColor" y2="6" y1="-10"></line>
24+
<text fill="currentColor" y="9" dy="0.71em">20</text>
25+
</g>
26+
<g class="tick" opacity="1" transform="translate(96.5,0)">
27+
<line stroke="currentColor" y2="6" y1="-10"></line>
28+
<text fill="currentColor" y="9" dy="0.71em">40</text>
29+
</g>
30+
<g class="tick" opacity="1" transform="translate(144.5,0)">
31+
<line stroke="currentColor" y2="6" y1="-10"></line>
32+
<text fill="currentColor" y="9" dy="0.71em">60</text>
33+
</g>
34+
<g class="tick" opacity="1" transform="translate(192.5,0)">
35+
<line stroke="currentColor" y2="6" y1="-10"></line>
36+
<text fill="currentColor" y="9" dy="0.71em">80</text>
37+
</g>
38+
<g class="tick" opacity="1" transform="translate(240.5,0)">
39+
<line stroke="currentColor" y2="6" y1="-10"></line>
40+
<text fill="currentColor" y="9" dy="0.71em">100</text>
41+
</g>
42+
</g>
43+
</svg><svg class="plot" fill="currentColor" font-family="system-ui, sans-serif" font-size="10" text-anchor="middle" width="245" height="60" viewBox="0 0 245 60" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
44+
<style>
45+
.plot {
46+
display: block;
47+
background: white;
48+
height: auto;
49+
height: intrinsic;
50+
max-width: 100%;
51+
}
52+
53+
.plot text,
54+
.plot tspan {
55+
white-space: pre;
56+
}
57+
</style>
58+
<g aria-label="x-axis tick" fill="none" stroke="currentColor" transform="translate(0.5,0)">
59+
<path transform="translate(20,30)" d="M0,0L0,6"></path>
60+
<path transform="translate(122.5,30)" d="M0,0L0,6"></path>
61+
<path transform="translate(225,30)" d="M0,0L0,6"></path>
62+
</g>
63+
<g aria-label="x-axis tick label" font-variant="tabular-nums" transform="translate(0.5,9.5)">
64+
<text y="0.71em" transform="translate(20,30)">0.0</text>
65+
<text y="0.71em" transform="translate(122.5,30)">0.5</text>
66+
<text y="0.71em" transform="translate(225,30)">1.0</text>
67+
</g>
68+
<g aria-label="dot" transform="translate(0.5,0.5)">
69+
<circle cx="20" cy="15" r="10" fill="rgb(35, 23, 27)"></circle>
70+
<circle cx="40.5" cy="15" r="10" fill="rgb(74, 88, 221)"></circle>
71+
<circle cx="81.5" cy="15" r="10" fill="rgb(39, 215, 196)"></circle>
72+
<circle cx="225" cy="15" r="10" fill="rgb(144, 12, 0)"></circle>
73+
</g>
74+
<g aria-label="tip" fill="white" stroke="currentColor" text-anchor="start" transform="translate(0.5,0.5)"></g>
75+
</svg></figure>

test/plots/tip.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,11 @@ export async function tipRuleAnchored() {
194194
]
195195
});
196196
}
197+
198+
export async function tipTransform() {
199+
return Plot.plot({
200+
width: 245,
201+
color: {percent: true, legend: true},
202+
marks: [Plot.dotX([0, 0.1, 0.3, 1], {fill: Plot.identity, r: 10, frameAnchor: "middle", tip: true})]
203+
});
204+
}

0 commit comments

Comments
 (0)