Skip to content

Commit 194e1f4

Browse files
authored
fix paired tip label (#1769)
1 parent 13b5223 commit 194e1f4

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

src/marks/tip.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ export class Tip extends Mark {
135135
const value = channel.value[i];
136136
if (!defined(value) && channel.scale == null) continue;
137137
if (key === "x2" && "x1" in sources) {
138-
yield {name: formatLabel(scales, channel, "x"), value: formatPair(sources.x1, channel, i)};
138+
yield {name: formatPairLabel(scales, sources.x1, channel, "x"), value: formatPair(sources.x1, channel, i)};
139139
} else if (key === "y2" && "y1" in sources) {
140-
yield {name: formatLabel(scales, channel, "y"), value: formatPair(sources.y1, channel, i)};
140+
yield {name: formatPairLabel(scales, sources.y1, channel, "y"), value: formatPair(sources.y1, channel, i)};
141141
} else {
142142
const scale = channel.scale;
143143
const line = {name: formatLabel(scales, channel, key), value: formatDefault(value)};
@@ -334,6 +334,12 @@ function formatPair(c1, c2, i) {
334334
: `${formatDefault(c1.value[i])}${formatDefault(c2.value[i])}`;
335335
}
336336

337+
function formatPairLabel(scales, c1, c2, defaultLabel) {
338+
const l1 = formatLabel(scales, c1, defaultLabel);
339+
const l2 = formatLabel(scales, c2, defaultLabel);
340+
return l1 === l2 ? l1 : `${l1}${l2}`;
341+
}
342+
337343
function formatLabel(scales, c, defaultLabel) {
338344
return String(scales[c.scale]?.label ?? c?.label ?? defaultLabel);
339345
}

test/output/tipAreaBand.svg

Lines changed: 66 additions & 0 deletions
Loading

test/plots/tip.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import * as Plot from "@observablehq/plot";
22
import * as d3 from "d3";
33
import {feature, mesh} from "topojson-client";
44

5+
export async function tipAreaBand() {
6+
const aapl = await d3.csv<any>("data/aapl.csv", d3.autoType);
7+
return Plot.areaY(aapl, {x: "Date", y1: "Low", y2: "High", tip: true, curve: "step", stroke: "currentColor"}).plot();
8+
}
9+
510
export async function tipAreaStack() {
611
const industries = await d3.csv<any>("data/bls-industry-unemployment.csv", d3.autoType);
712
return Plot.areaY(industries, {x: "date", y: "unemployed", fill: "industry", tip: true}).plot({marginLeft: 50});

0 commit comments

Comments
 (0)