Skip to content

Commit 7c28505

Browse files
authored
Merge pull request #3915 from plotly/fix2950-parcoords-cannot-select-first-enum
Fix2950 parcoords cannot select first enum
2 parents 36a48f7 + 0555cfa commit 7c28505

File tree

3 files changed

+106
-27
lines changed

3 files changed

+106
-27
lines changed

src/traces/parcoords/axisbrush.js

+21-27
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,31 @@ function closeToCovering(v, vAdjacent) { return v * (1 - snapClose) + vAdjacent
2525
// so it's clear we're covering it
2626
// find the interval we're in, and snap to 1/4 the distance to the next
2727
// these two could be unified at a slight loss of readability / perf
28-
function ordinalScaleSnapLo(a, v, existingRanges) {
28+
function ordinalScaleSnap(isHigh, a, v, existingRanges) {
2929
if(overlappingExisting(v, existingRanges)) return v;
3030

31-
var aPrev = a[0];
32-
var aPrevPrev = aPrev;
33-
for(var i = 1; i < a.length; i++) {
34-
var aNext = a[i];
31+
var dir = isHigh ? -1 : 1;
3532

36-
// very close to the previous - snap down to it
37-
if(v < closeToCovering(aPrev, aNext)) return snapOvershoot(aPrev, aPrevPrev);
38-
if(v < aNext || i === a.length - 1) return snapOvershoot(aNext, aPrev);
39-
40-
aPrevPrev = aPrev;
41-
aPrev = aNext;
33+
var first = 0;
34+
var last = a.length - 1;
35+
if(dir < 0) {
36+
var tmp = first;
37+
first = last;
38+
last = tmp;
4239
}
43-
}
44-
45-
function ordinalScaleSnapHi(a, v, existingRanges) {
46-
if(overlappingExisting(v, existingRanges)) return v;
4740

48-
var aPrev = a[a.length - 1];
49-
var aPrevPrev = aPrev;
50-
for(var i = a.length - 2; i >= 0; i--) {
51-
var aNext = a[i];
41+
var aHere = a[first];
42+
var aPrev = aHere;
43+
for(var i = first; dir * i < dir * last; i += dir) {
44+
var nextI = i + dir;
45+
var aNext = a[nextI];
5246

5347
// very close to the previous - snap down to it
54-
if(v > closeToCovering(aPrev, aNext)) return snapOvershoot(aPrev, aPrevPrev);
55-
if(v > aNext || i === a.length - 1) return snapOvershoot(aNext, aPrev);
48+
if(dir * v < dir * closeToCovering(aHere, aNext)) return snapOvershoot(aHere, aPrev);
49+
if(dir * v < dir * aNext || nextI === last) return snapOvershoot(aNext, aHere);
5650

57-
aPrevPrev = aPrev;
58-
aPrev = aNext;
51+
aPrev = aHere;
52+
aHere = aNext;
5953
}
6054
}
6155

@@ -333,8 +327,8 @@ function attachDragBehavior(selection) {
333327
var a = d.unitTickvals;
334328
if(a[a.length - 1] < a[0]) a.reverse();
335329
s.newExtent = [
336-
ordinalScaleSnapLo(a, s.newExtent[0], s.stayingIntervals),
337-
ordinalScaleSnapHi(a, s.newExtent[1], s.stayingIntervals)
330+
ordinalScaleSnap(0, a, s.newExtent[0], s.stayingIntervals),
331+
ordinalScaleSnap(1, a, s.newExtent[1], s.stayingIntervals)
338332
];
339333
var hasNewExtent = s.newExtent[1] > s.newExtent[0];
340334
s.extent = s.stayingIntervals.concat(hasNewExtent ? [s.newExtent] : []);
@@ -505,8 +499,8 @@ function cleanRanges(ranges, dimension) {
505499
var sortedTickVals = dimension.tickvals.slice().sort(sortAsc);
506500
ranges = ranges.map(function(ri) {
507501
var rSnapped = [
508-
ordinalScaleSnapLo(sortedTickVals, ri[0], []),
509-
ordinalScaleSnapHi(sortedTickVals, ri[1], [])
502+
ordinalScaleSnap(0, sortedTickVals, ri[0], []),
503+
ordinalScaleSnap(1, sortedTickVals, ri[1], [])
510504
];
511505
if(rSnapped[1] > rSnapped[0]) return rSnapped;
512506
})
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"data": [
3+
{
4+
"type": "parcoords",
5+
"line": {
6+
"colorscale": "Portland",
7+
"showscale": true,
8+
"reversescale": true,
9+
"color": [
10+
1,
11+
2,
12+
3,
13+
4,
14+
5,
15+
6,
16+
7,
17+
8
18+
]
19+
},
20+
"dimensions": [
21+
{
22+
"label": "A",
23+
"values": [
24+
1,
25+
2,
26+
3,
27+
4,
28+
5,
29+
6,
30+
7,
31+
8
32+
],
33+
"tickvals": [
34+
1,
35+
2,
36+
3,
37+
4,
38+
5,
39+
6,
40+
7,
41+
8
42+
],
43+
"constraintrange": [
44+
[
45+
1,
46+
1
47+
],
48+
[
49+
3,
50+
3
51+
],
52+
[
53+
5,
54+
6
55+
],
56+
[
57+
8,
58+
8
59+
]
60+
]
61+
},
62+
{
63+
"label": "B",
64+
"values": [
65+
1,
66+
2,
67+
3,
68+
5,
69+
8,
70+
13,
71+
22,
72+
35
73+
]
74+
}
75+
]
76+
}
77+
],
78+
"layout": {
79+
"width": 400,
80+
"height": 400,
81+
"title": {
82+
"text": "should select first and last enum"
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)