Skip to content

Commit fc6486d

Browse files
authored
Merge pull request #4540 from plotly/contour-label-extra-pad-and-correct-minus-sign
Contour label extra pad and correct minus sign
2 parents 9c7c398 + fd0b9fb commit fc6486d

19 files changed

+138
-25
lines changed

src/traces/contour/plot.js

+32-25
Original file line numberDiff line numberDiff line change
@@ -394,21 +394,21 @@ exports.labelFormatter = function(gd, cd0) {
394394
var trace = cd0.trace;
395395
var contours = trace.contours;
396396

397+
var formatAxis = {
398+
type: 'linear',
399+
_id: 'ycontour',
400+
showexponent: 'all',
401+
exponentformat: 'B'
402+
};
403+
397404
if(contours.labelformat) {
398-
return fullLayout._d3locale.numberFormat(contours.labelformat);
405+
formatAxis.tickformat = contours.labelformat;
406+
setConvert(formatAxis, fullLayout);
399407
} else {
400-
var formatAxis;
401408
var cOpts = Colorscale.extractOpts(trace);
402409
if(cOpts && cOpts.colorbar && cOpts.colorbar._axis) {
403410
formatAxis = cOpts.colorbar._axis;
404411
} else {
405-
formatAxis = {
406-
type: 'linear',
407-
_id: 'ycontour',
408-
showexponent: 'all',
409-
exponentformat: 'B'
410-
};
411-
412412
if(contours.type === 'constraint') {
413413
var value = contours.value;
414414
if(Array.isArray(value)) {
@@ -429,22 +429,24 @@ exports.labelFormatter = function(gd, cd0) {
429429
formatAxis._tmin = null;
430430
formatAxis._tmax = null;
431431
}
432-
return function(v) {
433-
return Axes.tickText(formatAxis, v).text;
434-
};
435432
}
433+
434+
return function(v) { return Axes.tickText(formatAxis, v).text; };
436435
};
437436

438437
exports.calcTextOpts = function(level, contourFormat, dummyText, gd) {
439438
var text = contourFormat(level);
440439
dummyText.text(text)
441440
.call(svgTextUtils.convertToTspans, gd);
442-
var bBox = Drawing.bBox(dummyText.node(), true);
441+
442+
var el = dummyText.node();
443+
var bBox = Drawing.bBox(el, true);
443444

444445
return {
445446
text: text,
446447
width: bBox.width,
447448
height: bBox.height,
449+
fontSize: +(el.style['font-size'].replace('px', '')),
448450
level: level,
449451
dy: (bBox.top + bBox.bottom) / 2
450452
};
@@ -544,24 +546,29 @@ function locationCost(loc, textOpts, labelData, bounds) {
544546
}
545547

546548
exports.addLabelData = function(loc, textOpts, labelData, labelClipPathData) {
547-
var halfWidth = textOpts.width / 2;
548-
var halfHeight = textOpts.height / 2;
549+
var fontSize = textOpts.fontSize;
550+
var w = textOpts.width + fontSize / 3;
551+
var h = Math.max(0, textOpts.height - fontSize / 3);
549552

550553
var x = loc.x;
551554
var y = loc.y;
552555
var theta = loc.theta;
553556

554557
var sin = Math.sin(theta);
555558
var cos = Math.cos(theta);
556-
var dxw = halfWidth * cos;
557-
var dxh = halfHeight * sin;
558-
var dyw = halfWidth * sin;
559-
var dyh = -halfHeight * cos;
559+
560+
var rotateXY = function(dx, dy) {
561+
return [
562+
x + dx * cos - dy * sin,
563+
y + dx * sin + dy * cos
564+
];
565+
};
566+
560567
var bBoxPts = [
561-
[x - dxw - dxh, y - dyw - dyh],
562-
[x + dxw - dxh, y + dyw - dyh],
563-
[x + dxw + dxh, y + dyw + dyh],
564-
[x - dxw + dxh, y - dyw + dyh],
568+
rotateXY(-w / 2, -h / 2),
569+
rotateXY(-w / 2, h / 2),
570+
rotateXY(w / 2, h / 2),
571+
rotateXY(w / 2, -h / 2)
565572
];
566573

567574
labelData.push({
@@ -571,8 +578,8 @@ exports.addLabelData = function(loc, textOpts, labelData, labelClipPathData) {
571578
dy: textOpts.dy,
572579
theta: theta,
573580
level: textOpts.level,
574-
width: textOpts.width,
575-
height: textOpts.height
581+
width: w,
582+
height: h
576583
});
577584

578585
labelClipPathData.push(bBoxPts);

test/image/baselines/cheater.png

45 Bytes
Loading
-669 Bytes
Loading
68 Bytes
Loading
Loading
-150 Bytes
Loading
Loading
Loading
Loading
-552 Bytes
Loading
Loading
-62 Bytes
Loading
566 Bytes
Loading
40 Bytes
Loading
-381 Bytes
Loading
Loading
Loading
1.87 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"data": [
3+
{
4+
"type": "contour",
5+
"x": [1, 2, 3, 4],
6+
"y": [1, 2, 3, 4],
7+
"z": [
8+
[-1, -2, -4, -8],
9+
[-2, -4, -8, -1],
10+
[-4, -8, -1, -2],
11+
[-8, -1, -2, -4]
12+
],
13+
"contours": {"showlabels": true, "labelfont": {"size": 4}},
14+
"showscale": false
15+
},
16+
{
17+
"type": "contour",
18+
"x": [1, 2, 3, 4],
19+
"y": [1, 2, 3, 4],
20+
"z": [
21+
[-1, -2, -4, -8],
22+
[-2, -4, -8, -1],
23+
[-4, -8, -1, -2],
24+
[-8, -1, -2, -4]
25+
],
26+
"contours": {"showlabels": true, "labelfont": {"size": 8}},
27+
"showscale": false,
28+
"xaxis": "x2",
29+
"yaxis": "y2"
30+
},
31+
{
32+
"type": "contour",
33+
"x": [1, 2, 3, 4],
34+
"y": [1, 2, 3, 4],
35+
"z": [
36+
[-1, -2, -4, -8],
37+
[-2, -4, -8, -1],
38+
[-4, -8, -1, -2],
39+
[-8, -1, -2, -4]
40+
],
41+
"contours": {"showlabels": true, "labelfont": {"size": 12}},
42+
"showscale": false,
43+
"xaxis": "x3",
44+
"yaxis": "y3"
45+
},
46+
{
47+
"type": "contour",
48+
"x": [1, 2, 3, 4],
49+
"y": [1, 2, 3, 4],
50+
"z": [
51+
[-1, -2, -4, -8],
52+
[-2, -4, -8, -1],
53+
[-4, -8, -1, -2],
54+
[-8, -1, -2, -4]
55+
],
56+
"contours": {"showlabels": true, "labelfont": {"size": 16}},
57+
"showscale": false,
58+
"xaxis": "x4",
59+
"yaxis": "y4"
60+
},
61+
{
62+
"type": "contour",
63+
"x": [1, 2, 3, 4],
64+
"y": [1, 2, 3, 4],
65+
"z": [
66+
[-1, -2, -4, -8],
67+
[-2, -4, -8, -1],
68+
[-4, -8, -1, -2],
69+
[-8, -1, -2, -4]
70+
],
71+
"contours": {"showlabels": true, "labelfont": {"size": 20}},
72+
"showscale": false,
73+
"xaxis": "x5",
74+
"yaxis": "y5"
75+
},
76+
{
77+
"type": "contour",
78+
"x": [1, 2, 3, 4],
79+
"y": [1, 2, 3, 4],
80+
"z": [
81+
[-1, -2, -4, -8],
82+
[-2, -4, -8, -1],
83+
[-4, -8, -1, -2],
84+
[-8, -1, -2, -4]
85+
],
86+
"contours": {"showlabels": true, "labelfont": {"size": 24}},
87+
"showscale": false,
88+
"xaxis": "x6",
89+
"yaxis": "y6"
90+
}
91+
],
92+
"layout": {
93+
"grid": {"rows": 3, "columns": 2, "pattern": "independent"},
94+
95+
"template": {
96+
"data": {
97+
"contour": [{
98+
"hoverlabel": {"namelength": -1}
99+
}]
100+
}
101+
},
102+
103+
"width": 800,
104+
"height": 1200
105+
}
106+
}

0 commit comments

Comments
 (0)