From 96ce35db9132e15c3f0642a46f1475616f1acb79 Mon Sep 17 00:00:00 2001 From: ben <226082140+benjibunbun@users.noreply.github.com> Date: Thu, 3 Dec 2026 02:13:15 +1100 Subject: [PATCH 1/2] Add `cliponaxis` to `scattergl` `scatter` has `cliponaxis`, but `scattergl` always clips markers at the plot area, so a marker on an axis shows only in part. If `cliponaxis` is `false`, the marker layers now get a larger viewport and range, so these markers show in full. Lines, fills, error bars and text stay clipped. --- draftlogs/XXXX_add.md | 1 + src/traces/scattergl/attributes.js | 6 + src/traces/scattergl/calc.js | 13 ++ src/traces/scattergl/convert.js | 8 + src/traces/scattergl/defaults.js | 1 + src/traces/scattergl/plot.js | 25 +-- src/traces/scattergl/scene_update.js | 31 +++- src/types/generated/schema.d.ts | 5 + test/image/mocks/gl2d_cliponaxis_false.json | 72 +++++++++ test/jasmine/tests/gl2d_plot_interact_test.js | 59 +++++++ test/jasmine/tests/scattergl_test.js | 147 ++++++++++++++++++ test/plot-schema.json | 6 + 12 files changed, 348 insertions(+), 26 deletions(-) create mode 100644 draftlogs/XXXX_add.md create mode 100644 test/image/mocks/gl2d_cliponaxis_false.json diff --git a/draftlogs/XXXX_add.md b/draftlogs/XXXX_add.md new file mode 100644 index 00000000000..218637cae14 --- /dev/null +++ b/draftlogs/XXXX_add.md @@ -0,0 +1 @@ +- Add `cliponaxis` to `scattergl` traces, so markers on the edge of the plot area show in full [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)], with thanks to @benjibunbun for the contribution! diff --git a/src/traces/scattergl/attributes.js b/src/traces/scattergl/attributes.js index 59188554c52..e837cd47b12 100644 --- a/src/traces/scattergl/attributes.js +++ b/src/traces/scattergl/attributes.js @@ -87,6 +87,12 @@ var attrs = (module.exports = overrideAll( }) }), connectgaps: scatterAttrs.connectgaps, + cliponaxis: extendFlat({}, scatterAttrs.cliponaxis, { + description: [ + 'Determines whether or not the subplot axes clip the markers.', + 'Lines, fills, error bars and text stay clipped.' + ].join(' ') + }), fill: extendFlat({}, scatterAttrs.fill, { dflt: 'none' }), fillcolor: makeFillcolorAttr(), diff --git a/src/traces/scattergl/calc.js b/src/traces/scattergl/calc.js index 240418b07af..10cc1433f28 100644 --- a/src/traces/scattergl/calc.js +++ b/src/traces/scattergl/calc.js @@ -108,6 +108,18 @@ module.exports = function calc(gd, trace) { if(opts.text && !scene.glText) scene.glText = true; if(opts.marker) opts.marker.snap = len; + let markerPad = 0; + if (opts.marker && trace.cliponaxis === false) { + const { sizes, size, sizeMax, borderSizes, borderSizeMax } = opts.marker; + const markerSize = Math.max(sizes ? sizeMax : size, opts.markerSel.size || 0, opts.markerUnsel.size || 0); + const borderSize = sizes ? borderSizeMax : borderSizes; + const symbol = trace.marker.symbol; + const hasSymbols = Lib.isArrayOrTypedArray(symbol) || symbol !== 'circle'; + + // regl-scatter2d draws circles in sprites `size + borderSize` wide and other symbols in sprites `2 * size` wide + markerPad = Math.max(hasSymbols ? markerSize : 0, (markerSize + borderSize) / 2); + } + scene.lineOptions.push(opts.line); scene.errorXOptions.push(opts.errorX); scene.errorYOptions.push(opts.errorY); @@ -115,6 +127,7 @@ module.exports = function calc(gd, trace) { scene.markerOptions.push(opts.marker); scene.markerSelectedOptions.push(opts.markerSel); scene.markerUnselectedOptions.push(opts.markerUnsel); + scene.markerPads.push(markerPad); scene.textOptions.push(opts.text); scene.textSelectedOptions.push(opts.textSel); scene.textUnselectedOptions.push(opts.textUnsel); diff --git a/src/traces/scattergl/convert.js b/src/traces/scattergl/convert.js index 7e5f0c0f464..b2c1fbdd8f8 100644 --- a/src/traces/scattergl/convert.js +++ b/src/traces/scattergl/convert.js @@ -366,11 +366,14 @@ function convertMarkerStyle(gd, trace) { var borderSizes = (optsOut.borderSizes = new Array(count)); var sizeTotal = 0; var sizeAvg; + let sizeMax = 0; + let borderSizeMax = 0; if (multiSize) { for (i = 0; i < count; i++) { sizes[i] = markerSizeFunc(optsIn.size[i]); sizeTotal += sizes[i]; + if (sizes[i] > sizeMax) sizeMax = sizes[i]; } sizeAvg = sizeTotal / count; } else { @@ -378,21 +381,26 @@ function convertMarkerStyle(gd, trace) { for (i = 0; i < count; i++) { sizes[i] = s; } + sizeMax = s; } // See https://github.com/plotly/plotly.js/pull/1781#discussion_r121820798 if (multiLineWidth) { for (i = 0; i < count; i++) { borderSizes[i] = optsIn.line.width[i]; + if (borderSizes[i] > borderSizeMax) borderSizeMax = +borderSizes[i]; } } else { s = optsIn.line.width; for (i = 0; i < count; i++) { borderSizes[i] = s; } + borderSizeMax = s; } optsOut.sizeAvg = sizeAvg; + optsOut.sizeMax = sizeMax; + optsOut.borderSizeMax = borderSizeMax; } else { optsOut.size = markerSizeFunc((optsIn && optsIn.size) || 10); optsOut.borderSizes = markerSizeFunc(optsIn.line.width); diff --git a/src/traces/scattergl/defaults.js b/src/traces/scattergl/defaults.js index 47f1dbd7e10..3d977eed909 100644 --- a/src/traces/scattergl/defaults.js +++ b/src/traces/scattergl/defaults.js @@ -47,6 +47,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout noStandOff: true }); coerce('marker.line.width', isOpen || isBubble ? 1 : 0); + coerce('cliponaxis'); } if (subTypes.hasLines(traceOut)) { diff --git a/src/traces/scattergl/plot.js b/src/traces/scattergl/plot.js index d271f8dda8a..a615c8a7202 100644 --- a/src/traces/scattergl/plot.js +++ b/src/traces/scattergl/plot.js @@ -356,7 +356,7 @@ var exports = module.exports = function plot(gd, subplot, cdata) { } // provide viewport and range - var vpRange0 = { + scene.update({ viewport: getViewport(fullLayout, xaxis, yaxis, gd._context.plotGlPixelRatio), // TODO do we need those fallbacks? range: [ @@ -365,28 +365,7 @@ var exports = module.exports = function plot(gd, subplot, cdata) { (xaxis._rl || xaxis.range)[1], (yaxis._rl || yaxis.range)[1] ] - }; - var vpRange = Lib.repeat(vpRange0, scene.count); - - // upload viewport/range data to GPU - if(scene.fill2d) { - scene.fill2d.update(vpRange); - } - if(scene.line2d) { - scene.line2d.update(vpRange); - } - if(scene.error2d) { - scene.error2d.update(vpRange.concat(vpRange)); - } - if(scene.scatter2d) { - scene.scatter2d.update(vpRange); - } - if(scene.select2d) { - scene.select2d.update(vpRange); - } - if(scene.glText) { - scene.glText.forEach(function(text) { text.update(vpRange0); }); - } + }); }; exports.reglPrecompiled = reglPrecompiled; diff --git a/src/traces/scattergl/scene_update.js b/src/traces/scattergl/scene_update.js index 080c524525c..50cf1c4db0b 100644 --- a/src/traces/scattergl/scene_update.js +++ b/src/traces/scattergl/scene_update.js @@ -24,7 +24,9 @@ module.exports = function sceneUpdate(gd, subplot) { textUnselectedOptions: [], // selection batches selectBatch: [], - unselectBatch: [] + unselectBatch: [], + // for each trace, how far in px the markers can draw past the plot area + markerPads: [] }; // regl- component stubs, initialized in dirty plot call @@ -50,11 +52,33 @@ module.exports = function sceneUpdate(gd, subplot) { scene.update = function update(opt) { var opts = Lib.repeat(opt, scene.count); + // grow the marker viewport by the pad, and the range by the same amount, so that edge markers show in full + const pixelRatio = gd._context.plotGlPixelRatio; + const markerOpts = opts.map((_, i) => { + const pad = scene.markerPads[i]; + if (!pad) return opt; + + // regl truncates viewports to whole device px, so pad by whole device px to keep markers on the lines + const vpPad = Math.ceil(pad * pixelRatio); + const markerOpt = { ...opt }; + if (opt.viewport) { + const [left, bottom, right, top] = opt.viewport; + markerOpt.viewport = [left - vpPad, bottom - vpPad, right + vpPad, top + vpPad]; + } + if (opt.range) { + const [x0, y0, x1, y1] = opt.range; + const dx = ((x1 - x0) * vpPad) / (subplot.xaxis._length * pixelRatio); + const dy = ((y1 - y0) * vpPad) / (subplot.yaxis._length * pixelRatio); + markerOpt.range = [x0 - dx, y0 - dy, x1 + dx, y1 + dy]; + } + return markerOpt; + }); + if(scene.fill2d) scene.fill2d.update(opts); - if(scene.scatter2d) scene.scatter2d.update(opts); + if (scene.scatter2d) scene.scatter2d.update(markerOpts); if(scene.line2d) scene.line2d.update(opts); if(scene.error2d) scene.error2d.update(opts.concat(opts)); - if(scene.select2d) scene.select2d.update(opts); + if (scene.select2d) scene.select2d.update(markerOpts); if(scene.glText) { for(var i = 0; i < scene.count; i++) { scene.glText[i].update(opt); @@ -132,6 +156,7 @@ module.exports = function sceneUpdate(gd, subplot) { scene.selectBatch = null; scene.unselectBatch = null; + scene.markerPads = null; // we can't just delete _scene, because `destroy` is called in the // middle of supplyDefaults, before relinkPrivateKeys which will put it back. diff --git a/src/types/generated/schema.d.ts b/src/types/generated/schema.d.ts index 853b8c815f9..9f6a044ef67 100644 --- a/src/types/generated/schema.d.ts +++ b/src/types/generated/schema.d.ts @@ -8395,6 +8395,11 @@ export interface ScattergeoData { } export interface ScatterglData { + /** + * Determines whether or not the subplot axes clip the markers. Lines, fills, error bars and text stay clipped. + * @default true + */ + cliponaxis?: boolean; /** * Determines whether or not gaps (i.e. {nan} or missing values) in the provided data arrays are connected. * @default false diff --git a/test/image/mocks/gl2d_cliponaxis_false.json b/test/image/mocks/gl2d_cliponaxis_false.json new file mode 100644 index 00000000000..f811396cd1a --- /dev/null +++ b/test/image/mocks/gl2d_cliponaxis_false.json @@ -0,0 +1,72 @@ +{ + "data": [ + { + "type": "scattergl", + "mode": "lines+markers", + "x": [0, 1, 2, 2.5, 3], + "y": [0, 2, 1, 3.6, 3], + "fill": "tozeroy", + "marker": { "size": [30, 20, 20, 20, 40] }, + "cliponaxis": false + }, + { + "type": "scattergl", + "mode": "markers+text", + "x": [0, 1.5, 3, 3], + "y": [3, 3, 1.5, 0], + "text": ["text stays clipped", "b", "c", "d"], + "textposition": "top right", + "marker": { + "size": 30, + "symbol": ["diamond", "star", "square", "cross"], + "line": { "width": 2, "color": "black" } + }, + "error_y": { "array": [1, 1, 1, 1] }, + "cliponaxis": false + }, + { + "type": "scattergl", + "mode": "lines+markers", + "x": [0, 1, 2, 2.5, 3], + "y": [0, 2, 1, 3.6, 3], + "fill": "tozeroy", + "marker": { "size": [30, 20, 20, 20, 40] }, + "xaxis": "x2", + "yaxis": "y2" + }, + { + "type": "scattergl", + "mode": "markers+text", + "x": [0, 1.5, 3, 3], + "y": [3, 3, 1.5, 0], + "text": ["text stays clipped", "b", "c", "d"], + "textposition": "top right", + "marker": { + "size": 30, + "symbol": ["diamond", "star", "square", "cross"], + "line": { "width": 2, "color": "black" } + }, + "error_y": { "array": [1, 1, 1, 1] }, + "xaxis": "x2", + "yaxis": "y2" + } + ], + "layout": { + "width": 800, + "height": 450, + "showlegend": false, + "xaxis": { + "domain": [0, 0.45], + "range": [0, 3], + "title": { "text": "cliponaxis: false" } + }, + "yaxis": { "range": [0, 3] }, + "xaxis2": { + "domain": [0.55, 1], + "range": [0, 3], + "anchor": "y2", + "title": { "text": "cliponaxis: true (default)" } + }, + "yaxis2": { "range": [0, 3], "anchor": "x2" } + } +} diff --git a/test/jasmine/tests/gl2d_plot_interact_test.js b/test/jasmine/tests/gl2d_plot_interact_test.js index 1ee3b711a77..bc65298a042 100644 --- a/test/jasmine/tests/gl2d_plot_interact_test.js +++ b/test/jasmine/tests/gl2d_plot_interact_test.js @@ -13,6 +13,7 @@ var touchEvent = require('../assets/touch_event'); var drag = require('../assets/drag'); var selectButton = require('../assets/modebar_button'); var delay = require('../assets/delay'); +const readPixel = require('../assets/read_pixel'); function countCanvases() { return d3SelectAll('canvas').size(); @@ -716,4 +717,62 @@ describe('Test gl2d plot interactions:', function() { }) .then(done, done.fail); }); + + it('@gl should keep the markers of `cliponaxis: false` traces unclipped while panning', (done) => { + const fns = drag.makeFns({ pos0: [200, 200], posN: [250, 150] }); + + const assertRanges = (msg) => { + const scene = gd._fullLayout._plots.xy._scene; + const [x0, x1] = gd._fullLayout.xaxis.range; + const [y0, y1] = gd._fullLayout.yaxis.range; + // the 20px marker radius is a tenth of the 200px plot area + const dx = (x1 - x0) / 10; + const dy = (y1 - y0) / 10; + + expect(scene.scatter2d.groups[0].range).toBeCloseToArray( + [x0 - dx, y0 - dy, x1 + dx, y1 + dy], + 2, + `markers ${msg}` + ); + expect(scene.scatter2d.groups[1].range).toBeCloseToArray([x0, y0, x1, y1], 2, `default markers ${msg}`); + expect(scene.line2d.passes[0].range).toBeCloseToArray([x0, y0, x1, y1], 2, `line ${msg}`); + }; + + Plotly.newPlot( + gd, + [ + { + type: 'scattergl', + mode: 'lines+markers', + x: [0, 1, 1.5], + y: [1, 1, 1.5], + marker: { size: 40 }, + cliponaxis: false + }, + { type: 'scattergl', mode: 'markers', x: [1], y: [0], marker: { size: 40 } } + ], + { + width: 400, + height: 400, + margin: { l: 100, r: 100, t: 100, b: 100 }, + xaxis: { range: [0, 2] }, + yaxis: { range: [0, 2] }, + dragmode: 'pan', + showlegend: false + }, + { plotGlPixelRatio: 2 } + ) + .then(fns.start) + .then(() => { + expect(gd._fullLayout.xaxis.range).toBeCloseToArray([-0.5, 1.5]); + expect(gd._fullLayout.yaxis.range).toBeCloseToArray([-0.5, 1.5]); + assertRanges('while panning'); + + // the marker at (1.5, 1.5) is now on the top right corner of the plot area + expect(readPixel(gd.querySelector('.gl-canvas-context'), 620, 620)[3]).not.toBe(0); + }) + .then(fns.end) + .then(() => assertRanges('after panning')) + .then(done, done.fail); + }); }); diff --git a/test/jasmine/tests/scattergl_test.js b/test/jasmine/tests/scattergl_test.js index 1e8b904ed28..892e1d64d85 100644 --- a/test/jasmine/tests/scattergl_test.js +++ b/test/jasmine/tests/scattergl_test.js @@ -11,6 +11,7 @@ var failTest = require('../assets/fail_test'); var delay = require('../assets/delay'); var readPixel = require('../assets/read_pixel'); var checkTextTemplate = require('../assets/check_texttemplate'); +const supplyAllDefaults = require('../assets/supply_defaults'); describe('scattergl marker symbol variants', function() { var symbolCases = [ @@ -43,6 +44,22 @@ describe('scattergl marker symbol variants', function() { }); }); +describe('scattergl defaults', () => { + it('should coerce `cliponaxis` only for traces with markers', () => { + const gd = { + data: [ + { type: 'scattergl', mode: 'markers', y: [1] }, + { type: 'scattergl', mode: 'lines+markers', y: [1], cliponaxis: false }, + { type: 'scattergl', mode: 'lines', y: [1], cliponaxis: false }, + { type: 'scattergl', mode: 'text', y: [1], text: ['a'], cliponaxis: false } + ] + }; + supplyAllDefaults(gd); + + expect(gd._fullData.map((trace) => trace.cliponaxis)).toEqual([true, false, undefined, undefined]); + }); +}); + describe('end-to-end scattergl tests', function() { var gd; @@ -803,6 +820,136 @@ describe('end-to-end scattergl tests', function() { }) .then(done, done.fail); }); + + it('@gl should draw the markers of `cliponaxis: false` traces past the axes, but not their lines and error bars', (done) => { + const isDrawn = (x, y) => readPixel(gd.querySelector('.gl-canvas-context'), x, y)[3] !== 0; + + Plotly.newPlot( + gd, + [ + { + type: 'scattergl', + mode: 'lines+markers', + x: [-1, 1, 2], + y: [1, 1, 2], + marker: { size: 40 }, + error_y: { array: [0, 2, 0] }, + cliponaxis: false + }, + { type: 'scattergl', mode: 'markers', x: [0.5], y: [0], marker: { size: 40 } } + ], + { + width: 400, + height: 400, + margin: { l: 100, r: 100, t: 100, b: 100 }, + xaxis: { range: [0, 2] }, + yaxis: { range: [0, 2] }, + showlegend: false + }, + { plotGlPixelRatio: 2 } + ) + .then(() => { + const scene = gd._fullLayout._plots.xy._scene; + const plotArea = { x: 200, y: 200, width: 400, height: 400 }; + + // the pad is the 20px marker radius: 40 device px, or 0.2 in data units + expect(scene.scatter2d.groups[0].viewport).toEqual({ x: 160, y: 160, width: 480, height: 480 }); + expect(scene.scatter2d.groups[0].range).toBeCloseToArray([-0.2, -0.2, 2.2, 2.2]); + expect(scene.scatter2d.groups[1].viewport).toEqual(plotArea); + expect(scene.scatter2d.groups[1].range).toEqual([0, 0, 2, 2]); + expect(scene.line2d.passes[0].viewport).toEqual(plotArea); + expect(scene.error2d.groups[2].viewport).toEqual(plotArea); + + expect(isDrawn(620, 620)).toBe(true, 'marker past the top right corner'); + expect(isDrawn(220, 400)).toBe(true, 'line inside'); + expect(isDrawn(180, 400)).toBe(false, 'line past the left edge'); + expect(isDrawn(400, 560)).toBe(true, 'error bar inside'); + expect(isDrawn(400, 620)).toBe(false, 'error bar past the top edge'); + expect(isDrawn(300, 220)).toBe(true, 'default marker inside'); + expect(isDrawn(300, 180)).toBe(false, 'default marker past the bottom edge'); + }) + .then(done, done.fail); + }); + + it('@gl should pad the marker viewport of `cliponaxis: false` traces by the largest marker sprite', (done) => { + const viewportLeft = ({ viewport }) => viewport.x; + const assertViewportLeft = (expected, msg) => { + const scene = gd._fullLayout._plots.xy._scene; + expect(scene.scatter2d.groups.map(viewportLeft)).toEqual(expected, `scatter2d ${msg}`); + expect(scene.select2d.groups.map(viewportLeft)).toEqual(expected, `select2d ${msg}`); + }; + + Plotly.newPlot( + gd, + [ + { + type: 'scattergl', + mode: 'markers', + x: [1], + y: [1], + marker: { size: 15, line: { width: 0.5 } }, + cliponaxis: false + }, + { + type: 'scattergl', + mode: 'markers', + x: [1], + y: [1], + marker: { size: 10, symbol: 'square', line: { width: 4 } }, + cliponaxis: false + }, + { + type: 'scattergl', + mode: 'markers', + x: [1, 2], + y: [1, 2], + marker: { size: [10, 30] }, + selected: { marker: { size: 50 } }, + selectedpoints: [0], + cliponaxis: false + }, + { + type: 'scattergl', + mode: 'markers', + x: [1, 2], + y: [1, 2], + marker: { size: [10, 30], line: { width: [6, 2] } }, + cliponaxis: false + } + ], + { + width: 400, + height: 400, + margin: { l: 100, r: 100, t: 100, b: 100 }, + xaxis: { range: [0, 2] }, + yaxis: { range: [0, 2] }, + showlegend: false + }, + { plotGlPixelRatio: 2 } + ) + .then(() => { + const scene = gd._fullLayout._plots.xy._scene; + assertViewportLeft( + [ + // a circle pads by (15 + 0.5) / 2 = 7.75px, which is 15.5 device px and rounds up to 16 + 200 - 16, + // a square pads by its 10px size, which is 20 device px + 200 - 20, + // the selected size and the 1px bubble border set the pad: (50 + 1) / 2 = 25.5px + 200 - 51, + // the largest size and the largest border set the pad: (30 + 6) / 2 = 18px + 200 - 36 + ], + 'with `cliponaxis: false`' + ); + // the range grows by the same 16 device px: 2 * 16 / 400 + expect(scene.scatter2d.groups[0].range).toBeCloseToArray([-0.08, -0.08, 2.08, 2.08]); + + return Plotly.restyle(gd, 'cliponaxis', true); + }) + .then(() => assertViewportLeft([200, 200, 200, 200], 'after the restyle to `cliponaxis: true`')) + .then(done, done.fail); + }); }); describe('Test scattergl autorange:', function() { diff --git a/test/plot-schema.json b/test/plot-schema.json index 7ab1ae48d50..dd63274b932 100644 --- a/test/plot-schema.json +++ b/test/plot-schema.json @@ -61766,6 +61766,12 @@ "scattergl": { "animatable": false, "attributes": { + "cliponaxis": { + "description": "Determines whether or not the subplot axes clip the markers. Lines, fills, error bars and text stay clipped.", + "dflt": true, + "editType": "calc", + "valType": "boolean" + }, "connectgaps": { "description": "Determines whether or not gaps (i.e. {nan} or missing values) in the provided data arrays are connected.", "dflt": false, From 9b1b0aa6b67b83c8109a4c370c1a2459579b49fb Mon Sep 17 00:00:00 2001 From: ben <226082140+benjibunbun@users.noreply.github.com> Date: Thu, 3 Dec 2026 02:14:25 +1100 Subject: [PATCH 2/2] Rename the draftlog to the pull request number --- draftlogs/8222_add.md | 1 + draftlogs/XXXX_add.md | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 draftlogs/8222_add.md delete mode 100644 draftlogs/XXXX_add.md diff --git a/draftlogs/8222_add.md b/draftlogs/8222_add.md new file mode 100644 index 00000000000..bfb5244fbf1 --- /dev/null +++ b/draftlogs/8222_add.md @@ -0,0 +1 @@ +- Add `cliponaxis` to `scattergl` traces, so markers on the edge of the plot area show in full [[#8222](https://github.com/plotly/plotly.js/pull/8222)], with thanks to @benjibunbun for the contribution! diff --git a/draftlogs/XXXX_add.md b/draftlogs/XXXX_add.md deleted file mode 100644 index 218637cae14..00000000000 --- a/draftlogs/XXXX_add.md +++ /dev/null @@ -1 +0,0 @@ -- Add `cliponaxis` to `scattergl` traces, so markers on the edge of the plot area show in full [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)], with thanks to @benjibunbun for the contribution!