Skip to content

Commit 86067a6

Browse files
committed
🌴 cartesian/ternary/polar line/grid default logic
... and locking down the coerce2 pattern while at it ;)
1 parent 5751de5 commit 86067a6

File tree

4 files changed

+129
-103
lines changed

4 files changed

+129
-103
lines changed

src/plots/cartesian/axis_defaults.js

+8-37
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,20 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

12-
var colorMix = require('tinycolor2').mix;
13-
1411
var Registry = require('../../registry');
1512
var Lib = require('../../lib');
16-
var lightFraction = require('../../components/color/attributes').lightFraction;
1713

1814
var layoutAttributes = require('./layout_attributes');
1915
var handleTickValueDefaults = require('./tick_value_defaults');
2016
var handleTickMarkDefaults = require('./tick_mark_defaults');
2117
var handleTickLabelDefaults = require('./tick_label_defaults');
2218
var handleCategoryOrderDefaults = require('./category_order_defaults');
19+
var handleLineGridDefaults = require('./line_grid_defaults');
2320
var setConvert = require('./set_convert');
2421
var orderedCategories = require('./ordered_categories');
2522

26-
2723
/**
2824
* options: object containing:
2925
*
@@ -40,10 +36,6 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
4036
var letter = options.letter;
4137
var font = options.font || {};
4238

43-
function coerce2(attr, dflt) {
44-
return Lib.coerce2(containerIn, containerOut, layoutAttributes, attr, dflt);
45-
}
46-
4739
var visible = coerce('visible', !options.cheateronly);
4840

4941
var axType = containerOut.type;
@@ -84,35 +76,14 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
8476
handleTickValueDefaults(containerIn, containerOut, coerce, axType);
8577
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options);
8678
handleTickMarkDefaults(containerIn, containerOut, coerce, options);
79+
handleLineGridDefaults(containerIn, containerOut, coerce, {
80+
dfltColor: dfltColor,
81+
bgColor: options.bgColor,
82+
showGrid: options.showGrid,
83+
attributes: layoutAttributes
84+
});
8785

88-
var lineColor = coerce2('linecolor', dfltColor),
89-
lineWidth = coerce2('linewidth'),
90-
showLine = coerce('showline', !!lineColor || !!lineWidth);
91-
92-
if(!showLine) {
93-
delete containerOut.linecolor;
94-
delete containerOut.linewidth;
95-
}
96-
97-
if(showLine || containerOut.ticks) coerce('mirror');
98-
99-
var gridColor = coerce2('gridcolor', colorMix(dfltColor, options.bgColor, lightFraction).toRgbString()),
100-
gridWidth = coerce2('gridwidth'),
101-
showGridLines = coerce('showgrid', options.showGrid || !!gridColor || !!gridWidth);
102-
103-
if(!showGridLines) {
104-
delete containerOut.gridcolor;
105-
delete containerOut.gridwidth;
106-
}
107-
108-
var zeroLineColor = coerce2('zerolinecolor', dfltColor),
109-
zeroLineWidth = coerce2('zerolinewidth'),
110-
showZeroLine = coerce('zeroline', options.showGrid || !!zeroLineColor || !!zeroLineWidth);
111-
112-
if(!showZeroLine) {
113-
delete containerOut.zerolinecolor;
114-
delete containerOut.zerolinewidth;
115-
}
86+
if(containerOut.showline || containerOut.ticks) coerce('mirror');
11687

11788
return containerOut;
11889
};
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var colorMix = require('tinycolor2').mix;
12+
var lightFraction = require('../../components/color/attributes').lightFraction;
13+
var Lib = require('../../lib');
14+
15+
/**
16+
* @param {object} opts :
17+
* - dfltColor {string} : default axis color
18+
* - bgColor {string} : combined subplot bg color
19+
* - blend {number, optional} : blend percentage (to compute dflt grid color)
20+
* - showLine {boolean} : show line by default
21+
* - showGrid {boolean} : show grid by default
22+
* - noZeroLine {boolean} : don't coerce zeroline* attributes
23+
* - attributes {object} : attribute object associated with input containers
24+
*/
25+
module.exports = function handleLineGridDefaults(containerIn, containerOut, coerce, opts) {
26+
opts = opts || {};
27+
28+
var dfltColor = opts.dfltColor;
29+
30+
function coerce2(attr, dflt) {
31+
return Lib.coerce2(containerIn, containerOut, opts.attributes, attr, dflt);
32+
}
33+
34+
var lineColor = coerce2('linecolor', dfltColor);
35+
var lineWidth = coerce2('linewidth');
36+
var showLine = coerce('showline', opts.showLine || !!lineColor || !!lineWidth);
37+
38+
if(!showLine) {
39+
delete containerOut.linecolor;
40+
delete containerOut.linewidth;
41+
}
42+
43+
var gridColorDflt = colorMix(dfltColor, opts.bgColor, opts.blend || lightFraction).toRgbString();
44+
var gridColor = coerce2('gridcolor', gridColorDflt);
45+
var gridWidth = coerce2('gridwidth');
46+
var showGridLines = coerce('showgrid', opts.showGrid || !!gridColor || !!gridWidth);
47+
48+
if(!showGridLines) {
49+
delete containerOut.gridcolor;
50+
delete containerOut.gridwidth;
51+
}
52+
53+
if(!opts.noZeroLine) {
54+
var zeroLineColor = coerce2('zerolinecolor', dfltColor);
55+
var zeroLineWidth = coerce2('zerolinewidth');
56+
var showZeroLine = coerce('zeroline', opts.showGrid || !!zeroLineColor || !!zeroLineWidth);
57+
58+
if(!showZeroLine) {
59+
delete containerOut.zerolinecolor;
60+
delete containerOut.zerolinewidth;
61+
}
62+
}
63+
};

