From fb16f12bf316a92e0a45cc5d1a0c8368c928baaa Mon Sep 17 00:00:00 2001 From: Eliot5566 Date: Fri, 20 Nov 2026 14:54:31 +0800 Subject: [PATCH 1/2] Split scattergl tozero fills where the line crosses zero A scattergl tozeroy or tozerox fill was one polygon closed along the zero line. When the line crosses zero, that closing edge crosses the line, and the triangulation of the self-intersecting polygon fills the wrong areas. Close the polygon on the zero line at each crossing and at each point on zero, start a new one there, and split the fill at the null point between them. Gaps are skipped, so the fill goes across them as in SVG scatter. Fixes #4017 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hq1ACTsK85bRrY28fUQ7b3 --- draftlogs/XXXX_fix.md | 1 + src/traces/scattergl/plot.js | 57 +++++++++++++++++++++++++++- test/jasmine/tests/scattergl_test.js | 48 +++++++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 draftlogs/XXXX_fix.md diff --git a/draftlogs/XXXX_fix.md b/draftlogs/XXXX_fix.md new file mode 100644 index 00000000000..203bac38225 --- /dev/null +++ b/draftlogs/XXXX_fix.md @@ -0,0 +1 @@ +- Fix `scattergl` `tozeroy` and `tozerox` fills when the line crosses zero [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)] diff --git a/src/traces/scattergl/plot.js b/src/traces/scattergl/plot.js index d271f8dda8a..101bf6d714a 100644 --- a/src/traces/scattergl/plot.js +++ b/src/traces/scattergl/plot.js @@ -42,6 +42,57 @@ function getViewport(fullLayout, xaxis, yaxis, plotGlPixelRatio) { ]; } +// Positions of a 'tozerox' or 'tozeroy' fill between the first and the last +// defined points, without the two end points on the zero line. +// The fill closes its polygon along the zero line. If the line crosses zero, +// that closing edge crosses the line and the polygon cannot be triangulated. +// So close the polygon on the zero line at each crossing and at each point on +// zero, and start a new one there. A null point separates the polygons, and the +// fill splits at it with the `splitNull` option. Gaps are skipped, so the fill +// goes across them as in SVG scatter. +// `ax` is the index of the coordinate that goes to zero: 1 for y, 0 for x. +function fillToZeroPositions(srcPos, first, last, ax) { + var pos = []; + var prev = -1; + + function zeroPoint(x, y) { + return ax ? [x, 0] : [0, y]; + } + + for(var j = first; j <= last; j += 2) { + var x = srcPos[j]; + var y = srcPos[j + 1]; + if(isNaN(x) || isNaN(y)) continue; + + if(prev !== -1) { + var x0 = srcPos[prev]; + var y0 = srcPos[prev + 1]; + var v0 = ax ? y0 : x0; + var v = ax ? y : x; + + if((v0 < 0 && v > 0) || (v0 > 0 && v < 0)) { + var t = v0 / (v0 - v); + var cross = zeroPoint(x0 + t * (x - x0), y0 + t * (y - y0)); + pos.push.apply(pos, cross); + pos.push(null, null); + pos.push.apply(pos, cross); + } + } + + pos.push(x, y); + + // a point on the zero line closes the polygon too + if((ax ? y : x) === 0 && j < last) { + pos.push(null, null); + pos.push(x, y); + } + + prev = j; + } + + return pos; +} + var exports = module.exports = function plot(gd, subplot, cdata) { if(!cdata.length) return; @@ -178,7 +229,8 @@ var exports = module.exports = function plot(gd, subplot, cdata) { if(srcPos[firstptdef + 1] !== 0) { pos = [srcPos[firstptdef], 0]; } - pos = pos.concat(srcPos.slice(firstptdef, lastptdef + 2)); + pos = pos.concat(fillToZeroPositions(srcPos, firstptdef, lastptdef, 1)); + fillOptions.splitNull = pos.indexOf(null) !== -1; if(srcPos[lastptdef + 1] !== 0) { pos = pos.concat([srcPos[lastptdef], 0]); } @@ -194,7 +246,8 @@ var exports = module.exports = function plot(gd, subplot, cdata) { if(srcPos[firstptdef] !== 0) { pos = [0, srcPos[firstptdef + 1]]; } - pos = pos.concat(srcPos.slice(firstptdef, lastptdef + 2)); + pos = pos.concat(fillToZeroPositions(srcPos, firstptdef, lastptdef, 0)); + fillOptions.splitNull = pos.indexOf(null) !== -1; if(srcPos[lastptdef] !== 0) { pos = pos.concat([ 0, srcPos[lastptdef + 1]]); } diff --git a/test/jasmine/tests/scattergl_test.js b/test/jasmine/tests/scattergl_test.js index 1e8b904ed28..7f325d177cf 100644 --- a/test/jasmine/tests/scattergl_test.js +++ b/test/jasmine/tests/scattergl_test.js @@ -645,6 +645,54 @@ describe('end-to-end scattergl tests', function() { .then(done, done.fail); }); + it('@gl should split tozero fills where the line crosses zero', function(done) { + function fillOptions(i) { + return gd._fullLayout._plots.xy._scene.fillOptions[i]; + } + + Plotly.newPlot(gd, [{ + type: 'scattergl', + mode: 'lines', + fill: 'tozeroy', + x: [0, 1, 2, 3], + y: [2, -2, null, 2] + }, { + type: 'scattergl', + mode: 'lines', + fill: 'tozerox', + x: [2, 0, -2], + y: [0, 1, 2] + }, { + type: 'scattergl', + mode: 'lines', + fill: 'tozeroy', + x: [0, 1, 2], + y: [1, 2, 1] + }]) + .then(function() { + // the gap is skipped, so the fill also splits where the line crosses zero across it + expect(fillOptions(0).positions).toEqual([ + 0, 0, 0, 2, 0.5, 0, null, null, + 0.5, 0, 1, -2, 2, 0, null, null, + 2, 0, 3, 2, 3, 0 + ], 'tozeroy'); + expect(fillOptions(0).splitNull).toBe(true, 'tozeroy splitNull'); + + // the point on zero closes the first polygon + expect(fillOptions(1).positions).toEqual([ + 0, 0, 2, 0, 0, 1, null, null, + 0, 1, -2, 2, 0, 2 + ], 'tozerox'); + expect(fillOptions(1).splitNull).toBe(true, 'tozerox splitNull'); + + expect(fillOptions(2).positions).toEqual([ + 0, 0, 0, 1, 1, 2, 2, 1, 2, 0 + ], 'no crossing'); + expect(fillOptions(2).splitNull).toBe(false, 'no crossing splitNull'); + }) + .then(done, done.fail); + }); + it('@gl should reset the sanp to length after react and not to TOO_MANY_POINTS constant', function(done) { Lib.seedPseudoRandom(); From 741c910c2e93ba6a7b349fa3588231944af83f0f Mon Sep 17 00:00:00 2001 From: Eliot5566 Date: Fri, 20 Nov 2026 14:56:21 +0800 Subject: [PATCH 2/2] Name the draftlog after PR 8181 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hq1ACTsK85bRrY28fUQ7b3 --- draftlogs/8181_fix.md | 1 + draftlogs/XXXX_fix.md | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 draftlogs/8181_fix.md delete mode 100644 draftlogs/XXXX_fix.md diff --git a/draftlogs/8181_fix.md b/draftlogs/8181_fix.md new file mode 100644 index 00000000000..77b3c161750 --- /dev/null +++ b/draftlogs/8181_fix.md @@ -0,0 +1 @@ +- Fix `scattergl` `tozeroy` and `tozerox` fills when the line crosses zero [[#8181](https://github.com/plotly/plotly.js/pull/8181)] diff --git a/draftlogs/XXXX_fix.md b/draftlogs/XXXX_fix.md deleted file mode 100644 index 203bac38225..00000000000 --- a/draftlogs/XXXX_fix.md +++ /dev/null @@ -1 +0,0 @@ -- Fix `scattergl` `tozeroy` and `tozerox` fills when the line crosses zero [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)]