-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathaxis_defaults.js
188 lines (152 loc) · 6.22 KB
/
axis_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
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
/**
* Copyright 2012-2020, 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 Registry = require('../../registry');
var Lib = require('../../lib');
var handleArrayContainerDefaults = require('../array_container_defaults');
var layoutAttributes = require('./layout_attributes');
var handleTickValueDefaults = require('./tick_value_defaults');
var handleTickMarkDefaults = require('./tick_mark_defaults');
var handleTickLabelDefaults = require('./tick_label_defaults');
var handleCategoryOrderDefaults = require('./category_order_defaults');
var handleLineGridDefaults = require('./line_grid_defaults');
var setConvert = require('./set_convert');
var ONEDAY = require('../../constants/numerical').ONEDAY;
/**
* options: object containing:
*
* letter: 'x' or 'y'
* title: name of the axis (ie 'Colorbar') to go in default title
* font: the default font to inherit
* outerTicks: boolean, should ticks default to outside?
* showGrid: boolean, should gridlines be shown by default?
* noHover: boolean, this axis doesn't support hover effects?
* noTickson: boolean, this axis doesn't support 'tickson'
* data: the plot data, used to manage categories
* bgColor: the plot background color, to calculate default gridline colors
* calendar:
* splomStash:
* visibleDflt: boolean
* reverseDflt: boolean
* automargin: boolean
*/
module.exports = function handleAxisDefaults(containerIn, containerOut, coerce, options, layoutOut) {
var letter = options.letter;
var font = options.font || {};
var splomStash = options.splomStash || {};
var visible = coerce('visible', !options.visibleDflt);
var axType = containerOut.type;
if(axType === 'date') {
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleDefaults');
handleCalendarDefaults(containerIn, containerOut, 'calendar', options.calendar);
}
setConvert(containerOut, layoutOut);
var autorangeDflt = !containerOut.isValidRange(containerIn.range);
if(autorangeDflt && options.reverseDflt) autorangeDflt = 'reversed';
var autoRange = coerce('autorange', autorangeDflt);
if(autoRange && (axType === 'linear' || axType === '-')) coerce('rangemode');
coerce('range');
containerOut.cleanRange();
handleCategoryOrderDefaults(containerIn, containerOut, coerce, options);
if(axType !== 'category' && !options.noHover) coerce('hoverformat');
var dfltColor = coerce('color');
// if axis.color was provided, use it for fonts too; otherwise,
// inherit from global font color in case that was provided.
// Compare to dflt rather than to containerIn, so we can provide color via
// template too.
var dfltFontColor = (dfltColor !== layoutAttributes.color.dflt) ? dfltColor : font.color;
// try to get default title from splom trace, fallback to graph-wide value
var dfltTitle = splomStash.label || layoutOut._dfltTitle[letter];
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options, {pass: 1});
if(!visible) return containerOut;
coerce('title.text', dfltTitle);
Lib.coerceFont(coerce, 'title.font', {
family: font.family,
size: Math.round(font.size * 1.2),
color: dfltFontColor
});
handleTickValueDefaults(containerIn, containerOut, coerce, axType);
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options, {pass: 2});
handleTickMarkDefaults(containerIn, containerOut, coerce, options);
handleLineGridDefaults(containerIn, containerOut, coerce, {
dfltColor: dfltColor,
bgColor: options.bgColor,
showGrid: options.showGrid,
attributes: layoutAttributes
});
if(containerOut.showline || containerOut.ticks) coerce('mirror');
if(options.automargin) coerce('automargin');
var isMultiCategory = containerOut.type === 'multicategory';
if(!options.noTickson &&
(containerOut.type === 'category' || isMultiCategory) &&
(containerOut.ticks || containerOut.showgrid)
) {
var ticksonDflt;
if(isMultiCategory) ticksonDflt = 'boundaries';
coerce('tickson', ticksonDflt);
}
if(isMultiCategory) {
var showDividers = coerce('showdividers');
if(showDividers) {
coerce('dividercolor');
coerce('dividerwidth');
}
}
// TODO
// - does this make sense for 'log', 'category' and 'multicategory' axis types ??
var breaks = containerIn.breaks;
if(Array.isArray(breaks) && breaks.length) {
handleArrayContainerDefaults(containerIn, containerOut, {
name: 'breaks',
inclusionAttr: 'enabled',
handleItemDefaults: breaksDefaults
});
setConvert(containerOut, layoutOut);
}
return containerOut;
};
function breaksDefaults(itemIn, itemOut, containerOut) {
function coerce(attr, dflt) {
return Lib.coerce(itemIn, itemOut, layoutAttributes.breaks, attr, dflt);
}
var enabled = coerce('enabled');
if(enabled) {
var isDateAxis = containerOut.type === 'date';
var bnds = coerce('bounds');
if(bnds && bnds.length >= 2) {
if(bnds.length > 2) {
itemOut.bounds = itemOut.bounds.slice(0, 2);
}
if(containerOut.autorange === false) {
var rng = containerOut.range;
// if bounds are bigger than the (set) range, disable break
if(rng[0] < rng[1]) {
if(bnds[0] < rng[0] && bnds[1] > rng[1]) {
itemOut.enabled = false;
return;
}
} else if(bnds[0] > rng[0] && bnds[1] < rng[1]) {
itemOut.enabled = false;
return;
}
}
if(isDateAxis) {
coerce('pattern');
}
} else {
var values = coerce('values');
if(values && values.length) {
coerce('dvalue', isDateAxis ? ONEDAY : 1);
} else {
itemOut.enabled = false;
return;
}
}
coerce('operation');
}
}