Skip to content

Commit 1620e61

Browse files
committed
make 'color' a shared attribute among layers at all types
1 parent cf27e55 commit 1620e61

File tree

4 files changed

+58
-74
lines changed

4 files changed

+58
-74
lines changed

src/plots/mapbox/layout_attributes.js

+22-34
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ module.exports = {
136136
].join(' ')
137137
},
138138

139+
// attributes shared between all types
139140
below: {
140141
valType: 'string',
141142
dflt: '',
@@ -147,7 +148,28 @@ module.exports = {
147148
'the layer will be inserted above every existing layer.'
148149
].join(' ')
149150
},
151+
color: {
152+
valType: 'color',
153+
dflt: defaultLine,
154+
role: 'style',
155+
description: [
156+
'Sets the primary layer color.',
157+
'If `type` is *circle*, color corresponds to the circle color',
158+
'If `type` is *line*, color corresponds to the line color',
159+
'If `type` is *fill*, color corresponds to the fill color',
160+
'If `type` is *symbol*, color corresponds to the icon color'
161+
].join(' ')
162+
},
163+
opacity: {
164+
valType: 'number',
165+
min: 0,
166+
max: 1,
167+
dflt: 1,
168+
role: 'info',
169+
description: 'Sets the opacity of the layer.'
170+
},
150171

172+
// type-specific style attributes
151173
circle: {
152174
radius: {
153175
valType: 'number',
@@ -157,15 +179,6 @@ module.exports = {
157179
'Sets the circle radius.',
158180
'Has an effect only when `type` is set to *circle*.'
159181
].join(' ')
160-
},
161-
color: {
162-
valType: 'color',
163-
dflt: defaultLine,
164-
role: 'style',
165-
description: [
166-
'Sets the circle color.',
167-
'Has an effect only when `type` is set to *circle*.'
168-
].join(' ')
169182
}
170183
},
171184

@@ -178,28 +191,10 @@ module.exports = {
178191
'Sets the line radius.',
179192
'Has an effect only when `type` is set to *line*.'
180193
].join(' ')
181-
},
182-
color: {
183-
valType: 'color',
184-
dflt: defaultLine,
185-
role: 'style',
186-
description: [
187-
'Sets the line color.',
188-
'Has an effect only when `type` is set to *line*.'
189-
].join(' ')
190194
}
191195
},
192196

193197
fill: {
194-
color: {
195-
valType: 'color',
196-
dflt: defaultLine,
197-
role: 'style',
198-
description: [
199-
'Sets the fill color.',
200-
'Has an effect only when `type` is set to *fill*.'
201-
].join(' ')
202-
},
203198
outlinecolor: {
204199
valType: 'color',
205200
dflt: defaultLine,
@@ -211,13 +206,6 @@ module.exports = {
211206
}
212207
},
213208

214-
opacity: {
215-
valType: 'number',
216-
min: 0,
217-
max: 1,
218-
dflt: 1,
219-
role: 'info',
220-
description: 'Sets the opacity of the layer.'
221209
}
222210
}
223211

src/plots/mapbox/layout_defaults.js

+5-14
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,25 @@ function handleLayerDefaults(containerIn, containerOut) {
5757

5858
if(sourceType === 'vector') coerce('sourcelayer');
5959

60-
// maybe add smart default based off GeoJSON geometry
60+
// maybe add smart default based off GeoJSON geometry?
6161
var type = coerce('type');
6262

63-
var dfltColor;
63+
coerce('below');
64+
coerce('color');
65+
coerce('opacity');
6466

6567
if(type === 'circle') {
66-
dfltColor = (layerIn.line || {}).color || (layerIn.fill || {}).color;
67-
68-
coerce('circle.color', dfltColor);
6968
coerce('circle.radius');
7069
}
7170

7271
if(type === 'line') {
73-
dfltColor = (layerIn.circle || {}).color || (layerIn.fill || {}).color;
74-
75-
coerce('line.color', dfltColor);
7672
coerce('line.width');
7773
}
7874

7975
if(type === 'fill') {
80-
dfltColor = (layerIn.circle || {}).color || (layerIn.line || {}).color;
81-
82-
coerce('fill.color', dfltColor);
83-
coerce('fill.outlinecolor', dfltColor);
76+
coerce('fill.outlinecolor');
8477
}
8578

86-
coerce('below');
87-
coerce('opacity');
8879

8980
layersOut.push(layerOut);
9081
}

test/image/mocks/mapbox_layers.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,7 @@
532532
},
533533
"type": "fill",
534534
"below": "water",
535-
"fill": {
536-
"color": "#ece2f0"
537-
},
535+
"color": "#ece2f0",
538536
"opacity": 0.8
539537
},
540538
{

test/jasmine/tests/mapbox_test.js

+30-23
Original file line numberDiff line numberDiff line change
@@ -85,40 +85,47 @@ describe('mapbox defaults', function() {
8585
});
8686

8787
it('should only coerce relevant layer style attributes', function() {
88+
var base = {
89+
line: { width: 3 },
90+
fill: { outlinecolor: '#d3d3d3' },
91+
circle: { radius: 20 },
92+
symbol: { icon: 'monument' }
93+
};
94+
8895
layoutIn = {
8996
mapbox: {
90-
layers: [{
91-
sourcetype: 'vector',
92-
type: 'line',
93-
line: {
94-
color: 'red',
95-
width: 3
96-
},
97-
fillcolor: 'blue'
98-
}, {
99-
sourcetype: 'geojson',
100-
type: 'fill',
101-
line: {
102-
color: 'red',
103-
width: 3
104-
},
105-
fill: {
106-
color: 'blue',
107-
outlinecolor: 'green'
108-
}
109-
}]
97+
layers: [
98+
Lib.extendFlat({}, base, {
99+
type: 'line',
100+
color: 'red'
101+
}),
102+
Lib.extendFlat({}, base, {
103+
type: 'fill',
104+
color: 'blue'
105+
}),
106+
Lib.extendFlat({}, base, {
107+
type: 'circle',
108+
color: 'green'
109+
}),
110110
}
111111
};
112112

113113
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
114114

115-
expect(layoutOut.mapbox.layers[0].line.color).toEqual('red');
115+
expect(layoutOut.mapbox.layers[0].color).toEqual('red');
116116
expect(layoutOut.mapbox.layers[0].line.width).toEqual(3);
117117
expect(layoutOut.mapbox.layers[0].fill).toBeUndefined();
118+
expect(layoutOut.mapbox.layers[0].circle).toBeUndefined();
118119

120+
expect(layoutOut.mapbox.layers[1].color).toEqual('blue');
121+
expect(layoutOut.mapbox.layers[1].fill.outlinecolor).toEqual('#d3d3d3');
119122
expect(layoutOut.mapbox.layers[1].line).toBeUndefined();
120-
expect(layoutOut.mapbox.layers[1].fill.color).toEqual('blue');
121-
expect(layoutOut.mapbox.layers[1].fill.outlinecolor).toEqual('green');
123+
expect(layoutOut.mapbox.layers[1].circle).toBeUndefined();
124+
125+
expect(layoutOut.mapbox.layers[2].color).toEqual('green');
126+
expect(layoutOut.mapbox.layers[2].circle.radius).toEqual(20);
127+
expect(layoutOut.mapbox.layers[2].line).toBeUndefined();
128+
expect(layoutOut.mapbox.layers[2].fill).toBeUndefined();
122129
});
123130
});
124131

0 commit comments

Comments
 (0)