Skip to content
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

Scatterpolargl fixes #3098

Merged
merged 8 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions src/plots/polar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ function plot(gd) {

function clean(newFullData, newFullLayout, oldFullData, oldFullLayout) {
var oldIds = oldFullLayout._subplots[name] || [];
var hadGl = (oldFullLayout._has && oldFullLayout._has('gl'));
var hasGl = (newFullLayout._has && newFullLayout._has('gl'));
var mustCleanScene = hadGl && !hasGl;

for(var i = 0; i < oldIds.length; i++) {
var id = oldIds[i];
Expand All @@ -66,6 +69,11 @@ function clean(newFullData, newFullLayout, oldFullData, oldFullLayout) {
oldSubplot.clipPaths[k].remove();
}
}

if(mustCleanScene && oldSubplot._scene) {
oldSubplot._scene.destroy();
oldSubplot._scene = null;
}
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/traces/scatterpolargl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var isNumeric = require('fast-isnumeric');

var ScatterGl = require('../scattergl');
var calcColorscales = require('../scatter/colorscale_calc');
var calcMarkerSize = require('../scatter/calc').calcMarkerSize;
var Axes = require('../../plots/cartesian/axes');
var makeHoverPointText = require('../scatterpolar/hover').makeHoverPointText;
var subTypes = require('../scatter/subtypes');
Expand All @@ -26,22 +27,28 @@ function calc(container, trace) {
var angularAxis = layout[subplotId].angularaxis;
var rArray = radialAxis.makeCalcdata(trace, 'r');
var thetaArray = angularAxis.makeCalcdata(trace, 'theta');
var len = trace._length;
var stash = {};

if(trace._length < rArray.length) rArray = rArray.slice(0, trace._length);
if(trace._length < thetaArray.length) thetaArray = thetaArray.slice(0, trace._length);

calcColorscales(trace);
if(len < rArray.length) rArray = rArray.slice(0, len);
if(len < thetaArray.length) thetaArray = thetaArray.slice(0, len);

stash.r = rArray;
stash.theta = thetaArray;

trace._extremes.x = Axes.findExtremes(radialAxis, rArray, {tozero: true});
// We could add TOO_MANY_POINTS logic like in Scattergl.calc,
// if users asks for scaterpolargl charts with > 1e5 pts
var ppad = calcMarkerSize(trace, len);
trace._extremes.x = Axes.findExtremes(radialAxis, rArray, {ppad: ppad});

calcColorscales(trace);

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

function plot(container, subplot, cdata) {
if(!cdata.length) return;

var radialAxis = subplot.radialAxis;
var angularAxis = subplot.angularAxis;

Expand Down
Binary file modified test/image/baselines/glpolar_scatter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/glpolar_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/glpolar_subplots.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/polar_r0dr-theta0dtheta.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions test/jasmine/tests/scatterpolargl_test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var Plotly = require('@lib');
var Lib = require('@src/lib');

var d3 = require('d3');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var failTest = require('../assets/fail_test');
var mouseEvent = require('../assets/mouse_event');
var readPixel = require('../assets/read_pixel');

var customAssertions = require('../assets/custom_assertions');
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
Expand Down Expand Up @@ -102,3 +104,69 @@ describe('Test scatterpolargl hover:', function() {
});
});
});

describe('Test scatterpolargl interactions:', function() {
var gd;

afterEach(function() {
Plotly.purge(gd);
destroyGraphDiv();
});

function countCanvases() {
return d3.selectAll('canvas').size();
}

function totalPixels() {
return readPixel(gd.querySelector('.gl-canvas-context'), 0, 0, 400, 400)
.reduce(function(acc, v) { return acc + v; }, 0);
}

it('@gl should be able to toggle from svg to gl', function(done) {
gd = createGraphDiv();

var scene;

Plotly.plot(gd, [{
type: 'scatterpolar',
r: [1, 2, 1],
}], {
width: 400,
height: 400
})
.then(function() {
expect(countCanvases()).toBe(0);
expect(d3.selectAll('.scatterlayer > .trace').size()).toBe(1);

return Plotly.restyle(gd, 'type', 'scatterpolargl');
})
.then(function() {
expect(countCanvases()).toBe(3);
expect(totalPixels()).not.toBe(0);
expect(d3.selectAll('.scatterlayer > .trace').size()).toBe(0);

scene = gd._fullLayout.polar._subplot._scene;
spyOn(scene, 'destroy').and.callThrough();

return Plotly.restyle(gd, 'type', 'scatterpolar');
})
.then(function() {
expect(countCanvases()).toBe(0);
expect(scene.destroy).toHaveBeenCalledTimes(1);
expect(gd._fullLayout.polar._subplot._scene).toBe(null);
expect(d3.selectAll('.scatterlayer > .trace').size()).toBe(1);

return Plotly.restyle(gd, 'type', 'scatterpolargl');
})
.then(function() {
expect(countCanvases()).toBe(3);
// this here was failing before
// https://github.com/plotly/plotly.js/issues/3094
// got fixed
expect(totalPixels()).not.toBe(0);
expect(d3.selectAll('.scatterlayer > .trace').size()).toBe(0);
})
.catch(failTest)
.then(done);
});
});