Skip to content

DRY up color attributes #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 7, 2016
14 changes: 0 additions & 14 deletions src/components/colorscale/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

'use strict';


module.exports = {
zauto: {
valType: 'boolean',
Expand Down Expand Up @@ -68,18 +67,5 @@ module.exports = {
description: [
'Determines whether or not a colorbar is displayed for this trace.'
].join(' ')
},

_deprecated: {
scl: {
valType: 'colorscale',
role: 'style',
description: 'Renamed to `colorscale`.'
},
reversescl: {
valType: 'boolean',
role: 'style',
description: 'Renamed to `reversescale`.'
}
}
};
78 changes: 78 additions & 0 deletions src/components/colorscale/color_attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright 2012-2016, 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 colorScaleAttributes = require('./attributes');
var extendDeep = require('../../lib/extend').extendDeep;

module.exports = function makeColorScaleAttributes(context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

return {
color: {
valType: 'color',
arrayOk: true,
role: 'style',
description: [
'Sets the ', context, ' color. It accepts either a specific color',
' or an array of values that are mapped to the colorscale',
' relative to the max and min values of the array or relative to',
' `cmin` and `cmax` if set.'
].join('')
},
colorscale: extendDeep({}, colorScaleAttributes.colorscale, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extendFlat would suffice here, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes in general I might have been overly defensive with extendDeep. Should I convert all these extendDeep calls to extendFlat? It would work unless someone mutated the attribute object by accident.

description: [
'Sets the colorscale and only has an effect',
' if `', context, '.color` is set to a numerical array.',
' The colorscale must be an array containing',
' arrays mapping a normalized value to an',
' rgb, rgba, hex, hsl, hsv, or named color string.',
' At minimum, a mapping for the lowest (0) and highest (1)',
' values are required. For example,',
' `[[0, \'rgb(0,0,255)\', [1, \'rgb(255,0,0)\']]`.',
' To control the bounds of the colorscale in color space,',
' use `', context, '.cmin` and `', context, '.cmax`.'
].join('')
}),
cauto: extendDeep({}, colorScaleAttributes.zauto, {
description: [
'Has an effect only if `', context, '.color` is set to a numerical array.',
' Determines the whether or not the color domain is computed',
' automatically.'
].join('')
}),
cmax: extendDeep({}, colorScaleAttributes.zmax, {
description: [
'Has an effect only if `', context, '.color` is set to a numerical array.',
' Sets the upper bound of the color domain.',
' Value should be associated to the `', context, '.color` array index,',
' and if set, `', context, '.cmin` must be set as well.'
].join('')
}),
cmin: extendDeep({}, colorScaleAttributes.zmin, {
description: [
'Has an effect only if `', context, '.color` is set to a numerical array.',
' Sets the lower bound of the color domain.',
' Value should be associated to the `', context, '.color` array index,',
' and if set, `', context, '.cmax` must be set as well.'
].join('')
}),
autocolorscale: extendDeep({}, colorScaleAttributes.autocolorscale, {
description: [
'Has an effect only if `', context, '.color` is set to a numerical array.',
' Determines whether or not the colorscale is picked using',
' values inside `', context, '.color`.'
].join('')
}),
reversescale: extendDeep({}, colorScaleAttributes.reversescale, {
description: [
'Has an effect only if `', context, '.color` is set to a numerical array.',
' Reverses the colorscale.'
].join('')
})
};
};
34 changes: 12 additions & 22 deletions src/traces/bar/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

'use strict';

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

var scatterMarkerAttrs = scatterAttrs.marker;
var scatterMarkerLineAttrs = scatterMarkerAttrs.line;

