Skip to content

Commit a69c3ad

Browse files
committed
simplify handling of equivalent constraint operations
1 parent 8134a61 commit a69c3ad

File tree

11 files changed

+43
-40
lines changed

11 files changed

+43
-40
lines changed

src/components/legend/style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module.exports = function style(s, gd) {
6767

6868
if(contours && contours.type === 'constraint') {
6969
showLine = contours.showlines;
70-
showFill = contours.operation !== '=';
70+
showFill = contours._operation !== '=';
7171
}
7272

7373
var fill = d3.select(this).select('.legendfill').selectAll('path')

src/constants/filter_ops.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,25 @@ module.exports = {
1212
COMPARISON_OPS: ['=', '!=', '<', '>=', '>', '<='],
1313
COMPARISON_OPS2: ['=', '<', '>=', '>', '<='],
1414
INTERVAL_OPS: ['[]', '()', '[)', '(]', '][', ')(', '](', ')['],
15-
SET_OPS: ['{}', '}{']
15+
SET_OPS: ['{}', '}{'],
16+
CONSTRAINT_REDUCTION: {
17+
// for contour constraints, open/closed endpoints are equivalent
18+
'=': '=',
19+
20+
'<': '<',
21+
'<=': '<',
22+
23+
'>': '>',
24+
'>=': '>',
25+
26+
'[]': '[]',
27+
'()': '[]',
28+
'[)': '[]',
29+
'(]': '[]',
30+
31+
'][': '][',
32+
')(': '][',
33+
'](': '][',
34+
')[': ']['
35+
}
1636
};

src/traces/contour/attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ module.exports = extendFlat({
230230
'(' + INTERVAL_OPS + ')',
231231
'*value* is expected to be 2-item array where the first item',
232232
'is the lower bound and the second item is the upper bound.',
233+
234+
'Open vs. closed intervals make no difference to constraint display.'
233235
].join(' ')
234236
},
235237
editType: 'calc',

src/traces/contour/close_boundaries.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,23 @@ module.exports = function(pathinfo, operation, perimeter, trace) {
3838

3939
switch(operation) {
4040
case '>':
41-
case '>=':
4241
if(contours.value > boundaryMax) {
4342
pi0.prefixBoundary = true;
4443
}
4544
break;
4645
case '<':
47-
case '<=':
4846
if(contours.value < boundaryMin) {
4947
pi0.prefixBoundary = true;
5048
}
5149
break;
5250
case '[]':
53-
case '()':
5451
v1 = Math.min.apply(null, contours.value);
5552
v2 = Math.max.apply(null, contours.value);
5653
if(v2 < boundaryMin || v1 > boundaryMax) {
5754
pi0.prefixBoundary = true;
5855
}
5956
break;
6057
case '][':
61-
case ')(':
6258
v1 = Math.min.apply(null, contours.value);
6359
v2 = Math.max.apply(null, contours.value);
6460
if(v1 < boundaryMin && v2 > boundaryMax) {

src/traces/contour/constraint_defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ var handleLabelDefaults = require('./label_defaults');
1414
var Color = require('../../components/color');
1515
var addOpacity = Color.addOpacity;
1616
var opacity = Color.opacity;
17+
var CONSTRAINT_REDUCTION = require('../../constants/filter_ops').CONSTRAINT_REDUCTION;
1718

1819
module.exports = function handleConstraintDefaults(traceIn, traceOut, coerce, layout, defaultColor, opts) {
1920
var contours = traceOut.contours;
2021
var showLines, lineColor, fillColor;
2122

2223
var operation = coerce('contours.operation');
24+
contours._operation = CONSTRAINT_REDUCTION[operation];
2325

2426
handleConstraintValueDefaults(coerce, contours);
2527

src/traces/contour/constraint_mapping.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,13 @@ var isNumeric = require('fast-isnumeric');
1313

1414
// This syntax conforms to the existing filter transform syntax, but we don't care
1515
// about open vs. closed intervals for simply drawing contours constraints:
16-
module.exports['[]'] = makeRangeSettings('[]');
17-
module.exports['()'] = makeRangeSettings('()');
18-
module.exports['[)'] = makeRangeSettings('[)');
19-
module.exports['(]'] = makeRangeSettings('(]');
20-
21-
// Inverted intervals simply flip the sign:
22-
module.exports[']['] = makeRangeSettings('][');
23-
module.exports[')('] = makeRangeSettings(')(');
24-
module.exports[')['] = makeRangeSettings(')[');
25-
module.exports[']('] = makeRangeSettings('](');
26-
27-
module.exports['>'] = makeInequalitySettings('>');
28-
module.exports['>='] = makeInequalitySettings('>=');
29-
module.exports['<'] = makeInequalitySettings('<');
30-
module.exports['<='] = makeInequalitySettings('<=');
31-
module.exports['='] = makeInequalitySettings('=');
16+
module.exports = {
17+
'[]': makeRangeSettings('[]'),
18+
'][': makeRangeSettings(']['),
19+
'>': makeInequalitySettings('>'),
20+
'<': makeInequalitySettings('<'),
21+
'=': makeInequalitySettings('=')
22+
};
3223

3324
// This does not in any way shape or form support calendars. It's adapted from
3425
// transforms/filter.js.

src/traces/contour/constraint_value_defaults.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
// var constraintMapping = require('./constraint_mapping');
1313
var isNumeric = require('fast-isnumeric');
14+
var COMPARISON_OPS2 = require('../../constants/filter_ops').COMPARISON_OPS2;
1415

1516
module.exports = function(coerce, contours) {
1617
var zvalue;
17-
var scalarValuedOps = ['=', '<', '<=', '>', '>='];
1818

19-
if(scalarValuedOps.indexOf(contours.operation) === -1) {
19+
if(COMPARISON_OPS2.indexOf(contours.operation) === -1) {
2020
// Requires an array of two numbers:
2121
coerce('contours.value', [0, 1]);
2222

src/traces/contour/convert_to_constraints.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ module.exports = function(pathinfo, operation) {
2323
switch(operation) {
2424
case '=':
2525
case '<':
26-
case '<=':
2726
return pathinfo;
28-
case '>=':
2927
case '>':
3028
if(pathinfo.length !== 1) {
3129
Lib.warn('Contour data invalid for the specified inequality operation.');
@@ -45,18 +43,12 @@ module.exports = function(pathinfo, operation) {
4543
}
4644
return pathinfo;
4745
case '][':
48-
case ')[':
49-
case '](':
50-
case ')(':
5146
var tmp = op0;
5247
op0 = op1;
5348
op1 = tmp;
5449
// It's a nice rule, except this definitely *is* what's intended here.
5550
/* eslint-disable: no-fallthrough */
5651
case '[]':
57-
case '[)':
58-
case '(]':
59-
case '()':
6052
/* eslint-enable: no-fallthrough */
6153
if(pathinfo.length !== 2) {
6254
Lib.warn('Contour data invalid for the specified inequality range operation.');

src/traces/contour/empty_pathinfo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var endPlus = require('./end_plus');
1414

1515
module.exports = function emptyPathinfo(contours, plotinfo, cd0) {
1616
var contoursFinal = (contours.type === 'constraint') ?
17-
constraintMapping[contours.operation](contours.value) :
17+
constraintMapping[contours._operation](contours.value) :
1818
contours;
1919

2020
var cs = contoursFinal.size;

src/traces/contour/plot.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ function plotOne(gd, plotinfo, cd) {
8585

8686
var fillPathinfo = pathinfo;
8787
if(contours.type === 'constraint') {
88-
fillPathinfo = convertToConstraints(pathinfo, contours.operation);
89-
closeBoundaries(fillPathinfo, contours.operation, perimeter, trace);
88+
fillPathinfo = convertToConstraints(pathinfo, contours._operation);
89+
closeBoundaries(fillPathinfo, contours._operation, perimeter, trace);
9090
}
9191

9292
// draw everything
@@ -131,7 +131,7 @@ function makeFills(plotgroup, pathinfo, perimeter, contours) {
131131
.classed('contourfill', true);
132132

133133
var fillitems = fillgroup.selectAll('path')
134-
.data(contours.coloring === 'fill' || (contours.type === 'constraint' && contours.operation !== '=') ? pathinfo : []);
134+
.data(contours.coloring === 'fill' || (contours.type === 'constraint' && contours._operation !== '=') ? pathinfo : []);
135135
fillitems.enter().append('path');
136136
fillitems.exit().remove();
137137
fillitems.each(function(pi) {

src/traces/contourcarpet/plot.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ function plotOne(gd, plotinfo, cd) {
5050
var id = 'contour' + uid;
5151
var pathinfo = emptyPathinfo(contours, plotinfo, cd[0]);
5252
var isConstraint = contours.type === 'constraint';
53-
var isEquality = contours.operation === '=';
54-
var coloring = isConstraint ? (isEquality ? 'lines' : 'fill') : contours.coloring;
53+
var operation = contours._operation;
54+
var coloring = isConstraint ? (operation === '=' ? 'lines' : 'fill') : contours.coloring;
5555

5656
// Map [a, b] (data) --> [i, j] (pixels)
5757
function ab2p(ab) {
@@ -88,8 +88,8 @@ function plotOne(gd, plotinfo, cd) {
8888
// See: https://github.com/plotly/plotly.js/issues/1356
8989
var fillPathinfo = pathinfo;
9090
if(contours.type === 'constraint') {
91-
fillPathinfo = convertToConstraints(pathinfo, contours.operation);
92-
closeBoundaries(fillPathinfo, contours.operation, perimeter, trace);
91+
fillPathinfo = convertToConstraints(pathinfo, operation);
92+
closeBoundaries(fillPathinfo, operation, perimeter, trace);
9393
}
9494

9595
// Map the paths in a/b coordinates to pixel coordinates:

0 commit comments

Comments
 (0)