-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathstyle.js
286 lines (242 loc) · 9.61 KB
/
style.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
/**
* 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 d3 = require('d3');
var Registry = require('../../registry');
var Lib = require('../../lib');
var Drawing = require('../drawing');
var Color = require('../color');
var subTypes = require('../../traces/scatter/subtypes');
var stylePie = require('../../traces/pie/style_one');
module.exports = function style(s, gd) {
s.each(function(d) {
var traceGroup = d3.select(this);
var layers = Lib.ensureSingle(traceGroup, 'g', 'layers');
layers.style('opacity', d[0].trace.opacity);
var fill = layers
.selectAll('g.legendfill')
.data([d]);
fill.enter().append('g')
.classed('legendfill', true);
var line = layers
.selectAll('g.legendlines')
.data([d]);
line.enter().append('g')
.classed('legendlines', true);
var symbol = layers
.selectAll('g.legendsymbols')
.data([d]);
symbol.enter().append('g')
.classed('legendsymbols', true);
symbol.selectAll('g.legendpoints')
.data([d])
.enter().append('g')
.classed('legendpoints', true);
})
.each(styleBars)
.each(styleBoxes)
.each(stylePies)
.each(styleLines)
.each(stylePoints)
.each(styleCandles)
.each(styleOHLC);
function styleLines(d) {
var trace = d[0].trace;
var showFill = trace.visible && trace.fill && trace.fill !== 'none';
var showLine = subTypes.hasLines(trace);
var contours = trace.contours;
if(contours && contours.type === 'constraint') {
showLine = contours.showlines;
showFill = contours._operation !== '=';
}
var fill = d3.select(this).select('.legendfill').selectAll('path')
.data(showFill ? [d] : []);
fill.enter().append('path').classed('js-fill', true);
fill.exit().remove();
fill.attr('d', 'M5,0h30v6h-30z')
.call(Drawing.fillGroupStyle);
var line = d3.select(this).select('.legendlines').selectAll('path')
.data(showLine ? [d] : []);
line.enter().append('path').classed('js-line', true)
.attr('d', 'M5,0h30');
line.exit().remove();
line.call(Drawing.lineGroupStyle);
}
function stylePoints(d) {
var d0 = d[0],
trace = d0.trace,
showMarkers = subTypes.hasMarkers(trace),
showText = subTypes.hasText(trace),
showLines = subTypes.hasLines(trace);
var dMod, tMod;
// 'scatter3d' and 'scattergeo' don't use gd.calcdata yet;
// use d0.trace to infer arrayOk attributes
function boundVal(attrIn, arrayToValFn, bounds) {
var valIn = Lib.nestedProperty(trace, attrIn).get(),
valToBound = (Array.isArray(valIn) && arrayToValFn) ?
arrayToValFn(valIn) : valIn;
if(bounds) {
if(valToBound < bounds[0]) return bounds[0];
else if(valToBound > bounds[1]) return bounds[1];
}
return valToBound;
}
function pickFirst(array) { return array[0]; }
// constrain text, markers, etc so they'll fit on the legend
if(showMarkers || showText || showLines) {
var dEdit = {};
var tEdit = {};
if(showMarkers) {
dEdit.mc = boundVal('marker.color', pickFirst);
dEdit.mo = boundVal('marker.opacity', Lib.mean, [0.2, 1]);
dEdit.ms = boundVal('marker.size', Lib.mean, [2, 16]);
dEdit.mlc = boundVal('marker.line.color', pickFirst);
dEdit.mlw = boundVal('marker.line.width', Lib.mean, [0, 5]);
tEdit.marker = {
sizeref: 1,
sizemin: 1,
sizemode: 'diameter'
};
}
if(showLines) {
tEdit.line = {
width: boundVal('line.width', pickFirst, [0, 10])
};
}
if(showText) {
dEdit.tx = 'Aa';
dEdit.tp = boundVal('textposition', pickFirst);
dEdit.ts = 10;
dEdit.tc = boundVal('textfont.color', pickFirst);
dEdit.tf = boundVal('textfont.family', pickFirst);
}
dMod = [Lib.minExtend(d0, dEdit)];
tMod = Lib.minExtend(trace, tEdit);
// always show legend items in base state
tMod.selectedpoints = null;
}
var ptgroup = d3.select(this).select('g.legendpoints');
var pts = ptgroup.selectAll('path.scatterpts')
.data(showMarkers ? dMod : []);
pts.enter().append('path').classed('scatterpts', true)
.attr('transform', 'translate(20,0)');
pts.exit().remove();
pts.call(Drawing.pointStyle, tMod, gd);
// 'mrc' is set in pointStyle and used in textPointStyle:
// constrain it here
if(showMarkers) dMod[0].mrc = 3;
var txt = ptgroup.selectAll('g.pointtext')
.data(showText ? dMod : []);
txt.enter()
.append('g').classed('pointtext', true)
.append('text').attr('transform', 'translate(20,0)');
txt.exit().remove();
txt.selectAll('text').call(Drawing.textPointStyle, tMod, gd);
}
function styleBars(d) {
var trace = d[0].trace,
marker = trace.marker || {},
markerLine = marker.line || {},
barpath = d3.select(this).select('g.legendpoints')
.selectAll('path.legendbar')
.data(Registry.traceIs(trace, 'bar') ? [d] : []);
barpath.enter().append('path').classed('legendbar', true)
.attr('d', 'M6,6H-6V-6H6Z')
.attr('transform', 'translate(20,0)');
barpath.exit().remove();
barpath.each(function(d) {
var p = d3.select(this),
d0 = d[0],
w = (d0.mlw + 1 || markerLine.width + 1) - 1;
p.style('stroke-width', w + 'px')
.call(Color.fill, d0.mc || marker.color);
if(w) {
p.call(Color.stroke, d0.mlc || markerLine.color);
}
});
}
function styleBoxes(d) {
var trace = d[0].trace,
pts = d3.select(this).select('g.legendpoints')
.selectAll('path.legendbox')
.data(Registry.traceIs(trace, 'box-violin') && trace.visible ? [d] : []);
pts.enter().append('path').classed('legendbox', true)
// if we want the median bar, prepend M6,0H-6
.attr('d', 'M6,6H-6V-6H6Z')
.attr('transform', 'translate(20,0)');
pts.exit().remove();
pts.each(function() {
var w = trace.line.width,
p = d3.select(this);
p.style('stroke-width', w + 'px')
.call(Color.fill, trace.fillcolor);
if(w) {
Color.stroke(p, trace.line.color);
}
});
}
function styleCandles(d) {
var trace = d[0].trace,
pts = d3.select(this).select('g.legendpoints')
.selectAll('path.legendcandle')
.data(trace.type === 'candlestick' && trace.visible ? [d, d] : []);
pts.enter().append('path').classed('legendcandle', true)
.attr('d', function(_, i) {
if(i) return 'M-15,0H-8M-8,6V-6H8Z'; // increasing
return 'M15,0H8M8,-6V6H-8Z'; // decreasing
})
.attr('transform', 'translate(20,0)')
.style('stroke-miterlimit', 1);
pts.exit().remove();
pts.each(function(_, i) {
var container = trace[i ? 'increasing' : 'decreasing'];
var w = container.line.width,
p = d3.select(this);
p.style('stroke-width', w + 'px')
.call(Color.fill, container.fillcolor);
if(w) {
Color.stroke(p, container.line.color);
}
});
}
function styleOHLC(d) {
var trace = d[0].trace,
pts = d3.select(this).select('g.legendpoints')
.selectAll('path.legendohlc')
.data(trace.type === 'ohlc' && trace.visible ? [d, d] : []);
pts.enter().append('path').classed('legendohlc', true)
.attr('d', function(_, i) {
if(i) return 'M-15,0H0M-8,-6V0'; // increasing
return 'M15,0H0M8,6V0'; // decreasing
})
.attr('transform', 'translate(20,0)')
.style('stroke-miterlimit', 1);
pts.exit().remove();
pts.each(function(_, i) {
var container = trace[i ? 'increasing' : 'decreasing'];
var w = container.line.width,
p = d3.select(this);
p.style('fill', 'none')
.call(Drawing.dashLine, container.line.dash, w);
if(w) {
Color.stroke(p, container.line.color);
}
});
}
function stylePies(d) {
var trace = d[0].trace,
pts = d3.select(this).select('g.legendpoints')
.selectAll('path.legendpie')
.data(Registry.traceIs(trace, 'pie') && trace.visible ? [d] : []);
pts.enter().append('path').classed('legendpie', true)
.attr('d', 'M6,6H-6V-6H6Z')
.attr('transform', 'translate(20,0)');
pts.exit().remove();
if(pts.size()) pts.call(stylePie, d[0], trace);
}
};