Skip to content

prefer lineX when Y is monotonic, lineY when X is monotonic, for better tooltips #1558

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

Merged
merged 3 commits into from
May 15, 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
13 changes: 11 additions & 2 deletions src/marks/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,21 @@ export function autoSpec(data, options) {
colorMode = "stroke";
break;
case "line":
markImpl = X && Y ? line : X ? lineX : lineY; // 1d line by index
markImpl =
(X && Y) || xReduce || yReduce // same logic as area (see below), but default to line
? yZero || yReduce || (X && isMonotonic(X))
? lineY
: xZero || xReduce || (Y && isMonotonic(Y))
? lineX
: line
: X // 1d line by index
? lineX
: lineY;
Comment on lines +101 to +102
Copy link
Contributor Author

@Fil Fil May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per the above comment:

Suggested change
? lineX
: lineY;
? lineY
: lineX;

i'd also want to add this test

export async function autoLineHistogramY() {
  const aapl = await d3.csv<any>("data/aapl.csv", d3.autoType);
  return Plot.auto(aapl, {y: "Volume", mark: "line"}).plot({marginLeft: 80});
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the problem, but I don’t think that’s the right fix. I think the additional context in this example is that there’s a reducer for y, and so even those only x is provided as input, x is binned, effectively meaning that x is monotonic (and y is derived). So, we need to consider the reducers when determining the mark implementation, too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I think the latest commit fixes the problem.

colorMode = "stroke";
if (isHighCardinality(C)) Z = null; // TODO only if z not set by user
break;
case "area":
markImpl = yZero ? areaY : xZero || (Y && isMonotonic(Y)) ? areaX : areaY; // favor areaY if unsure
markImpl = !(yZero || yReduce) && (xZero || xReduce || (Y && isMonotonic(Y))) ? areaX : areaY; // favor areaY if unsure
colorMode = "fill";
if (isHighCardinality(C)) Z = null; // TODO only if z not set by user
break;
Expand Down
96 changes: 93 additions & 3 deletions test/marks/auto-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,39 @@ it("Plot.autoSpec makes a bar chart from an ordinal dimension", () => {
});
});

it("Plot.autoSpec makes a line from a monotonic dimension", () => {
it("Plot.autoSpec makes a lineY from monotonic x", () => {
const data = [
{date: 1, value: 1},
{date: 2, value: 0},
{date: 3, value: 38}
];
assert.deepStrictEqual(Plot.autoSpec(data, {x: "date", y: "value"}), {
fx: null,
fy: null,
x: {value: "date", reduce: null, zero: false},
y: {value: "value", reduce: null, zero: false},
color: {value: null, reduce: null},
size: {value: null, reduce: null},
mark: "line",
markImpl: "lineY",
markOptions: {
fx: undefined,
fy: undefined,
x: Object.assign([1, 2, 3], {label: "date"}),
y: Object.assign([1, 0, 38], {label: "value"}),
stroke: undefined,
z: undefined,
r: undefined
},
transformImpl: undefined,
transformOptions: {stroke: undefined, r: undefined},
colorMode: "stroke"
});
});

it("Plot.autoSpec makes a lineY from monotonic x and monotonic y", () => {
const data = [
{date: 1, value: 0},
{date: 2, value: 1},
{date: 3, value: 38}
];
Expand All @@ -67,12 +97,72 @@ it("Plot.autoSpec makes a line from a monotonic dimension", () => {
color: {value: null, reduce: null},
size: {value: null, reduce: null},
mark: "line",
markImpl: "line",
markImpl: "lineY",
markOptions: {
fx: undefined,
fy: undefined,
x: Object.assign([1, 2, 3], {label: "date"}),
y: Object.assign([1, 1, 38], {label: "value"}),
y: Object.assign([0, 1, 38], {label: "value"}),
stroke: undefined,
z: undefined,
r: undefined
},
transformImpl: undefined,
transformOptions: {stroke: undefined, r: undefined},
colorMode: "stroke"
});
});

it("Plot.autoSpec makes a lineX from monotonic y", () => {
const data = [
{date: 1, value: 1},
{date: 2, value: 0},
{date: 3, value: 38}
];
assert.deepStrictEqual(Plot.autoSpec(data, {x: "value", y: "date"}), {
fx: null,
fy: null,
x: {value: "value", reduce: null, zero: false},
y: {value: "date", reduce: null, zero: false},
color: {value: null, reduce: null},
size: {value: null, reduce: null},
mark: "line",
markImpl: "lineX",
markOptions: {
fx: undefined,
fy: undefined,
x: Object.assign([1, 0, 38], {label: "value"}),
y: Object.assign([1, 2, 3], {label: "date"}),
stroke: undefined,
z: undefined,
r: undefined
},
transformImpl: undefined,
transformOptions: {stroke: undefined, r: undefined},
colorMode: "stroke"
});
});

it("Plot.autoSpec makes a line from non-monotonic x and non-monotonic y", () => {
const data = [
{date: 2, value: 1},
{date: 1, value: 0},
{date: 3, value: 38}
];
assert.deepStrictEqual(Plot.autoSpec(data, {x: "date", y: "value", mark: "line"}), {
fx: null,
fy: null,
x: {value: "date", reduce: null, zero: false},
y: {value: "value", reduce: null, zero: false},
color: {value: null, reduce: null},
size: {value: null, reduce: null},
mark: "line",
markImpl: "line",
markOptions: {
fx: undefined,
fy: undefined,
x: Object.assign([2, 1, 3], {label: "date"}),
y: Object.assign([1, 0, 38], {label: "value"}),
stroke: undefined,
z: undefined,
r: undefined
Expand Down