Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix paired tip label #1769

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/marks/tip.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ export class Tip extends Mark {
const value = channel.value[i];
if (!defined(value) && channel.scale == null) continue;
if (key === "x2" && "x1" in sources) {
yield {name: formatLabel(scales, channel, "x"), value: formatPair(sources.x1, channel, i)};
yield {name: formatPairLabel(scales, sources.x1, channel, "x"), value: formatPair(sources.x1, channel, i)};
} else if (key === "y2" && "y1" in sources) {
yield {name: formatLabel(scales, channel, "y"), value: formatPair(sources.y1, channel, i)};
yield {name: formatPairLabel(scales, sources.y1, channel, "y"), value: formatPair(sources.y1, channel, i)};
} else {
const scale = channel.scale;
const line = {name: formatLabel(scales, channel, key), value: formatDefault(value)};
Expand Down Expand Up @@ -334,6 +334,12 @@ function formatPair(c1, c2, i) {
: `${formatDefault(c1.value[i])}–${formatDefault(c2.value[i])}`;
}

function formatPairLabel(scales, c1, c2, defaultLabel) {
const l1 = formatLabel(scales, c1, defaultLabel);
const l2 = formatLabel(scales, c2, defaultLabel);
return l1 === l2 ? l1 : `${l1}–${l2}`;
}

function formatLabel(scales, c, defaultLabel) {
return String(scales[c.scale]?.label ?? c?.label ?? defaultLabel);
}
66 changes: 66 additions & 0 deletions test/output/tipAreaBand.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/plots/tip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import * as Plot from "@observablehq/plot";
import * as d3 from "d3";
import {feature, mesh} from "topojson-client";

export async function tipAreaBand() {
const aapl = await d3.csv<any>("data/aapl.csv", d3.autoType);
return Plot.areaY(aapl, {x: "Date", y1: "Low", y2: "High", tip: true, curve: "step", stroke: "currentColor"}).plot();
}

export async function tipAreaStack() {
const industries = await d3.csv<any>("data/bls-industry-unemployment.csv", d3.autoType);
return Plot.areaY(industries, {x: "date", y: "unemployed", fill: "industry", tip: true}).plot({marginLeft: 50});
Expand Down