Skip to content

Commit 09354bf

Browse files
committed
move legend style step to own module:
- un-exposed legend.lines, legend.bars, legend.points, legend.boxes and legend.pie
1 parent 67eda75 commit 09354bf

File tree

2 files changed

+217
-201
lines changed

2 files changed

+217
-201
lines changed

src/components/legend/index.js

Lines changed: 1 addition & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -29,207 +29,6 @@ var constants = require('./constants');
2929
legend.layoutAttributes = require('./attributes');
3030

3131
legend.supplyLayoutDefaults = require('./defaults');
32-
// -----------------------------------------------------
33-
// styling functions for traces in legends.
34-
// same functions for styling traces in the popovers
35-
// -----------------------------------------------------
36-
37-
legend.lines = function(d) {
38-
var trace = d[0].trace,
39-
showFill = trace.visible && trace.fill && trace.fill!=='none',
40-
showLine = subTypes.hasLines(trace);
41-
42-
var fill = d3.select(this).select('.legendfill').selectAll('path')
43-
.data(showFill ? [d] : []);
44-
fill.enter().append('path').classed('js-fill',true);
45-
fill.exit().remove();
46-
fill.attr('d', 'M5,0h30v6h-30z')
47-
.call(Drawing.fillGroupStyle);
48-
49-
var line = d3.select(this).select('.legendlines').selectAll('path')
50-
.data(showLine ? [d] : []);
51-
line.enter().append('path').classed('js-line',true)
52-
.attr('d', 'M5,0h30');
53-
line.exit().remove();
54-
line.call(Drawing.lineGroupStyle);
55-
};
56-
57-
legend.points = function(d) {
58-
var d0 = d[0],
59-
trace = d0.trace,
60-
showMarkers = subTypes.hasMarkers(trace),
61-
showText = subTypes.hasText(trace),
62-
showLines = subTypes.hasLines(trace);
63-
64-
var dMod, tMod;
65-
66-
// 'scatter3d' and 'scattergeo' don't use gd.calcdata yet;
67-
// use d0.trace to infer arrayOk attributes
68-
69-
function boundVal(attrIn, arrayToValFn, bounds) {
70-
var valIn = Lib.nestedProperty(trace, attrIn).get(),
71-
valToBound = (Array.isArray(valIn) && arrayToValFn) ?
72-
arrayToValFn(valIn) : valIn;
73-
74-
if(bounds) {
75-
if(valToBound < bounds[0]) return bounds[0];
76-
else if(valToBound > bounds[1]) return bounds[1];
77-
}
78-
return valToBound;
79-
}
80-
81-
function pickFirst(array) { return array[0]; }
82-
83-
// constrain text, markers, etc so they'll fit on the legend
84-
if(showMarkers || showText || showLines) {
85-
var dEdit = {},
86-
tEdit = {};
87-
88-
if(showMarkers) {
89-
dEdit.mc = boundVal('marker.color', pickFirst);
90-
dEdit.mo = boundVal('marker.opacity', Lib.mean, [0.2, 1]);
91-
dEdit.ms = boundVal('marker.size', Lib.mean, [2, 16]);
92-
dEdit.mlc = boundVal('marker.line.color', pickFirst);
93-
dEdit.mlw = boundVal('marker.line.width', Lib.mean, [0, 5]);
94-
tEdit.marker = {
95-
sizeref: 1,
96-
sizemin: 1,
97-
sizemode: 'diameter'
98-
};
99-
}
100-
101-
if(showLines) {
102-
tEdit.line = {
103-
width: boundVal('line.width', pickFirst, [0, 10])
104-
};
105-
}
106-
107-
if(showText) {
108-
dEdit.tx = 'Aa';
109-
dEdit.tp = boundVal('textposition', pickFirst);
110-
dEdit.ts = 10;
111-
dEdit.tc = boundVal('textfont.color', pickFirst);
112-
dEdit.tf = boundVal('textfont.family', pickFirst);
113-
}
114-
115-
dMod = [Lib.minExtend(d0, dEdit)];
116-
tMod = Lib.minExtend(trace, tEdit);
117-
}
118-
119-
var ptgroup = d3.select(this).select('g.legendpoints');
120-
121-
var pts = ptgroup.selectAll('path.scatterpts')
122-
.data(showMarkers ? dMod : []);
123-
pts.enter().append('path').classed('scatterpts', true)
124-
.attr('transform', 'translate(20,0)');
125-
pts.exit().remove();
126-
pts.call(Drawing.pointStyle, tMod);
127-
128-
// 'mrc' is set in pointStyle and used in textPointStyle:
129-
// constrain it here
130-
if(showMarkers) dMod[0].mrc = 3;
131-
132-
var txt = ptgroup.selectAll('g.pointtext')
133-
.data(showText ? dMod : []);
134-
txt.enter()
135-
.append('g').classed('pointtext',true)
136-
.append('text').attr('transform', 'translate(20,0)');
137-
txt.exit().remove();
138-
txt.selectAll('text').call(Drawing.textPointStyle, tMod);
139-
140-
};
141-
142-
legend.bars = function(d) {
143-
var trace = d[0].trace,
144-
marker = trace.marker||{},
145-
markerLine = marker.line||{},
146-
barpath = d3.select(this).select('g.legendpoints')
147-
.selectAll('path.legendbar')
148-
.data(Plots.traceIs(trace, 'bar') ? [d] : []);
149-
barpath.enter().append('path').classed('legendbar',true)
150-
.attr('d','M6,6H-6V-6H6Z')
151-
.attr('transform','translate(20,0)');
152-
barpath.exit().remove();
153-
barpath.each(function(d) {
154-
var w = (d.mlw+1 || markerLine.width+1) - 1,
155-
p = d3.select(this);
156-
p.style('stroke-width',w+'px')
157-
.call(Color.fill, d.mc || marker.color);
158-
if(w) {
159-
p.call(Color.stroke, d.mlc || markerLine.color);
160-
}
161-
});
162-
};
163-
164-
legend.boxes = function(d) {
165-
var trace = d[0].trace,
166-
pts = d3.select(this).select('g.legendpoints')
167-
.selectAll('path.legendbox')
168-
.data(Plots.traceIs(trace, 'box') && trace.visible ? [d] : []);
169-
pts.enter().append('path').classed('legendbox', true)
170-
// if we want the median bar, prepend M6,0H-6
171-
.attr('d', 'M6,6H-6V-6H6Z')
172-
.attr('transform', 'translate(20,0)');
173-
pts.exit().remove();
174-
pts.each(function(d) {
175-
var w = (d.lw+1 || trace.line.width+1) - 1,
176-
p = d3.select(this);
177-
p.style('stroke-width', w+'px')
178-
.call(Color.fill, d.fc || trace.fillcolor);
179-
if(w) {
180-
p.call(Color.stroke, d.lc || trace.line.color);
181-
}
182-
});
183-
};
184-
185-
legend.pie = function(d) {
186-
var trace = d[0].trace,
187-
pts = d3.select(this).select('g.legendpoints')
188-
.selectAll('path.legendpie')
189-
.data(Plots.traceIs(trace, 'pie') && trace.visible ? [d] : []);
190-
pts.enter().append('path').classed('legendpie', true)
191-
.attr('d', 'M6,6H-6V-6H6Z')
192-
.attr('transform', 'translate(20,0)');
193-
pts.exit().remove();
194-
195-
if(pts.size()) pts.call(styleOne, d[0], trace);
196-
};
197-
198-
legend.style = function(s) {
199-
s.each(function(d) {
200-
var traceGroup = d3.select(this);
201-
202-
var fill = traceGroup
203-
.selectAll('g.legendfill')
204-
.data([d]);
205-
fill.enter().append('g')
206-
.classed('legendfill',true);
207-
208-
var line = traceGroup
209-
.selectAll('g.legendlines')
210-
.data([d]);
211-
line.enter().append('g')
212-
.classed('legendlines',true);
213-
214-
var symbol = traceGroup
215-
.selectAll('g.legendsymbols')
216-
.data([d]);
217-
symbol.enter().append('g')
218-
.classed('legendsymbols',true);
219-
symbol.style('opacity', d[0].trace.opacity);
220-
221-
symbol.selectAll('g.legendpoints')
222-
.data([d])
223-
.enter().append('g')
224-
.classed('legendpoints',true);
225-
})
226-
.each(legend.bars)
227-
.each(legend.boxes)
228-
.each(legend.pie)
229-
.each(legend.lines)
230-
.each(legend.points);
231-
};
232-
23332
legend.texts = function(context, td, d, i, traces) {
23433
var fullLayout = td._fullLayout,
23534
trace = d[0].trace,
@@ -724,6 +523,7 @@ legend.repositionLegend = function(td, traces) {
724523
lx = Math.round(lx);
725524
ly = Math.round(ly);
726525

526+
legend.style = require('./style');
727527
// lastly check if the margin auto-expand has changed
728528
Plots.autoMargin(td,'legend',{
729529
x: opts.x,

0 commit comments

Comments
 (0)