src/plots/polar/layout_defaults.js

+47-48
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
'use strict';
1010

11-
var colorMix = require('tinycolor2').mix;
12-
1311
var Lib = require('../../lib');
1412
var Color = require('../../components/color');
1513
var Plots = require('../plots');
@@ -20,6 +18,7 @@ var handleTickValueDefaults = require('../cartesian/tick_value_defaults');
2018
var handleTickMarkDefaults = require('../cartesian/tick_mark_defaults');
2119
var handleTickLabelDefaults = require('../cartesian/tick_label_defaults');
2220
var handleCategoryOrderDefaults = require('../cartesian/category_order_defaults');
21+
var handleLineGridDefaults = require('../cartesian/line_grid_defaults');
2322
var autoType = require('../cartesian/axis_autotype');
2423
var orderedCategories = require('../cartesian/ordered_categories');
2524
var setConvert = require('../cartesian/set_convert');
@@ -69,10 +68,17 @@ function handleDefaults(contIn, contOut, coerce, opts) {
6968
handleCalendarDefaults(axIn, axOut, 'calendar', layoutOut.calendar);
7069
}
7170

72-
coerceAxis('visible');
73-
71+
var visible = coerceAxis('visible');
7472
setConvert(axOut, layoutOut);
7573

74+
var dfltColor;
75+
var dfltFontColor;
76+
77+
if(visible) {
78+
dfltColor = coerceAxis('color');
79+
dfltFontColor = (dfltColor === axIn.color) ? dfltColor : opts.font.color;
80+
}
81+
7682
// We don't want to make downstream code call ax.setScale,
7783
// as both radial and angular axes don't have a set domain.
7884
// Furthermore, angular axes don't have a set range.
@@ -89,11 +95,13 @@ function handleDefaults(contIn, contOut, coerce, opts) {
8995
coerceAxis('range');
9096
axOut.cleanRange('range', {dfltRange: [0, 1]});
9197

92-
if(axOut.visible) {
98+
if(visible) {
9399
coerceAxis('side');
94100
coerceAxis('position', sector[0]);
101+
95102
}
96103
break;
104+
97105
case 'angularaxis':
98106
if(axType === 'linear') {
99107
coerceAxis('thetaunit');
@@ -111,10 +119,42 @@ function handleDefaults(contIn, contOut, coerce, opts) {
111119
break;
112120
}
113121

114-
if(axOut.visible) {
115-
handleAxisStyleDefaults(axIn, axOut, coerceAxis, opts);
122+
if(visible) {
123+
handleTickValueDefaults(axIn, axOut, coerceAxis, axOut.type);
124+
handleTickLabelDefaults(axIn, axOut, coerceAxis, axOut.type, {
125+
noHover: false,
126+
tickSuffixDflt: axOut.thetaunit === 'degrees' ? '°' : undefined
127+
});
128+
handleTickMarkDefaults(axIn, axOut, coerceAxis, {outerTicks: true});
129+
130+
var showTickLabels = coerceAxis('showticklabels');
131+
if(showTickLabels) {
132+
Lib.coerceFont(coerceAxis, 'tickfont', {
133+
family: opts.font.family,
134+
size: opts.font.size,
135+
color: dfltFontColor
136+
});
137+
coerceAxis('tickangle');
138+
coerceAxis('tickformat');
139+
}
140+
141+
handleLineGridDefaults(axIn, axOut, coerceAxis, {
142+
dfltColor: dfltColor,
143+
bgColor: opts.bgColor,
144+
// default grid color is darker here (60%, vs cartesian default ~91%)
145+
// because the grid is not square so the eye needs heavier cues to follow
146+
blend: 60,
147+
showLine: true,
148+
showGrid: true,
149+
noZeroLine: true,
150+
attributes: layoutAttributes[axName]
151+
});
152+
153+
coerceAxis('layer');
116154
}
117155

156+
coerceAxis('hoverformat');
157+
118158
axOut._input = axIn;
119159
}
120160
}
@@ -151,47 +191,6 @@ function handleAxisTypeDefaults(axIn, axOut, coerce, subplotData, dataAttr) {
151191
return axOut.type;
152192
}
153193

154-
function handleAxisStyleDefaults(axIn, axOut, coerce, opts) {
155-
var dfltColor = coerce('color');
156-
var dfltFontColor = (dfltColor === axIn.color) ? dfltColor : opts.font.color;
157-
158-
handleTickValueDefaults(axIn, axOut, coerce, axOut.type);
159-
handleTickLabelDefaults(axIn, axOut, coerce, axOut.type, {
160-
noHover: false,
161-
tickSuffixDflt: axOut.thetaunit === 'degrees' ? '°' : undefined
162-
});
163-
handleTickMarkDefaults(axIn, axOut, coerce, {outerTicks: true});
164-
165-
var showTickLabels = coerce('showticklabels');
166-
if(showTickLabels) {
167-
Lib.coerceFont(coerce, 'tickfont', {
168-
family: opts.font.family,
169-
size: opts.font.size,
170-
color: dfltFontColor
171-
});
172-
coerce('tickangle');
173-
coerce('tickformat');
174-
}
175-
176-
// TODO should use coerce2 pattern !!
177-
178-
var showLine = coerce('showline');
179-
if(showLine) {
180-
coerce('linecolor', dfltColor);
181-
coerce('linewidth');
182-
}
183-
184-
var showGridLines = coerce('showgrid');
185-
if(showGridLines) {
186-
// default grid color is darker here (60%, vs cartesian default ~91%)
187-
// because the grid is not square so the eye needs heavier cues to follow
188-
coerce('gridcolor', colorMix(dfltColor, opts.bgColor, 60).toRgbString());
189-
coerce('gridwidth');
190-
}
191-
192-
coerce('layer');
193-
}
194-
195194
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
196195
handleSubplotDefaults(layoutIn, layoutOut, fullData, {
197196
type: constants.name,

src/plots/ternary/layout/axis_defaults.js

+11-18
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
11-
var colorMix = require('tinycolor2').mix;
1210

1311
var Lib = require('../../../lib');
14-
1512
var layoutAttributes = require('./axis_attributes');
1613
var handleTickLabelDefaults = require('../../cartesian/tick_label_defaults');
1714
var handleTickMarkDefaults = require('../../cartesian/tick_mark_defaults');
1815
var handleTickValueDefaults = require('../../cartesian/tick_value_defaults');
19-
16+
var handleLineGridDefaults = require('../../cartesian/line_grid_defaults');
2017

2118
module.exports = function supplyLayoutDefaults(containerIn, containerOut, options) {
22-
2319
function coerce(attr, dflt) {
2420
return Lib.coerce(containerIn, containerOut, layoutAttributes, attr, dflt);
2521
}
@@ -64,21 +60,18 @@ module.exports = function supplyLayoutDefaults(containerIn, containerOut, option
6460
coerce('tickformat');
6561
}
6662

67-
coerce('hoverformat');
68-
69-
var showLine = coerce('showline');
70-
if(showLine) {
71-
coerce('linecolor', dfltColor);
72-
coerce('linewidth');
73-
}
74-
75-
var showGridLines = coerce('showgrid');
76-
if(showGridLines) {
63+
handleLineGridDefaults(containerIn, containerOut, coerce, {
64+
dfltColor: dfltColor,
65+
bgColor: options.bgColor,
7766
// default grid color is darker here (60%, vs cartesian default ~91%)
7867
// because the grid is not square so the eye needs heavier cues to follow
79-
coerce('gridcolor', colorMix(dfltColor, options.bgColor, 60).toRgbString());
80-
coerce('gridwidth');
81-
}
68+
blend: 60,
69+
showLine: true,
70+
showGrid: true,
71+
noZeroLine: true,
72+
attributes: layoutAttributes
73+
});
8274

75+
coerce('hoverformat');
8376
coerce('layer');
8477
};

0 commit comments

Comments
 (0)