Skip to content

Commit a671683

Browse files
committed
add smart default for range selector 'x' and 'y':
- based off corresponding xaxis and yaxis domain
1 parent 1ca424a commit a671683

File tree

5 files changed

+100
-16
lines changed

5 files changed

+100
-16
lines changed

src/components/rangeselector/attributes.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ module.exports = {
3434
valType: 'number',
3535
min: -2,
3636
max: 3,
37-
dflt: 0,
3837
role: 'style',
3938
description: 'Sets the x position (in normalized coordinates) of the range selector.'
4039
},
@@ -53,7 +52,6 @@ module.exports = {
5352
valType: 'number',
5453
min: -2,
5554
max: 3,
56-
dflt: 1.02,
5755
role: 'style',
5856
description: 'Sets the y position (in normalized coordinates) of the range selector.'
5957
},

src/components/rangeselector/constants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@
1010

1111

1212
module.exports = {
13+
14+
// 'y' position pad above counter axis domain
15+
yPad: 0.02,
16+
17+
// minimum button width (regardless of text size)
1318
minButtonWidth: 30
1419
};

src/components/rangeselector/defaults.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ var Lib = require('../../lib');
1212

1313
var attributes = require('./attributes');
1414
var buttonAttrs = require('./button_attributes');
15+
var constants = require('./constants');
1516

1617

