-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathhelpers.js
175 lines (146 loc) · 5.23 KB
/
helpers.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
/**
* Copyright 2012-2019, 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 Lib = require('../../lib');
var Color = require('../../components/color');
var setCursor = require('../../lib/setcursor');
var pieHelpers = require('../pie/helpers');
function labelStr(label) {
return (label || label === 0) ? String(label) : '';
}
exports.findEntryWithLevel = function(hierarchy, level) {
var out;
var key = labelStr(level);
if(key) {
hierarchy.eachAfter(function(pt) {
if(exports.getPtId(pt) === key) {
return out = pt.copy();
}
});
}
return out || hierarchy;
};
exports.findEntryWithChild = function(hierarchy, childId) {
var out;
var key = labelStr(childId);
hierarchy.eachAfter(function(pt) {
var children = pt.children || [];
for(var i = 0; i < children.length; i++) {
var child = children[i];
if(exports.getPtId(child) === key) {
return out = pt.copy();
}
}
});
return out || hierarchy;
};
exports.isEntry = function(pt) {
return !pt.parent;
};
exports.isLeaf = function(pt) {
return !pt.children;
};
exports.getPtId = function(pt) {
return pt.data.data.id;
};
exports.isHierarchyRoot = function(pt) {
return pt.data.data.pid === '';
};
exports.setSliceCursor = function(sliceTop, gd, opts) {
var hide = opts.isTransitioning;
if(!hide) {
var pt = sliceTop.datum();
hide = (
(opts.hideOnRoot && exports.isHierarchyRoot(pt)) ||
(opts.hideOnLeaves && exports.isLeaf(pt))
);
}
setCursor(sliceTop, hide ? null : 'pointer');
};
function determineOutsideTextFont(trace, pt, layoutFont) {
return {
color: exports.getOutsideTextFontKey('color', trace, pt, layoutFont),
family: exports.getOutsideTextFontKey('family', trace, pt, layoutFont),
size: exports.getOutsideTextFontKey('size', trace, pt, layoutFont)
};
}
function determineInsideTextFont(trace, pt, layoutFont, cont) {
var cdi = pt.data.data;
var ptNumber = cdi.i;
var customColor = Lib.castOption(trace, ptNumber, 'insidetextfont.color');
if(!customColor && trace._input.textfont) {
// Why not simply using trace.textfont? Because if not set, it
// defaults to layout.font which has a default color. But if
// textfont.color and insidetextfont.color don't supply a value,
// a contrasting color shall be used.
customColor = Lib.castOption(trace._input, ptNumber, 'textfont.color');
}
return {
color: customColor || Color.contrast(cdi.color),
family: exports.getInsideTextFontKey('family', cont || trace, pt, layoutFont),
size: exports.getInsideTextFontKey('size', cont || trace, pt, layoutFont)
};
}
exports.getInsideTextFontKey = function(keyStr, trace, pt, layoutFont) {
var ptNumber = pt.data.data.i;
return (
Lib.castOption(trace, ptNumber, 'insidetextfont.' + keyStr) ||
Lib.castOption(trace, ptNumber, 'textfont.' + keyStr) ||
layoutFont.size
);
};
exports.getOutsideTextFontKey = function(keyStr, trace, pt, layoutFont) {
var ptNumber = pt.data.data.i;
return (
Lib.castOption(trace, ptNumber, 'outsidetextfont.' + keyStr) ||
Lib.castOption(trace, ptNumber, 'textfont.' + keyStr) ||
layoutFont.size
);
};
exports.isOutsideText = function(trace, pt) {
return !trace._hasColorscale && exports.isHierarchyRoot(pt);
};
exports.determineTextFont = function(trace, pt, layoutFont, cont) {
return exports.isOutsideText(trace, pt) ?
determineOutsideTextFont(trace, pt, layoutFont) :
determineInsideTextFont(trace, pt, layoutFont, cont);
};
exports.hasTransition = function(transitionOpts) {
// We could optimize hasTransition per trace,
// as sunburst & treemap have no cross-trace logic!
return !!(transitionOpts && transitionOpts.duration > 0);
};
exports.getMaxDepth = function(trace) {
return trace.maxdepth >= 0 ? trace.maxdepth : Infinity;
};
exports.isHeader = function(pt, trace) { // it is only used in treemap.
return !(exports.isLeaf(pt) || pt.depth === trace._maxDepth - 1);
};
exports.getLabelStr = function(label) {
return labelStr(label).split('<br>').join(' ');
};
exports.getLabelString = function(label) { // used in hover to reference to the "root"
var str = exports.getLabelStr(label);
return str === '' ? '"root"' : str;
};
exports.listPath = function(d, keyStr) {
var parent = d.parent;
if(!parent) return [];
var list = keyStr ? [parent.data[keyStr]] : [parent];
return exports.listPath(parent, keyStr).concat(list);
};
exports.getPath = function(d) {
return exports.listPath(d, 'label').join('/') + '/';
};
exports.formatValue = pieHelpers.formatPieValue;
// TODO: should combine the two in a separate PR - Also please note Lib.formatPercent should support separators.
exports.formatPercent = function(v, separators) {
var tx = Lib.formatPercent(v, 0); // use funnel(area) version
if(tx === '0%') tx = pieHelpers.formatPiePercent(v, separators); // use pie version
return tx;
};