Skip to content
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
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.
6 changes: 2 additions & 4 deletions src/plots/smith/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,10 @@ var imaginaryAxisAttrs = extendFlat({
visible: extendFlat({}, axesAttrs.visible, {dflt: true}),

tickvals: {
dflt: [-5, -2, -1, -0.5, -0.2, 0, 0.2, 0.5, 1, 2, 5],
valType: 'data_array',
editType: 'plot',
description: [
'Sets the values at which ticks on this axis appear.',
'Defaults to `realaxis.tickvals` plus the same as negatives and zero.'
].join(' ')
description: 'Sets the values at which ticks on this axis appear.'
},

ticks: axesAttrs.ticks,
Expand Down
12 changes: 1 addition & 11 deletions src/plots/smith/layout_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,7 @@ function handleDefaults(contIn, contOut, coerce, opts) {
var isRealAxis = axName === 'realaxis';
if(isRealAxis) coerceAxis('side');

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


coerceAxis('tickvals', imagTickvalsDflt);
}
coerceAxis('tickvals');

var dfltColor;
var dfltFontColor;
Expand Down
3 changes: 3 additions & 0 deletions test/image/mocks/smith_basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"smith": {
"realaxis": {
"tickvals": [0.2, 0.4, 0.6, 0.8, 1, 1.5, 2, 3, 4, 5, 10, 20]
},
"imaginaryaxis": {
"tickvals": [-20, -10, -5, -4, -3, -2, -1.5, -1, -0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1, 1.5, 2, 3, 4, 5, 10, 20]
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/jasmine/tests/smith_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ describe('Test relayout on smith subplots:', function() {
return Plotly.relayout(gd, 'smith.imaginaryaxis', {ticks: 'inside'});
})
.then(function() {
check(26, 'M-0.5,0h-5');
check(12, 'M-0.5,0h-5');
return Plotly.relayout(gd, 'smith.imaginaryaxis', {ticks: 'outside'});
})
.then(function() {
check(26, 'M0.5,0h5');
check(12, 'M0.5,0h5');
return Plotly.relayout(gd, 'smith.imaginaryaxis', {ticks: ''});
})
.then(function() {
check(0);
return Plotly.relayout(gd, 'smith.imaginaryaxis', {ticks: 'inside'});
})
.then(function() {
check(26, 'M-0.5,0h-5');
check(12, 'M-0.5,0h-5');
})
.then(done, done.fail);
});
Expand Down
15 changes: 14 additions & 1 deletion test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7510,7 +7510,20 @@
"valType": "string"
},
"tickvals": {
"description": "Sets the values at which ticks on this axis appear. Defaults to `realaxis.tickvals` plus the same as negatives and zero.",
"description": "Sets the values at which ticks on this axis appear.",
"dflt": [
-5,
-2,
-1,
-0.5,
-0.2,
0,
0.2,
0.5,
1,
2,
5
],
"editType": "plot",
"valType": "data_array"
},
Expand Down