Skip to content

Commit c9f76ab

Browse files
committed
add positionning logic to range selector draw step
1 parent 86331c7 commit c9f76ab

File tree

2 files changed

+71
-19
lines changed

2 files changed

+71
-19
lines changed

src/components/rangeselector/constants.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010

1111

1212
module.exports = {
13-
14-
13+
minButtonWidth: 30
1514
};

src/components/rangeselector/draw.js

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var d3 = require('d3');
1313

1414
var Plotly = require('../../plotly');
15+
var Plots = require('../../plots/plots');
1516
var Color = require('../color');
1617
var Drawing = require('../drawing');
1718
var svgTextUtils = require('../../lib/svg_text_utils');
@@ -58,17 +59,22 @@ module.exports = function draw(gd) {
5859
button.call(drawButtonText, selectorLayout, d);
5960

6061
button.on('click', function() {
62+
if(gd._dragged) return;
63+
6164
var update = getUpdateObject(axisLayout, d);
6265

6366
Plotly.relayout(gd, update);
6467
});
6568
});
6669

67-
reposition(buttons, selectorLayout, fullLayout._size);
70+
// N.B. this mutates selectorLayout
71+
reposition(gd, buttons, selectorLayout);
72+
73+
selector.attr('transform', 'translate(' +
74+
selectorLayout.lx + ',' + selectorLayout.ly +
75+
')');
6876
});
6977

70-
// FAKE HARDCODED POSITION - PLZ REMOVE BEFORE MERGE
71-
selectors.attr('transform', 'translate(80, 60)');
7278
};
7379

7480
function makeSelectorData(gd) {
@@ -127,26 +133,37 @@ function drawButtonText(button, selectorLayout, d) {
127133
.call(textLayout);
128134
}
129135

130-
function reposition(buttons, opts, graphSize) {
136+
function reposition(gd, buttons, opts) {
131137
opts.width = 0;
132138
opts.height = 0;
133139

134140
var borderWidth = opts.borderwidth;
135141

136-
buttons.each(function(d) {
142+
buttons.each(function() {
143+
var button = d3.select(this),
144+
text = button.select('.selector-text'),
145+
tspans = text.selectAll('tspan');
146+
147+
var tHeight = opts.font.size * 1.3,
148+
tLines = tspans[0].length || 1,
149+
hEff = Math.max(tHeight * tLines, 16) + 3;
150+
151+
opts.height = Math.max(opts.height, hEff);
152+
});
153+
154+
buttons.each(function() {
137155
var button = d3.select(this),
138156
rect = button.select('.selector-rect'),
139157
text = button.select('.selector-text'),
140-
tspans = text.selectAll('tspan'),
141-
mathJaxGroup = button.select('g[class*=math-group]');
158+
tspans = text.selectAll('tspan');
142159

143160
var tWidth = text.node() && Drawing.bBox(text.node()).width,
144161
tHeight = opts.font.size * 1.3,
145162
tLines = tspans[0].length || 1;
146163

164+
var wEff = Math.max(tWidth + 10, constants.minButtonWidth);
147165

148-
var wEff = Math.max(tWidth + 10, 30),
149-
hEff = Math.max(tHeight * tLines, 16) + 3;
166+
// mathJaxGroup = button.select('g[class*=math-group]');
150167

151168
// TODO add buttongap attribute
152169

@@ -157,22 +174,58 @@ function reposition(buttons, opts, graphSize) {
157174
rect.attr({
158175
x: 0,
159176
y: 0,
160-
width: wEff
177+
width: wEff,
178+
height: opts.height
161179
});
162180

163-
var textAttr = {
181+
var textAttrs = {
164182
x: wEff / 2,
165-
y: tHeight * (1.5 - tLines / 2) - 2 // could do better
183+
y: opts.height / 2 - ((tLines - 1) * tHeight / 2) + 3
166184
};
167185

168-
text.attr(textAttr);
169-
tspans.attr(textAttr);
186+
text.attr(textAttrs);
187+
tspans.attr(textAttrs);
170188

171189
opts.width += wEff + 5;
172-
opts.height = Math.max(opts.height, hEff);
173-
174-
console.log([wEff, opts.width], [hEff, opts.height]);
175190
});
176191

177192
buttons.selectAll('rect').attr('height', opts.height);
193+
194+
var graphSize = gd._fullLayout._size;
195+
opts.lx = graphSize.l + graphSize.w * opts.x;
196+
opts.ly = graphSize.t + graphSize.h * (1 - opts.y);
197+
198+
var xanchor = 'left';
199+
if(anchorUtils.isRightAnchor(opts)) {
200+
opts.lx -= opts.width;
201+
xanchor = 'right';
202+
}
203+
if(anchorUtils.isCenterAnchor(opts)) {
204+
opts.lx -= opts.width / 2;
205+
xanchor = 'center';
206+
}
207+
208+
var yanchor = 'top';
209+
if(anchorUtils.isBottomAnchor(opts)) {
210+
opts.ly -= opts.height;
211+
yanchor = 'bottom';
212+
}
213+
if(anchorUtils.isMiddleAnchor(opts)) {
214+
opts.ly -= opts.height / 2;
215+
yanchor = 'middle';
216+
}
217+
218+
opts.width = Math.ceil(opts.width);
219+
opts.height = Math.ceil(opts.height);
220+
opts.lx = Math.round(opts.lx);
221+
opts.ly = Math.round(opts.ly);
222+
223+
Plots.autoMargin(gd, 'range-selector', {
224+
x: opts.x,
225+
y: opts.y,
226+
l: opts.width * ({right: 1, center: 0.5}[xanchor] || 0),
227+
r: opts.width * ({left: 1, center: 0.5}[xanchor] || 0),
228+
b: opts.height * ({top: 1, middle: 0.5}[yanchor] || 0),
229+
t: opts.height * ({bottom: 1, middle: 0.5}[yanchor] || 0)
230+
});
178231
}

0 commit comments

Comments
 (0)