-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Handle invalid values and zero totals for pie and funnelarea #4416
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,21 +13,52 @@ var attributes = require('./attributes'); | |
var handleDomainDefaults = require('../../plots/domain').defaults; | ||
var handleText = require('../bar/defaults').handleText; | ||
|
||
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) { | ||
function handleLabelsAndValues(labels, values) { | ||
var hasLabels = Array.isArray(labels); | ||
var hasValues = Lib.isArrayOrTypedArray(values); | ||
var len = Math.min( | ||
hasLabels ? labels.length : Infinity, | ||
hasValues ? values.length : Infinity | ||
); | ||
|
||
if(!isFinite(len)) len = 0; | ||
|
||
if(len && hasValues) { | ||
var sum = 0; | ||
for(var i = 0; i < len; i++) { | ||
var v = +values[i]; | ||
if(v < 0) { | ||
sum = 0; | ||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. Don't you mean If I understand correctly, this makes any trace with one (or more) negative values have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. Done in 1967fd4. |
||
} | ||
sum += v; | ||
} | ||
if(!sum) len = 0; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moreover, you're potentially looping here over all the As There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call. Done in 1967fd4. |
||
|
||
return { | ||
hasLabels: hasLabels, | ||
hasValues: hasValues, | ||
len: len | ||
}; | ||
} | ||
|
||
function supplyDefaults(traceIn, traceOut, defaultColor, layout) { | ||
function coerce(attr, dflt) { | ||
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt); | ||
} | ||
|
||
var len; | ||
var vals = coerce('values'); | ||
var hasVals = Lib.isArrayOrTypedArray(vals); | ||
var labels = coerce('labels'); | ||
if(Array.isArray(labels)) { | ||
len = labels.length; | ||
if(hasVals) len = Math.min(len, vals.length); | ||
} else if(hasVals) { | ||
len = vals.length; | ||
var values = coerce('values'); | ||
|
||
var res = handleLabelsAndValues(labels, values); | ||
var len = res.len; | ||
traceOut._hasLabels = res.hasLabels; | ||
traceOut._hasValues = res.hasValues; | ||
|
||
if(!traceOut._hasLabels && | ||
traceOut._hasValues | ||
) { | ||
coerce('label0'); | ||
coerce('dlabel'); | ||
} | ||
|
@@ -86,4 +117,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout | |
coerce('direction'); | ||
coerce('rotation'); | ||
coerce('pull'); | ||
} | ||
|
||
module.exports = { | ||
handleLabelsAndValues: handleLabelsAndValues, | ||
supplyDefaults: supplyDefaults | ||
}; |
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's wrong with
labels = new Array(len);
? I'm not a fan an implicit array-pushing e.g.like you have here.
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. Done in 1967fd4.