Skip to content

Support z in the difference mark #1906

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 9 commits into from
Nov 7, 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: 9 additions & 1 deletion src/marks/difference.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {ChannelValueSpec} from "../channel.js";
import type {ChannelValue, ChannelValueSpec} from "../channel.js";
import type {CurveOptions} from "../curve.js";
import type {Data, MarkOptions, RenderableMark} from "../mark.js";

Expand Down Expand Up @@ -61,6 +61,14 @@ export interface DifferenceOptions extends MarkOptions, CurveOptions {
* defaults to **fillOpacity**.
*/
negativeFillOpacity?: number;

/**
* An optional ordinal channel for grouping data into series to be drawn as
* separate areas; defaults to **fillPositive** if a channel for the positive
* area, **fillNegative** if a channel for the negative area, or **stroke** if
* a channel.
*/
z?: ChannelValue;
}

/** TODO */
Expand Down
24 changes: 13 additions & 11 deletions src/marks/difference.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export function differenceY(
stroke,
strokeOpacity,
z = maybeColorChannel(stroke)[0],
clip = true,
tip,
render,
...options
Expand All @@ -45,7 +44,7 @@ export function differenceY(
z,
fill: positiveFill,
fillOpacity: positiveFillOpacity,
render: composeRender(render, clipDifference(true)),
render: composeRender(render, clipDifferenceY(true)),
...options
}),
{ariaLabel: "positive difference"}
Expand All @@ -61,7 +60,7 @@ export function differenceY(
z,
fill: negativeFill,
fillOpacity: negativeFillOpacity,
render: composeRender(render, clipDifference(false)),
render: composeRender(render, clipDifferenceY(false)),
...options
}),
{ariaLabel: "negative difference"}
Expand All @@ -74,7 +73,7 @@ export function differenceY(
stroke,
strokeOpacity,
tip,
clip,
clip: true,
...options
})
);
Expand Down Expand Up @@ -108,20 +107,23 @@ function memo(v) {
return {transform: (data) => V || (V = valueof(data, value)), label};
}

function clipDifference(positive) {
function clipDifferenceY(positive) {
return (index, scales, channels, dimensions, context, next) => {
const clip = getClipId();
const clipPath = create("svg:clipPath", context).attr("id", clip).node();
const {x1, x2} = channels;
const {height} = dimensions;
const y1 = new Float32Array(x1.length);
const y2 = new Float32Array(x2.length);
(positive === inferScaleOrder(scales.y) < 0 ? y1 : y2).fill(height);
const c = next(index, scales, {...channels, x2: x1, y2}, dimensions, context);
clipPath.append(...c.childNodes);
const c = next(index, scales, {...channels, x2: x1, y2}, dimensions, context).querySelectorAll("path");
const g = next(index, scales, {...channels, x1: x2, y1}, dimensions, context);
g.insertBefore(clipPath, g.firstChild);
g.setAttribute("clip-path", `url(#${clip})`);
let i = -1;
for (const node of g.querySelectorAll("path")) {
const clip = getClipId();
const clipPath = create("svg:clipPath", context).attr("id", clip).node();
clipPath.append(c[++i]);
node.parentElement.insertBefore(clipPath, node);
node.setAttribute("clip-path", `url(#${clip})`);
}
return g;
};
}
8 changes: 4 additions & 4 deletions test/output/differenceFilterX.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions test/output/differenceFilterY1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions test/output/differenceFilterY2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions test/output/differenceY.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions test/output/differenceY1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions test/output/differenceYClip.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 107 additions & 0 deletions test/output/differenceYClipVariable.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions test/output/differenceYConstant.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions test/output/differenceYCurve.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/output/differenceYNegative.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions test/output/differenceYOrdinal.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions test/output/differenceYOrdinalFlip.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading