Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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 DownExpand 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
Copy Markdown
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:

functionmemoize(fn,keyFn){varcache={};returnfunction(val){varnewKey=keyFn ? keyFn(val) : val;if(newKeyincache){returncache[newKey];}out=fn(val);cache[newKey]=out;returnout;}}varmakeImagDflt=memoizeOne(function(realTickvals){returnrealTickvals.slice().reverse().map(function(x){return-x;}).concat([0]).concat(realTickvals);},String);vara=makeImagDflt([2,4,5])varb=makeImagDflt([2,4,5])a===b// truevarc=makeImagDflt([1,2,3])vard=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
Copy Markdown
ContributorAuthor

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 DownExpand 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;
};
}