Skip to content
Open
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
1 change: 1 addition & 0 deletions draftlogs/8222_add.md
Original file line number Diff line number Diff line change
@@ -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!
6 changes: 6 additions & 0 deletions src/traces/scattergl/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down
13 changes: 13 additions & 0 deletions src/traces/scattergl/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,26 @@ 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);
scene.fillOptions.push(opts.fill);
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);
Expand Down
8 changes: 8 additions & 0 deletions src/traces/scattergl/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,33 +366,41 @@ 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 {
s = markerSizeFunc(optsIn.size);
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);
Expand Down
1 change: 1 addition & 0 deletions src/traces/scattergl/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
25 changes: 2 additions & 23 deletions src/traces/scattergl/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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;
31 changes: 28 additions & 3 deletions src/traces/scattergl/scene_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/types/generated/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 72 additions & 0 deletions test/image/mocks/gl2d_cliponaxis_false.json
Original file line number Diff line number Diff line change
@@ -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" }
}
}
59 changes: 59 additions & 0 deletions test/jasmine/tests/gl2d_plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
});
Loading
Loading