Skip to content

Transform react #2577

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

Merged
merged 26 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bbe3533
remove no longer needed hack for finance in findArrayAttributes
alexcjohnson Apr 18, 2018
ed24765
_commonLength -> _length
alexcjohnson Apr 18, 2018
fcc459d
fix #2508, fix #2470 - problems with Plotly.react and aggregate trans…
alexcjohnson Apr 18, 2018
7044a13
standardize transforms handling of _length
alexcjohnson Apr 19, 2018
88b7b43
:racehorse: refactor sort transform from O(n^2) to O(n)
alexcjohnson Apr 19, 2018
cf4c9c3
heatmap&carpet/has_columns -> Lib.is1D
alexcjohnson Apr 20, 2018
e84d4b9
close #1410 - yes, stop pruning in nestedProperty
alexcjohnson Apr 20, 2018
b436d52
ensure every trace defines _length in supplyDefaults, and abort trans…
alexcjohnson Apr 22, 2018
2a41f9e
react+transforms PR review edits
alexcjohnson Apr 24, 2018
965bcfb
fixes and test for checklist in #2508
alexcjohnson Apr 24, 2018
6fae229
some fixes and tests for empty data arrays
alexcjohnson Apr 24, 2018
79295f1
rename violins mock that doesn't contain violin traces
alexcjohnson Apr 25, 2018
5e9aa65
transforms mock
alexcjohnson Apr 26, 2018
690eb95
clean up keyedContainer for possibly missing array
alexcjohnson Apr 26, 2018
a244cec
violin should not explicitly set whiskerwidth
alexcjohnson Apr 26, 2018
92bd5d2
add _length and stop slicing in scattercarpet
alexcjohnson Apr 26, 2018
dc6de2f
clean up handling of colorscale defaults
alexcjohnson Apr 26, 2018
03956e1
include count aggregates in _arrayAttrs - so they remap correctly
alexcjohnson Apr 26, 2018
f439e41
in diffData I had _fullInput in the new trace, but not the old!
alexcjohnson Apr 26, 2018
e47e6a9
_compareAsJSON for groupby styles
alexcjohnson Apr 26, 2018
aa30ad6
update plot_api_test to :lock: recent changes in react/transforms etc
alexcjohnson Apr 26, 2018
4c70826
lint
alexcjohnson Apr 26, 2018
7279b55
+shapes & annotations3d in react-noop test
alexcjohnson Apr 26, 2018
3e250df
tweak docs & remove commented-out code
alexcjohnson Apr 26, 2018
cd08479
reactWith -> reactTo
alexcjohnson Apr 26, 2018
0414147
separate svg and gl traces in react-noop tests
alexcjohnson Apr 26, 2018
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
73 changes: 49 additions & 24 deletions src/components/colorscale/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,47 @@ var flipScale = require('./flip_scale');


