Skip to content

Fix heatmap brick generation edge cases #651

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
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 15 additions & 5 deletions src/traces/heatmap/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,36 +173,43 @@ function makeBoundArray(trace, arrayIn, v0In, dvIn, numbricks, ax) {
dv,
i;

if(Array.isArray(arrayIn) && !isHist && (ax.type !== 'category')) {
var isArrayOfTwoItemsOrMore = Array.isArray(arrayIn) && arrayIn.length > 1;

if(isArrayOfTwoItemsOrMore && !isHist && (ax.type !== 'category')) {
arrayIn = arrayIn.map(ax.d2c);
var len = arrayIn.length;

// given vals are brick centers
// hopefully length==numbricks, but use this method even if too few are supplied
// hopefully length === numbricks, but use this method even if too few are supplied
// and extend it linearly based on the last two points
if(len <= numbricks) {
// contour plots only want the centers
if(isContour || isGL2D) arrayOut = arrayIn.slice(0, numbricks);
else if(numbricks === 1) arrayOut = [arrayIn[0] - 0.5, arrayIn[0] + 0.5];
else if(numbricks === 1) {
arrayOut = [arrayIn[0] - 0.5, arrayIn[0] + 0.5];
}
else {
arrayOut = [1.5 * arrayIn[0] - 0.5 * arrayIn[1]];

for(i = 1; i < len; i++) {
arrayOut.push((arrayIn[i - 1] + arrayIn[i]) * 0.5);
}

arrayOut.push(1.5 * arrayIn[len - 1] - 0.5 * arrayIn[len - 2]);
}

if(len < numbricks) {
var lastPt = arrayOut[arrayOut.length - 1],
delta = lastPt - arrayOut[arrayOut.length - 2];

for(i = len; i < numbricks; i++) {
lastPt += delta;
arrayOut.push(lastPt);
}
}
}
else {
// hopefully length==numbricks+1, but do something regardless:
// hopefully length === numbricks+1, but do something regardless:
// given vals are brick boundaries
return isContour ?
arrayIn.slice(0, numbricks) : // we must be strict for contours
Expand All @@ -211,14 +218,17 @@ function makeBoundArray(trace, arrayIn, v0In, dvIn, numbricks, ax) {
}
else {
dv = dvIn || 1;
if(v0In === undefined) v0 = 0;

if(Array.isArray(arrayIn) && arrayIn.length === 1) v0 = arrayIn[0];
else if(v0In === undefined) v0 = 0;
else if(isHist || ax.type === 'category') v0 = v0In;
else v0 = ax.d2c(v0In);

for(i = (isContour || isGL2D) ? 0 : -0.5; i < numbricks; i++) {
arrayOut.push(v0 + dv * i);
}
}

return arrayOut;
}

Expand Down
63 changes: 59 additions & 4 deletions test/jasmine/assets/custom_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,76 @@ module.exports = {
toBeCloseToArray: function() {
return {
compare: function(actual, expected, precision) {
if(precision !== 0) {
precision = Math.pow(10, -precision) / 2 || 0.005;
}
precision = coercePosition(precision);

var tested = actual.map(function(element, i) {
return Math.abs(expected[i] - element) < precision;
});

var passed = tested.indexOf(false) < 0;
var passed = (
expected.length === actual.length &&
Copy link
Contributor Author

@etpinard etpinard Jun 15, 2016

Choose a reason for hiding this comment

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

toBeCloseArray loops over the actual array which led to false positive assertions when the expected array is longer than the actual array.

The patch fixes this problem.

tested.indexOf(false) < 0
);

return {
pass: passed,
message: 'Expected ' + actual + ' to be close to ' + expected + '.'
};
}
};
},

// toBeCloseTo... but for 2D arrays
toBeCloseTo2DArray: function() {
return {
compare: function(actual, expected, precision) {
precision = coercePosition(precision);

var passed = true;

if(expected.length !== actual.length) passed = false;
else {
for(var i = 0; i < expected.length; ++i) {
if(expected[i].length !== actual[i].length) {
passed = false;
break;
}

for(var j = 0; j < expected[i].length; ++j) {
var isClose = Math.abs(expected[i][j] - actual[i][j]) < precision;

if(!isClose) {
passed = false;
break;
}
}
}
}

var message = [
'Expected',
arrayToStr(actual.map(arrayToStr)),
'to be close to',
arrayToStr(expected.map(arrayToStr))
].join(' ');

return {
pass: passed,
message: message
};
}
};
}
};

function coercePosition(precision) {
if(precision !== 0) {
precision = Math.pow(10, -precision) / 2 || 0.005;
}

return precision;
}

function arrayToStr(array) {
return '[ ' + array.join(', ') + ' ]';
}
Loading