-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathplot.js
451 lines (377 loc) · 14.6 KB
/
plot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/**
* Copyright 2012-2019, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var svgTextUtils = require('../../lib/svg_text_utils');
var Color = require('../../components/color');
var Drawing = require('../../components/drawing');
var Registry = require('../../registry');
var attributes = require('./attributes');
var attributeText = attributes.text;
var attributeTextPosition = attributes.textposition;
var helpers = require('./helpers');
var style = require('./style');
// padding in pixels around text
var TEXTPAD = 3;
module.exports = function plot(gd, plotinfo, cdbar, barLayer) {
var xa = plotinfo.xaxis;
var ya = plotinfo.yaxis;
var fullLayout = gd._fullLayout;
var bartraces = Lib.makeTraceGroups(barLayer, cdbar, 'trace bars').each(function(cd) {
var plotGroup = d3.select(this);
var cd0 = cd[0];
var trace = cd0.trace;
var isWaterfall = (trace.type === 'waterfall');
var isTriangle = (isWaterfall) ? (trace.marker.shape === 'triangle') : false;
var isVertical = (trace.orientation === 'v');
if(!plotinfo.isRangePlot) cd0.node3 = plotGroup;
var pointGroup = Lib.ensureSingle(plotGroup, 'g', 'points');
var bars = pointGroup.selectAll('g.point').data(Lib.identity);
bars.enter().append('g')
.classed('point', true);
bars.exit().remove();
bars.each(function(di, i) {
var bar = d3.select(this);
// now display the bar
// clipped xf/yf (2nd arg true): non-positive
// log values go off-screen by plotwidth
// so you see them continue if you drag the plot
var x0, x1, y0, y1;
if(trace.orientation === 'h') {
y0 = ya.c2p(di.p0, true);
y1 = ya.c2p(di.p1, true);
x0 = xa.c2p(di.s0, true);
x1 = xa.c2p(di.s1, true);
// for selections
di.ct = [x1, (y0 + y1) / 2];
}
else {
x0 = xa.c2p(di.p0, true);
x1 = xa.c2p(di.p1, true);
y0 = ya.c2p(di.s0, true);
y1 = ya.c2p(di.s1, true);
// for selections
di.ct = [(x0 + x1) / 2, y1];
}
if(!isNumeric(x0) || !isNumeric(x1) ||
!isNumeric(y0) || !isNumeric(y1) ||
x0 === x1 || y0 === y1) {
bar.remove();
return;
}
var lw = (di.mlw + 1 || trace.marker.line.width + 1 ||
(di.trace ? di.trace.marker.line.width : 0) + 1) - 1;
var offset = d3.round((lw / 2) % 1, 2);
function roundWithLine(v) {
// if there are explicit gaps, don't round,
// it can make the gaps look crappy
return (fullLayout.bargap === 0 && fullLayout.bargroupgap === 0) ?
d3.round(Math.round(v) - offset, 2) : v;
}
function expandToVisible(v, vc) {
// if it's not in danger of disappearing entirely,
// round more precisely
return Math.abs(v - vc) >= 2 ? roundWithLine(v) :
// but if it's very thin, expand it so it's
// necessarily visible, even if it might overlap
// its neighbor
(v > vc ? Math.ceil(v) : Math.floor(v));
}
if(!gd._context.staticPlot) {
// if bars are not fully opaque or they have a line
// around them, round to integer pixels, mainly for
// safari so we prevent overlaps from its expansive
// pixelation. if the bars ARE fully opaque and have
// no line, expand to a full pixel to make sure we
// can see them
var op = Color.opacity(di.mc || trace.marker.color);
var fixpx = (op < 1 || lw > 0.01) ? roundWithLine : expandToVisible;
x0 = fixpx(x0, x1);
x1 = fixpx(x1, x0);
y0 = fixpx(y0, y1);
y1 = fixpx(y1, y0);
}
var shape;
if(isWaterfall && isTriangle && i > 0 && cd[i].isFall === false) {
if(isVertical) {
shape = 'M' + x0 + ',' + y0 + 'L' + (0.5 * (x1 + x0)) + ',' + y1 + 'L' + x1 + ',' + y0 + 'Z';
} else {
shape = 'M' + x0 + ',' + y0 + 'L' + x1 + ',' + (0.5 * (y1 + y0)) + 'L' + x0 + ',' + y1 + 'Z';
}
} else {
shape = 'M' + x0 + ',' + y0 + 'V' + y1 + 'H' + x1 + 'V' + y0 + 'Z';
}
Lib.ensureSingle(bar, 'path')
.style('vector-effect', 'non-scaling-stroke')
.attr('d', shape)
.call(Drawing.setClipUrl, plotinfo.layerClipId, gd);
appendBarText(gd, bar, cd, i, x0, x1, y0, y1);
if(plotinfo.layerClipId) {
Drawing.hideOutsideRangePoint(di, bar.select('text'), xa, ya, trace.xcalendar, trace.ycalendar);
}
});
// lastly, clip points groups of `cliponaxis !== false` traces
// on `plotinfo._hasClipOnAxisFalse === true` subplots
var hasClipOnAxisFalse = cd0.trace.cliponaxis === false;
Drawing.setClipUrl(plotGroup, hasClipOnAxisFalse ? null : plotinfo.layerClipId, gd);
});
// error bars are on the top
Registry.getComponentMethod('errorbars', 'plot')(gd, bartraces, plotinfo);
};
function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
var textPosition;
function appendTextNode(bar, text, textFont) {
var textSelection = Lib.ensureSingle(bar, 'text')
.text(text)
.attr({
'class': 'bartext bartext-' + textPosition,
transform: '',
'text-anchor': 'middle',
// prohibit tex interpretation until we can handle
// tex and regular text together
'data-notex': 1
})
.call(Drawing.font, textFont)
.call(svgTextUtils.convertToTspans, gd);
return textSelection;
}
// get trace attributes
var trace = calcTrace[0].trace;
var orientation = trace.orientation;
var text = getText(trace, i);
textPosition = getTextPosition(trace, i);
if(!text || textPosition === 'none') {
bar.select('text').remove();
return;
}
var layoutFont = gd._fullLayout.font;
var barColor = style.getBarColor(calcTrace[i], trace);
var insideTextFont = style.getInsideTextFont(trace, i, layoutFont, barColor);
var outsideTextFont = style.getOutsideTextFont(trace, i, layoutFont);
// compute text position
var barmode = gd._fullLayout.barmode;
var inStackMode = (barmode === 'stack');
var inRelativeMode = (barmode === 'relative');
var inStackOrRelativeMode = inStackMode || inRelativeMode;
var calcBar = calcTrace[i];
var isOutmostBar = !inStackOrRelativeMode || calcBar._outmost;
var barWidth = Math.abs(x1 - x0) - 2 * TEXTPAD; // padding excluded
var barHeight = Math.abs(y1 - y0) - 2 * TEXTPAD; // padding excluded
var textSelection;
var textBB;
var textWidth;
var textHeight;
if(textPosition === 'outside') {
if(!isOutmostBar && !calcBar.hasB) textPosition = 'inside';
}
if(textPosition === 'auto') {
if(isOutmostBar) {
// draw text using insideTextFont and check if it fits inside bar
textPosition = 'inside';
textSelection = appendTextNode(bar, text, insideTextFont);
textBB = Drawing.bBox(textSelection.node()),
textWidth = textBB.width,
textHeight = textBB.height;
var textHasSize = (textWidth > 0 && textHeight > 0);
var fitsInside = (textWidth <= barWidth && textHeight <= barHeight);
var fitsInsideIfRotated = (textWidth <= barHeight && textHeight <= barWidth);
var fitsInsideIfShrunk = (orientation === 'h') ?
(barWidth >= textWidth * (barHeight / textHeight)) :
(barHeight >= textHeight * (barWidth / textWidth));
if(textHasSize &&
(fitsInside || fitsInsideIfRotated || fitsInsideIfShrunk)) {
textPosition = 'inside';
}
else {
textPosition = 'outside';
textSelection.remove();
textSelection = null;
}
}
else textPosition = 'inside';
}
if(!textSelection) {
textSelection = appendTextNode(bar, text,
(textPosition === 'outside') ?
outsideTextFont : insideTextFont);
textBB = Drawing.bBox(textSelection.node()),
textWidth = textBB.width,
textHeight = textBB.height;
if(textWidth <= 0 || textHeight <= 0) {
textSelection.remove();
return;
}
}
// compute text transform
var transform, constrained;
if(textPosition === 'outside') {
constrained = trace.constraintext === 'both' || trace.constraintext === 'outside';
transform = getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB,
orientation, constrained);
}
else {
constrained = trace.constraintext === 'both' || trace.constraintext === 'inside';
transform = getTransformToMoveInsideBar(x0, x1, y0, y1, textBB,
orientation, constrained);
}
textSelection.attr('transform', transform);
}
function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation, constrained) {
// compute text and target positions
var textWidth = textBB.width;
var textHeight = textBB.height;
var textX = (textBB.left + textBB.right) / 2;
var textY = (textBB.top + textBB.bottom) / 2;
var barWidth = Math.abs(x1 - x0);
var barHeight = Math.abs(y1 - y0);
var targetWidth;
var targetHeight;
var targetX;
var targetY;
// apply text padding
var textpad;
if(barWidth > (2 * TEXTPAD) && barHeight > (2 * TEXTPAD)) {
textpad = TEXTPAD;
barWidth -= 2 * textpad;
barHeight -= 2 * textpad;
}
else textpad = 0;
// compute rotation and scale
var rotate,
scale;
if(textWidth <= barWidth && textHeight <= barHeight) {
// no scale or rotation is required
rotate = false;
scale = 1;
}
else if(textWidth <= barHeight && textHeight <= barWidth) {
// only rotation is required
rotate = true;
scale = 1;
}
else if((textWidth < textHeight) === (barWidth < barHeight)) {
// only scale is required
rotate = false;
scale = constrained ? Math.min(barWidth / textWidth, barHeight / textHeight) : 1;
}
else {
// both scale and rotation are required
rotate = true;
scale = constrained ? Math.min(barHeight / textWidth, barWidth / textHeight) : 1;
}
if(rotate) rotate = 90; // rotate clockwise
// compute text and target positions
if(rotate) {
targetWidth = scale * textHeight;
targetHeight = scale * textWidth;
}
else {
targetWidth = scale * textWidth;
targetHeight = scale * textHeight;
}
if(orientation === 'h') {
if(x1 < x0) {
// bar end is on the left hand side
targetX = x1 + textpad + targetWidth / 2;
targetY = (y0 + y1) / 2;
}
else {
targetX = x1 - textpad - targetWidth / 2;
targetY = (y0 + y1) / 2;
}
}
else {
if(y1 > y0) {
// bar end is on the bottom
targetX = (x0 + x1) / 2;
targetY = y1 - textpad - targetHeight / 2;
}
else {
targetX = (x0 + x1) / 2;
targetY = y1 + textpad + targetHeight / 2;
}
}
return getTransform(textX, textY, targetX, targetY, scale, rotate);
}
function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation, constrained) {
var barWidth = (orientation === 'h') ?
Math.abs(y1 - y0) :
Math.abs(x1 - x0);
var textpad;
// Keep the padding so the text doesn't sit right against
// the bars, but don't factor it into barWidth
if(barWidth > 2 * TEXTPAD) {
textpad = TEXTPAD;
}
// compute rotation and scale
var scale = 1;
if(constrained) {
scale = (orientation === 'h') ?
Math.min(1, barWidth / textBB.height) :
Math.min(1, barWidth / textBB.width);
}
// compute text and target positions
var textX = (textBB.left + textBB.right) / 2;
var textY = (textBB.top + textBB.bottom) / 2;
var targetWidth;
var targetHeight;
var targetX;
var targetY;
targetWidth = scale * textBB.width;
targetHeight = scale * textBB.height;
if(orientation === 'h') {
if(x1 < x0) {
// bar end is on the left hand side
targetX = x1 - textpad - targetWidth / 2;
targetY = (y0 + y1) / 2;
}
else {
targetX = x1 + textpad + targetWidth / 2;
targetY = (y0 + y1) / 2;
}
}
else {
if(y1 > y0) {
// bar end is on the bottom
targetX = (x0 + x1) / 2;
targetY = y1 + textpad + targetHeight / 2;
}
else {
targetX = (x0 + x1) / 2;
targetY = y1 - textpad - targetHeight / 2;
}
}
return getTransform(textX, textY, targetX, targetY, scale, false);
}
function getTransform(textX, textY, targetX, targetY, scale, rotate) {
var transformScale;
var transformRotate;
var transformTranslate;
if(scale < 1) transformScale = 'scale(' + scale + ') ';
else {
scale = 1;
transformScale = '';
}
transformRotate = (rotate) ?
'rotate(' + rotate + ' ' + textX + ' ' + textY + ') ' : '';
// Note that scaling also affects the center of the text box
var translateX = (targetX - scale * textX);
var translateY = (targetY - scale * textY);
transformTranslate = 'translate(' + translateX + ' ' + translateY + ')';
return transformTranslate + transformScale + transformRotate;
}
function getText(trace, index) {
var value = helpers.getValue(trace.text, index);
return helpers.coerceString(attributeText, value);
}
function getTextPosition(trace, index) {
var value = helpers.getValue(trace.textposition, index);
return helpers.coerceEnumerated(attributeTextPosition, value);
}