Skip to content

Commit 3196777

Browse files
committed
correct pattern implementation when having colorscale on both modes
- also fixing missing bar legend color issue 5285
1 parent d6189b4 commit 3196777

File tree

9 files changed

+171
-24
lines changed

9 files changed

+171
-24
lines changed

src/components/drawing/attributes.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ exports.pattern = {
4343
arrayOk: true,
4444
editType: 'style',
4545
description: [
46-
'Sets the background color of the pattern fill.',
46+
'When there is no colorscale sets the background color of the pattern fill.',
4747
'Defaults to a `marker.color` background when `fillmode` is *overlay*.',
4848
'Otherwise, defaults to a transparent background.'
4949
].join(' ')
@@ -53,7 +53,7 @@ exports.pattern = {
5353
arrayOk: true,
5454
editType: 'style',
5555
description: [
56-
'Sets the foreground color of the pattern fill.',
56+
'When there is no colorscale sets the foreground color of the pattern fill.',
5757
'Defaults to a `marker.color` background when `fillmode` is *replace*.',
5858
'Otherwise, defaults to dark grey or white',
5959
'to increase contrast with the `bgcolor`.',
@@ -84,5 +84,8 @@ exports.pattern = {
8484
'and solidty of 1 shows only the foreground color without pattern.',
8585
].join(' ')
8686
},
87-
editType: 'style'
87+
editType: 'style',
88+
description: [
89+
'Sets the pattern within the marker.'
90+
].join(' '),
8891
};

src/components/drawing/index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,17 @@ drawing.gradient = function(sel, gd, gradientID, type, colorscale, prop) {
368368
* @param {number} solidity: how solid lines of this pattern are
369369
* @param {string} prop: the property to apply to, 'fill' or 'stroke'
370370
*/
371-
drawing.pattern = function(sel, gd, patternID, shape, bgcolor, fgcolor, size, solidity, prop) {
371+
drawing.pattern = function(sel, gd, patternID, shape, bgcolor, fgcolor, size, solidity, mcc, fillmode, prop) {
372+
if(mcc) {
373+
if(fillmode === 'overlay') {
374+
bgcolor = mcc;
375+
fgcolor = Color.contrast(bgcolor);
376+
} else {
377+
bgcolor = undefined;
378+
fgcolor = mcc;
379+
}
380+
}
381+
372382
var fullLayout = gd._fullLayout;
373383
var fullID = 'p' + fullLayout._uid + '-' + patternID;
374384
var width, height;
@@ -704,16 +714,17 @@ drawing.singlePointStyle = function(d, sel, trace, fns, gd) {
704714
var patternFGColor = drawing.getPatternAttr(markerPattern.fgcolor, d.i, null);
705715
var patternSize = drawing.getPatternAttr(markerPattern.size, d.i, 8);
706716
var patternSolidity = drawing.getPatternAttr(markerPattern.solidity, d.i, 0.3);
707-
var perPointPattern = Lib.isArrayOrTypedArray(markerPattern.shape) ||
708-
Lib.isArrayOrTypedArray(markerPattern.bgcolor) ||
709-
Lib.isArrayOrTypedArray(markerPattern.size) ||
710-
Lib.isArrayOrTypedArray(markerPattern.solidity);
717+
var perPointPattern = d.mcc ||
718+
Lib.isArrayOrTypedArray(markerPattern.shape) ||
719+
Lib.isArrayOrTypedArray(markerPattern.bgcolor) ||
720+
Lib.isArrayOrTypedArray(markerPattern.size) ||
721+
Lib.isArrayOrTypedArray(markerPattern.solidity);
711722

712723
var patternID = trace.uid;
713724
if(perPointPattern) patternID += '-' + d.i;
714725

715726
drawing.pattern(sel, gd, patternID, patternShape, patternBGColor, patternFGColor,
716-
patternSize, patternSolidity, 'fill');
727+
patternSize, patternSolidity, d.mcc, markerPattern.fillmode, 'fill');
717728
} else {
718729
Color.fill(sel, fillColor);
719730
}

src/components/legend/style.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,11 @@ module.exports = function style(s, gd, legend) {
348348

349349
p.style('stroke-width', w + 'px');
350350

351-
var fillColor = d0.mc || marker.color;
351+
var mcc;
352+
if('mc' in d0) {
353+
mcc = Drawing.tryColorscale(marker, '')(d0.mc);
354+
}
355+
var fillColor = mcc || d0.mc || marker.color;
352356

353357
var markerPattern = marker.pattern;
354358
var patternShape = markerPattern && Drawing.getPatternAttr(markerPattern.shape, 0, '');
@@ -360,7 +364,7 @@ module.exports = function style(s, gd, legend) {
360364
var patternSolidity = Drawing.getPatternAttr(markerPattern.solidity, 0, 0.3);
361365
var patternID = 'legend-' + trace.uid;
362366
p.call(Drawing.pattern, gd, patternID, patternShape, patternBGColor,
363-
patternFGColor, patternSize, patternSolidity, 'fill');
367+
patternFGColor, patternSize, patternSolidity, mcc, markerPattern.fillmode, 'fill');
364368
} else {
365369
p.call(Color.fill, fillColor);
366370
}

src/lib/coerce.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -428,23 +428,25 @@ exports.coerceFont = function(coerce, attr, dfltObj) {
428428
/*
429429
* Shortcut to coerce the pattern attributes
430430
*/
431-
exports.coercePattern = function(coerce, attr, markerColor) {
431+
exports.coercePattern = function(coerce, attr, markerColor, hasMarkerColorscale) {
432432
var shape = coerce(attr + '.shape');
433433
if(shape) {
434434
coerce(attr + '.solidity');
435435
coerce(attr + '.size');
436436
var fillmode = coerce(attr + '.fillmode');
437437
var isOverlay = fillmode === 'overlay';
438438

439-
var bgcolor = coerce(attr + '.bgcolor', isOverlay ?
440-
markerColor :
441-
undefined
442-
);
439+
if(!hasMarkerColorscale) {
440+
var bgcolor = coerce(attr + '.bgcolor', isOverlay ?
441+
markerColor :
442+
undefined
443+
);
443444

444-
coerce(attr + '.fgcolor', isOverlay ?
445-
Color.contrast(bgcolor) :
446-
markerColor
447-
);
445+
coerce(attr + '.fgcolor', isOverlay ?
446+
Color.contrast(bgcolor) :
447+
markerColor
448+
);
449+
}
448450
}
449451
};
450452

src/traces/bar/style_defaults.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ var coercePattern = require('../../lib').coercePattern;
77

88
module.exports = function handleStyleDefaults(traceIn, traceOut, coerce, defaultColor, layout) {
99
var markerColor = coerce('marker.color', defaultColor);
10-
11-
if(hasColorscale(traceIn, 'marker')) {
10+
var hasMarkerColorscale = hasColorscale(traceIn, 'marker');
11+
if(hasMarkerColorscale) {
1212
colorscaleDefaults(
1313
traceIn, traceOut, layout, coerce, {prefix: 'marker.', cLetter: 'c'}
1414
);
@@ -24,8 +24,7 @@ module.exports = function handleStyleDefaults(traceIn, traceOut, coerce, default
2424

2525
coerce('marker.line.width');
2626
coerce('marker.opacity');
27-
coercePattern(coerce, 'marker.pattern', markerColor);
28-
27+
coercePattern(coerce, 'marker.pattern', markerColor, hasMarkerColorscale);
2928
coerce('selected.marker.color');
3029
coerce('unselected.marker.color');
3130
};

tasks/test_mock.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ function notBlackListed(name) {
227227
'ohlc_first',
228228
'pattern_bars',
229229
'pattern_fgcolor_overlay_fillmode',
230+
'pattern_with_colorscale',
230231
'plot_types',
231232
'polar_blank',
232233
'polar_dates',
Loading
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"data": [
3+
{
4+
"legendrank": 4,
5+
"x": ["a", "b", "c", "d"],
6+
"y": [4, 3, 2, 1],
7+
"name": "replace bar",
8+
"type": "bar",
9+
"textposition": "outside",
10+
"marker": {
11+
"line": { "color": "lightblue", "width": 2 },
12+
"color": [4, 3, 2, 1],
13+
"pattern": {
14+
"fillmode": "replace",
15+
"shape": "/"
16+
}
17+
}
18+
},
19+
{
20+
"legendrank": 3,
21+
"xaxis": "x2",
22+
"yaxis": "y2",
23+
"x": ["a", "b", "c", "d"],
24+
"y": [4, 3, 2, 1],
25+
"name": "overlay bar",
26+
"type": "bar",
27+
"textposition": "outside",
28+
"marker": {
29+
"line": { "color": "lightblue", "width": 2 },
30+
"color": [4, 3, 2, 1],
31+
"pattern": {
32+
"fillmode": "overlay",
33+
"shape": "/"
34+
}
35+
}
36+
},
37+
38+
{
39+
"legendrank": 2,
40+
"t": ["M", "N", "O", "P"],
41+
"r": [4, 3, 2, 1],
42+
"type": "barpolar",
43+
"name": "replace polar",
44+
"marker": {
45+
"line": { "color": "lightblue", "width": 2 },
46+
"color": [4, 3, 2, 1],
47+
"pattern": {
48+
"fillmode": "replace",
49+
"shape": "."
50+
}
51+
}
52+
},
53+
54+
{
55+
"legendrank": 1,
56+
"subplot": "polar2",
57+
"t": ["M", "N", "O", "P"],
58+
"r": [4, 3, 2, 1],
59+
"type": "barpolar",
60+
"name": "overlay polar",
61+
"marker": {
62+
"line": { "color": "lightblue", "width": 2 },
63+
"color": [4, 3, 2, 1],
64+
"pattern": {
65+
"fillmode": "overlay",
66+
"shape": "."
67+
}
68+
}
69+
}
70+
],
71+
"layout": {
72+
"title": {
73+
"text": "pattern with colorscale and different fillmodes"
74+
},
75+
"width": 600,
76+
"height": 600,
77+
78+
"xaxis": {
79+
"domain": [0, 1]
80+
},
81+
"yaxis": {
82+
"domain": [0, 0.3]
83+
},
84+
85+
"xaxis2": {
86+
"anchor": "y2",
87+
"domain": [0, 1]
88+
},
89+
"yaxis2": {
90+
"anchor": "x2",
91+
"domain": [0.35, 0.65]
92+
},
93+
94+
"polar": {
95+
"domain": {
96+
"x": [0, 0.475],
97+
"y": [0.7, 1]
98+
}
99+
},
100+
101+
"polar2": {
102+
"domain": {
103+
"x": [0.525, 1],
104+
"y": [0.7, 1]
105+
}
106+
}
107+
}
108+
}

test/jasmine/tests/bar_test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@ describe('Bar.supplyDefaults', function() {
294294
expect(traceOut.marker.pattern.bgcolor).toBe('green');
295295
expect(traceOut.marker.pattern.fgcolor).toBe('#fff');
296296
});
297+
298+
it('should not coerce marker.pattern.bgcolor and marker.pattern.fgcolor when marker.colorscale is present', function() {
299+
traceIn = {
300+
marker: {
301+
colorscale: 'Blues',
302+
pattern: {
303+
shape: '+'
304+
}
305+
},
306+
color: [1, 2, 3],
307+
y: [1, 2, 3]
308+
};
309+
var layout = {};
310+
311+
supplyDefaults(traceIn, traceOut, defaultColor, layout);
312+
313+
expect(traceOut.marker.pattern.bgcolor).toBeUndefined();
314+
expect(traceOut.marker.pattern.fgcolor).toBeUndefined();
315+
});
297316
});
298317

299318
describe('bar calc / crossTraceCalc (formerly known as setPositions)', function() {

0 commit comments

Comments
 (0)