forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout_defaults.js
161 lines (130 loc) · 5.31 KB
/
layout_defaults.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
'use strict';
var Lib = require('../../lib');
var Color = require('../../components/color');
var Template = require('../../plot_api/plot_template');
var handleSubplotDefaults = require('../subplot_defaults');
var getSubplotData = require('../get_data').getSubplotData;
var handlePrefixSuffixDefaults = require('../cartesian/prefix_suffix_defaults');
var handleTickLabelDefaults = require('../cartesian/tick_label_defaults');
var handleLineGridDefaults = require('../cartesian/line_grid_defaults');
var setConvertCartesian = require('../cartesian/set_convert');
var layoutAttributes = require('./layout_attributes');
var constants = require('./constants');
var axisNames = constants.axisNames;
var makeImagDflt = memoize(function(realTickvals) {
// TODO: handle this case outside supply defaults step
if(Lib.isTypedArray(realTickvals)) realTickvals = Array.from(realTickvals);
return realTickvals.slice().reverse().map(function(x) { return -x; })
.concat([0])
.concat(realTickvals);
}, String);
function handleDefaults(contIn, contOut, coerce, opts) {
var bgColor = coerce('bgcolor');
opts.bgColor = Color.combine(bgColor, opts.paper_bgcolor);
var subplotData = getSubplotData(opts.fullData, constants.name, opts.id);
var layoutOut = opts.layoutOut;
var axName;
function coerceAxis(attr, dflt) {
return coerce(axName + '.' + attr, dflt);
}
for(var i = 0; i < axisNames.length; i++) {
axName = axisNames[i];
if(!Lib.isPlainObject(contIn[axName])) {
contIn[axName] = {};
}
var axIn = contIn[axName];
var axOut = Template.newContainer(contOut, axName);
axOut._id = axOut._name = axName;
axOut._attr = opts.id + '.' + axName;
axOut._traceIndices = subplotData.map(function(t) { return t._expandedIndex; });
var visible = coerceAxis('visible');
axOut.type = 'linear';
setConvertCartesian(axOut, layoutOut);
handlePrefixSuffixDefaults(axIn, axOut, coerceAxis, axOut.type);
if(visible) {
var isRealAxis = axName === 'realaxis';
if(isRealAxis) coerceAxis('side');
if(isRealAxis) {
coerceAxis('tickvals');
} else {
var imagTickvalsDflt = makeImagDflt(
contOut.realaxis.tickvals ||
layoutAttributes.realaxis.tickvals.dflt
);
coerceAxis('tickvals', imagTickvalsDflt);
}
// TODO: handle this case outside supply defaults step
if(Lib.isTypedArray(axOut.tickvals)) axOut.tickvals = Array.from(axOut.tickvals);
var dfltColor;
var dfltFontColor;
var dfltFontSize;
var dfltFontFamily;
var font = opts.font || {};
if(visible) {
dfltColor = coerceAxis('color');
dfltFontColor = (dfltColor === axIn.color) ? dfltColor : font.color;
dfltFontSize = font.size;
dfltFontFamily = font.family;
}
handleTickLabelDefaults(axIn, axOut, coerceAxis, axOut.type, {
noAutotickangles: true,
noTicklabelshift: true,
noTicklabelstandoff: true,
noTicklabelstep: true,
noAng: !isRealAxis,
noExp: true,
font: {
color: dfltFontColor,
size: dfltFontSize,
family: dfltFontFamily
}
});
Lib.coerce2(contIn, contOut, layoutAttributes, axName + '.ticklen');
Lib.coerce2(contIn, contOut, layoutAttributes, axName + '.tickwidth');
Lib.coerce2(contIn, contOut, layoutAttributes, axName + '.tickcolor', contOut.color);
var showTicks = coerceAxis('ticks');
if(!showTicks) {
delete contOut[axName].ticklen;
delete contOut[axName].tickwidth;
delete contOut[axName].tickcolor;
}
handleLineGridDefaults(axIn, axOut, coerceAxis, {
dfltColor: dfltColor,
bgColor: opts.bgColor,
// default grid color is darker here (60%, vs cartesian default ~91%)
// because the grid is not square so the eye needs heavier cues to follow
blend: 60,
showLine: true,
showGrid: true,
noZeroLine: true,
attributes: layoutAttributes[axName]
});
coerceAxis('layer');
}
coerceAxis('hoverformat');
delete axOut.type;
axOut._input = axIn;
}
}
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
handleSubplotDefaults(layoutIn, layoutOut, fullData, {
noUirevision: true,
type: constants.name,
attributes: layoutAttributes,
handleDefaults: handleDefaults,
font: layoutOut.font,
paper_bgcolor: layoutOut.paper_bgcolor,
fullData: fullData,
layoutOut: layoutOut
});
};
function memoize(fn, keyFn) {
var cache = {};
return function(val) {
var newKey = keyFn ? keyFn(val) : val;
if(newKey in cache) { return cache[newKey]; }
var out = fn(val);
cache[newKey] = out;
return out;
};
}