Skip to content

Commit a27d278

Browse files
committed
Fix contrasting text color for bars without explicit marker color [2951]
1 parent 61b9d50 commit a27d278

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

src/traces/bar/plot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
176176
}
177177

178178
var layoutFont = gd._fullLayout.font;
179-
var barColor = calcTrace[i].mc;
179+
var barColor = style.getBarColor(calcTrace[i], trace);
180180
var insideTextFont = style.getInsideTextFont(trace, i, layoutFont, barColor);
181181
var outsideTextFont = style.getOutsideTextFont(trace, i, layoutFont);
182182

src/traces/bar/style.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function determineFont(tx, d, trace, gd) {
104104
var textFont = trace.textfont;
105105

106106
if(tx.classed('bartext-inside')) {
107-
var barColor = d.mc;
107+
var barColor = getBarColor(d, trace);
108108
textFont = getInsideTextFont(trace, d.i, layoutFont, barColor);
109109
} else if(tx.classed('bartext-outside')) {
110110
textFont = getOutsideTextFont(trace, d.i, layoutFont);
@@ -159,9 +159,14 @@ function getFontValue(attributeDefinition, attributeValue, index, defaultValue)
159159
};
160160
}
161161

162+
function getBarColor(cd, trace) {
163+
return cd.mc || trace.marker.color;
164+
}
165+
162166
module.exports = {
163167
style: style,
164168
styleOnSelect: styleOnSelect,
165169
getInsideTextFont: getInsideTextFont,
166-
getOutsideTextFont: getOutsideTextFont
170+
getOutsideTextFont: getOutsideTextFont,
171+
getBarColor: getBarColor
167172
};
225 Bytes
Loading

test/jasmine/tests/bar_test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,8 +1081,16 @@ describe('A bar plot', function() {
10811081
};
10821082

10831083
it('should use inside text colors contrasting to bar colors by default', function(done) {
1084-
Plotly.plot(gd, [insideTextTestsTrace])
1085-
.then(assertTextFontColors([DARK, DARK, LIGHT, LIGHT, DARK, LIGHT]))
1084+
var noMarkerTrace = Lib.extendFlat({}, insideTextTestsTrace);
1085+
delete noMarkerTrace.marker;
1086+
1087+
Plotly.plot(gd, [insideTextTestsTrace, noMarkerTrace])
1088+
.then(function() {
1089+
var trace1Colors = [DARK, DARK, LIGHT, LIGHT, DARK, LIGHT];
1090+
var trace2Colors = Lib.repeat(DARK, 6);
1091+
var allExpectedColors = trace1Colors.concat(trace2Colors);
1092+
assertTextFontColors(allExpectedColors)();
1093+
})
10861094
.catch(failTest)
10871095
.then(done);
10881096
});

0 commit comments

Comments
 (0)