17-
module.exports = function rangeSelectorDefaults(containerIn, containerOut, layout) {
18+
module.exports = function rangeSelectorDefaults(containerIn, containerOut, layout, counterAxes) {
1819
var selectorIn = containerIn.rangeselector || {},
1920
selectorOut = containerOut.rangeselector = {};
2021

@@ -27,8 +28,9 @@ module.exports = function rangeSelectorDefaults(containerIn, containerOut, layou
2728
var visible = coerce('visible', buttons.length > 0);
2829
if(!visible) return;
2930

30-
coerce('x');
31-
coerce('y');
31+
var posDflt = getPosDflt(containerOut, layout, counterAxes);
32+
coerce('x', posDflt[0]);
33+
coerce('y', posDflt[1]);
3234
Lib.noneOrAll(containerIn, containerOut, ['x', 'y']);
3335

3436
coerce('xanchor');
@@ -68,3 +70,16 @@ function buttonsDefaults(containerIn, containerOut) {
6870

6971
return buttonsOut;
7072
}
73+
74+
function getPosDflt(containerOut, layout, counterAxes) {
75+
var anchoredList = counterAxes.filter(function(ax) {
76+
return layout[ax].anchor === containerOut._id;
77+
});
78+
79+
var posY = 0;
80+
for(var i = 0; i < anchoredList.length; i++) {
81+
posY = Math.max(layout[anchoredList[i]].domain[1], posY);
82+
}
83+
84+
return [containerOut.domain[0], posY + constants.yPad];
85+
}

src/plots/cartesian/layout_defaults.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,6 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
134134
handleAxisDefaults(axLayoutIn, axLayoutOut, coerce, defaultOptions);
135135
handlePositionDefaults(axLayoutIn, axLayoutOut, coerce, positioningOptions);
136136

137-
if(axLetter === 'x' && axLayoutOut.type === 'date') {
138-
RangeSelector.supplyLayoutDefaults(axLayoutIn, axLayoutOut, layoutOut);
139-
}
140-
141137
layoutOut[axName] = axLayoutOut;
142138

143139
// so we don't have to repeat autotype unnecessarily,
@@ -148,12 +144,18 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
148144

149145
});
150146

151-
// quick second pass for rangeslider defaults
147+
// quick second pass for range slider and selector defaults
152148
axesList.forEach(function(axName) {
153149
var axLetter = axName.charAt(0),
150+
axLayoutIn = layoutIn[axName],
151+
axLayoutOut = layoutOut[axName],
154152
counterAxes = {x: yaList, y: xaList}[axLetter];
155153

156154
RangeSlider.supplyLayoutDefaults(layoutIn, layoutOut, axName, counterAxes);
155+
156+
if(axLetter === 'x' && axLayoutOut.type === 'date') {
157+
RangeSelector.supplyLayoutDefaults(axLayoutIn, axLayoutOut, layoutOut, counterAxes);
158+
}
157159
});
158160

159161
// plot_bgcolor only makes sense if there's a (2D) plot!

test/jasmine/tests/range_selector_test.js

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,23 @@ describe('[range selector suite]', function() {
1616
describe('defaults:', function() {
1717
var supplyLayoutDefaults = RangeSelector.supplyLayoutDefaults;
1818

19+
function supply(containerIn, containerOut) {
20+
containerOut.domain = [0, 1];
21+
22+
var layout = {
23+
yaxis: { domain: [0, 1] }
24+
};
25+
26+
var counterAxes = ['yaxis'];
27+
28+
supplyLayoutDefaults(containerIn, containerOut, layout, counterAxes);
29+
}
30+
1931
it('should set \'visible\' to false when no buttons are present', function() {
2032
var containerIn = {};
2133
var containerOut = {};
2234

23-
supplyLayoutDefaults(containerIn, containerOut, {});
35+
supply(containerIn, containerOut);
2436

2537
expect(containerOut.rangeselector)
2638
.toEqual({
@@ -37,7 +49,7 @@ describe('[range selector suite]', function() {
3749
};
3850
var containerOut = {};
3951

40-
supplyLayoutDefaults(containerIn, containerOut, {});
52+
supply(containerIn, containerOut);
4153

4254
expect(containerIn.rangeselector.buttons).toEqual([{}]);
4355
expect(containerOut.rangeselector.buttons).toEqual([{
@@ -60,7 +72,7 @@ describe('[range selector suite]', function() {
6072
};
6173
var containerOut = {};
6274

63-
supplyLayoutDefaults(containerIn, containerOut, {});
75+
supply(containerIn, containerOut, {}, []);
6476

6577
expect(containerOut.rangeselector.visible).toBe(true);
6678
expect(containerOut.rangeselector.buttons).toEqual([
@@ -80,14 +92,67 @@ describe('[range selector suite]', function() {
8092
};
8193
var containerOut = {};
8294

83-
supplyLayoutDefaults(containerIn, containerOut, {});
95+
supply(containerIn, containerOut, {}, []);
8496

8597
expect(containerOut.rangeselector.buttons).toEqual([{
8698
step: 'all',
8799
label: 'full range'
88100
}]);
89101
});
90102

103+
it('should use axis and counter axis to determine \'x\' and \'y\' defaults (case 1 y)', function() {
104+
var containerIn = {
105+
rangeselector: { buttons: [{}] }
106+
};
107+
var containerOut = {
108+
_id: 'x',
109+
domain: [0, 0.5]
110+
};
111+
var layout = {
112+
xaxis: containerIn,
113+
yaxis: {
114+
anchor: 'x',
115+
domain: [0, 0.45]
116+
}
117+
};
118+
var counterAxes = ['yaxis'];
119+
120+
supplyLayoutDefaults(containerIn, containerOut, layout, counterAxes);
121+
122+
expect(containerOut.rangeselector.x).toEqual(0);
123+
expect(containerOut.rangeselector.y).toBeCloseTo(0.47);
124+
});
125+
126+
it('should use axis and counter axis to determine \'x\' and \'y\' defaults (case multi y)', function() {
127+
var containerIn = {
128+
rangeselector: { buttons: [{}] }
129+
};
130+
var containerOut = {
131+
_id: 'x',
132+
domain: [0.5, 1]
133+
};
134+
var layout = {
135+
xaxis: containerIn,
136+
yaxis: {
137+
anchor: 'x',
138+
domain: [0, 0.25]
139+
},
140+
yaxis2: {
141+
anchor: 'x',
142+
domain: [0.3, 0.55]
143+
},
144+
yaxis3: {
145+
anchor: 'x',
146+
domain: [0.6, 0.85]
147+
}
148+
};
149+
var counterAxes = ['yaxis', 'yaxis2', 'yaxis3'];
150+
151+
supplyLayoutDefaults(containerIn, containerOut, layout, counterAxes);
152+
153+
expect(containerOut.rangeselector.x).toEqual(0.5);
154+
expect(containerOut.rangeselector.y).toBeCloseTo(0.87);
155+
});
91156
});
92157

93158
describe('getUpdateObject:', function() {
@@ -331,13 +396,12 @@ describe('[range selector suite]', function() {
331396
expect(d3.selectAll(query).size()).toEqual(cnt);
332397
}
333398

334-
it('should display ', function() {
399+
it('should display the correct nodes', function() {
335400
assertNodeCount('.rangeselector', 1);
336401
assertNodeCount('.button', mockCopy.layout.xaxis.rangeselector.buttons.length);
337402
});
338403

339404
it('should be able to be removed by `relayout`', function(done) {
340-
341405
Plotly.relayout(gd, 'xaxis.rangeselector.visible', false).then(function() {
342406
assertNodeCount('.rangeselector', 0);
343407
assertNodeCount('.button', 0);

0 commit comments

Comments
 (0)