Skip to content

Cross-chart context management #1989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
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
38 changes: 35 additions & 3 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3027,9 +3027,41 @@ function makePlotFramework(gd) {
// TODO: sort out all the ordering so we don't have to
// explicitly delete anything
fullLayout._glcontainer = fullLayout._paperdiv.selectAll('.gl-container')
.data([0]);
fullLayout._glcontainer.enter().append('div')
.classed('gl-container', true);
.data([{}]);

// FIXME: bring this constant to some plotly constants module
// it is taken from parcoords lineLayerModel
fullLayout._glcanvas = fullLayout._glcontainer.enter().append('div')
.classed('gl-container', true)
.selectAll('.gl-canvas')
.data([{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to make objects here: .data(['context', 'focus', 'pick'], String) would suffice, where String is the key function.

Copy link
Contributor Author

@dy dy Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etpinard these objects are used by parcoords later, strings do not allow for putting additional stuff in them.
In particular, https://github.com/plotly/plotly.js/pull/1989/files#diff-2fad23de82bf8c7625ea8e11436233b7R301

key: 'contextLayer'
}, {
key: 'focusLayer'
}, {
key: 'pickLayer'
}]);

// create canvases only in case if there is at least one regl component
// FIXME: probably there is a better d3 way of doing so
for(var i = 0; i < fullLayout._modules.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using fullLayout._has would clean this up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fullLayout._has works for plots, not traces, if I get it right

var module = fullLayout._modules[i];
if(module.categories && module.categories.indexOf('gl') >= 0) {
fullLayout._glcanvas.enter().append('canvas')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. so this create the canvases properly, but we'll need to clear them when fullLayout._has('gl') === false.

Copy link
Contributor Author

@dy dy Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit more complicated, from interaction tests... Working on that. Sometimes we deal with

Plotly.plot(document.body, [{}]);
Plotly.restyle(document.body, {type: 'parcoords', x: [[1]]}, 0);

.attr('class', function(d) {
return 'gl-canvas gl-canvas-' + d.key.replace('Layer', '');
})
.attr('width', fullLayout.width)
.attr('height', fullLayout.height)
.style('position', 'absolute')
.style('top', 0)
.style('left', 0)
.style('pointer-events', 'none')
.style('overflow', 'visible');

break;
}
}

fullLayout._paperdiv.selectAll('.main-svg').remove();

Expand Down
41 changes: 15 additions & 26 deletions src/traces/parcoords/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var extendDeep = require('../../lib/extend').extendDeep;
var extendFlat = require('../../lib/extend').extendFlat;

module.exports = {

domain: {
x: {
valType: 'info_array',
Expand Down Expand Up @@ -120,36 +119,26 @@ module.exports = {
description: 'The dimensions (variables) of the parallel coordinates chart. 2..60 dimensions are supported.'
},

line: extendFlat({},

line: extendFlat(
// the default autocolorscale isn't quite usable for parcoords due to context ambiguity around 0 (grey, off-white)
// autocolorscale therefore defaults to false too, to avoid being overridden by the blue-white-red autocolor palette
// autocolorscale therefore defaults to false too, to avoid being overridden by the blue-white-red autocolor palette
extendDeep(
{},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this?

colorAttributes and friends are used in other place in plotly.js, so we can't just extend them here. We need to make a copy of them and then overwrite a few keys.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etpinard colorAttributes(str) returns a new object every call, no need to copy a copy.

colorAttributes('line'),
{
colorscale: extendDeep(
{},
colorAttributes('line').colorscale,
{dflt: colorscales.Viridis}
),
autocolorscale: extendDeep(
{},
colorAttributes('line').autocolorscale,
{
dflt: false,
description: [
'Has an effect only if line.color` is set to a numerical array.',
'Determines whether the colorscale is a default palette (`autocolorscale: true`)',
'or the palette determined by `line.colorscale`.',
'In case `colorscale` is unspecified or `autocolorscale` is true, the default ',
'palette will be chosen according to whether numbers in the `color` array are',
'all positive, all negative or mixed.',
'The default value is false, so that `parcoords` colorscale can default to `Viridis`.'
].join(' ')
}
)

colorscale: {dflt: colorscales.Viridis},
autocolorscale: {
dflt: false,
description: [
'Has an effect only if line.color` is set to a numerical array.',
'Determines whether the colorscale is a default palette (`autocolorscale: true`)',
'or the palette determined by `line.colorscale`.',
'In case `colorscale` is unspecified or `autocolorscale` is true, the default ',
'palette will be chosen according to whether numbers in the `color` array are',
'all positive, all negative or mixed.',
'The default value is false, so that `parcoords` colorscale can default to `Viridis`.'
].join(' ')
}
}
),

Expand Down
19 changes: 7 additions & 12 deletions src/traces/parcoords/base_plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var d3 = require('d3');
var Plots = require('../../plots/plots');
var parcoordsPlot = require('./plot');
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
var c = require('./constants');

exports.name = 'parcoords';

Expand All @@ -28,8 +27,6 @@ exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout)
var hasParcoords = (newFullLayout._has && newFullLayout._has('parcoords'));

if(hadParcoords && !hasParcoords) {
oldFullLayout._paperdiv.selectAll('.parcoords-line-layers').remove();
oldFullLayout._paperdiv.selectAll('.parcoords-line-layers').remove();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this done now?

Copy link
Contributor Author

@dy dy Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we keep canvases, I guessed we have to store them instead of recreating every time.
Actually removing and creating anew would solve the problem... Should I try to do that?

oldFullLayout._paperdiv.selectAll('.parcoords').remove();
oldFullLayout._paperdiv.selectAll('.parcoords').remove();
oldFullLayout._glimages.selectAll('*').remove();
Expand All @@ -41,23 +38,21 @@ exports.toSVG = function(gd) {
var imageRoot = gd._fullLayout._glimages;
var root = d3.select(gd).selectAll('.svg-container');
var canvases = root.filter(function(d, i) {return i === root.size() - 1;})
.selectAll('.parcoords-lines.context, .parcoords-lines.focus');
.selectAll('.gl-canvas-context, .gl-canvas-focus');

function canvasToImage(d) {
function canvasToImage() {
var canvas = this;
var imageData = canvas.toDataURL('image/png');
var image = imageRoot.append('svg:image');
var size = gd._fullLayout._size;
var domain = gd._fullData[d.model.key].domain;

image.attr({
xmlns: xmlnsNamespaces.svg,
'xlink:href': imageData,
x: size.l + size.w * domain.x[0] - c.overdrag,
y: size.t + size.h * (1 - domain.y[1]),
width: (domain.x[1] - domain.x[0]) * size.w + 2 * c.overdrag,
height: (domain.y[1] - domain.y[0]) * size.h,
preserveAspectRatio: 'none'
preserveAspectRatio: 'none',
x: 0,
y: 0,
width: canvas.width,
height: canvas.height
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice simplification 👌

});
}

Expand Down
40 changes: 33 additions & 7 deletions src/traces/parcoords/lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

var createREGL = require('regl');
var glslify = require('glslify');
var verticalPadding = require('./constants').verticalPadding;
var c = require('./constants');
var vertexShaderSource = glslify('./shaders/vertex.glsl');
var pickVertexShaderSource = glslify('./shaders/pick_vertex.glsl');
var fragmentShaderSource = glslify('./shaders/fragment.glsl');
Expand Down Expand Up @@ -56,7 +56,8 @@ function renderBlock(regl, glAes, renderState, blockLineCount, sampleCount, item
item.offset = sectionVertexCount * blockNumber * blockLineCount;
item.count = sectionVertexCount * count;
if(blockNumber === 0) {
window.cancelAnimationFrame(renderState.currentRafs[rafKey]); // stop drawing possibly stale glyphs before clearing
// stop drawing possibly stale glyphs before clearing
window.cancelAnimationFrame(renderState.currentRafs[rafKey]);
delete renderState.currentRafs[rafKey];
clear(regl, item.scissorX, item.scissorY, item.scissorWidth, item.viewBoxSize[1]);
}
Expand Down Expand Up @@ -165,7 +166,19 @@ function valid(i, offset, panelCount) {
return i + offset <= panelCount;
}

module.exports = function(canvasGL, lines, canvasWidth, canvasHeight, initialDimensions, initialPanels, unitToColor, context, pick, scatter) {
module.exports = function(canvasGL, d, scatter) {
var model = d.model,
vm = d.viewModel,
domain = model.domain;

var lines = model.lines,
canvasWidth = model.canvasWidth,
canvasHeight = model.canvasHeight,
initialDimensions = vm.dimensions,
initialPanels = vm.panels,
unitToColor = model.unitToColor,
context = d.context,
pick = d.pick;

var renderState = {
currentRafs: {},
Expand Down Expand Up @@ -248,6 +261,13 @@ module.exports = function(canvasGL, lines, canvasWidth, canvasHeight, initialDim
}
},

viewport: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely ❤️

x: regl.prop('viewportX'),
y: regl.prop('viewportY'),
width: regl.prop('viewportWidth'),
height: regl.prop('viewportHeight')
},

dither: false,

vert: pick ? pickVertexShaderSource : vertexShaderSource,
Expand Down Expand Up @@ -297,7 +317,7 @@ module.exports = function(canvasGL, lines, canvasWidth, canvasHeight, initialDim
function makeItem(i, ii, x, y, panelSizeX, canvasPanelSizeY, crossfilterDimensionIndex, scatter, I, leftmost, rightmost) {
var loHi, abcd, d, index;
var leftRight = [i, ii];
var filterEpsilon = verticalPadding / canvasPanelSizeY;
var filterEpsilon = c.verticalPadding / canvasPanelSizeY;

var dims = [0, 1].map(function() {return [0, 1, 2, 3].map(function() {return new Float32Array(16);});});
var lims = [0, 1].map(function() {return [0, 1, 2, 3].map(function() {return new Float32Array(16);});});
Expand Down Expand Up @@ -341,10 +361,16 @@ module.exports = function(canvasGL, lines, canvasWidth, canvasHeight, initialDim

colorClamp: colorClamp,
scatter: scatter || 0,
scissorX: I === leftmost ? 0 : x + overdrag,

scissorX: (I === leftmost ? 0 : x + overdrag) + (model.pad.l - overdrag) + model.layoutWidth * domain.x[0],
scissorWidth: (I === rightmost ? canvasWidth - x + overdrag : panelSizeX + 0.5) + (I === leftmost ? x + overdrag : 0),
scissorY: y,
scissorHeight: canvasPanelSizeY
scissorY: y + model.pad.b + model.layoutHeight * domain.y[0],
scissorHeight: canvasPanelSizeY,

viewportX: model.pad.l - overdrag + model.layoutWidth * domain.x[0],
viewportY: model.pad.b + model.layoutHeight * domain.y[0],
viewportWidth: canvasWidth,
viewportHeight: canvasHeight
};
}

Expand Down
Loading