Skip to content

Adjust imaginary tickvals default to fix react #5992

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 4 commits into from
Oct 29, 2021
Merged
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
2 changes: 1 addition & 1 deletion draftlogs/5956_add.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
- Add `smith` subplots and the `scattersmith` trace type for displaying Smith charts [[#5956](https://github.com/plotly/plotly.js/pull/5956)],
- Add `smith` subplots and the `scattersmith` trace type for displaying Smith charts [[#5956](https://github.com/plotly/plotly.js/pull/5956), [#5992](https://github.com/plotly/plotly.js/pull/5992)],
with thanks to Kitware and @waxlamp for kicking off this effort.
27 changes: 22 additions & 5 deletions src/plots/smith/layout_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ var layoutAttributes = require('./layout_attributes');
var constants = require('./constants');
var axisNames = constants.axisNames;

var makeImagDflt = memoize(function(realTickvals) {
return realTickvals.slice().reverse().map(function(x) { return -x; })
.concat([0])
.concat(realTickvals);
}, String);

function handleDefaults(contIn, contOut, coerce, opts) {
var bgColor = coerce('bgcolor');
opts.bgColor = Color.combine(bgColor, opts.paper_bgcolor);
Expand Down Expand Up @@ -55,11 +61,10 @@ function handleDefaults(contIn, contOut, coerce, opts) {
if(isRealAxis) {
coerceAxis('tickvals');
} else {
var realTickvals = contOut.realaxis.tickvals || layoutAttributes.realaxis.tickvals.dflt;
var imagTickvalsDflt =
realTickvals.slice().reverse().map(function(x) { return -x; })
.concat([0])
.concat(realTickvals);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I imagine the problem with this is we get a new array each time, so Plotly.react thinks this has changed and needs replotting? I do think as a default this behavior makes sense, can we fix it with a simple memoizer? Something like:

function memoize(fn, keyFn) {
    var cache = {};
    return function(val) {
        var newKey = keyFn ? keyFn(val) : val;
        if (newKey in cache) { return cache[newKey]; }
        out = fn(val);
        cache[newKey] = out;
        return out;
    }
}

var makeImagDflt = memoizeOne(function(realTickvals) {
    return realTickvals.slice().reverse().map(function(x) { return -x; })
        .concat([0])
        .concat(realTickvals);
}, String);

var a = makeImagDflt([2,4,5])
var b = makeImagDflt([2,4,5])
a===b // true

var c = makeImagDflt([1,2,3])
var d = makeImagDflt([2,4,5])
d===b // true

A memoizer might be useful in other contexts too... we use them quite a lot in Dash anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a kind of magic!
Applied in 844e24c

var imagTickvalsDflt = makeImagDflt(
contOut.realaxis.tickvals ||
layoutAttributes.realaxis.tickvals.dflt
);

coerceAxis('tickvals', imagTickvalsDflt);
}
Expand Down Expand Up @@ -132,3 +137,15 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
layoutOut: layoutOut
});
};

function memoize(fn, keyFn) {
var cache = {};
return function(val) {
var newKey = keyFn ? keyFn(val) : val;
if(newKey in cache) { return cache[newKey]; }

var out = fn(val);
cache[newKey] = out;
return out;
};
}