Skip to content

Commit 32fa3cd

Browse files
committed
Remove some plotly dependency
1 parent 43b33e7 commit 32fa3cd

File tree

1 file changed

+57
-48
lines changed

1 file changed

+57
-48
lines changed

Diff for: src/components/legend/index.js

+57-48
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
var Plotly = require('../../plotly');
1313
var d3 = require('d3');
1414

15+
var Lib = require('../../lib');
16+
17+
var Plots = require('../../plots/plots');
18+
var Fx = require('../../plots/cartesian/graph_interact');
19+
20+
var Color = require('../color');
21+
var Drawing = require('../drawing');
22+
1523
var subTypes = require('../../traces/scatter/subtypes');
1624
var styleOne = require('../../traces/pie/style_one');
1725

@@ -33,10 +41,10 @@ legend.supplyLayoutDefaults = function(layoutIn, layoutOut, fullData) {
3341
if(legendGetsTrace(trace)) {
3442
visibleTraces++;
3543
// always show the legend by default if there's a pie
36-
if(Plotly.Plots.traceIs(trace, 'pie')) visibleTraces++;
44+
if(Plots.traceIs(trace, 'pie')) visibleTraces++;
3745
}
3846

39-
if((Plotly.Plots.traceIs(trace, 'bar') && layoutOut.barmode==='stack') ||
47+
if((Plots.traceIs(trace, 'bar') && layoutOut.barmode==='stack') ||
4048
['tonextx','tonexty'].indexOf(trace.fill)!==-1) {
4149
defaultOrder = isGrouped({traceorder: defaultOrder}) ?
4250
'grouped+reversed' : 'reversed';
@@ -49,28 +57,29 @@ legend.supplyLayoutDefaults = function(layoutIn, layoutOut, fullData) {
4957
}
5058

5159
function coerce(attr, dflt) {
52-
return Plotly.Lib.coerce(containerIn, containerOut,
60+
return Lib.coerce(containerIn, containerOut,
5361
legend.layoutAttributes, attr, dflt);
5462
}
5563

56-
var showLegend = Plotly.Lib.coerce(layoutIn, layoutOut,
57-
Plotly.Plots.layoutAttributes, 'showlegend', visibleTraces > 1);
64+
var showLegend = Lib.coerce(layoutIn, layoutOut,
65+
Plots.layoutAttributes, 'showlegend', visibleTraces > 1);
5866

5967
if(showLegend === false) return;
6068

6169
coerce('bgcolor', layoutOut.paper_bgcolor);
6270
coerce('bordercolor');
6371
coerce('borderwidth');
64-
Plotly.Lib.coerceFont(coerce, 'font', layoutOut.font);
72+
Lib.coerceFont(coerce, 'font', layoutOut.font);
6573

74+
coerce('type');
6675
coerce('traceorder', defaultOrder);
6776
if(isGrouped(layoutOut.legend)) coerce('tracegroupgap');
6877

6978
coerce('x');
7079
coerce('xanchor');
7180
coerce('y');
7281
coerce('yanchor');
73-
Plotly.Lib.noneOrAll(containerIn, containerOut, ['x', 'y']);
82+
Lib.noneOrAll(containerIn, containerOut, ['x', 'y']);
7483
};
7584

7685
// -----------------------------------------------------
@@ -88,14 +97,14 @@ legend.lines = function(d){
8897
fill.enter().append('path').classed('js-fill',true);
8998
fill.exit().remove();
9099
fill.attr('d', 'M5,0h30v6h-30z')
91-
.call(Plotly.Drawing.fillGroupStyle);
100+
.call(Drawing.fillGroupStyle);
92101

93102
var line = d3.select(this).select('.legendlines').selectAll('path')
94103
.data(showLine ? [d] : []);
95104
line.enter().append('path').classed('js-line',true)
96105
.attr('d', 'M5,0h30');
97106
line.exit().remove();
98-
line.call(Plotly.Drawing.lineGroupStyle);
107+
line.call(Drawing.lineGroupStyle);
99108
};
100109

101110
legend.points = function(d){
@@ -111,7 +120,7 @@ legend.points = function(d){
111120
// use d0.trace to infer arrayOk attributes
112121

113122
function boundVal(attrIn, arrayToValFn, bounds) {
114-
var valIn = Plotly.Lib.nestedProperty(trace, attrIn).get(),
123+
var valIn = Lib.nestedProperty(trace, attrIn).get(),
115124
valToBound = (Array.isArray(valIn) && arrayToValFn) ?
116125
arrayToValFn(valIn) : valIn;
117126

@@ -131,10 +140,10 @@ legend.points = function(d){
131140

132141
if(showMarkers) {
133142
dEdit.mc = boundVal('marker.color', pickFirst);
134-
dEdit.mo = boundVal('marker.opacity', Plotly.Lib.mean, [0.2, 1]);
135-
dEdit.ms = boundVal('marker.size', Plotly.Lib.mean, [2, 16]);
143+
dEdit.mo = boundVal('marker.opacity', Lib.mean, [0.2, 1]);
144+
dEdit.ms = boundVal('marker.size', Lib.mean, [2, 16]);
136145
dEdit.mlc = boundVal('marker.line.color', pickFirst);
137-
dEdit.mlw = boundVal('marker.line.width', Plotly.Lib.mean, [0, 5]);
146+
dEdit.mlw = boundVal('marker.line.width', Lib.mean, [0, 5]);
138147
tEdit.marker = {
139148
sizeref: 1,
140149
sizemin: 1,
@@ -156,8 +165,8 @@ legend.points = function(d){
156165
dEdit.tf = boundVal('textfont.family', pickFirst);
157166
}
158167

159-
dMod = [Plotly.Lib.minExtend(d0, dEdit)];
160-
tMod = Plotly.Lib.minExtend(trace, tEdit);
168+
dMod = [Lib.minExtend(d0, dEdit)];
169+
tMod = Lib.minExtend(trace, tEdit);
161170
}
162171

163172
var ptgroup = d3.select(this).select('g.legendpoints');
@@ -167,7 +176,7 @@ legend.points = function(d){
167176
pts.enter().append('path').classed('scatterpts', true)
168177
.attr('transform', 'translate(20,0)');
169178
pts.exit().remove();
170-
pts.call(Plotly.Drawing.pointStyle, tMod);
179+
pts.call(Drawing.pointStyle, tMod);
171180

172181
// 'mrc' is set in pointStyle and used in textPointStyle:
173182
// constrain it here
@@ -179,7 +188,7 @@ legend.points = function(d){
179188
.append('g').classed('pointtext',true)
180189
.append('text').attr('transform', 'translate(20,0)');
181190
txt.exit().remove();
182-
txt.selectAll('text').call(Plotly.Drawing.textPointStyle, tMod);
191+
txt.selectAll('text').call(Drawing.textPointStyle, tMod);
183192

184193
};
185194

@@ -189,7 +198,7 @@ legend.bars = function(d){
189198
markerLine = marker.line||{},
190199
barpath = d3.select(this).select('g.legendpoints')
191200
.selectAll('path.legendbar')
192-
.data(Plotly.Plots.traceIs(trace, 'bar') ? [d] : []);
201+
.data(Plots.traceIs(trace, 'bar') ? [d] : []);
193202
barpath.enter().append('path').classed('legendbar',true)
194203
.attr('d','M6,6H-6V-6H6Z')
195204
.attr('transform','translate(20,0)');
@@ -198,9 +207,9 @@ legend.bars = function(d){
198207
var w = (d.mlw+1 || markerLine.width+1) - 1,
199208
p = d3.select(this);
200209
p.style('stroke-width',w+'px')
201-
.call(Plotly.Color.fill, d.mc || marker.color);
210+
.call(Color.fill, d.mc || marker.color);
202211
if(w) {
203-
p.call(Plotly.Color.stroke, d.mlc || markerLine.color);
212+
p.call(Color.stroke, d.mlc || markerLine.color);
204213
}
205214
});
206215
};
@@ -209,7 +218,7 @@ legend.boxes = function(d){
209218
var trace = d[0].trace,
210219
pts = d3.select(this).select('g.legendpoints')
211220
.selectAll('path.legendbox')
212-
.data(Plotly.Plots.traceIs(trace, 'box') && trace.visible ? [d] : []);
221+
.data(Plots.traceIs(trace, 'box') && trace.visible ? [d] : []);
213222
pts.enter().append('path').classed('legendbox', true)
214223
// if we want the median bar, prepend M6,0H-6
215224
.attr('d', 'M6,6H-6V-6H6Z')
@@ -219,9 +228,9 @@ legend.boxes = function(d){
219228
var w = (d.lw+1 || trace.line.width+1) - 1,
220229
p = d3.select(this);
221230
p.style('stroke-width', w+'px')
222-
.call(Plotly.Color.fill, d.fc || trace.fillcolor);
231+
.call(Color.fill, d.fc || trace.fillcolor);
223232
if(w) {
224-
p.call(Plotly.Color.stroke, d.lc || trace.line.color);
233+
p.call(Color.stroke, d.lc || trace.line.color);
225234
}
226235
});
227236
};
@@ -230,7 +239,7 @@ legend.pie = function(d) {
230239
var trace = d[0].trace,
231240
pts = d3.select(this).select('g.legendpoints')
232241
.selectAll('path.legendpie')
233-
.data(Plotly.Plots.traceIs(trace, 'pie') && trace.visible ? [d] : []);
242+
.data(Plots.traceIs(trace, 'pie') && trace.visible ? [d] : []);
234243
pts.enter().append('path').classed('legendpie', true)
235244
.attr('d', 'M6,6H-6V-6H6Z')
236245
.attr('transform', 'translate(20,0)');
@@ -277,7 +286,7 @@ legend.style = function(s) {
277286
legend.texts = function(context, td, d, i, traces){
278287
var fullLayout = td._fullLayout,
279288
trace = d[0].trace,
280-
isPie = Plotly.Plots.traceIs(trace, 'pie'),
289+
isPie = Plots.traceIs(trace, 'pie'),
281290
traceIndex = trace.index,
282291
name = isPie ? d[0].label : trace.name;
283292

@@ -289,7 +298,7 @@ legend.texts = function(context, td, d, i, traces){
289298
y: 0
290299
})
291300
.style('text-anchor', 'start')
292-
.call(Plotly.Drawing.font, fullLayout.legend.font)
301+
.call(Drawing.font, fullLayout.legend.font)
293302
.text(name)
294303
.attr({'data-unformatted': name});
295304

@@ -319,7 +328,7 @@ legend.texts = function(context, td, d, i, traces){
319328
// -----------------------------------------------------
320329

321330
function legendGetsTrace(trace) {
322-
return trace.visible && Plotly.Plots.traceIs(trace, 'showLegend');
331+
return trace.visible && Plots.traceIs(trace, 'showLegend');
323332
}
324333

325334
function isGrouped(legendLayout) {
@@ -365,7 +374,7 @@ legend.getLegendData = function(calcdata, opts) {
365374

366375
if(!legendGetsTrace(trace) || !trace.showlegend) continue;
367376

368-
if(Plotly.Plots.traceIs(trace, 'pie')) {
377+
if(Plots.traceIs(trace, 'pie')) {
369378
if(!slicesShown[lgroup]) slicesShown[lgroup] = {};
370379
for(j = 0; j < cd.length; j++) {
371380
labelj = cd[j].label;
@@ -426,7 +435,7 @@ legend.draw = function(td) {
426435

427436
if(!fullLayout.showlegend || !legendData.length) {
428437
fullLayout._infolayer.selectAll('.legend').remove();
429-
Plotly.Plots.autoMargin(td, 'legend');
438+
Plots.autoMargin(td, 'legend');
430439
return;
431440
}
432441

@@ -443,8 +452,8 @@ legend.draw = function(td) {
443452
bgRect.enter(0).append('rect')
444453
.attr('class','bg');
445454
bgRect
446-
.call(Plotly.Color.stroke, opts.bordercolor)
447-
.call(Plotly.Color.fill, opts.bgcolor)
455+
.call(Color.stroke, opts.bordercolor)
456+
.call(Color.fill, opts.bgcolor)
448457
.style('stroke-width', opts.borderwidth+'px');
449458

450459
var groups = legendsvg.selectAll('g.groups')
@@ -460,15 +469,15 @@ legend.draw = function(td) {
460469
}
461470

462471
var traces = groups.selectAll('g.traces')
463-
.data(Plotly.Lib.identity);
472+
.data(Lib.identity);
464473

465474
traces.enter().append('g').attr('class', 'traces');
466475
traces.exit().remove();
467476

468477
traces.call(legend.style)
469478
.style('opacity', function(d) {
470479
var trace = d[0].trace;
471-
if(Plotly.Plots.traceIs(trace, 'pie')) {
480+
if(Plots.traceIs(trace, 'pie')) {
472481
return hiddenSlices.indexOf(d[0].label) !== -1 ? 0.5 : 1;
473482
} else {
474483
return trace.visible === 'legendonly' ? 0.5 : 1;
@@ -483,7 +492,7 @@ legend.draw = function(td) {
483492
.classed('legendtoggle', true)
484493
.style('cursor', 'pointer')
485494
.attr('pointer-events', 'all')
486-
.call(Plotly.Color.fill, 'rgba(0,0,0,0)');
495+
.call(Color.fill, 'rgba(0,0,0,0)');
487496
traceToggle.on('click', function() {
488497
if(td._dragged) return;
489498

@@ -494,7 +503,7 @@ legend.draw = function(td) {
494503
tracei,
495504
newVisible;
496505

497-
if(Plotly.Plots.traceIs(trace, 'pie')) {
506+
if(Plots.traceIs(trace, 'pie')) {
498507
var thisLabel = d[0].label,
499508
newHiddenSlices = hiddenSlices.slice(),
500509
thisLabelIndex = newHiddenSlices.indexOf(thisLabel);
@@ -531,31 +540,31 @@ legend.draw = function(td) {
531540
lw,
532541
lh;
533542

534-
Plotly.Fx.dragElement({
543+
Fx.dragElement({
535544
element: legendsvg.node(),
536545
prepFn: function() {
537546
x0 = Number(legendsvg.attr('x'));
538547
y0 = Number(legendsvg.attr('y'));
539548
lw = Number(legendsvg.attr('width'));
540549
lh = Number(legendsvg.attr('height'));
541-
Plotly.Fx.setCursor(legendsvg);
550+
Fx.setCursor(legendsvg);
542551
},
543552
moveFn: function(dx, dy) {
544553
var gs = td._fullLayout._size;
545554

546-
legendsvg.call(Plotly.Drawing.setPosition, x0+dx, y0+dy);
555+
legendsvg.call(Drawing.setPosition, x0+dx, y0+dy);
547556

548-
xf = Plotly.Fx.dragAlign(x0+dx, lw, gs.l, gs.l+gs.w,
557+
xf = Fx.dragAlign(x0+dx, lw, gs.l, gs.l+gs.w,
549558
opts.xanchor);
550-
yf = Plotly.Fx.dragAlign(y0+dy+lh, -lh, gs.t+gs.h, gs.t,
559+
yf = Fx.dragAlign(y0+dy+lh, -lh, gs.t+gs.h, gs.t,
551560
opts.yanchor);
552561

553-
var csr = Plotly.Fx.dragCursors(xf, yf,
562+
var csr = Fx.dragCursors(xf, yf,
554563
opts.xanchor, opts.yanchor);
555-
Plotly.Fx.setCursor(legendsvg, csr);
564+
Fx.setCursor(legendsvg, csr);
556565
},
557566
doneFn: function(dragged) {
558-
Plotly.Fx.setCursor(legendsvg);
567+
Fx.setCursor(legendsvg);
559568
if(dragged && xf!==undefined && yf!==undefined) {
560569
Plotly.relayout(td, {'legend.x': xf, 'legend.y': yf});
561570
}
@@ -583,7 +592,7 @@ legend.repositionLegend = function(td, traces){
583592
tspans = g.selectAll('.legendtext>tspan'),
584593
tHeight = opts.font.size * 1.3,
585594
tLines = tspans[0].length||1,
586-
tWidth = text.node() && Plotly.Drawing.bBox(text.node()).width,
595+
tWidth = text.node() && Drawing.bBox(text.node()).width,
587596
mathjaxGroup = g.select('g[class*=math-group]'),
588597
textY,
589598
tHeightFull;
@@ -594,7 +603,7 @@ legend.repositionLegend = function(td, traces){
594603
}
595604

596605
if(mathjaxGroup.node()) {
597-
var mathjaxBB = Plotly.Drawing.bBox(mathjaxGroup.node());
606+
var mathjaxBB = Drawing.bBox(mathjaxGroup.node());
598607
tHeight = mathjaxBB.height;
599608
tWidth = mathjaxBB.width;
600609
mathjaxGroup.attr('transform','translate(0,'+(tHeight/4)+')');
@@ -664,13 +673,13 @@ legend.repositionLegend = function(td, traces){
664673
ly = Math.round(ly);
665674

666675
fullLayout._infolayer.selectAll('svg.legend')
667-
.call(Plotly.Drawing.setRect, lx, ly, legendwidth, legendheight);
676+
.call(Drawing.setRect, lx, ly, legendwidth, legendheight);
668677
fullLayout._infolayer.selectAll('svg.legend .bg')
669-
.call(Plotly.Drawing.setRect, borderwidth/2, borderwidth/2,
678+
.call(Drawing.setRect, borderwidth/2, borderwidth/2,
670679
legendwidth-borderwidth, legendheight-borderwidth);
671680

672681
// lastly check if the margin auto-expand has changed
673-
Plotly.Plots.autoMargin(td,'legend',{
682+
Plots.autoMargin(td,'legend',{
674683
x: opts.x,
675684
y: opts.y,
676685
l: legendwidth * ({right:1, center:0.5}[xanchor]||0),

0 commit comments

Comments
 (0)