Skip to content

Fix interval constraint value normalization in contour traces - #8125

Merged
camdecoster merged 8 commits into
plotly:mainfrom
Lexachoc:fix-constraint-contour
Nov 19, 2026
Merged

camdecoster merged 8 commits into
plotly:mainfrom
Lexachoc:fix-constraint-contour

Conversation

@Lexachoc

Copy link
Copy Markdown
Contributor

Closes #8124

Fix incorrect normalization of contours.value for interval constraints.

This causes cases such as:

[]           -> [NaN, NaN]
[5]          -> [5, NaN]
[2,4,8,10]   -> [8,10]

This PR normalizes them as:

[]           -> [0,1]
[5]          -> [5,6]
[2,4,8,10]   -> [2,4]

The issue is in constraint_defaults.js, where the array length is checked against contours.length instead of contours.value.length, and arrays longer than two values are handled with slice(2) instead of slice(0, 2).

The relevant logic is this part:

} else if(contours.value.length > 2) {
contours.value = contours.value.slice(2);
} else if(contours.length === 0) {
contours.value = [0, 1];
} else if(contours.length < 2) {
zvalue = parseFloat(contours.value[0]);
contours.value = [zvalue, zvalue + 1];
} else {
contours.value = [
parseFloat(contours.value[0]),
parseFloat(contours.value[1])
];
}

This affects both contour and contourcarpet, since they share the same constraint defaults.

@camdecoster camdecoster self-assigned this Oct 15, 2026
@camdecoster

Copy link
Copy Markdown
Contributor

Thanks for putting this together. I'll review and follow up with you.

@camdecoster camdecoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a good bug fix. Could you please address my comments? With that, it should be ready to approve.

Comment on lines 59 to 71

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's not your fault, but this logic seems confusing. What do you think of this as an alternative?

} else {
    switch (contours.value.length) {
        case 0:
            contours.value = [0, 1];
            break;
        case 1:
            zvalue = parseFloat(contours.value[0]);
            contours.value = [zvalue, zvalue + 1];
            break;
        default:
            // Keep only the first two values
            contours.value = [parseFloat(contours.value[0]), parseFloat(contours.value[1])];
    }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's much cleaner this way. I've made the changes based on your suggestion.

Comment thread test/jasmine/tests/contour_test.js Outdated
Comment on lines +134 to +135
value: [10, 20, 30],
expected: [10, 20]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you update this case to use float values to check the call to parseFloat?

expect(traceOut.ycalendar).toBe('ethiopian');
});

it('should coerce constraint interval values to two numbers', function() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you add a case that checks a float array?

@camdecoster
camdecoster merged commit d9b903e into plotly:main Nov 19, 2026
87 of 89 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Incorrect interval constraint normalization for contours.value in contour traces

2 participants