Skip to content

Commit 7721e25

Browse files
author
Austin Martin
committed
fixing cdi.density array length to accommodate both endpoints. n intervals means n + 1 samples.
1 parent c44f20f commit 7721e25

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/traces/violin/calc.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = function calc(gd, trace) {
3939
// step that well covers the bandwidth and is multiple of span distance
4040
var dist = span[1] - span[0];
4141
var n = Math.ceil(dist / (bandwidth / 3));
42-
var step = (n > 1) ? dist / (n - 1) : 0;
42+
var step = dist/ n;
4343

4444
if(!isFinite(step) || !isFinite(n)) {
4545
Lib.error('Something went wrong with computing the violin span');
@@ -48,9 +48,10 @@ module.exports = function calc(gd, trace) {
4848
}
4949

5050
var kde = helpers.makeKDE(cdi, trace, vals);
51-
cdi.density = new Array(n);
51+
// n intervals means n + 1 sample points to include both endpoints
52+
cdi.density = new Array(n + 1);
5253

53-
for(var k = 0; k < n; k++) {
54+
for(var k = 0; k <= n; k++) {
5455
var t = span[0] + k * step;
5556
var v = kde(t);
5657
cdi.density[k] = {v: v, t: t};

test/jasmine/tests/violin_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ describe('Test violin calc:', function() {
402402
expect(cd[0].bandwidth).toBe(0, 'bandwidth');
403403
});
404404

405-
it('should produce exactly n density samples for tiny or near-equal spans', function() {
405+
it('should produce exactly n + 1 density samples for tiny or near-equal spans', function() {
406406
var cd = _calc({
407407
type: 'violin',
408408
x: [0, 0],
@@ -413,7 +413,7 @@ describe('Test violin calc:', function() {
413413
var dist = cdi.span[1] - cdi.span[0];
414414
var n = Math.ceil(dist / (cdi.bandwidth / 3));
415415

416-
expect(cdi.density.length).toBe(n);
416+
expect(cdi.density.length).toBe(n + 1);
417417
});
418418
});
419419

0 commit comments

Comments
 (0)