Skip to content

Commit 2750392

Browse files
committed
Merge pull request #609 from monfera/581a-dry-color-attributes
DRY up color attributes
2 parents 21b0341 + 01d1041 commit 2750392

File tree

15 files changed

+352
-518
lines changed

15 files changed

+352
-518
lines changed

src/components/colorscale/attributes.js

-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
'use strict';
1010

11-
1211
module.exports = {
1312
zauto: {
1413
valType: 'boolean',
@@ -68,18 +67,5 @@ module.exports = {
6867
description: [
6968
'Determines whether or not a colorbar is displayed for this trace.'
7069
].join(' ')
71-
},
72-
73-
_deprecated: {
74-
scl: {
75-
valType: 'colorscale',
76-
role: 'style',
77-
description: 'Renamed to `colorscale`.'
78-
},
79-
reversescl: {
80-
valType: 'boolean',
81-
role: 'style',
82-
description: 'Renamed to `reversescale`.'
83-
}
8470
}
8571
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright 2012-2016, 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 colorScaleAttributes = require('./attributes');
12+
var extendDeep = require('../../lib/extend').extendDeep;
13+
14+
module.exports = function makeColorScaleAttributes(context) {
15+
return {
16+
color: {
17+
valType: 'color',
18+
arrayOk: true,
19+
role: 'style',
20+
description: [
21+
'Sets the ', context, ' color. It accepts either a specific color',
22+
' or an array of values that are mapped to the colorscale',
23+
' relative to the max and min values of the array or relative to',
24+
' `cmin` and `cmax` if set.'
25+
].join('')
26+
},
27+
colorscale: extendDeep({}, colorScaleAttributes.colorscale, {
28+
description: [
29+
'Sets the colorscale and only has an effect',
30+
' if `', context, '.color` is set to a numerical array.',
31+
' The colorscale must be an array containing',
32+
' arrays mapping a normalized value to an',
33+
' rgb, rgba, hex, hsl, hsv, or named color string.',
34+
' At minimum, a mapping for the lowest (0) and highest (1)',
35+
' values are required. For example,',
36+
' `[[0, \'rgb(0,0,255)\', [1, \'rgb(255,0,0)\']]`.',
37+
' To control the bounds of the colorscale in color space,',
38+
' use `', context, '.cmin` and `', context, '.cmax`.'
39+
].join('')
40+
}),
41+
cauto: extendDeep({}, colorScaleAttributes.zauto, {
42+
description: [
43+
'Has an effect only if `', context, '.color` is set to a numerical array.',
44+
' Determines the whether or not the color domain is computed',
45+
' automatically.'
46+
].join('')
47+
}),
48+
cmax: extendDeep({}, colorScaleAttributes.zmax, {
49+
description: [
50+
'Has an effect only if `', context, '.color` is set to a numerical array.',
51+
' Sets the upper bound of the color domain.',
52+
' Value should be associated to the `', context, '.color` array index,',
53+
' and if set, `', context, '.cmin` must be set as well.'
54+
].join('')
55+
}),
56+
cmin: extendDeep({}, colorScaleAttributes.zmin, {
57+
description: [
58+
'Has an effect only if `', context, '.color` is set to a numerical array.',
59+
' Sets the lower bound of the color domain.',
60+
' Value should be associated to the `', context, '.color` array index,',
61+
' and if set, `', context, '.cmax` must be set as well.'
62+
].join('')
63+
}),
64+
autocolorscale: extendDeep({}, colorScaleAttributes.autocolorscale, {
65+
description: [
66+
'Has an effect only if `', context, '.color` is set to a numerical array.',
67+
' Determines whether or not the colorscale is picked using',
68+
' values inside `', context, '.color`.'
69+
].join('')
70+
}),
71+
reversescale: extendDeep({}, colorScaleAttributes.reversescale, {
72+
description: [
73+
'Has an effect only if `', context, '.color` is set to a numerical array.',
74+
' Reverses the colorscale.'
75+
].join('')
76+
})
77+
};
78+
};

src/traces/bar/attributes.js

+12-22
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
'use strict';
1010

11-
var scatterAttrs = require('../scatter/attributes'),
12-
scatterMarkerAttrs = scatterAttrs.marker,
13-
scatterMarkerLineAttrs = scatterMarkerAttrs.line;
11+
var scatterAttrs = require('../scatter/attributes');
12+
var colorAttributes = require('../../components/colorscale/color_attributes');
13+
var extendFlat = require('../../lib/extend').extendFlat;
1414

15+
var scatterMarkerAttrs = scatterAttrs.marker;
16+
var scatterMarkerLineAttrs = scatterMarkerAttrs.line;
1517

1618
module.exports = {
1719
x: scatterAttrs.x,
@@ -31,26 +33,14 @@ module.exports = {
3133
'along the vertical (horizontal).'
3234
].join(' ')
3335
},
34-
marker: {
35-
color: scatterMarkerAttrs.color,
36-
colorscale: scatterMarkerAttrs.colorscale,
37-
cauto: scatterMarkerAttrs.cauto,
38-
cmax: scatterMarkerAttrs.cmax,
39-
cmin: scatterMarkerAttrs.cmin,
40-
autocolorscale: scatterMarkerAttrs.autocolorscale,
41-
reversescale: scatterMarkerAttrs.reversescale,
36+
marker: extendFlat({}, {
4237
showscale: scatterMarkerAttrs.showscale,
43-
line: {
44-
color: scatterMarkerLineAttrs.color,
45-
colorscale: scatterMarkerLineAttrs.colorscale,
46-
cauto: scatterMarkerLineAttrs.cauto,
47-
cmax: scatterMarkerLineAttrs.cmax,
48-
cmin: scatterMarkerLineAttrs.cmin,
49-
width: scatterMarkerLineAttrs.width,
50-
autocolorscale: scatterMarkerLineAttrs.autocolorscale,
51-
reversescale: scatterMarkerLineAttrs.reversescale
52-
}
53-
},
38+
line: extendFlat({},
39+
{width: scatterMarkerLineAttrs.width},
40+
colorAttributes('marker.line')
41+
)},
42+
colorAttributes('marker')
43+
),
5444

5545
r: scatterAttrs.r,
5646
t: scatterAttrs.t,

src/traces/choropleth/attributes.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var extendFlat = require('../../lib/extend').extendFlat;
1515

1616
var ScatterGeoMarkerLineAttrs = ScatterGeoAttrs.marker.line;
1717

18-
module.exports = {
18+
module.exports = extendFlat({}, {
1919
locations: {
2020
valType: 'data_array',
2121
description: [
@@ -38,17 +38,12 @@ module.exports = {
3838
width: ScatterGeoMarkerLineAttrs.width
3939
}
4040
},
41-
zauto: colorscaleAttrs.zauto,
42-
zmin: colorscaleAttrs.zmin,
43-
zmax: colorscaleAttrs.zmax,
44-
colorscale: colorscaleAttrs.colorscale,
45-
autocolorscale: colorscaleAttrs.autocolorscale,
46-
reversescale: colorscaleAttrs.reversescale,
47-
showscale: colorscaleAttrs.showscale,
4841
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
4942
flags: ['location', 'z', 'text', 'name']
5043
}),
5144
_nestedModules: {
5245
'colorbar': 'Colorbar'
5346
}
54-
};
47+
},
48+
colorscaleAttrs
49+
);

src/traces/contour/attributes.js

+95-98
Original file line numberDiff line numberDiff line change
@@ -10,116 +10,113 @@
1010

1111
var heatmapAttrs = require('../heatmap/attributes');
1212
var scatterAttrs = require('../scatter/attributes');
13+
var colorscaleAttrs = require('../../components/colorscale/attributes');
1314
var extendFlat = require('../../lib/extend').extendFlat;
1415

1516
var scatterLineAttrs = scatterAttrs.line;
1617

17-
module.exports = {
18-
z: heatmapAttrs.z,
19-
x: heatmapAttrs.x,
20-
x0: heatmapAttrs.x0,
21-
dx: heatmapAttrs.dx,
22-
y: heatmapAttrs.y,
23-
y0: heatmapAttrs.y0,
24-
dy: heatmapAttrs.dy,
25-
text: heatmapAttrs.text,
26-
transpose: heatmapAttrs.transpose,
27-
xtype: heatmapAttrs.xtype,
28-
ytype: heatmapAttrs.ytype,
18+
module.exports = extendFlat({},
19+
{
20+
z: heatmapAttrs.z,
21+
x: heatmapAttrs.x,
22+
x0: heatmapAttrs.x0,
23+
dx: heatmapAttrs.dx,
24+
y: heatmapAttrs.y,
25+
y0: heatmapAttrs.y0,
26+
dy: heatmapAttrs.dy,
27+
text: heatmapAttrs.text,
28+
transpose: heatmapAttrs.transpose,
29+
xtype: heatmapAttrs.xtype,
30+
ytype: heatmapAttrs.ytype,
2931

30-
zauto: heatmapAttrs.zauto,
31-
zmin: heatmapAttrs.zmin,
32-
zmax: heatmapAttrs.zmax,
33-
colorscale: heatmapAttrs.colorscale,
34-
autocolorscale: heatmapAttrs.autocolorscale,
35-
reversescale: heatmapAttrs.reversescale,
36-
showscale: heatmapAttrs.showscale,
32+
connectgaps: heatmapAttrs.connectgaps,
3733

38-
connectgaps: heatmapAttrs.connectgaps,
39-
40-
autocontour: {
41-
valType: 'boolean',
42-
dflt: true,
43-
role: 'style',
44-
description: [
45-
'Determines whether or not the contour level attributes are',
46-
'picked by an algorithm.',
47-
'If *true*, the number of contour levels can be set in `ncontours`.',
48-
'If *false*, set the contour level attributes in `contours`.'
49-
].join(' ')
50-
},
51-
ncontours: {
52-
valType: 'integer',
53-
dflt: 0,
54-
role: 'style',
55-
description: [
56-
'Sets the number of contour levels.',
57-
'Has an effect only if `autocontour` is *true*.'
58-
].join(' ')
59-
},
60-
61-
contours: {
62-
start: {
63-
valType: 'number',
64-
dflt: null,
65-
role: 'style',
66-
description: 'Sets the starting contour level value.'
67-
},
68-
end: {
69-
valType: 'number',
70-
dflt: null,
71-
role: 'style',
72-
description: 'Sets the end contour level value.'
73-
},
74-
size: {
75-
valType: 'number',
76-
dflt: null,
77-
role: 'style',
78-
description: 'Sets the step between each contour level.'
79-
},
80-
coloring: {
81-
valType: 'enumerated',
82-
values: ['fill', 'heatmap', 'lines', 'none'],
83-
dflt: 'fill',
34+
autocontour: {
35+
valType: 'boolean',
36+
dflt: true,
8437
role: 'style',
8538
description: [
86-
'Determines the coloring method showing the contour values.',
87-
'If *fill*, coloring is done evenly between each contour level',
88-
'If *heatmap*, a heatmap gradient coloring is applied',
89-
'between each contour level.',
90-
'If *lines*, coloring is done on the contour lines.',
91-
'If *none*, no coloring is applied on this trace.'
39+
'Determines whether or not the contour level attributes are',
40+
'picked by an algorithm.',
41+
'If *true*, the number of contour levels can be set in `ncontours`.',
42+
'If *false*, set the contour level attributes in `contours`.'
9243
].join(' ')
9344
},
94-
showlines: {
95-
valType: 'boolean',
96-
dflt: true,
45+
ncontours: {
46+
valType: 'integer',
47+
dflt: 0,
9748
role: 'style',
9849
description: [
99-
'Determines whether or not the contour lines are drawn.',
100-
'Has only an effect if `contours.coloring` is set to *fill*.'
50+
'Sets the number of contour levels.',
51+
'Has an effect only if `autocontour` is *true*.'
10152
].join(' ')
102-
}
103-
},
53+
},
10454

105-
line: {
106-
color: extendFlat({}, scatterLineAttrs.color, {
107-
description: [
108-
'Sets the color of the contour level.',
109-
'Has no if `contours.coloring` is set to *lines*.'
110-
].join(' ')
111-
}),
112-
width: scatterLineAttrs.width,
113-
dash: scatterLineAttrs.dash,
114-
smoothing: extendFlat({}, scatterLineAttrs.smoothing, {
115-
description: [
116-
'Sets the amount of smoothing for the contour lines,',
117-
'where *0* corresponds to no smoothing.'
118-
].join(' ')
119-
})
120-
},
55+
contours: {
56+
start: {
57+
valType: 'number',
58+
dflt: null,
59+
role: 'style',
60+
description: 'Sets the starting contour level value.'
61+
},
62+
end: {
63+
valType: 'number',
64+
dflt: null,
65+
role: 'style',
66+
description: 'Sets the end contour level value.'
67+
},
68+
size: {
69+
valType: 'number',
70+
dflt: null,
71+
role: 'style',
72+
description: 'Sets the step between each contour level.'
73+
},
74+
coloring: {
75+
valType: 'enumerated',
76+
values: ['fill', 'heatmap', 'lines', 'none'],
77+
dflt: 'fill',
78+
role: 'style',
79+
description: [
80+
'Determines the coloring method showing the contour values.',
81+
'If *fill*, coloring is done evenly between each contour level',
82+
'If *heatmap*, a heatmap gradient coloring is applied',
83+
'between each contour level.',
84+
'If *lines*, coloring is done on the contour lines.',
85+
'If *none*, no coloring is applied on this trace.'
86+
].join(' ')
87+
},
88+
showlines: {
89+
valType: 'boolean',
90+
dflt: true,
91+
role: 'style',
92+
description: [
93+
'Determines whether or not the contour lines are drawn.',
94+
'Has only an effect if `contours.coloring` is set to *fill*.'
95+
].join(' ')
96+
}
97+
},
98+
99+
line: {
100+
color: extendFlat({}, scatterLineAttrs.color, {
101+
description: [
102+
'Sets the color of the contour level.',
103+
'Has no if `contours.coloring` is set to *lines*.'
104+
].join(' ')
105+
}),
106+
width: scatterLineAttrs.width,
107+
dash: scatterLineAttrs.dash,
108+
smoothing: extendFlat({}, scatterLineAttrs.smoothing, {
109+
description: [
110+
'Sets the amount of smoothing for the contour lines,',
111+
'where *0* corresponds to no smoothing.'
112+
].join(' ')
113+
})
114+
},
121115

122-
_nestedModules: {
123-
'colorbar': 'Colorbar'
124-
}
125-
};
116+
_nestedModules: {
117+
'colorbar': 'Colorbar'
118+
}
119+
},
120+
colorscaleAttrs,
121+
{autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale, {dflt: false})}
122+
);

0 commit comments

Comments
 (0)