Skip to content

Commit f8a18a1

Browse files
authored
tip opacity swatch (#1594)
1 parent 86ac53e commit f8a18a1

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/marks/tip.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class Tip extends Mark {
120120
if ("title" in sources) {
121121
const text = sources.title.value[i];
122122
for (const line of mark.splitLines(formatDefault(text))) {
123-
yield ["", mark.clipLine(line)];
123+
yield {name: "", value: mark.clipLine(line)};
124124
}
125125
return;
126126
}
@@ -130,17 +130,19 @@ export class Tip extends Mark {
130130
const channel = sources[key];
131131
const value = channel.value[i];
132132
if (!defined(value) && channel.scale == null) continue;
133-
const color = channel.scale === "color" ? values[key][i] : undefined;
134133
if (key === "x2" && "x1" in sources) {
135-
yield [formatLabel(scales, channel, "x"), formatPair(sources.x1, channel, i)];
134+
yield {name: formatLabel(scales, channel, "x"), value: formatPair(sources.x1, channel, i)};
136135
} else if (key === "y2" && "y1" in sources) {
137-
yield [formatLabel(scales, channel, "y"), formatPair(sources.y1, channel, i)];
136+
yield {name: formatLabel(scales, channel, "y"), value: formatPair(sources.y1, channel, i)};
138137
} else {
139-
yield [formatLabel(scales, channel, key), formatDefault(value), color];
138+
const scale = channel.scale;
139+
const line = {name: formatLabel(scales, channel, key), value: formatDefault(value)};
140+
if (scale === "color" || scale === "opacity") line[scale] = values[key][i];
141+
yield line;
140142
}
141143
}
142-
if (index.fi != null && fx) yield [String(fx.label ?? "fx"), formatFx(index.fx)];
143-
if (index.fi != null && fy) yield [String(fy.label ?? "fy"), formatFy(index.fy)];
144+
if (index.fi != null && fx) yield {name: String(fx.label ?? "fx"), value: formatFx(index.fx)};
145+
if (index.fi != null && fy) yield {name: String(fy.label ?? "fy"), value: formatFy(index.fy)};
144146
}
145147

146148
// We don’t call applyChannelStyles because we only use the channels to
@@ -167,10 +169,11 @@ export class Tip extends Mark {
167169
this.setAttribute("stroke", "none");
168170
// iteratively render each channel value
169171
const names = new Set();
170-
for (const [name, value, color] of format(sources, i)) {
172+
for (const line of format(sources, i)) {
173+
const name = line.name;
171174
if (name && names.has(name)) continue;
172175
else names.add(name);
173-
renderLine(that, name, value, color);
176+
renderLine(that, line);
174177
}
175178
})
176179
)
@@ -181,7 +184,8 @@ export class Tip extends Mark {
181184
// just the initial layout of the text; in postrender we will compute the
182185
// exact text metrics and translate the text as needed once we know the
183186
// tip’s orientation (anchor).
184-
function renderLine(selection, name, value, color) {
187+
function renderLine(selection, {name, value, color, opacity}) {
188+
const swatch = color != null || opacity != null;
185189
let title;
186190
let w = lineWidth * 100;
187191
const [j] = cut(name, w, widthof, ee);
@@ -191,7 +195,7 @@ export class Tip extends Mark {
191195
title = value.trim();
192196
value = "";
193197
} else {
194-
if (name || (!value && !color)) value = " " + value;
198+
if (name || (!value && !swatch)) value = " " + value;
195199
const [k] = cut(value, w - widthof(name), widthof, ee);
196200
if (k >= 0) {
197201
// value is truncated
@@ -202,7 +206,7 @@ export class Tip extends Mark {
202206
const line = selection.append("tspan").attr("x", 0).attr("dy", `${lineHeight}em`).text("\u200b"); // zwsp for double-click
203207
if (name) line.append("tspan").attr("font-weight", "bold").text(name);
204208
if (value) line.append(() => document.createTextNode(value));
205-
if (color) line.append("tspan").text(" ■").attr("fill", color).style("user-select", "none");
209+
if (swatch) line.append("tspan").text(" ■").attr("fill", color).attr("fill-opacity", opacity).style("user-select", "none"); // prettier-ignore
206210
if (title) line.append("title").text(title);
207211
}
208212

0 commit comments

Comments
 (0)