Skip to content
Draft
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/XXXX_change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Upload `Float32Array` `x` and `y` of markers-only `scattergl` traces without a copy, and build the hover index of large traces only when the graph has hover [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)]
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"probe-image-size": "^7.5.0",
"regl-error2d": "^2.0.12",
"regl-line2d": "^3.1.4",
"regl-scatter2d": "^3.4.0",
"regl-scatter2d": "github:settylab/regl-scatter2d#dd2fb74d0c2f964025a3054953723f2e533cbdd3",
"regl-splom": "^1.0.14",
"strongly-connected-components": "^1.0.1",
"svg-path-sdf": "^2.0.0",
Expand Down
154 changes: 134 additions & 20 deletions src/traces/scattergl/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var convert = require('./convert');
var sceneUpdate = require('./scene_update');

var BADNUM = require('../../constants/numerical').BADNUM;
const FP_SAFE = require('../../constants/numerical').FP_SAFE;
var TOO_MANY_POINTS = require('./constants').TOO_MANY_POINTS;

module.exports = function calc(gd, trace) {
Expand Down Expand Up @@ -50,33 +51,51 @@ module.exports = function calc(gd, trace) {
trace._yEnds = yObj.ends;
}

// we need hi-precision for scatter2d,
// regl-scatter2d uses NaNs for bad/missing values
var positions = new Array(len2);
var _ids = new Array(len);
for(i = 0; i < len; i++) {
positions[i * 2] = x[i] === BADNUM ? NaN : x[i];
positions[i * 2 + 1] = y[i] === BADNUM ? NaN : y[i];
// Pre-compute ids.
_ids[i] = i;
}
// regl-scatter2d uploads Float32Array x and y without a copy, so calc builds no positions for them
const xy = xyPositions(gd, trace, xa, ya, x, y, len);
let positions = xy;
let _ids;
if (!xy) {
// we need hi-precision for scatter2d,
// regl-scatter2d uses NaNs for bad/missing values
positions = new Array(len2);
_ids = new Array(len);
for(i = 0; i < len; i++) {
positions[i * 2] = x[i] === BADNUM ? NaN : x[i];
positions[i * 2 + 1] = y[i] === BADNUM ? NaN : y[i];
// Pre-compute ids.
_ids[i] = i;
}

if(xa.type === 'log') {
for(i = 0; i < len2; i += 2) {
positions[i] = xa.c2l(positions[i]);
if(xa.type === 'log') {
for(i = 0; i < len2; i += 2) {
positions[i] = xa.c2l(positions[i]);
}
}
}
if(ya.type === 'log') {
for(i = 1; i < len2; i += 2) {
positions[i] = ya.c2l(positions[i]);
if(ya.type === 'log') {
for(i = 1; i < len2; i += 2) {
positions[i] = ya.c2l(positions[i]);
}
}
}

// we don't build a tree for log axes since it takes long to convert log2px
// and it is also
if(hasTooManyPoints && (xa.type !== 'log' && ya.type !== 'log')) {
// FIXME: delegate this to webworker
stash.tree = cluster(positions);
// Only hover reads the tree. Without hover, the first read builds the tree.
const makeTree = () => cluster(xy ? interleaved(x, y, len) : positions);
if (fullLayout.hovermode === false || trace.hoverinfo === 'skip') {
lazyProp(stash, 'tree', makeTree);
} else {
stash.tree = makeTree();
}
} else if (xy) {
lazyProp(stash, 'ids', () => {
const ids = new Uint32Array(len);
for (let k = 0; k < len; k++) ids[k] = k;
return ids;
});
} else {
stash.ids = _ids;
}
Expand All @@ -96,7 +115,12 @@ module.exports = function calc(gd, trace) {
} else if(opts.marker) {
ppad = opts.marker.sizeAvg || Math.max(opts.marker.size, 3);
}
calcAxisExpansion(gd, trace, xa, ya, x, y, ppad);
// A restyle recalculates every trace, so the extremes of Float32Array data come from a cache
if (xy && !Array.isArray(ppad)) {
calcAxisExpansion(gd, trace, xa, ya, extremesOf(x, len, xy.rev), extremesOf(y, len, xy.rev), ppad);
} else {
calcAxisExpansion(gd, trace, xa, ya, x, y, ppad);
}
if(opts.errorX) expandForErrorBars(trace, xa, opts.errorX);
if(opts.errorY) expandForErrorBars(trace, ya, opts.errorY);

Expand Down Expand Up @@ -125,12 +149,102 @@ module.exports = function calc(gd, trace) {
stash.index = scene.count;
stash.x = x;
stash.y = y;
stash.positions = positions;
if (!xy) stash.positions = positions;
scene.count++;

return [{x: false, y: false, t: stash, trace: trace}];
};

/**
* Get the positions of a trace that regl-scatter2d can upload without a copy.
*
* @param gd - the graph div
* @param trace - the full trace
* @param xa - the full x axis
* @param ya - the full y axis
* @param x - the calc x values
* @param y - the calc y values
* @param len - the number of points
* @returns `{x, y, count, rev}` for a markers-only trace on linear axes with Float32Array x and y, else null.
* `rev` is `layout.datarevision`. The same buffer range with the same `rev` is the same data.
*/
function xyPositions(gd, trace, xa, ya, x, y, len) {
if (!isFloat32Array(x) || !isFloat32Array(y)) return null;
if (xa.type !== 'linear' || ya.type !== 'linear') return null;
if (trace.xperiodalignment || trace.yperiodalignment) return null;
if (trace.mode !== 'markers') return null;
if (trace.fill && trace.fill !== 'none') return null;
if (trace.error_x?.visible || trace.error_y?.visible) return null;
if (x.length < len || y.length < len) return null;
return { x, y, count: len, rev: gd.layout?.datarevision };
}

const extremesCache = new WeakMap();

/**
* Get the finite extremes of a Float32Array. The function scans the array one time per buffer range and `rev`.
*
* @param v - the Float32Array
* @param len - the number of values to read
* @param rev - the revision of the data (`layout.datarevision`)
* @returns `[min, max]`, with the limits that `findExtremes` applies
*/
function extremesOf(v, len, rev) {
let byBuffer = extremesCache.get(v.buffer);
if (!byBuffer || byBuffer.rev !== rev) {
byBuffer = { rev, extremes: {} };
extremesCache.set(v.buffer, byBuffer);
}
const key = `${v.byteOffset}:${len}`;
let extremes = byBuffer.extremes[key];
if (extremes) return extremes;
let min = Infinity;
let max = -Infinity;
for (let i = 0; i < len; i++) {
const vi = v[i];
if (vi < min && vi > -FP_SAFE) min = vi;
if (vi > max && vi < FP_SAFE) max = vi;
}
extremes = byBuffer.extremes[key] = [min, max];
return extremes;
}

// `instanceof` fails for an array from another window
function isFloat32Array(v) {
return Object.prototype.toString.call(v) === '[object Float32Array]';
}

function interleaved(x, y, len) {
const positions = new Float64Array(len * 2);
for (let i = 0; i < len; i++) {
positions[i * 2] = x[i];
positions[i * 2 + 1] = y[i];
}
return positions;
}

/**
* Define a property that computes its value at the first read.
*
* @param stash - the object that gets the property
* @param key - the property name
* @param make - the function that returns the value
*/
function lazyProp(stash, key, make) {
let value;
Object.defineProperty(stash, key, {
configurable: true,
enumerable: true,
get: () => {
if (value === undefined) value = make();
return value;
},
set: (newValue) => {
value = newValue;
}
});
}

function expandForErrorBars(trace, ax, opts) {
var extremes = trace._extremes[ax._id];
var errExt = findExtremes(ax, opts._bnds, {padded: true});
Expand Down
Loading
Loading