Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix contour label rendering for non-monotonically increasing x/y #3934

Merged
merged 2 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/traces/contour/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ exports.plot = function plot(gd, plotinfo, cdcontours, contourLayer) {
// draw everything
makeBackground(plotGroup, perimeter, contours);
makeFills(plotGroup, fillPathinfo, perimeter, contours);
makeLinesAndLabels(plotGroup, pathinfo, gd, cd0, contours, perimeter);
makeLinesAndLabels(plotGroup, pathinfo, gd, cd0, contours);
clipGaps(plotGroup, plotinfo, gd, cd0, perimeter);
});
};
Expand Down Expand Up @@ -209,7 +209,7 @@ function joinAllPaths(pi, perimeter) {
return fullpath;
}

function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours, perimeter) {
function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours) {
var lineContainer = Lib.ensureSingle(plotgroup, 'g', 'contourlines');
var showLines = contours.showlines !== false;
var showLabels = contours.showlabels;
Expand Down Expand Up @@ -244,16 +244,19 @@ function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours, perimeter) {
.attr('data-notex', 1)
.call(Drawing.font, contours.labelfont);

var _x = cd0.x.slice().sort(Lib.sorterAsc);
var _y = cd0.y.slice().sort(Lib.sorterAsc);

var xa = pathinfo[0].xaxis;
var ya = pathinfo[0].yaxis;
var xLen = xa._length;
var yLen = ya._length;
var xRng = xa.range;
var yRng = ya.range;
var x0 = Math.max(perimeter[0][0], 0);
var x1 = Math.min(perimeter[2][0], xLen);
var y0 = Math.max(perimeter[0][1], 0);
var y1 = Math.min(perimeter[2][1], yLen);
var x0 = Math.max(xa.c2p(_x[0], true), 0);
var x1 = Math.min(xa.c2p(_x[_x.length - 1], true), xLen);
var y0 = Math.max(ya.c2p(_y[_y.length - 1], true), 0);
var y1 = Math.min(ya.c2p(_y[0], true), yLen);

// visible bounds of the contour trace (and the midpoints, to
// help with cost calculations)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 112 additions & 0 deletions test/image/mocks/contour_label-reversed-xy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"data": [
{
"type": "contour",
"name": "▲ x ▲ y",
"x": [1, 2, 3, 4],
"y": [1, 2, 3, 4],
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
"contours": {"showlabels": true},
"showscale": false
},
{
"type": "contour",
"name": "▼ x ▲ y",
"x": [4, 3, 2, 1],
"y": [1, 2, 3, 4],
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
"contours": {"showlabels": true},
"showscale": false,
"xaxis": "x2",
"yaxis": "y2"
},
{
"type": "contour",
"name": "▲ x ▼ y",
"x": [1, 2, 3, 4],
"y": [4, 3, 2, 1],
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
"contours": {"showlabels": true},
"showscale": false,
"xaxis": "x3",
"yaxis": "y3"
},
{
"type": "contour",
"name": "▼ x ▼ y",
"x": [4, 3, 2, 1],
"y": [4, 3, 2, 1],
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
"contours": {"showlabels": true},
"showscale": false,
"xaxis": "x4",
"yaxis": "y4"
},

{
"type": "contour",
"name": "▲ x ▲ y (dup)",
"x": [1, 2, 3, 4],
"y": [1, 2, 3, 4],
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
"contours": {"showlabels": true},
"showscale": false,
"xaxis": "x5",
"yaxis": "y5"
},
{
"type": "contour",
"name": "▼ x (reversed rng) ▲ y",
"x": [4, 3, 2, 1],
"y": [1, 2, 3, 4],
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
"contours": {"showlabels": true},
"showscale": false,
"xaxis": "x6",
"yaxis": "y6"
},
{
"type": "contour",
"name": "▲ x ▼ y (reversed rng)",
"x": [1, 2, 3, 4],
"y": [4, 3, 2, 1],
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
"contours": {"showlabels": true},
"showscale": false,
"xaxis": "x7",
"yaxis": "y7"
},
{
"type": "contour",
"name": "▼ x (reversed rng) ▼ y (reversed rng)",
"x": [4, 3, 2, 1],
"y": [4, 3, 2, 1],
"z": [[1, 2, 3, 4], [1, 2, 3, 4], [2, 3, 4, 5], [4, 5, 6, 7]],
"contours": {"showlabels": true},
"showscale": false,
"xaxis": "x8",
"yaxis": "y8"
}
],
"layout": {
"grid": {"rows": 4, "columns": 2, "pattern": "independent"},

"template": {
"data": {
"contour": [{
"hoverlabel": {"namelength": -1}
}]
}
},

"xaxis6": {"autorange": "reversed"},

"yaxis7": {"autorange": "reversed"},

"xaxis8": {"autorange": "reversed"},
"yaxis8": {"autorange": "reversed"},

"width": 500,
"height": 800
}
}