Skip to content

Fix relayout axis category #510

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 6 commits into from
May 5, 2016
Merged
Changes from 1 commit
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
31 changes: 12 additions & 19 deletions src/plots/cartesian/category_order_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,25 @@

'use strict';

var layoutAttributes = require('./layout_attributes');

module.exports = function handleCategoryOrderDefaults(containerIn, containerOut, coerce) {
if(containerOut.type !== 'category') return;

if(containerIn.type !== 'category') return;
var arrayIn = containerIn.categoryarray,
orderDefault;

var validCategories = layoutAttributes.categoryorder.values;
var isValidArray = (Array.isArray(arrayIn) && arrayIn.length);
Copy link
Contributor

Choose a reason for hiding this comment

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

I also like the brevity of a.length rather than a.length > 0 but assume that most places want the more explicit version :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I'll change it to arrayIn.length > 0


var propercategoryarray = Array.isArray(containerIn.categoryarray) && containerIn.categoryarray.length > 0;
// override default 'categoryorder' value when non-empty array is supplied
if(isValidArray) orderDefault = 'array';

if(validCategories.indexOf(containerIn.categoryorder) === -1 && propercategoryarray) {
var order = coerce('categoryorder', orderDefault);
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, this made me learn a bit about the safety/necessity of doing the coercion unconditionally, off the bat.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

doing the coercion unconditionally, off the bat.

Not sure if it's better, but coercing off the bat is definitely the most common approach in our code base.


// when unspecified or invalid, use the default, unless categoryarray implies 'array'
coerce('categoryorder', 'array'); // promote to 'array'

} else if(containerIn.categoryorder === 'array' && !propercategoryarray) {

// when mode is 'array' but no list is given, revert to default

containerIn.categoryorder = 'trace'; // revert to default
coerce('categoryorder');

} else {

// otherwise use the supplied mode, or the default one if unsupplied or invalid
coerce('categoryorder');
// coerce 'categoryarray' only in array order case
if(order === 'array') coerce('categoryarray');

// cannot set 'categoryorder' to 'array' with an invalid 'categoryarray'
if(!isValidArray && order === 'array') {
containerOut.categoryorder = 'trace';
}
};