-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathstyle.js
172 lines (141 loc) · 5.29 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
'use strict';
var d3 = require('@plotly/d3');
var Color = require('../../components/color');
var Drawing = require('../../components/drawing');
var Lib = require('../../lib');
var Registry = require('../../registry');
var resizeText = require('./uniform_text').resizeText;
var attributes = require('./attributes');
var attributeTextFont = attributes.textfont;
var attributeInsideTextFont = attributes.insidetextfont;
var attributeOutsideTextFont = attributes.outsidetextfont;
var helpers = require('./helpers');
function style(gd) {
var s = d3.select(gd).selectAll('g.barlayer').selectAll('g.trace');
resizeText(gd, s, 'bar');
var barcount = s.size();
var fullLayout = gd._fullLayout;
// trace styling
s.style('opacity', function(d) { return d[0].trace.opacity; })
// for gapless (either stacked or neighboring grouped) bars use
// crispEdges to turn off antialiasing so an artificial gap
// isn't introduced.
.each(function(d) {
if((fullLayout.barmode === 'stack' && barcount > 1) ||
(fullLayout.bargap === 0 &&
fullLayout.bargroupgap === 0 &&
!d[0].trace.marker.line.width)) {
d3.select(this).attr('shape-rendering', 'crispEdges');
}
});
s.selectAll('g.points').each(function(d) {
var sel = d3.select(this);
var trace = d[0].trace;
stylePoints(sel, trace, gd);
});
Registry.getComponentMethod('errorbars', 'style')(s);
}
function stylePoints(sel, trace, gd) {
Drawing.pointStyle(sel.selectAll('path'), trace, gd);
styleTextPoints(sel, trace, gd);
}
function styleTextPoints(sel, trace, gd) {
sel.selectAll('text').each(function(d) {
var tx = d3.select(this);
var font = Lib.ensureUniformFontSize(gd, determineFont(tx, d, trace, gd));
Drawing.font(tx, font);
});
}
function styleOnSelect(gd, cd, sel) {
var trace = cd[0].trace;
if(trace.selectedpoints) {
stylePointsInSelectionMode(sel, trace, gd);
} else {
stylePoints(sel, trace, gd);
Registry.getComponentMethod('errorbars', 'style')(sel);
}
}
function stylePointsInSelectionMode(s, trace, gd) {
Drawing.selectedPointStyle(s.selectAll('path'), trace);
styleTextInSelectionMode(s.selectAll('text'), trace, gd);
}
function styleTextInSelectionMode(txs, trace, gd) {
txs.each(function(d) {
var tx = d3.select(this);
var font;
if(d.selected) {
font = Lib.ensureUniformFontSize(gd, determineFont(tx, d, trace, gd));
var selectedFontColor = trace.selected.textfont && trace.selected.textfont.color;
if(selectedFontColor) {
font.color = selectedFontColor;
}
Drawing.font(tx, font);
} else {
Drawing.selectedTextStyle(tx, trace);
}
});
}
function determineFont(tx, d, trace, gd) {
var layoutFont = gd._fullLayout.font;
var textFont = trace.textfont;
if(tx.classed('bartext-inside')) {
var barColor = getBarColor(d, trace);
textFont = getInsideTextFont(trace, d.i, layoutFont, barColor);
} else if(tx.classed('bartext-outside')) {
textFont = getOutsideTextFont(trace, d.i, layoutFont);
}
return textFont;
}
function getTextFont(trace, index, defaultValue) {
return getFontValue(
attributeTextFont, trace.textfont, index, defaultValue);
}
function getInsideTextFont(trace, index, layoutFont, barColor) {
var defaultFont = getTextFont(trace, index, layoutFont);
var wouldFallBackToLayoutFont =
(trace._input.textfont === undefined || trace._input.textfont.color === undefined) ||
(Array.isArray(trace.textfont.color) && trace.textfont.color[index] === undefined);
if(wouldFallBackToLayoutFont) {
defaultFont = {
color: Color.contrast(barColor),
family: defaultFont.family,
size: defaultFont.size
};
}
return getFontValue(
attributeInsideTextFont, trace.insidetextfont, index, defaultFont);
}
function getOutsideTextFont(trace, index, layoutFont) {
var defaultFont = getTextFont(trace, index, layoutFont);
return getFontValue(
attributeOutsideTextFont, trace.outsidetextfont, index, defaultFont);
}
function getFontValue(attributeDefinition, attributeValue, index, defaultValue) {
attributeValue = attributeValue || {};
var familyValue = helpers.getValue(attributeValue.family, index);
var sizeValue = helpers.getValue(attributeValue.size, index);
var colorValue = helpers.getValue(attributeValue.color, index);
return {
family: helpers.coerceString(
attributeDefinition.family, familyValue, defaultValue.family),
size: helpers.coerceNumber(
attributeDefinition.size, sizeValue, defaultValue.size),
color: helpers.coerceColor(
attributeDefinition.color, colorValue, defaultValue.color)
};
}
function getBarColor(cd, trace) {
if(trace.type === 'waterfall') {
return trace[cd.dir].marker.color;
}
return cd.mcc || cd.mc || trace.marker.color;
}
module.exports = {
style: style,
styleTextPoints: styleTextPoints,
styleOnSelect: styleOnSelect,
getInsideTextFont: getInsideTextFont,
getOutsideTextFont: getOutsideTextFont,
getBarColor: getBarColor,
resizeText: resizeText
};