module.exports = function calc(trace, vals, containerStr, cLetter) {
var container, inputContainer;
var container = trace;
var inputContainer = trace._input;
var fullInputContainer = trace._fullInput;

// set by traces with groupby transforms
var updateStyle = trace.updateStyle;

function doUpdate(attr, inputVal, fullVal) {
if(fullVal === undefined) fullVal = inputVal;

if(updateStyle) {
updateStyle(trace._input, containerStr ? (containerStr + '.' + attr) : attr, inputVal);
}
else {
inputContainer[attr] = inputVal;
}

container[attr] = fullVal;
if(fullInputContainer && (trace !== trace._fullInput)) {
if(updateStyle) {
updateStyle(trace._fullInput, containerStr ? (containerStr + '.' + attr) : attr, fullVal);
}
else {
fullInputContainer[attr] = fullVal;
}
}
}

if(containerStr) {
container = Lib.nestedProperty(trace, containerStr).get();
inputContainer = Lib.nestedProperty(trace._input, containerStr).get();
}
else {
container = trace;
inputContainer = trace._input;
container = Lib.nestedProperty(container, containerStr).get();
inputContainer = Lib.nestedProperty(inputContainer, containerStr).get();
fullInputContainer = Lib.nestedProperty(fullInputContainer, containerStr).get() || {};
}

var autoAttr = cLetter + 'auto',
minAttr = cLetter + 'min',
maxAttr = cLetter + 'max',
auto = container[autoAttr],
min = container[minAttr],
max = container[maxAttr],
scl = container.colorscale;
var autoAttr = cLetter + 'auto';
var minAttr = cLetter + 'min';
var maxAttr = cLetter + 'max';
var auto = container[autoAttr];
var min = container[minAttr];
var max = container[maxAttr];
var scl = container.colorscale;

if(auto !== false || min === undefined) {
min = Lib.aggNums(Math.min, null, vals);
Expand All @@ -48,11 +71,8 @@ module.exports = function calc(trace, vals, containerStr, cLetter) {
max += 0.5;
}

container[minAttr] = min;
container[maxAttr] = max;

inputContainer[minAttr] = min;
inputContainer[maxAttr] = max;
doUpdate(minAttr, min);
doUpdate(maxAttr, max);

/*
* If auto was explicitly false but min or max was missing,
Expand All @@ -61,17 +81,22 @@ module.exports = function calc(trace, vals, containerStr, cLetter) {
* Otherwise make sure the trace still looks auto as far as later
* changes are concerned.
*/
inputContainer[autoAttr] = (auto !== false ||
(min === undefined && max === undefined));
doUpdate(autoAttr, (auto !== false || (min === undefined && max === undefined)));

if(container.autocolorscale) {
if(min * max < 0) scl = scales.RdBu;
else if(min >= 0) scl = scales.Reds;
else scl = scales.Blues;

// reversescale is handled at the containerOut level
inputContainer.colorscale = scl;
if(container.reversescale) scl = flipScale(scl);
container.colorscale = scl;
doUpdate('colorscale', scl, container.reversescale ? flipScale(scl) : scl);

// We pushed a colorscale back to input, which will change the default autocolorscale next time
// to avoid spurious redraws from Plotly.react, update resulting autocolorscale now
// This is a conscious decision so that changing the data later does not unexpectedly
// give you a new colorscale
Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for 📚

if(!inputContainer.autocolorscale) {
doUpdate('autocolorscale', false);
}
}
};
16 changes: 11 additions & 5 deletions src/traces/contour/set_contours.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,30 @@
'use strict';

var Axes = require('../../plots/cartesian/axes');
var extendFlat = require('../../lib').extendFlat;
var Lib = require('../../lib');


module.exports = function setContours(trace) {
var contours = trace.contours;

// check if we need to auto-choose contour levels
if(trace.autocontour) {
var dummyAx = autoContours(trace.zmin, trace.zmax, trace.ncontours);
var zmin = trace.zmin;
var zmax = trace.zmax;
if(zmin === undefined || zmax === undefined) {
zmin = Lib.aggNums(Math.min, null, trace._z);
zmax = Lib.aggNums(Math.max, null, trace._z);
}
var dummyAx = autoContours(zmin, zmax, trace.ncontours);

contours.size = dummyAx.dtick;

contours.start = Axes.tickFirst(dummyAx);
dummyAx.range.reverse();
contours.end = Axes.tickFirst(dummyAx);

if(contours.start === trace.zmin) contours.start += contours.size;
if(contours.end === trace.zmax) contours.end -= contours.size;
if(contours.start === zmin) contours.start += contours.size;
if(contours.end === zmax) contours.end -= contours.size;

// if you set a small ncontours, *and* the ends are exactly on zmin/zmax
// there's an edge case where start > end now. Make sure there's at least
Expand All @@ -40,7 +46,7 @@ module.exports = function setContours(trace) {
// previously we copied the whole contours object back, but that had
// other info (coloring, showlines) that should be left to supplyDefaults
if(!trace._input.contours) trace._input.contours = {};
extendFlat(trace._input.contours, {
Lib.extendFlat(trace._input.contours, {
start: contours.start,
end: contours.end,
size: contours.size
Expand Down
2 changes: 1 addition & 1 deletion src/traces/contourcarpet/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function heatmappishCalc(gd, trace) {
z: z,
};

if(trace.contours.type === 'levels') {
if(trace.contours.type === 'levels' && trace.contours.coloring !== 'none') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did omitting coloring !== 'none' lead to a bug outside of Plotly.react?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

possibly for the editor - zmin, zmax, and zauto were set in _fullData, implying that these were useful attributes for this case when in fact they are ignored.

// auto-z and autocolorscale if applicable
colorscaleCalc(trace, z, '', 'z');
}
Expand Down
21 changes: 18 additions & 3 deletions src/transforms/groupby.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ function transformOne(trace, state) {
var groupNameObj;

var opts = state.transform;
var groups = trace.transforms[state.transformIndex].groups;
var transformIndex = state.transformIndex;
var groups = trace.transforms[transformIndex].groups;
var originalPointsAccessor = pointsAccessorFunction(trace.transforms, opts);

if(!(Array.isArray(groups)) || groups.length === 0) {
Expand Down Expand Up @@ -196,7 +197,10 @@ function transformOne(trace, state) {
// Start with a deep extend that just copies array references.
newTrace = newData[i] = Lib.extendDeepNoArrays({}, trace);
newTrace._group = groupName;
newTrace.transforms[state.transformIndex]._indexToPoints = {};
// helper function for when we need to push updates back to the input,
// outside of the normal restyle/relayout pathway, like filling in auto values
newTrace.updateStyle = styleUpdater(groupName, transformIndex);
newTrace.transforms[transformIndex]._indexToPoints = {};

var suppliedName = null;
if(groupNameObj) {
Expand Down Expand Up @@ -254,7 +258,7 @@ function transformOne(trace, state) {
for(j = 0; j < len; j++) {
newTrace = newData[indexLookup[groups[j]]];

var indexToPoints = newTrace.transforms[state.transformIndex]._indexToPoints;
var indexToPoints = newTrace.transforms[transformIndex]._indexToPoints;
indexToPoints[indexCnts[groups[j]]] = originalPointsAccessor(j);
indexCnts[groups[j]]++;
}
Expand All @@ -272,3 +276,14 @@ function transformOne(trace, state) {

return newData;
}

function styleUpdater(groupName, transformIndex) {
return function(trace, attr, value) {
Lib.keyedContainer(
Copy link
Contributor

Choose a reason for hiding this comment

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

Ricky's dream lives on 🌈

trace,
'transforms[' + transformIndex + '].styles',
'target',
'value.' + attr
).set(String(groupName), value);
};
}
21 changes: 17 additions & 4 deletions test/jasmine/tests/colorscale_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,20 +364,33 @@ describe('Test colorscale:', function() {
type: 'heatmap',
z: [[0, -1.5], [-2, -10]],
autocolorscale: true,
_input: {}
_input: {autocolorscale: true}
};
z = [[0, -1.5], [-2, -10]];
calcColorscale(trace, z, '', 'z');
expect(trace.autocolorscale).toBe(true);
expect(trace.colorscale[5]).toEqual([1, 'rgb(220,220,220)']);
});

it('should set autocolorscale to false if it wasn\'t explicitly set true in input', function() {
trace = {
type: 'heatmap',
z: [[0, -1.5], [-2, -10]],
autocolorscale: true,
_input: {}
};
z = [[0, -1.5], [-2, -10]];
calcColorscale(trace, z, '', 'z');
expect(trace.autocolorscale).toBe(false);
expect(trace.colorscale[5]).toEqual([1, 'rgb(220,220,220)']);
});

it('should be Blues when the only numerical z <= -0.5', function() {
trace = {
type: 'heatmap',
z: [['a', 'b'], [-0.5, 'd']],
autocolorscale: true,
_input: {}
_input: {autocolorscale: true}
};
z = [[undefined, undefined], [-0.5, undefined]];
calcColorscale(trace, z, '', 'z');
Expand All @@ -390,7 +403,7 @@ describe('Test colorscale:', function() {
type: 'heatmap',
z: [['a', 'b'], [0.5, 'd']],
autocolorscale: true,
_input: {}
_input: {autocolorscale: true}
};
z = [[undefined, undefined], [0.5, undefined]];
calcColorscale(trace, z, '', 'z');
Expand All @@ -404,7 +417,7 @@ describe('Test colorscale:', function() {
z: [['a', 'b'], [0.5, 'd']],
autocolorscale: true,
reversescale: true,
_input: {}
_input: {autocolorscale: true}
};
z = [[undefined, undefined], [0.5, undefined]];
calcColorscale(trace, z, '', 'z');
Expand Down