Skip to content

Add 'cumulative' histogram 'mode' for CDF #1189

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 8 commits into from
Jan 18, 2017
Merged
10 changes: 10 additions & 0 deletions src/traces/histogram/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ module.exports = {
].join(' ')
},

mode: {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

or maybe alternatively, a boolean cumulative attribute could do the trick. I don't see any other possible modes for histograms.

Copy link
Member

@cldougl cldougl Nov 22, 2016

Choose a reason for hiding this comment

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

This is a cool addition--I think I'd prefer a cumulative boolean attribute. That's pretty explicit and +1 about not seeing any other possible modes.
Also, not sure if this is being too nitpicky across traces, but I'm not super convinced that this use case provides a good parallel to scatter mode

Copy link
Contributor Author

@etpinard etpinard Nov 22, 2016

Choose a reason for hiding this comment

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

⏫ the winning argument so far

valType: 'enumerated',
values: ['density', 'cumulative'],
dflt: 'density',
role: 'info',
description: [
''
].join('')
},

autobinx: {
valType: 'boolean',
dflt: null,
Expand Down
9 changes: 9 additions & 0 deletions src/traces/histogram/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = function calc(gd, trace) {

// prepare the raw data
var pos0 = pa.makeCalcdata(trace, maindata);

// calculate the bins
if((trace['autobin' + maindata] !== false) || !(maindata + 'bins' in trace)) {
trace[maindata + 'bins'] = Axes.autoBin(pos0, pa, trace['nbins' + maindata]);
Expand Down Expand Up @@ -112,6 +113,8 @@ module.exports = function calc(gd, trace) {
// average and/or normalize the data, if needed
if(doavg) total = doAvg(size, counts);
if(normfunc) normfunc(size, total, inc);
if(trace.mode === 'cumulative') cdf(size);
Copy link
Contributor Author

@etpinard etpinard Nov 22, 2016

Choose a reason for hiding this comment

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

Maybe 'cumulative' should have a different meaning for histnorm !== '' and histfunc !== 'count'?



var serieslen = Math.min(pos.length, size.length),
cd = [],
Expand Down Expand Up @@ -140,3 +143,9 @@ module.exports = function calc(gd, trace) {

return cd;
};

function cdf(size) {
for(var i = 1; i < size.length; i++) {
size[i] += size[i - 1];
}
}
1 change: 1 addition & 0 deletions src/traces/histogram/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
var x = coerce('x'),
y = coerce('y');

coerce('mode');
coerce('text');

var orientation = coerce('orientation', (y && !x) ? 'h' : 'v'),
Expand Down