-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathindex.js
380 lines (296 loc) · 12.2 KB
/
index.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
/**
* Copyright 2012-2016, 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 Lib = require('../../lib');
var Plots = require('../plots');
var Axes = require('./axes');
var constants = require('./constants');
exports.name = 'cartesian';
exports.attr = ['xaxis', 'yaxis'];
exports.idRoot = ['x', 'y'];
exports.idRegex = constants.idRegex;
exports.attrRegex = constants.attrRegex;
exports.attributes = require('./attributes');
exports.transitionAxes = require('./transition_axes');
exports.plot = function(gd, traces, transitionOpts, makeOnCompleteCallback) {
var fullLayout = gd._fullLayout,
subplots = Plots.getSubplotIds(fullLayout, 'cartesian'),
calcdata = gd.calcdata,
i;
// If traces is not provided, then it's a complete replot and missing
// traces are removed
if(!Array.isArray(traces)) {
traces = [];
for(i = 0; i < calcdata.length; i++) {
traces.push(i);
}
}
for(i = 0; i < subplots.length; i++) {
var subplot = subplots[i],
subplotInfo = fullLayout._plots[subplot];
// Get all calcdata for this subplot:
var cdSubplot = [];
var pcd;
for(var j = 0; j < calcdata.length; j++) {
var cd = calcdata[j],
trace = cd[0].trace;
// Skip trace if whitelist provided and it's not whitelisted:
// if (Array.isArray(traces) && traces.indexOf(i) === -1) continue;
if(trace.xaxis + trace.yaxis === subplot) {
var needed = false;
// If this trace is specifically requested, add it to the list:
if(traces.indexOf(trace.index) !== -1) {
needed = true;
}
if(gd._fullLayout.barmode === 'stack') {
needed = true;
}
// Okay, so example: traces 0, 1, and 2 have fill = tonext. You animate
// traces 0 and 2. Trace 1 also needs to be updated, otherwise its fill
// is outdated. So this retroactively adds the previous trace if the
// traces are interdependent.
if(needed && pcd &&
['tonextx', 'tonexty', 'tonext'].indexOf(trace.fill) !== -1 &&
cdSubplot.indexOf(pcd) === -1) {
cdSubplot.push(pcd);
}
if(needed) {
cdSubplot.push(cd);
}
// Track the previous trace on this subplot for the retroactive-add step
// above:
pcd = cd;
}
}
plotOne(gd, subplotInfo, cdSubplot, transitionOpts, makeOnCompleteCallback);
}
};
function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback) {
var fullLayout = gd._fullLayout,
modules = fullLayout._modules;
// remove old traces, then redraw everything
//
// TODO: scatterlayer is manually excluded from this since it knows how
// to update instead of fully removing and redrawing every time. The
// remaining plot traces should also be able to do this. Once implemented,
// we won't need this - which should sometimes be a big speedup.
if(plotinfo.plot) {
plotinfo.plot.selectAll('g:not(.scatterlayer):not(.barlayer)').selectAll('g.trace').remove();
}
// plot all traces for each module at once
for(var j = 0; j < modules.length; j++) {
var _module = modules[j];
// skip over non-cartesian trace modules
if(_module.basePlotModule.name !== 'cartesian') continue;
// plot all traces of this type on this subplot at once
var cdModule = [];
for(var k = 0; k < cdSubplot.length; k++) {
var cd = cdSubplot[k],
trace = cd[0].trace;
if((trace._module === _module) && (trace.visible === true)) {
cdModule.push(cd);
}
}
_module.plot(gd, plotinfo, cdModule, transitionOpts, makeOnCompleteCallback);
}
}
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
var oldModules = oldFullLayout._modules || [],
newModules = newFullLayout._modules || [];
var hadScatter, hasScatter, i;
for(i = 0; i < oldModules.length; i++) {
if(oldModules[i].name === 'scatter') {
hadScatter = true;
break;
}
}
for(i = 0; i < newModules.length; i++) {
if(newModules[i].name === 'scatter') {
hasScatter = true;
break;
}
}
if(hadScatter && !hasScatter) {
var oldPlots = oldFullLayout._plots,
ids = Object.keys(oldPlots || {});
for(i = 0; i < ids.length; i++) {
var subplotInfo = oldPlots[ids[i]];
if(subplotInfo.plot) {
subplotInfo.plot.select('g.scatterlayer')
.selectAll('g.trace')
.remove();
}
}
}
var hadCartesian = (oldFullLayout._has && oldFullLayout._has('cartesian'));
var hasCartesian = (newFullLayout._has && newFullLayout._has('cartesian'));
if(hadCartesian && !hasCartesian) {
var subplotLayers = oldFullLayout._cartesianlayer.selectAll('.subplot');
subplotLayers.call(purgeSubplotLayers, oldFullLayout);
oldFullLayout._defs.selectAll('.axesclip').remove();
}
};
exports.drawFramework = function(gd) {
var fullLayout = gd._fullLayout,
subplotData = makeSubplotData(gd);
var subplotLayers = fullLayout._cartesianlayer.selectAll('.subplot')
.data(subplotData, Lib.identity);
subplotLayers.enter().append('g')
.attr('class', function(name) { return 'subplot ' + name; });
subplotLayers.order();
subplotLayers.exit()
.call(purgeSubplotLayers, fullLayout);
subplotLayers.each(function(name) {
var plotinfo = fullLayout._plots[name];
// keep ref to plot group
plotinfo.plotgroup = d3.select(this);
// initialize list of overlay subplots
plotinfo.overlays = [];
makeSubplotLayer(plotinfo);
// fill in list of overlay subplots
if(plotinfo.mainplot) {
var mainplot = fullLayout._plots[plotinfo.mainplot];
mainplot.overlays.push(plotinfo);
}
// make separate drag layers for each subplot,
// but append them to paper rather than the plot groups,
// so they end up on top of the rest
plotinfo.draglayer = joinLayer(fullLayout._draggers, 'g', name);
});
};
exports.rangePlot = function(gd, plotinfo, cdSubplot) {
makeSubplotLayer(plotinfo);
plotOne(gd, plotinfo, cdSubplot);
Plots.style(gd);
};
function makeSubplotData(gd) {
var fullLayout = gd._fullLayout,
subplots = Object.keys(fullLayout._plots);
var subplotData = [],
overlays = [];
for(var i = 0; i < subplots.length; i++) {
var subplot = subplots[i],
plotinfo = fullLayout._plots[subplot];
var xa = plotinfo.xaxis,
ya = plotinfo.yaxis;
// is this subplot overlaid on another?
// ax.overlaying is the id of another axis of the same
// dimension that this one overlays to be an overlaid subplot,
// the main plot must exist make sure we're not trying to
// overlay on an axis that's already overlaying another
var xa2 = Axes.getFromId(gd, xa.overlaying) || xa;
if(xa2 !== xa && xa2.overlaying) {
xa2 = xa;
xa.overlaying = false;
}
var ya2 = Axes.getFromId(gd, ya.overlaying) || ya;
if(ya2 !== ya && ya2.overlaying) {
ya2 = ya;
ya.overlaying = false;
}
var mainplot = xa2._id + ya2._id;
if(mainplot !== subplot && subplots.indexOf(mainplot) !== -1) {
plotinfo.mainplot = mainplot;
plotinfo.mainplotinfo = fullLayout._plots[mainplot];
overlays.push(subplot);
// for now force overlays to overlay completely... so they
// can drag together correctly and share backgrounds.
// Later perhaps we make separate axis domain and
// tick/line domain or something, so they can still share
// the (possibly larger) dragger and background but don't
// have to both be drawn over that whole domain
xa.domain = xa2.domain.slice();
ya.domain = ya2.domain.slice();
}
else {
subplotData.push(subplot);
}
}
// main subplots before overlays
subplotData = subplotData.concat(overlays);
return subplotData;
}
function makeSubplotLayer(plotinfo) {
var plotgroup = plotinfo.plotgroup,
id = plotinfo.id;
// Layers to keep plot types in the right order.
// from back to front:
// 1. heatmaps, 2D histos and contour maps
// 2. bars / 1D histos
// 3. errorbars for bars and scatter
// 4. scatter
// 5. box plots
function joinPlotLayers(parent) {
joinLayer(parent, 'g', 'imagelayer');
joinLayer(parent, 'g', 'maplayer');
joinLayer(parent, 'g', 'barlayer');
joinLayer(parent, 'g', 'boxlayer');
joinLayer(parent, 'g', 'scatterlayer');
}
if(!plotinfo.mainplot) {
plotinfo.bg = joinLayer(plotgroup, 'rect', 'bg');
plotinfo.bg.style('stroke-width', 0);
var backLayer = joinLayer(plotgroup, 'g', 'layer-subplot');
plotinfo.shapelayer = joinLayer(backLayer, 'g', 'shapelayer');
plotinfo.imagelayer = joinLayer(backLayer, 'g', 'imagelayer');
plotinfo.gridlayer = joinLayer(plotgroup, 'g', 'gridlayer');
plotinfo.overgrid = joinLayer(plotgroup, 'g', 'overgrid');
plotinfo.zerolinelayer = joinLayer(plotgroup, 'g', 'zerolinelayer');
plotinfo.overzero = joinLayer(plotgroup, 'g', 'overzero');
plotinfo.plot = joinLayer(plotgroup, 'g', 'plot');
plotinfo.overplot = joinLayer(plotgroup, 'g', 'overplot');
plotinfo.xlines = joinLayer(plotgroup, 'path', 'xlines');
plotinfo.ylines = joinLayer(plotgroup, 'path', 'ylines');
plotinfo.overlines = joinLayer(plotgroup, 'g', 'overlines');
plotinfo.xaxislayer = joinLayer(plotgroup, 'g', 'xaxislayer');
plotinfo.yaxislayer = joinLayer(plotgroup, 'g', 'yaxislayer');
plotinfo.overaxes = joinLayer(plotgroup, 'g', 'overaxes');
}
else {
var mainplotinfo = plotinfo.mainplotinfo;
// now make the components of overlaid subplots
// overlays don't have backgrounds, and append all
// their other components to the corresponding
// extra groups of their main plots.
plotinfo.gridlayer = joinLayer(mainplotinfo.overgrid, 'g', id);
plotinfo.zerolinelayer = joinLayer(mainplotinfo.overzero, 'g', id);
plotinfo.plot = joinLayer(mainplotinfo.overplot, 'g', id);
plotinfo.xlines = joinLayer(mainplotinfo.overlines, 'path', id);
plotinfo.ylines = joinLayer(mainplotinfo.overlines, 'path', id);
plotinfo.xaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id);
plotinfo.yaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id);
}
// common attributes for all subplots, overlays or not
plotinfo.plot.call(joinPlotLayers);
plotinfo.xlines
.style('fill', 'none')
.classed('crisp', true);
plotinfo.ylines
.style('fill', 'none')
.classed('crisp', true);
}
function purgeSubplotLayers(layers, fullLayout) {
if(!layers) return;
layers.each(function(subplot) {
var plotgroup = d3.select(this),
clipId = 'clip' + fullLayout._uid + subplot + 'plot';
plotgroup.remove();
fullLayout._draggers.selectAll('g.' + subplot).remove();
fullLayout._defs.select('#' + clipId).remove();
// do not remove individual axis <clipPath>s here
// as other subplots may need them
});
}
function joinLayer(parent, nodeType, className) {
var layer = parent.selectAll('.' + className)
.data([0]);
layer.enter().append(nodeType)
.classed(className, true);
return layer;
}