Skip to content

Commit

Permalink
Merge pull request #4356 from plotly/fix4354-contour-legends-with-col…
Browse files Browse the repository at this point in the history
…orscale

Apply reversed colorscale setting and coloraxis in contour and histogramcontour legends
  • Loading branch information
archmoj authored Nov 18, 2019
2 parents 8951847 + f988a64 commit 513491e
Show file tree
Hide file tree
Showing 25 changed files with 473 additions and 58 deletions.
61 changes: 36 additions & 25 deletions src/components/legend/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var Registry = require('../../registry');
var Lib = require('../../lib');
var Drawing = require('../drawing');
var Color = require('../color');
var extractOpts = require('../colorscale/helpers').extractOpts;

var subTypes = require('../../traces/scatter/subtypes');
var stylePie = require('../../traces/pie/style_one');
Expand All @@ -30,7 +31,7 @@ module.exports = function style(s, gd) {
var legend = fullLayout.legend;
var constantItemSizing = legend.itemsizing === 'constant';

function boundLineWidth(mlw, cont, max, cst) {
var boundLineWidth = function(mlw, cont, max, cst) {
var v;
if(mlw + 1) {
v = mlw;
Expand All @@ -40,7 +41,7 @@ module.exports = function style(s, gd) {
return 0;
}
return constantItemSizing ? cst : Math.min(v, max);
}
};

s.each(function(d) {
var traceGroup = d3.select(this);
Expand Down Expand Up @@ -104,6 +105,29 @@ module.exports = function style(s, gd) {
var showGradientFill = false;
var dMod, tMod;

var cOpts = extractOpts(trace);
var colorscale = cOpts.colorscale;
var reversescale = cOpts.reversescale;

var fillGradient = function(s) {
if(s.size()) {
var gradientID = 'legendfill-' + trace.uid;
Drawing.gradient(s, gd, gradientID,
getGradientDirection(reversescale),
colorscale, 'fill');
}
};

var lineGradient = function(s) {
if(s.size()) {
var gradientID = 'legendline-' + trace.uid;
Drawing.lineGroupStyle(s);
Drawing.gradient(s, gd, gradientID,
getGradientDirection(reversescale),
colorscale, 'stroke');
}
};

if(contours) {
var coloring = contours.coloring;

Expand Down Expand Up @@ -158,23 +182,6 @@ module.exports = function style(s, gd) {
// This issue (and workaround) exist across (Mac) Chrome, FF, and Safari
line.attr('d', pathStart + (showGradientLine ? 'l30,0.0001' : 'h30'))
.call(showLine ? Drawing.lineGroupStyle : lineGradient);

function fillGradient(s) {
if(s.size()) {
var gradientID = 'legendfill-' + trace.uid;
Drawing.gradient(s, gd, gradientID, 'horizontalreversed',
trace.colorscale, 'fill');
}
}

function lineGradient(s) {
if(s.size()) {
var gradientID = 'legendline-' + trace.uid;
Drawing.lineGroupStyle(s);
Drawing.gradient(s, gd, gradientID, 'horizontalreversed',
trace.colorscale, 'stroke');
}
}
}

function stylePoints(d) {
Expand Down Expand Up @@ -278,7 +285,7 @@ module.exports = function style(s, gd) {
var trace = d[0].trace;

var ptsData = [];
if(trace.type === 'waterfall' && trace.visible) {
if(trace.visible && trace.type === 'waterfall') {
ptsData = d[0].hasTotals ?
[['increasing', 'M-6,-6V6H0Z'], ['totals', 'M6,6H0L-6,-6H-0Z'], ['decreasing', 'M6,6V-6H0Z']] :
[['increasing', 'M-6,-6V6H6Z'], ['decreasing', 'M6,6V-6H-6Z']];
Expand Down Expand Up @@ -321,7 +328,7 @@ module.exports = function style(s, gd) {
var markerLine = marker.line || {};

var isVisible = (!desiredType) ? Registry.traceIs(trace, 'bar') :
(trace.type === desiredType && trace.visible);
(trace.visible && trace.type === desiredType);

var barpath = d3.select(lThis).select('g.legendpoints')
.selectAll('path.legend' + desiredType)
Expand All @@ -348,7 +355,7 @@ module.exports = function style(s, gd) {

var pts = d3.select(this).select('g.legendpoints')
.selectAll('path.legendbox')
.data(Registry.traceIs(trace, 'box-violin') && trace.visible ? [d] : []);
.data(trace.visible && Registry.traceIs(trace, 'box-violin') ? [d] : []);
pts.enter().append('path').classed('legendbox', true)
// if we want the median bar, prepend M6,0H-6
.attr('d', 'M6,6H-6V-6H6Z')
Expand Down Expand Up @@ -386,7 +393,7 @@ module.exports = function style(s, gd) {

var pts = d3.select(this).select('g.legendpoints')
.selectAll('path.legendcandle')
.data(trace.type === 'candlestick' && trace.visible ? [d, d] : []);
.data(trace.visible && trace.type === 'candlestick' ? [d, d] : []);
pts.enter().append('path').classed('legendcandle', true)
.attr('d', function(_, i) {
if(i) return 'M-15,0H-8M-8,6V-6H8Z'; // increasing
Expand All @@ -413,7 +420,7 @@ module.exports = function style(s, gd) {

var pts = d3.select(this).select('g.legendpoints')
.selectAll('path.legendohlc')
.data(trace.type === 'ohlc' && trace.visible ? [d, d] : []);
.data(trace.visible && trace.type === 'ohlc' ? [d, d] : []);
pts.enter().append('path').classed('legendohlc', true)
.attr('d', function(_, i) {
if(i) return 'M-15,0H0M-8,-6V0'; // increasing
Expand Down Expand Up @@ -448,7 +455,7 @@ module.exports = function style(s, gd) {
var trace = d0.trace;

var isVisible = (!desiredType) ? Registry.traceIs(trace, desiredType) :
(trace.type === desiredType && trace.visible);
(trace.visible && trace.type === desiredType);

var pts = d3.select(lThis).select('g.legendpoints')
.selectAll('path.legend' + desiredType)
Expand All @@ -472,3 +479,7 @@ module.exports = function style(s, gd) {
}
}
};

function getGradientDirection(reversescale) {
return reversescale ? 'horizontal' : 'horizontalreversed';
}
6 changes: 3 additions & 3 deletions src/traces/choropleth/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
var scatterGeoAttrs = require('../scattergeo/attributes');
var colorScaleAttrs = require('../../components/colorscale/attributes');
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var defaultLine = require('../../components/color/attributes').defaultLine;

var extendFlat = require('../../lib/extend').extendFlat;
Expand Down Expand Up @@ -73,11 +73,11 @@ module.exports = extendFlat({
editType: 'plot'
},

hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
editType: 'calc',
flags: ['location', 'z', 'text', 'name']
}),
hovertemplate: hovertemplateAttrs(),
hovertemplate: hovertemplateAttrs()
},

colorScaleAttrs('', {
Expand Down
4 changes: 2 additions & 2 deletions src/traces/densitymapbox/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

var colorScaleAttrs = require('../../components/colorscale/attributes');
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var scatterMapboxAttrs = require('../scattermapbox/attributes');

var extendFlat = require('../../lib/extend').extendFlat;
Expand Down Expand Up @@ -81,7 +81,7 @@ module.exports = extendFlat({
text: scatterMapboxAttrs.text,
hovertext: scatterMapboxAttrs.hovertext,

hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['lon', 'lat', 'z', 'text', 'name']
}),
hovertemplate: hovertemplateAttrs()
Expand Down
4 changes: 2 additions & 2 deletions src/traces/funnel/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

var barAttrs = require('../bar/attributes');
var lineAttrs = require('../scatter/attributes').line;
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
var constants = require('./constants');
Expand All @@ -30,7 +30,7 @@ module.exports = {
keys: constants.eventDataKeys
}),

hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['name', 'x', 'y', 'text', 'percent initial', 'percent previous', 'percent total']
}),

Expand Down
4 changes: 2 additions & 2 deletions src/traces/funnelarea/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'use strict';

var pieAttrs = require('../pie/attributes');
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var domainAttrs = require('../../plots/domain').attributes;
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
Expand Down Expand Up @@ -58,7 +58,7 @@ module.exports = {
keys: ['label', 'color', 'value', 'text', 'percent']
}),

hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['label', 'text', 'value', 'percent', 'name']
}),

Expand Down
4 changes: 2 additions & 2 deletions src/traces/image/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

'use strict';

var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
var extendFlat = require('../../lib/extend').extendFlat;
var colormodel = require('./constants').colormodel;
Expand Down Expand Up @@ -108,7 +108,7 @@ module.exports = extendFlat({
editType: 'plot',
description: 'Same as `text`.'
},
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['x', 'y', 'z', 'color', 'name', 'text'],
dflt: 'x+y+z+text+name'
}),
Expand Down
4 changes: 2 additions & 2 deletions src/traces/parcats/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'use strict';

var extendFlat = require('../../lib/extend').extendFlat;
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var fontAttrs = require('../../plots/font_attributes');
var colorScaleAttrs = require('../../components/colorscale/attributes');
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
Expand Down Expand Up @@ -47,7 +47,7 @@ var line = extendFlat(
module.exports = {
domain: domainAttrs({name: 'parcats', trace: true, editType: 'calc'}),

hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['count', 'probability'],
editType: 'plot',
arrayOk: false
Expand Down
4 changes: 2 additions & 2 deletions src/traces/pie/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

'use strict';

var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var domainAttrs = require('../../plots/domain').attributes;
var fontAttrs = require('../../plots/font_attributes');
var colorAttrs = require('../../components/color/attributes');
Expand Down Expand Up @@ -158,7 +158,7 @@ module.exports = {
'Determines which trace information appear on the graph.'
].join(' ')
},
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['label', 'text', 'value', 'percent', 'name']
}),
hovertemplate: hovertemplateAttrs({}, {
Expand Down
4 changes: 2 additions & 2 deletions src/traces/sankey/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'use strict';

var fontAttrs = require('../../plots/font_attributes');
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var colorAttrs = require('../../components/color/attributes');
var fxAttrs = require('../../components/fx/attributes');
var domainAttrs = require('../../plots/domain').attributes;
Expand All @@ -23,7 +23,7 @@ var overrideAll = require('../../plot_api/edit_types').overrideAll;
var FORMAT_LINK = require('../../constants/docs').FORMAT_LINK;

var attrs = module.exports = overrideAll({
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: [],
arrayOk: false,
description: [
Expand Down
4 changes: 2 additions & 2 deletions src/traces/scattercarpet/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'use strict';

var scatterAttrs = require('../scatter/attributes');
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
var colorScaleAttrs = require('../../components/colorscale/attributes');
Expand Down Expand Up @@ -118,7 +118,7 @@ module.exports = {
selected: scatterAttrs.selected,
unselected: scatterAttrs.unselected,

hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['a', 'b', 'text', 'name']
}),
hoveron: scatterAttrs.hoveron,
Expand Down
4 changes: 2 additions & 2 deletions src/traces/scattergeo/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
var scatterAttrs = require('../scatter/attributes');
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var colorAttributes = require('../../components/colorscale/attributes');
var dash = require('../../components/drawing/attributes').dash;

Expand Down Expand Up @@ -125,7 +125,7 @@ module.exports = overrideAll({
selected: scatterAttrs.selected,
unselected: scatterAttrs.unselected,

hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['lon', 'lat', 'location', 'text', 'name']
}),
hovertemplate: hovertemplateAttrs(),
Expand Down
4 changes: 2 additions & 2 deletions src/traces/scattergl/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

'use strict';

var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var scatterAttrs = require('../scatter/attributes');
var colorScaleAttrs = require('../../components/colorscale/attributes');

Expand Down Expand Up @@ -92,7 +92,7 @@ var attrs = module.exports = overrideAll({
textfont: scatterAttrs.unselected.textfont
},

opacity: plotAttrs.opacity
opacity: baseAttrs.opacity

}, 'calc', 'nested');

Expand Down
4 changes: 2 additions & 2 deletions src/traces/scattermapbox/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var texttemplateAttrs = require('../../plots/template_attributes').texttemplateA
var scatterGeoAttrs = require('../scattergeo/attributes');
var scatterAttrs = require('../scatter/attributes');
var mapboxAttrs = require('../../plots/mapbox/layout_attributes');
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var colorScaleAttrs = require('../../components/colorscale/attributes');

var extendFlat = require('../../lib/extend').extendFlat;
Expand Down Expand Up @@ -122,7 +122,7 @@ module.exports = overrideAll({
marker: scatterAttrs.unselected.marker
},

hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['lon', 'lat', 'text', 'name']
}),
hovertemplate: hovertemplateAttrs(),
Expand Down
4 changes: 2 additions & 2 deletions src/traces/scatterpolar/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplat
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
var extendFlat = require('../../lib/extend').extendFlat;
var scatterAttrs = require('../scatter/attributes');
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var lineAttrs = scatterAttrs.line;

module.exports = {
Expand Down Expand Up @@ -131,7 +131,7 @@ module.exports = {
// error_x (error_r, error_theta)
// error_y

hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['r', 'theta', 'text', 'name']
}),
hoveron: scatterAttrs.hoveron,
Expand Down
4 changes: 2 additions & 2 deletions src/traces/scatterternary/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
var scatterAttrs = require('../scatter/attributes');
var plotAttrs = require('../../plots/attributes');
var baseAttrs = require('../../plots/attributes');
var colorScaleAttrs = require('../../components/colorscale/attributes');
var dash = require('../../components/drawing/attributes').dash;

Expand Down Expand Up @@ -147,7 +147,7 @@ module.exports = {
selected: scatterAttrs.selected,
unselected: scatterAttrs.unselected,

hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
flags: ['a', 'b', 'c', 'text', 'name']
}),
hoveron: scatterAttrs.hoveron,
Expand Down
Loading

0 comments on commit 513491e

Please sign in to comment.