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/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();