diff --git a/src/traces/contour/plot.js b/src/traces/contour/plot.js index 808786cf409..78fd46147b9 100644 --- a/src/traces/contour/plot.js +++ b/src/traces/contour/plot.js @@ -394,21 +394,21 @@ exports.labelFormatter = function(gd, cd0) { var trace = cd0.trace; var contours = trace.contours; + var formatAxis = { + type: 'linear', + _id: 'ycontour', + showexponent: 'all', + exponentformat: 'B' + }; + if(contours.labelformat) { - return fullLayout._d3locale.numberFormat(contours.labelformat); + formatAxis.tickformat = contours.labelformat; + setConvert(formatAxis, fullLayout); } else { - var formatAxis; var cOpts = Colorscale.extractOpts(trace); if(cOpts && cOpts.colorbar && cOpts.colorbar._axis) { formatAxis = cOpts.colorbar._axis; } else { - formatAxis = { - type: 'linear', - _id: 'ycontour', - showexponent: 'all', - exponentformat: 'B' - }; - if(contours.type === 'constraint') { var value = contours.value; if(Array.isArray(value)) { @@ -429,22 +429,24 @@ exports.labelFormatter = function(gd, cd0) { formatAxis._tmin = null; formatAxis._tmax = null; } - return function(v) { - return Axes.tickText(formatAxis, v).text; - }; } + + return function(v) { return Axes.tickText(formatAxis, v).text; }; }; exports.calcTextOpts = function(level, contourFormat, dummyText, gd) { var text = contourFormat(level); dummyText.text(text) .call(svgTextUtils.convertToTspans, gd); - var bBox = Drawing.bBox(dummyText.node(), true); + + var el = dummyText.node(); + var bBox = Drawing.bBox(el, true); return { text: text, width: bBox.width, height: bBox.height, + fontSize: +(el.style['font-size'].replace('px', '')), level: level, dy: (bBox.top + bBox.bottom) / 2 }; @@ -544,8 +546,9 @@ function locationCost(loc, textOpts, labelData, bounds) { } exports.addLabelData = function(loc, textOpts, labelData, labelClipPathData) { - var halfWidth = textOpts.width / 2; - var halfHeight = textOpts.height / 2; + var fontSize = textOpts.fontSize; + var w = textOpts.width + fontSize / 3; + var h = Math.max(0, textOpts.height - fontSize / 3); var x = loc.x; var y = loc.y; @@ -553,15 +556,19 @@ exports.addLabelData = function(loc, textOpts, labelData, labelClipPathData) { var sin = Math.sin(theta); var cos = Math.cos(theta); - var dxw = halfWidth * cos; - var dxh = halfHeight * sin; - var dyw = halfWidth * sin; - var dyh = -halfHeight * cos; + + var rotateXY = function(dx, dy) { + return [ + x + dx * cos - dy * sin, + y + dx * sin + dy * cos + ]; + }; + var bBoxPts = [ - [x - dxw - dxh, y - dyw - dyh], - [x + dxw - dxh, y + dyw - dyh], - [x + dxw + dxh, y + dyw + dyh], - [x - dxw + dxh, y - dyw + dyh], + rotateXY(-w / 2, -h / 2), + rotateXY(-w / 2, h / 2), + rotateXY(w / 2, h / 2), + rotateXY(w / 2, -h / 2) ]; labelData.push({ @@ -571,8 +578,8 @@ exports.addLabelData = function(loc, textOpts, labelData, labelClipPathData) { dy: textOpts.dy, theta: theta, level: textOpts.level, - width: textOpts.width, - height: textOpts.height + width: w, + height: h }); labelClipPathData.push(bBoxPts); diff --git a/test/image/baselines/cheater.png b/test/image/baselines/cheater.png index adf96eaf62f..74b22b32610 100644 Binary files a/test/image/baselines/cheater.png and b/test/image/baselines/cheater.png differ diff --git a/test/image/baselines/cheater_contour.png b/test/image/baselines/cheater_contour.png index 8c7c33b9e57..b8581160880 100644 Binary files a/test/image/baselines/cheater_contour.png and b/test/image/baselines/cheater_contour.png differ diff --git a/test/image/baselines/contour_constraints.png b/test/image/baselines/contour_constraints.png index 4bc2f1f41d8..d921e3d4385 100644 Binary files a/test/image/baselines/contour_constraints.png and b/test/image/baselines/contour_constraints.png differ diff --git a/test/image/baselines/contour_constraints_equal_boundary_minmax.png b/test/image/baselines/contour_constraints_equal_boundary_minmax.png index d144612d734..d76c3213940 100644 Binary files a/test/image/baselines/contour_constraints_equal_boundary_minmax.png and b/test/image/baselines/contour_constraints_equal_boundary_minmax.png differ diff --git a/test/image/baselines/contour_edge_cases.png b/test/image/baselines/contour_edge_cases.png index 81d1e4be56b..b4d3425f9c6 100644 Binary files a/test/image/baselines/contour_edge_cases.png and b/test/image/baselines/contour_edge_cases.png differ diff --git a/test/image/baselines/contour_label-font-size.png b/test/image/baselines/contour_label-font-size.png new file mode 100644 index 00000000000..38f9d68a28d Binary files /dev/null and b/test/image/baselines/contour_label-font-size.png differ diff --git a/test/image/baselines/contour_label-formatting-via-colorbar.png b/test/image/baselines/contour_label-formatting-via-colorbar.png index 5b3ca64cc98..9e45af1c48a 100644 Binary files a/test/image/baselines/contour_label-formatting-via-colorbar.png and b/test/image/baselines/contour_label-formatting-via-colorbar.png differ diff --git a/test/image/baselines/contour_label-reversed-axes.png b/test/image/baselines/contour_label-reversed-axes.png index fde7fc16683..57f56d2affa 100644 Binary files a/test/image/baselines/contour_label-reversed-axes.png and b/test/image/baselines/contour_label-reversed-axes.png differ diff --git a/test/image/baselines/contour_label-reversed-xy.png b/test/image/baselines/contour_label-reversed-xy.png index fc1446ec253..92baaa3b7b2 100644 Binary files a/test/image/baselines/contour_label-reversed-xy.png and b/test/image/baselines/contour_label-reversed-xy.png differ diff --git a/test/image/baselines/contour_label-thousands-suffix.png b/test/image/baselines/contour_label-thousands-suffix.png index 9d9fa9ebb2c..c764dd458c5 100644 Binary files a/test/image/baselines/contour_label-thousands-suffix.png and b/test/image/baselines/contour_label-thousands-suffix.png differ diff --git a/test/image/baselines/contour_legend.png b/test/image/baselines/contour_legend.png index 34fb9616813..7282772aa4e 100644 Binary files a/test/image/baselines/contour_legend.png and b/test/image/baselines/contour_legend.png differ diff --git a/test/image/baselines/contour_lines_coloring.png b/test/image/baselines/contour_lines_coloring.png index 9b2fc5ca51f..7201cb2cac3 100644 Binary files a/test/image/baselines/contour_lines_coloring.png and b/test/image/baselines/contour_lines_coloring.png differ diff --git a/test/image/baselines/contour_nolines.png b/test/image/baselines/contour_nolines.png index 63b6b77f733..4dafc470d15 100644 Binary files a/test/image/baselines/contour_nolines.png and b/test/image/baselines/contour_nolines.png differ diff --git a/test/image/baselines/contour_scatter.png b/test/image/baselines/contour_scatter.png index aba6976d3d8..d6e5d0a84f5 100644 Binary files a/test/image/baselines/contour_scatter.png and b/test/image/baselines/contour_scatter.png differ diff --git a/test/image/baselines/contour_transposed-irregular.png b/test/image/baselines/contour_transposed-irregular.png index 71a8a98538c..f1be5145665 100644 Binary files a/test/image/baselines/contour_transposed-irregular.png and b/test/image/baselines/contour_transposed-irregular.png differ diff --git a/test/image/baselines/heatmap_contour_irregular_bricks.png b/test/image/baselines/heatmap_contour_irregular_bricks.png index 84a3a9ccb85..ac8d1a0a74b 100644 Binary files a/test/image/baselines/heatmap_contour_irregular_bricks.png and b/test/image/baselines/heatmap_contour_irregular_bricks.png differ diff --git a/test/image/baselines/heatmap_multicategory.png b/test/image/baselines/heatmap_multicategory.png index fd9d8ec5c93..f8066bdedbf 100644 Binary files a/test/image/baselines/heatmap_multicategory.png and b/test/image/baselines/heatmap_multicategory.png differ diff --git a/test/image/mocks/contour_label-font-size.json b/test/image/mocks/contour_label-font-size.json new file mode 100644 index 00000000000..5d67ea6d0d6 --- /dev/null +++ b/test/image/mocks/contour_label-font-size.json @@ -0,0 +1,106 @@ +{ + "data": [ + { + "type": "contour", + "x": [1, 2, 3, 4], + "y": [1, 2, 3, 4], + "z": [ + [-1, -2, -4, -8], + [-2, -4, -8, -1], + [-4, -8, -1, -2], + [-8, -1, -2, -4] + ], + "contours": {"showlabels": true, "labelfont": {"size": 4}}, + "showscale": false + }, + { + "type": "contour", + "x": [1, 2, 3, 4], + "y": [1, 2, 3, 4], + "z": [ + [-1, -2, -4, -8], + [-2, -4, -8, -1], + [-4, -8, -1, -2], + [-8, -1, -2, -4] + ], + "contours": {"showlabels": true, "labelfont": {"size": 8}}, + "showscale": false, + "xaxis": "x2", + "yaxis": "y2" + }, + { + "type": "contour", + "x": [1, 2, 3, 4], + "y": [1, 2, 3, 4], + "z": [ + [-1, -2, -4, -8], + [-2, -4, -8, -1], + [-4, -8, -1, -2], + [-8, -1, -2, -4] + ], + "contours": {"showlabels": true, "labelfont": {"size": 12}}, + "showscale": false, + "xaxis": "x3", + "yaxis": "y3" + }, + { + "type": "contour", + "x": [1, 2, 3, 4], + "y": [1, 2, 3, 4], + "z": [ + [-1, -2, -4, -8], + [-2, -4, -8, -1], + [-4, -8, -1, -2], + [-8, -1, -2, -4] + ], + "contours": {"showlabels": true, "labelfont": {"size": 16}}, + "showscale": false, + "xaxis": "x4", + "yaxis": "y4" + }, + { + "type": "contour", + "x": [1, 2, 3, 4], + "y": [1, 2, 3, 4], + "z": [ + [-1, -2, -4, -8], + [-2, -4, -8, -1], + [-4, -8, -1, -2], + [-8, -1, -2, -4] + ], + "contours": {"showlabels": true, "labelfont": {"size": 20}}, + "showscale": false, + "xaxis": "x5", + "yaxis": "y5" + }, + { + "type": "contour", + "x": [1, 2, 3, 4], + "y": [1, 2, 3, 4], + "z": [ + [-1, -2, -4, -8], + [-2, -4, -8, -1], + [-4, -8, -1, -2], + [-8, -1, -2, -4] + ], + "contours": {"showlabels": true, "labelfont": {"size": 24}}, + "showscale": false, + "xaxis": "x6", + "yaxis": "y6" + } + ], + "layout": { + "grid": {"rows": 3, "columns": 2, "pattern": "independent"}, + + "template": { + "data": { + "contour": [{ + "hoverlabel": {"namelength": -1} + }] + } + }, + + "width": 800, + "height": 1200 + } +}