Skip to content

Commit b52adf2

Browse files
committed
use contrast color with heatmap when there is no textfont.color
1 parent b8a4f3a commit b52adf2

File tree

9 files changed

+18
-4
lines changed

9 files changed

+18
-4
lines changed

src/traces/heatmap/defaults.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2929
coerce('hovertemplate');
3030
coerce('texttemplate');
3131

32-
var fontDflt = Lib.extendFlat({}, layout.font, {color: 'white'});
32+
var fontDflt = Lib.extendFlat({}, layout.font);
33+
fontDflt.color = undefined; // color contrast by default
3334
Lib.coerceFont(coerce, 'textfont', fontDflt);
3435

3536
handleStyleDefaults(traceIn, traceOut, coerce, layout);

src/traces/heatmap/plot.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Axes = require('../../plots/cartesian/axes');
99
var Lib = require('../../lib');
1010
var svgTextUtils = require('../../lib/svg_text_utils');
1111
var formatLabels = require('../scatter/format_labels');
12+
var Color = require('../../components/color');
1213
var extractOpts = require('../../components/colorscale').extractOpts;
1314
var makeColorScaleFuncFromTrace = require('../../components/colorscale').makeColorScaleFuncFromTrace;
1415
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
@@ -420,7 +421,8 @@ module.exports = function(gd, plotinfo, cdheatmaps, heatmapLayer) {
420421
textData.push({
421422
t: _t,
422423
x: _x,
423-
y: _y
424+
y: _y,
425+
z: zVal
424426
});
425427
}
426428
}
@@ -443,10 +445,19 @@ module.exports = function(gd, plotinfo, cdheatmaps, heatmapLayer) {
443445
.each(function(d) {
444446
var thisLabel = d3.select(this);
445447

448+
var fontColor = font.color;
449+
if(!fontColor) {
450+
fontColor = Color.contrast(
451+
'rgba(' +
452+
sclFunc(d.z).join() +
453+
')'
454+
);
455+
}
456+
446457
thisLabel
447458
.attr('data-notex', 1)
448459
.call(svgTextUtils.positionText, xFn(d), yFn(d))
449-
.call(Drawing.font, font.family, font.size, font.color)
460+
.call(Drawing.font, font.family, font.size, fontColor)
450461
.text(d.t)
451462
.call(svgTextUtils.convertToTspans, gd);
452463
});

test/image/baselines/13.png

9.5 KB
Loading

test/image/baselines/4.png

-125 Bytes
Loading
1.62 KB
Loading
-800 Bytes
Loading
16.8 KB
Loading

test/image/mocks/heatmap_categoryorder.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"type": "heatmap",
1616
"texttemplate": "x: %{x}<br>y: %{y}<br>z: %{z}",
1717
"textfont": {
18-
"family": "Raleway",
1918
"size": 14
2019
},
2120

test/image/mocks/zsmooth_methods.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"data": [
33
{
4+
"texttemplate": "%{z:.1f}",
45
"z": [
56
[
67
0.1189977,
@@ -31,6 +32,7 @@
3132
"yaxis": "y"
3233
},
3334
{
35+
"texttemplate": "%{z:.1f}",
3436
"z": [
3537
[
3638
0.1189977,
@@ -61,6 +63,7 @@
6163
"yaxis": "y2"
6264
},
6365
{
66+
"texttemplate": "%{z:.1f}",
6467
"z": [
6568
[
6669
0.1189977,

0 commit comments

Comments
 (0)