Skip to content

Commit c7afcb8

Browse files
committed
Merge branch 'fil/difference' into mbostock/difference
2 parents 36dd5ea + f1cee3d commit c7afcb8

22 files changed

+967
-68
lines changed

src/marks/difference.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {ChannelValueSpec} from "../channel.js";
1+
import type {ChannelValue, ChannelValueSpec} from "../channel.js";
22
import type {CurveOptions} from "../curve.js";
33
import type {Data, MarkOptions, RenderableMark} from "../mark.js";
44

@@ -61,6 +61,14 @@ export interface DifferenceOptions extends MarkOptions, CurveOptions {
6161
* defaults to **fillOpacity**.
6262
*/
6363
negativeFillOpacity?: number;
64+
65+
/**
66+
* An optional ordinal channel for grouping data into series to be drawn as
67+
* separate areas; defaults to **fillPositive** if a channel for the positive
68+
* area, **fillNegative** if a channel for the negative area, or **stroke** if
69+
* a channel.
70+
*/
71+
z?: ChannelValue;
6472
}
6573

6674
/** TODO */

src/marks/difference.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function differenceY(
2424
stroke,
2525
strokeOpacity,
2626
z = maybeColorChannel(stroke)[0],
27-
clip = true,
27+
clip, // optional additional clip for area
2828
tip,
2929
render,
3030
...options
@@ -45,7 +45,8 @@ export function differenceY(
4545
z,
4646
fill: positiveFill,
4747
fillOpacity: positiveFillOpacity,
48-
render: composeRender(render, clipDifference(true)),
48+
render: composeRender(render, clipDifferenceY(true)),
49+
clip,
4950
...options
5051
}),
5152
{ariaLabel: "positive difference"}
@@ -61,7 +62,8 @@ export function differenceY(
6162
z,
6263
fill: negativeFill,
6364
fillOpacity: negativeFillOpacity,
64-
render: composeRender(render, clipDifference(false)),
65+
render: composeRender(render, clipDifferenceY(false)),
66+
clip,
6567
...options
6668
}),
6769
{ariaLabel: "negative difference"}
@@ -74,7 +76,7 @@ export function differenceY(
7476
stroke,
7577
strokeOpacity,
7678
tip,
77-
clip,
79+
clip: true,
7880
...options
7981
})
8082
);
@@ -108,20 +110,24 @@ function memo(v) {
108110
return {transform: (data) => V || (V = valueof(data, value)), label};
109111
}
110112

111-
function clipDifference(positive) {
113+
function clipDifferenceY(positive) {
112114
return (index, scales, channels, dimensions, context, next) => {
113-
const clip = getClipId();
114-
const clipPath = create("svg:clipPath", context).attr("id", clip).node();
115115
const {x1, x2} = channels;
116116
const {height} = dimensions;
117117
const y1 = new Float32Array(x1.length);
118118
const y2 = new Float32Array(x2.length);
119119
(positive === inferScaleOrder(scales.y) < 0 ? y1 : y2).fill(height);
120-
const c = next(index, scales, {...channels, x2: x1, y2}, dimensions, context);
121-
clipPath.append(...c.childNodes);
122-
const g = next(index, scales, {...channels, x1: x2, y1}, dimensions, context);
123-
g.insertBefore(clipPath, g.firstChild);
124-
g.setAttribute("clip-path", `url(#${clip})`);
125-
return g;
120+
const oc = next(index, scales, {...channels, x2: x1, y2}, dimensions, context);
121+
const og = next(index, scales, {...channels, x1: x2, y1}, dimensions, context);
122+
const c = oc.querySelector("g") ?? oc; // applyClip
123+
const g = og.querySelector("g") ?? og; // applyClip
124+
for (let i = 0; c.firstChild; i += 2) {
125+
const id = getClipId();
126+
const clipPath = create("svg:clipPath", context).attr("id", id).node();
127+
clipPath.appendChild(c.firstChild);
128+
g.childNodes[i].setAttribute("clip-path", `url(#${id})`);
129+
g.insertBefore(clipPath, g.childNodes[i]);
130+
}
131+
return og;
126132
};
127133
}

test/output/differenceFilterX.svg

Lines changed: 4 additions & 4 deletions
Loading

test/output/differenceFilterY1.svg

Lines changed: 4 additions & 4 deletions
Loading

test/output/differenceFilterY2.svg

Lines changed: 4 additions & 4 deletions
Loading

test/output/differenceY.svg

Lines changed: 4 additions & 4 deletions
Loading

test/output/differenceY1.svg

Lines changed: 4 additions & 4 deletions
Loading

test/output/differenceYClip.svg

Lines changed: 87 additions & 0 deletions
Loading

test/output/differenceYClipVariable.svg

Lines changed: 107 additions & 0 deletions
Loading

test/output/differenceYConstant.svg

Lines changed: 4 additions & 4 deletions
Loading

test/output/differenceYCurve.svg

Lines changed: 4 additions & 4 deletions
Loading

test/output/differenceYNegative.svg

Lines changed: 2 additions & 2 deletions
Loading

test/output/differenceYOrdinal.svg

Lines changed: 66 additions & 0 deletions
Loading
Lines changed: 66 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)