module.exports = {
x: scatterAttrs.x,
Expand All @@ -31,26 +33,14 @@ module.exports = {
'along the vertical (horizontal).'
].join(' ')
},
marker: {
color: scatterMarkerAttrs.color,
colorscale: scatterMarkerAttrs.colorscale,
cauto: scatterMarkerAttrs.cauto,
cmax: scatterMarkerAttrs.cmax,
cmin: scatterMarkerAttrs.cmin,
autocolorscale: scatterMarkerAttrs.autocolorscale,
reversescale: scatterMarkerAttrs.reversescale,
marker: extendFlat({}, {
showscale: scatterMarkerAttrs.showscale,
line: {
color: scatterMarkerLineAttrs.color,
colorscale: scatterMarkerLineAttrs.colorscale,
cauto: scatterMarkerLineAttrs.cauto,
cmax: scatterMarkerLineAttrs.cmax,
cmin: scatterMarkerLineAttrs.cmin,
width: scatterMarkerLineAttrs.width,
autocolorscale: scatterMarkerLineAttrs.autocolorscale,
reversescale: scatterMarkerLineAttrs.reversescale
}
},
line: extendFlat({},
{width: scatterMarkerLineAttrs.width},
colorAttributes('marker.line')
)},
colorAttributes('marker')
),

r: scatterAttrs.r,
t: scatterAttrs.t,
Expand Down
13 changes: 4 additions & 9 deletions src/traces/choropleth/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var extendFlat = require('../../lib/extend').extendFlat;

var ScatterGeoMarkerLineAttrs = ScatterGeoAttrs.marker.line;

module.exports = {
module.exports = extendFlat({}, {
locations: {
valType: 'data_array',
description: [
Expand All @@ -38,17 +38,12 @@ module.exports = {
width: ScatterGeoMarkerLineAttrs.width
}
},
zauto: colorscaleAttrs.zauto,
zmin: colorscaleAttrs.zmin,
zmax: colorscaleAttrs.zmax,
colorscale: colorscaleAttrs.colorscale,
autocolorscale: colorscaleAttrs.autocolorscale,
reversescale: colorscaleAttrs.reversescale,
showscale: colorscaleAttrs.showscale,
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
flags: ['location', 'z', 'text', 'name']
}),
_nestedModules: {
'colorbar': 'Colorbar'
}
};
},
colorscaleAttrs
);
193 changes: 95 additions & 98 deletions src/traces/contour/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,116 +10,113 @@

var heatmapAttrs = require('../heatmap/attributes');
var scatterAttrs = require('../scatter/attributes');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var extendFlat = require('../../lib/extend').extendFlat;

var scatterLineAttrs = scatterAttrs.line;

