Skip to content

initial implementation of layout.colorscale - #3274

Merged
antoinerg merged 15 commits into
masterfrom
2925-default-colorbars
Nov 23, 2018
Merged

antoinerg merged 15 commits into
masterfrom
2925-default-colorbars

Conversation

@antoinerg

Copy link
Copy Markdown
Contributor

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 .

Comment thread src/plots/plots.js Outdated
coerce('colorscale.sequential');
coerce('colorscale.sequentialminus');
coerce('colorscale.diverging');
if(layoutIn.colorscale) {

Copy link
Copy Markdown
Contributor

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?

@antoinerg antoinerg Nov 21, 2018 •

Copy link
Copy Markdown
Contributor Author

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.

Comment thread src/plots/layout_attributes.js Outdated
editType: 'calc',
description: 'Sets the default trace colors.'
},
colorscale: {

@etpinard etpinard Nov 21, 2018 •

Copy link
Copy Markdown
Contributor

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:

plotly.js/src/plots/plots.js

Lines 1512 to 1518 in 695f311

for(component in componentsRegistry) {
_module = componentsRegistry[component];
if(_module.supplyLayoutDefaults) {
_module.supplyLayoutDefaults(layoutIn, layoutOut, fullData);
}
}

Comment thread src/plots/layout_attributes.js Outdated
colorscale: {
sequential: {
valType: 'colorscale',
dflt: false,

Copy link
Copy Markdown
Contributor

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 ?

@antoinerg antoinerg Nov 21, 2018 •

Copy link
Copy Markdown
Contributor Author

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 🎉

Comment thread src/components/colorscale/defaults.js Outdated
containerOut.sequentialminus = layoutColorscale.sequentialminus;
var dfltScl = containerOut.diverging;
var sclOut;
if(dfltScl) sclOut = coerce(prefix + 'colorscale', dfltScl);

Copy link
Copy Markdown
Contributor

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:

if(dflt === undefined) dflt = opts.dflt;

Copy link
Copy Markdown
Contributor

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.*.dflt match scales.RdBu, scales.Reds and scales.Blues, making those container.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.

Copy link
Copy Markdown
Contributor Author

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!

@antoinerg

Copy link
Copy Markdown
Contributor Author

Commit 93ce241 refactors colorscale_test.js.

I still need to improve test coverage to make sure layout.colorscale.(sequential|sequentialminus|diverging) are used appropriately when autorange: true and no colorscale is provided by the user. It should already work but it should be 🔒 down!

@antoinerg

Copy link
Copy Markdown
Contributor Author

Commit ee222d5 improves the test coverage!

Comment thread src/traces/bar/calc.js
// auto-z and autocolorscale if applicable
if(hasColorscale(trace, 'marker')) {
colorscaleCalc(trace, trace.marker.color, 'marker', 'c');
colorscaleCalc(gd, trace, {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking nice 💎

Comment thread 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;

Copy link
Copy Markdown
Contributor

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 (

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in commit 75e8910

@etpinard

Copy link
Copy Markdown
Contributor

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": {

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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

@etpinard

Copy link
Copy Markdown
Contributor

Nicely done 💃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature something new

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants