Skip to content

Commit 487f73e

Browse files
committed
Use contrasting inside text color for pie by default [2951]
1 parent cabd0be commit 487f73e

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

src/traces/pie/defaults.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
6060

6161
if(hasInside || hasOutside) {
6262
var dfltFont = coerceFont(coerce, 'textfont', layout.font);
63-
if(hasInside) coerceFont(coerce, 'insidetextfont', dfltFont);
63+
if(hasInside) {
64+
var insideTextFontDefault = Lib.extendFlat({}, dfltFont);
65+
var isTraceTextfontColorSet = traceIn.textfont && traceIn.textfont.color;
66+
var isColorInheritedFromLayoutFont = !isTraceTextfontColorSet;
67+
if(isColorInheritedFromLayoutFont) {
68+
delete insideTextFontDefault.color;
69+
}
70+
coerceFont(coerce, 'insidetextfont', insideTextFontDefault);
71+
}
6472
if(hasOutside) coerceFont(coerce, 'outsidetextfont', dfltFont);
6573
}
6674
}

src/traces/pie/plot.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ module.exports = function plot(gd, cdpie) {
264264
'text-anchor': 'middle'
265265
})
266266
.call(Drawing.font, textPosition === 'outside' ?
267-
trace.outsidetextfont : trace.insidetextfont)
267+
trace.outsidetextfont : determineInsideTextFont(trace, pt))
268268
.call(svgTextUtils.convertToTspans, gd);
269269

270270
// position the text relative to the slice
@@ -418,10 +418,10 @@ function prerenderTitles(cdpie, gd) {
418418

419419
if(trace.title) {
420420
var dummyTitle = Drawing.tester.append('text')
421-
.attr('data-notex', 1)
422-
.text(trace.title)
423-
.call(Drawing.font, trace.titlefont)
424-
.call(svgTextUtils.convertToTspans, gd);
421+
.attr('data-notex', 1)
422+
.text(trace.title)
423+
.call(Drawing.font, trace.titlefont)
424+
.call(svgTextUtils.convertToTspans, gd);
425425
var bBox = Drawing.bBox(dummyTitle.node(), true);
426426
cd0.titleBox = {
427427
width: bBox.width,
@@ -432,6 +432,11 @@ function prerenderTitles(cdpie, gd) {
432432
}
433433
}
434434

435+
function determineInsideTextFont(trace, pt) {
436+
var contrast = Color.contrast(pt.color);
437+
return Lib.extendFlat({}, { color: contrast }, trace.insidetextfont);
438+
}
439+
435440
function transformInsideText(textBB, pt, cd0) {
436441
var textDiameter = Math.sqrt(textBB.width * textBB.width + textBB.height * textBB.height);
437442
var textAspect = textBB.width / textBB.height;

test/jasmine/tests/pie_test.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ var click = require('../assets/click');
99
var getClientPosition = require('../assets/get_client_position');
1010
var mouseEvent = require('../assets/mouse_event');
1111
var supplyAllDefaults = require('../assets/supply_defaults');
12+
var color = require('../../../src/components/color');
13+
var rgb = color.rgb;
1214

1315
var customAssertions = require('../assets/custom_assertions');
1416
var assertHoverLabelStyle = customAssertions.assertHoverLabelStyle;
1517
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
1618

1719
var SLICES_SELECTOR = '.slice path';
20+
var SLICES_TEXT_SELECTOR = '.pielayer text.slicetext';
1821
var LEGEND_ENTRIES_SELECTOR = '.legendpoints path';
1922

2023
describe('Pie defaults', function() {
21-
function _supply(trace) {
24+
function _supply(trace, layout) {
2225
var gd = {
2326
data: [trace],
24-
layout: {}
27+
layout: layout || {}
2528
};
2629

2730
supplyAllDefaults(gd);
@@ -59,11 +62,24 @@ describe('Pie defaults', function() {
5962
out = _supply({type: 'pie', labels: ['A', 'B'], values: []});
6063
expect(out.visible).toBe(false);
6164
});
65+
66+
it('does not apply layout.font.color to insidetextfont.color (it\'ll be contrasting instead)', function() {
67+
var out = _supply({type: 'pie', values: [1, 2]}, {font: {color: 'blue'}});
68+
expect(out.insidetextfont.color).toBe(undefined);
69+
});
70+
71+
it('does apply textfont.color to insidetextfont.color', function() {
72+
var out = _supply({type: 'pie', values: [1, 2], textfont: {color: 'blue'}});
73+
expect(out.insidetextfont.color).toBe('blue');
74+
});
6275
});
6376

6477
describe('Pie traces', function() {
6578
'use strict';
6679

80+
var DARK = color.rgb('#444');
81+
var LIGHT = color.rgb('#fff');
82+
6783
var gd;
6884

6985
beforeEach(function() { gd = createGraphDiv(); });
@@ -149,6 +165,14 @@ describe('Pie traces', function() {
149165
};
150166
}
151167

168+
function _checkFontColors(expFontColors) {
169+
return function() {
170+
d3.selectAll(SLICES_TEXT_SELECTOR).each(function(d, i) {
171+
expect(this.style.fill).toBe(expFontColors[i], i);
172+
});
173+
};
174+
}
175+
152176
it('propagate explicit colors to the same labels in earlier OR later traces', function(done) {
153177
var data1 = [
154178
{type: 'pie', values: [3, 2], marker: {colors: ['red', 'black']}, domain: {x: [0.5, 1]}},
@@ -465,6 +489,36 @@ describe('Pie traces', function() {
465489
.catch(failTest)
466490
.then(done);
467491
});
492+
493+
var insideTextTestsTraceDef = {
494+
values: [6, 5, 4, 3, 2, 1],
495+
type: 'pie',
496+
marker: {
497+
colors: ['#ee1', '#eee', '#333', '#9467bd', '#dda', '#922'],
498+
}
499+
};
500+
501+
it('should use inside text colors contrasting to slice colors by default', function(done) {
502+
Plotly.plot(gd, [insideTextTestsTraceDef])
503+
.then(_checkFontColors([DARK, DARK, LIGHT, LIGHT, DARK, LIGHT]))
504+
.catch(failTest)
505+
.then(done);
506+
});
507+
508+
it('should use textfont.color for inside text instead of the contrasting default', function(done) {
509+
var data = Lib.extendFlat({}, insideTextTestsTraceDef, { textfont: { color: 'red' } });
510+
Plotly.plot(gd, [data])
511+
.then(_checkFontColors(Lib.repeat(rgb('red'), 6)))
512+
.catch(failTest)
513+
.then(done);
514+
});
515+
516+
it('should not use layout.font.color for inside text, but a contrasting color instead', function(done) {
517+
Plotly.plot(gd, [insideTextTestsTraceDef], {font: {color: 'green'}})
518+
.then(_checkFontColors([DARK, DARK, LIGHT, LIGHT, DARK, LIGHT]))
519+
.catch(failTest)
520+
.then(done);
521+
});
468522
});
469523

470524
describe('pie hovering', function() {

0 commit comments

Comments
 (0)