module.exports = {
z: heatmapAttrs.z,
x: heatmapAttrs.x,
x0: heatmapAttrs.x0,
dx: heatmapAttrs.dx,
y: heatmapAttrs.y,
y0: heatmapAttrs.y0,
dy: heatmapAttrs.dy,
text: heatmapAttrs.text,
transpose: heatmapAttrs.transpose,
xtype: heatmapAttrs.xtype,
ytype: heatmapAttrs.ytype,
module.exports = extendFlat({},
{
z: heatmapAttrs.z,
x: heatmapAttrs.x,
x0: heatmapAttrs.x0,
dx: heatmapAttrs.dx,
y: heatmapAttrs.y,
y0: heatmapAttrs.y0,
dy: heatmapAttrs.dy,
text: heatmapAttrs.text,
transpose: heatmapAttrs.transpose,
xtype: heatmapAttrs.xtype,
ytype: heatmapAttrs.ytype,

zauto: heatmapAttrs.zauto,
zmin: heatmapAttrs.zmin,
zmax: heatmapAttrs.zmax,
colorscale: heatmapAttrs.colorscale,
autocolorscale: heatmapAttrs.autocolorscale,
reversescale: heatmapAttrs.reversescale,
showscale: heatmapAttrs.showscale,
connectgaps: heatmapAttrs.connectgaps,

connectgaps: heatmapAttrs.connectgaps,

autocontour: {
valType: 'boolean',
dflt: true,
role: 'style',
description: [
'Determines whether or not the contour level attributes are',
'picked by an algorithm.',
'If *true*, the number of contour levels can be set in `ncontours`.',
'If *false*, set the contour level attributes in `contours`.'
].join(' ')
},
ncontours: {
valType: 'integer',
dflt: 0,
role: 'style',
description: [
'Sets the number of contour levels.',
'Has an effect only if `autocontour` is *true*.'
].join(' ')
},

contours: {
start: {
valType: 'number',
dflt: null,
role: 'style',
description: 'Sets the starting contour level value.'
},
end: {
valType: 'number',
dflt: null,
role: 'style',
description: 'Sets the end contour level value.'
},
size: {
valType: 'number',
dflt: null,
role: 'style',
description: 'Sets the step between each contour level.'
},
coloring: {
valType: 'enumerated',
values: ['fill', 'heatmap', 'lines', 'none'],
dflt: 'fill',
autocontour: {
valType: 'boolean',
dflt: true,
role: 'style',
description: [
'Determines the coloring method showing the contour values.',
'If *fill*, coloring is done evenly between each contour level',
'If *heatmap*, a heatmap gradient coloring is applied',
'between each contour level.',
'If *lines*, coloring is done on the contour lines.',
'If *none*, no coloring is applied on this trace.'
'Determines whether or not the contour level attributes are',
'picked by an algorithm.',
'If *true*, the number of contour levels can be set in `ncontours`.',
'If *false*, set the contour level attributes in `contours`.'
].join(' ')
},
showlines: {
valType: 'boolean',
dflt: true,
ncontours: {
valType: 'integer',
dflt: 0,
role: 'style',
description: [
'Determines whether or not the contour lines are drawn.',
'Has only an effect if `contours.coloring` is set to *fill*.'
'Sets the number of contour levels.',
'Has an effect only if `autocontour` is *true*.'
].join(' ')
}
},
},

line: {
color: extendFlat({}, scatterLineAttrs.color, {
description: [
'Sets the color of the contour level.',
'Has no if `contours.coloring` is set to *lines*.'
].join(' ')
}),
width: scatterLineAttrs.width,
dash: scatterLineAttrs.dash,
smoothing: extendFlat({}, scatterLineAttrs.smoothing, {
description: [
'Sets the amount of smoothing for the contour lines,',
'where *0* corresponds to no smoothing.'
].join(' ')
})
},
contours: {
start: {
valType: 'number',
dflt: null,
role: 'style',
description: 'Sets the starting contour level value.'
},
end: {
valType: 'number',
dflt: null,
role: 'style',
description: 'Sets the end contour level value.'
},
size: {
valType: 'number',
dflt: null,
role: 'style',
description: 'Sets the step between each contour level.'
},
coloring: {
valType: 'enumerated',
values: ['fill', 'heatmap', 'lines', 'none'],
dflt: 'fill',
role: 'style',
description: [
'Determines the coloring method showing the contour values.',
'If *fill*, coloring is done evenly between each contour level',
'If *heatmap*, a heatmap gradient coloring is applied',
'between each contour level.',
'If *lines*, coloring is done on the contour lines.',
'If *none*, no coloring is applied on this trace.'
].join(' ')
},
showlines: {
valType: 'boolean',
dflt: true,
role: 'style',
description: [
'Determines whether or not the contour lines are drawn.',
'Has only an effect if `contours.coloring` is set to *fill*.'
].join(' ')
}
},

line: {
color: extendFlat({}, scatterLineAttrs.color, {
description: [
'Sets the color of the contour level.',
'Has no if `contours.coloring` is set to *lines*.'
].join(' ')
}),
width: scatterLineAttrs.width,
dash: scatterLineAttrs.dash,
smoothing: extendFlat({}, scatterLineAttrs.smoothing, {
description: [
'Sets the amount of smoothing for the contour lines,',
'where *0* corresponds to no smoothing.'
].join(' ')
})
},

_nestedModules: {
'colorbar': 'Colorbar'
}
};
_nestedModules: {
'colorbar': 'Colorbar'
}
},
colorscaleAttrs,
{autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale, {dflt: false})}
);
Loading