-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
initial implementation of layout.colorscale
#3274
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
Conversation
src/plots/plots.js
Outdated
| coerce('colorscale.sequential'); | ||
| coerce('colorscale.sequentialminus'); | ||
| coerce('colorscale.diverging'); | ||
| if(layoutIn.colorscale) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coercing valType: colorScale will always give it a default colorscale. I initially wanted it to be falsey if not set by the user. There are other ways to do that.
src/plots/layout_attributes.js
Outdated
| editType: 'calc', | ||
| description: 'Sets the default trace colors.' | ||
| }, | ||
| colorscale: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put these new attributes and default logic in the Colorscale component module.
Create two new files:
- src/components/colorscale/layout_attributes.js
- src/components/colorscale/layout_defaults.js
and link their exports in src/components/colorscale/index.js as layoutAttributes and supplyLayoutDefaults. The latter will get automatically picked up in Plotly.supplyLayoutModuleDefaults here:
Lines 1512 to 1518 in 695f311
| for(component in componentsRegistry) { | |
| _module = componentsRegistry[component]; | |
| if(_module.supplyLayoutDefaults) { | |
| _module.supplyLayoutDefaults(layoutIn, layoutOut, fullData); | |
| } | |
| } |
src/plots/layout_attributes.js
Outdated
| colorscale: { | ||
| sequential: { | ||
| valType: 'colorscale', | ||
| dflt: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would happen if we set dflt: scales.RdBu ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's another way of doing it and I think it's much better than the current approach. We'd have the defaults clearly defined in the attributes where they should be 🎉
| containerOut.sequentialminus = layoutColorscale.sequentialminus; | ||
| var dfltScl = containerOut.diverging; | ||
| var sclOut; | ||
| if(dfltScl) sclOut = coerce(prefix + 'colorscale', dfltScl); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha I see. We wouldn't need this extra if-else block if you made the layout.colorscale.*.dflt undefined (or removed them entirely) instead of false. The relevant line in coerce.js is:
Line 384 in 695f311
| if(dflt === undefined) dflt = opts.dflt; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... But looking at this again, do we really need logic in colorscale/defaults.js and in colorscale/calc.js?
Ideally,
- I would leave colorscale/defaults.js untouched
- make the
layout.colorscale.*.dfltmatchscales.RdBu,scales.Redsandscales.Blues, making thosecontainer.diverging || scales.RdBu;in colorscale/calc.js unnecessary. - handle the autocolorscale logic in colorscale/calc.js
Now, I'm just thinking out-loud. @antoinerg let me know if my proposal has any flaws.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes a lot of sense. Let me rework this PR!
|
Commit 93ce241 refactors I still need to improve test coverage to make sure |
|
Commit ee222d5 improves the test coverage! |
| // auto-z and autocolorscale if applicable | ||
| if(hasColorscale(trace, 'marker')) { | ||
| colorscaleCalc(trace, trace.marker.color, 'marker', 'c'); | ||
| colorscaleCalc(gd, trace, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking nice 💎
src/components/colorscale/calc.js
Outdated
| if(min * max < 0) scl = scales.RdBu; | ||
| else if(min >= 0) scl = scales.Reds; | ||
| else scl = scales.Blues; | ||
| if(min * max < 0) scl = gd._fullLayout.colorscale.diverging; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor 🐄 , usually when a method uses fullLayout from gd we put a
var fullLayout = gd._fullLayout;just below the function (
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 75e8910
|
Nicely done @antoinerg Since this PR will mostly help out template users, we should add a JSON mock that uses one of the new attribute in a template. |
| "template": { | ||
| "layout": { | ||
| "title": "heatmap template", | ||
| "colorscale": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we put autocolorscale: true in the template container, does it still work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! I modified the mock in dca4350
|
Nicely done 💃 |
Attempt at closing #2925. As far as I can tell it works well but test coverage is pretty minimal at the moment.
In the process of making this PR, I uncovered issue #3273 .