-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdraw.js
377 lines (310 loc) · 12.3 KB
/
draw.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
/**
* Copyright 2012-2018, 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 Registry = require('../../registry');
var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
var Color = require('../color');
var Drawing = require('../drawing');
var dragElement = require('../dragelement');
var setCursor = require('../../lib/setcursor');
var constants = require('./constants');
var helpers = require('./helpers');
// Shapes are stored in gd.layout.shapes, an array of objects
// index can point to one item in this array,
// or non-numeric to simply add a new one
// or -1 to modify all existing
// opt can be the full options object, or one key (to be set to value)
// or undefined to simply redraw
// if opt is blank, val can be 'add' or a full options object to add a new
// annotation at that point in the array, or 'remove' to delete this one
module.exports = {
draw: draw,
drawOne: drawOne
};
function draw(gd) {
var fullLayout = gd._fullLayout;
// Remove previous shapes before drawing new in shapes in fullLayout.shapes
fullLayout._shapeUpperLayer.selectAll('path').remove();
fullLayout._shapeLowerLayer.selectAll('path').remove();
for(var k in fullLayout._plots) {
var shapelayer = fullLayout._plots[k].shapelayer;
if(shapelayer) shapelayer.selectAll('path').remove();
}
for(var i = 0; i < fullLayout.shapes.length; i++) {
if(fullLayout.shapes[i].visible) {
drawOne(gd, i);
}
}
// may need to resurrect this if we put text (LaTeX) in shapes
// return Plots.previousPromises(gd);
}
function drawOne(gd, index) {
// remove the existing shape if there is one.
// because indices can change, we need to look in all shape layers
gd._fullLayout._paperdiv
.selectAll('.shapelayer [data-index="' + index + '"]')
.remove();
var optionsIn = (gd.layout.shapes || [])[index],
options = gd._fullLayout.shapes[index];
// this shape is gone - quit now after deleting it
// TODO: use d3 idioms instead of deleting and redrawing every time
if(!optionsIn || options.visible === false) return;
if(options.layer !== 'below') {
drawShape(gd._fullLayout._shapeUpperLayer);
}
else if(options.xref === 'paper' || options.yref === 'paper') {
drawShape(gd._fullLayout._shapeLowerLayer);
}
else {
var plotinfo = gd._fullLayout._plots[options.xref + options.yref];
if(plotinfo) {
var mainPlot = plotinfo.mainplotinfo || plotinfo;
drawShape(mainPlot.shapelayer);
}
else {
// Fall back to _shapeLowerLayer in case the requested subplot doesn't exist.
// This can happen if you reference the shape to an x / y axis combination
// that doesn't have any data on it (and layer is below)
drawShape(gd._fullLayout._shapeLowerLayer);
}
}
function drawShape(shapeLayer) {
var attrs = {
'data-index': index,
'fill-rule': 'evenodd',
d: getPathString(gd, options)
},
lineColor = options.line.width ?
options.line.color : 'rgba(0,0,0,0)';
var path = shapeLayer.append('path')
.attr(attrs)
.style('opacity', options.opacity)
.call(Color.stroke, lineColor)
.call(Color.fill, options.fillcolor)
.call(Drawing.dashLine, options.line.dash, options.line.width);
// note that for layer="below" the clipAxes can be different from the
// subplot we're drawing this in. This could cause problems if the shape
// spans two subplots. See https://github.com/plotly/plotly.js/issues/1452
var clipAxes = (options.xref + options.yref).replace(/paper/g, '');
path.call(Drawing.setClipUrl, clipAxes ?
('clip' + gd._fullLayout._uid + clipAxes) :
null
);
if(gd._context.edits.shapePosition) setupDragElement(gd, path, options, index);
}
}
function setupDragElement(gd, shapePath, shapeOptions, index) {
var MINWIDTH = 10,
MINHEIGHT = 10;
var update;
var x0, y0, x1, y1, astrX0, astrY0, astrX1, astrY1;
var n0, s0, w0, e0, astrN, astrS, astrW, astrE, optN, optS, optW, optE;
var pathIn, astrPath;
var xa, ya, x2p, y2p, p2x, p2y;
var dragOptions = {
element: shapePath.node(),
gd: gd,
prepFn: startDrag,
doneFn: endDrag
},
dragBBox = dragOptions.element.getBoundingClientRect(),
dragMode;
dragElement.init(dragOptions);
shapePath.node().onmousemove = updateDragMode;
function updateDragMode(evt) {
// choose 'move' or 'resize'
// based on initial position of cursor within the drag element
var w = dragBBox.right - dragBBox.left,
h = dragBBox.bottom - dragBBox.top,
x = evt.clientX - dragBBox.left,
y = evt.clientY - dragBBox.top,
cursor = (w > MINWIDTH && h > MINHEIGHT && !evt.shiftKey) ?
dragElement.getCursor(x / w, 1 - y / h) :
'move';
setCursor(shapePath, cursor);
// possible values 'move', 'sw', 'w', 'se', 'e', 'ne', 'n', 'nw' and 'w'
dragMode = cursor.split('-')[0];
}
function startDrag(evt) {
// setup conversion functions
xa = Axes.getFromId(gd, shapeOptions.xref);
ya = Axes.getFromId(gd, shapeOptions.yref);
x2p = helpers.getDataToPixel(gd, xa);
y2p = helpers.getDataToPixel(gd, ya, true);
p2x = helpers.getPixelToData(gd, xa);
p2y = helpers.getPixelToData(gd, ya, true);
// setup update strings and initial values
var astr = 'shapes[' + index + ']';
if(shapeOptions.type === 'path') {
pathIn = shapeOptions.path;
astrPath = astr + '.path';
}
else {
x0 = x2p(shapeOptions.x0);
y0 = y2p(shapeOptions.y0);
x1 = x2p(shapeOptions.x1);
y1 = y2p(shapeOptions.y1);
astrX0 = astr + '.x0';
astrY0 = astr + '.y0';
astrX1 = astr + '.x1';
astrY1 = astr + '.y1';
}
if(x0 < x1) {
w0 = x0; astrW = astr + '.x0'; optW = 'x0';
e0 = x1; astrE = astr + '.x1'; optE = 'x1';
}
else {
w0 = x1; astrW = astr + '.x1'; optW = 'x1';
e0 = x0; astrE = astr + '.x0'; optE = 'x0';
}
if(y0 < y1) {
n0 = y0; astrN = astr + '.y0'; optN = 'y0';
s0 = y1; astrS = astr + '.y1'; optS = 'y1';
}
else {
n0 = y1; astrN = astr + '.y1'; optN = 'y1';
s0 = y0; astrS = astr + '.y0'; optS = 'y0';
}
update = {};
// setup dragMode and the corresponding handler
updateDragMode(evt);
dragOptions.moveFn = (dragMode === 'move') ? moveShape : resizeShape;
}
function endDrag() {
setCursor(shapePath);
Registry.call('relayout', gd, update);
}
function moveShape(dx, dy) {
if(shapeOptions.type === 'path') {
var moveX = function moveX(x) { return p2x(x2p(x) + dx); };
if(xa && xa.type === 'date') moveX = helpers.encodeDate(moveX);
var moveY = function moveY(y) { return p2y(y2p(y) + dy); };
if(ya && ya.type === 'date') moveY = helpers.encodeDate(moveY);
shapeOptions.path = movePath(pathIn, moveX, moveY);
update[astrPath] = shapeOptions.path;
}
else {
update[astrX0] = shapeOptions.x0 = p2x(x0 + dx);
update[astrY0] = shapeOptions.y0 = p2y(y0 + dy);
update[astrX1] = shapeOptions.x1 = p2x(x1 + dx);
update[astrY1] = shapeOptions.y1 = p2y(y1 + dy);
}
shapePath.attr('d', getPathString(gd, shapeOptions));
}
function resizeShape(dx, dy) {
if(shapeOptions.type === 'path') {
// TODO: implement path resize
var moveX = function moveX(x) { return p2x(x2p(x) + dx); };
if(xa && xa.type === 'date') moveX = helpers.encodeDate(moveX);
var moveY = function moveY(y) { return p2y(y2p(y) + dy); };
if(ya && ya.type === 'date') moveY = helpers.encodeDate(moveY);
shapeOptions.path = movePath(pathIn, moveX, moveY);
update[astrPath] = shapeOptions.path;
}
else {
var newN = (~dragMode.indexOf('n')) ? n0 + dy : n0,
newS = (~dragMode.indexOf('s')) ? s0 + dy : s0,
newW = (~dragMode.indexOf('w')) ? w0 + dx : w0,
newE = (~dragMode.indexOf('e')) ? e0 + dx : e0;
if(newS - newN > MINHEIGHT) {
update[astrN] = shapeOptions[optN] = p2y(newN);
update[astrS] = shapeOptions[optS] = p2y(newS);
}
if(newE - newW > MINWIDTH) {
update[astrW] = shapeOptions[optW] = p2x(newW);
update[astrE] = shapeOptions[optE] = p2x(newE);
}
}
shapePath.attr('d', getPathString(gd, shapeOptions));
}
}
function getPathString(gd, options) {
var type = options.type,
xa = Axes.getFromId(gd, options.xref),
ya = Axes.getFromId(gd, options.yref),
gs = gd._fullLayout._size,
x2r,
x2p,
y2r,
y2p;
if(xa) {
x2r = helpers.shapePositionToRange(xa);
x2p = function(v) { return xa._offset + xa.r2p(x2r(v, true)); };
}
else {
x2p = function(v) { return gs.l + gs.w * v; };
}
if(ya) {
y2r = helpers.shapePositionToRange(ya);
y2p = function(v) { return ya._offset + ya.r2p(y2r(v, true)); };
}
else {
y2p = function(v) { return gs.t + gs.h * (1 - v); };
}
if(type === 'path') {
if(xa && xa.type === 'date') x2p = helpers.decodeDate(x2p);
if(ya && ya.type === 'date') y2p = helpers.decodeDate(y2p);
return convertPath(options.path, x2p, y2p);
}
var x0 = x2p(options.x0),
x1 = x2p(options.x1),
y0 = y2p(options.y0),
y1 = y2p(options.y1);
if(type === 'line') return 'M' + x0 + ',' + y0 + 'L' + x1 + ',' + y1;
if(type === 'rect') return 'M' + x0 + ',' + y0 + 'H' + x1 + 'V' + y1 + 'H' + x0 + 'Z';
// circle
var cx = (x0 + x1) / 2,
cy = (y0 + y1) / 2,
rx = Math.abs(cx - x0),
ry = Math.abs(cy - y0),
rArc = 'A' + rx + ',' + ry,
rightPt = (cx + rx) + ',' + cy,
topPt = cx + ',' + (cy - ry);
return 'M' + rightPt + rArc + ' 0 1,1 ' + topPt +
rArc + ' 0 0,1 ' + rightPt + 'Z';
}
function convertPath(pathIn, x2p, y2p) {
// convert an SVG path string from data units to pixels
return pathIn.replace(constants.segmentRE, function(segment) {
var paramNumber = 0,
segmentType = segment.charAt(0),
xParams = constants.paramIsX[segmentType],
yParams = constants.paramIsY[segmentType],
nParams = constants.numParams[segmentType];
var paramString = segment.substr(1).replace(constants.paramRE, function(param) {
if(xParams[paramNumber]) param = x2p(param);
else if(yParams[paramNumber]) param = y2p(param);
paramNumber++;
if(paramNumber > nParams) param = 'X';
return param;
});
if(paramNumber > nParams) {
paramString = paramString.replace(/[\s,]*X.*/, '');
Lib.log('Ignoring extra params in segment ' + segment);
}
return segmentType + paramString;
});
}
function movePath(pathIn, moveX, moveY) {
return pathIn.replace(constants.segmentRE, function(segment) {
var paramNumber = 0,
segmentType = segment.charAt(0),
xParams = constants.paramIsX[segmentType],
yParams = constants.paramIsY[segmentType],
nParams = constants.numParams[segmentType];
var paramString = segment.substr(1).replace(constants.paramRE, function(param) {
if(paramNumber >= nParams) return param;
if(xParams[paramNumber]) param = moveX(param);
else if(yParams[paramNumber]) param = moveY(param);
paramNumber++;
return param;
});
return segmentType + paramString;
});
}