8000 Contour line labels by alexcjohnson · Pull Request #1815 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Contour line labels #1815

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

Merged
merged 39 commits into from
Jun 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
468ef5d
put all contour lines into one group (so we can clip it later)
alexcjohnson Jun 21, 2017
88cc33c
collect contour clipPath in the main <defs>
alexcjohnson Jun 21, 2017
ee7a839
more standard format for contour constants
alexcjohnson Jun 22, 2017
b56f4ea
move lineIntersect -> Lib.geometry2d
alexcjohnson Jun 22, 2017
d534fb2
segmentDistance and tests of geometry2d
alexcjohnson Jun 22, 2017
d1e3448
contour line label basic functionality
alexcjohnson Jun 22, 2017
b8c75c0
better test of alternate Drawing.font syntax
alexcjohnson Jun 23, 2017
2f3a712
use contour/attributes in contourcarpet/attributes
alexcjohnson Jun 23, 2017
6299303
pull path measurement routines out into Lib
alexcjohnson Jun 23, 2017
99f39f8
allow contour labels even without lines
alexcjohnson Jun 23, 2017
d518c83
simple contour label position optimization
alexcjohnson Jun 23, 2017
27f8cfe
use axis formatting for contour labels
alexcjohnson Jun 23, 2017
221b124
include label attrs in contourcarpet to avoid errors but they're noops
alexcjohnson Jun 23, 2017
ca1cced
location -> loc (window global potential confusion)
alexcjohnson Jun 26, 2017
d52cfba
scale min path length for contour labels to width + height
alexcjohnson Jun 26, 2017
0f37648
:cow2: remove obsolete regexp
alexcjohnson Jun 26, 2017
62156dd
:hocho: long-obsolete cache object
alexcjohnson Jun 27, 2017
539ca22
Drawing.bBox "inTester" mode for quicker testing of dummy elements
alexcjohnson Jun 27, 2017
2116a12
contours.font -> contours.labelfont
alexcjohnson Jun 27, 2017
e1880c1
more permissive Drawing.bBox test
alexcjohnson Jun 27, 2017
14c832e
image tests of contour labels
alexcjohnson Jun 27, 2017
1bbdf59
contour line-colored label test
alexcjohnson Jun 27, 2017
e5a2f91
use `editTypes` for contour/contourcarpet-specific attributes
alexcjohnson Jun 27, 2017
cbdf795
no editType nesting - just docalc on editing full containers
alexcjohnson Jun 27, 2017
4db549c
:hocho: obsolete import
alexcjohnson Jun 27, 2017
fb4b690
refactor contour/plot for smaller functions
alexcjohnson Jun 29, 2017
b0f9bb2
updated contour_edge_cases baseline
alexcjohnson Jun 29, 2017
e1caa9e
unify contour.style and contourcarpet.style
alexcjohnson Jun 29, 2017
0ff2dc4
cover contour edge cases with contourcarpet
alexcjohnson Jun 29, 2017
f5873f1
fix contour style when coloring=none
alexcjohnson Jun 29, 2017
b5a674f
a little more stuff contourcarpet can inherit from contour
alexcjohnson Jun 29, 2017
1f85a6f
fix carpet plot clip path
alexcjohnson Jun 30, 2017
6c2043e
:hocho: some obsolete code in carpet/plot
alexcjohnson Jun 30, 2017
bbb9574
update histogram2dcontour for contour.plot API change
alexcjohnson Jun 30, 2017
9495dee
nest scattercontour lines inside a contourlines group
alexcjohnson Jun 30, 2017
7f086e0
fixup font->labelfont in contourcarpet
alexcjohnson Jun 30, 2017
49b8141
labels on contourcarpet
alexcjohnson Jun 30, 2017
8934038
update comment on contourcarpet label bounds
alexcjohnson Jun 30, 2017
dbeecd0
update baselines for findBestTextLocation bugfix
alexcjohnson Jun 30, 2017
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
Prev Previous commit
Next Next commit
pull path measurement routines out into Lib
  • Loading branch information
alexcjohnson committed Jun 23, 2017
commit 6299303f19d37a0b4ecfbf0cd12f6624163064af
93 changes: 93 additions & 0 deletions src/lib/geometry2d.js
8000
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

'use strict';

var mod = require('./mod');

/*
* look for intersection of two line segments
* (1->2 and 3->4) - returns array [x,y] if they do, null if not
Expand Down Expand Up @@ -81,3 +83,94 @@ function perpDistance2(xab, yab, l2_ab, xac, yac) {
return crossProduct * crossProduct / l2_ab;
}
}

// a very short-term cache for getTextLocation, just because
// we're often looping over the same locations multiple times
// invalidated as soon as we look at a different path
var locationCache, workingPath, workingTextWidth;

// turn a path and position along it into x, y, and angle for the given text
exports.getTextLocation = function getTextLocation(path, totalPathLen, positionOnPath, textWidth) {
if(path !== workingPath || textWidth !== workingTextWidth) {
locationCache = {};
workingPath = path;
workingTextWidth = textWidth;
}
if(locationCache[positionOnPath]) {
return locationCache[positionOnPath];
}

// for the angle, use points on the path separated by the text width
// even though due to curvature, the text will cover a bit more than that
var p0 = path.getPointAtLength(mod(positionOnPath - textWidth / 2, totalPathLen));
var p1 = path.getPointAtLength(mod(positionOnPath + textWidth / 2, totalPathLen));
// note: atan handles 1/0 nicely
var theta = Math.atan((p1.y - p0.y) / (p1.x - p0.x));
// center the text at 2/3 of the center position plus 1/3 the p0/p1 midpoint
// that's the average position of this segment, assuming it's roughly quadratic
var pCenter = path.getPointAtLength(mod(positionOnPath, totalPathLen));
var x = (pCenter.x * 4 + p0.x + p1.x) / 6;
var y = (pCenter.y * 4 + p0.y + p1.y) / 6;

var out = {x: x, y: y, theta: theta};
locationCache[positionOnPath] = out;
return out;
};

exports.clearLocationCache = function() {
workingPath = null;
};

/*
* Find the segment of `path` that's within the visible area
* given by `bounds` {left, right, top, bottom}, to within a
* precision of `buffer` px
*
* returns {
* min: position where the path first enters bounds, or 0 if it
* starts within bounds
* max: position where the path last exits bounds, or the path length
* if it finishes within bounds
* total: the total path length - just included so the caller doesn't
* need to call path.getTotalLength() again
* }
*
* Works by starting from either end and repeatedly finding the distance from
* that point to the plot area, and if it's outside the plot, moving along the
* path by that distance (because the plot must be at least that far away on
* the path). Note that if a path enters, exits, and re-enters the plot, we
* will not capture this behavior.
*/
exports.getVisibleSegment = function getVisibleSegment(path, bounds, buffer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man, I've implemented something similar to this before. It's such a pain!

var left = bounds.left;
var right = bounds.right;
var top = bounds.top;
var bottom = bounds.bottom;

function getDistToPlot(len) {
var pt = path.getPointAtLength(len);
var dx = (pt.x < left) ? left - pt.x : (pt.x > right ? pt.x - right : 0);
var dy = (pt.y < top) ? top - pt.y : (pt.y > bottom ? pt.y - bottom : 0);
return Math.sqrt(dx * dx + dy * dy);
}

var pMin = 0;
var pTotal = path.getTotalLength();
var pMax = pTotal;

var distToPlot = getDistToPlot(pMin);
while(distToPlot) {
pMin += distToPlot + buffer;
if(pMin > pMax) return;
distToPlot = getDistToPlot(pMin);
}

distToPlot = getDistToPlot(pMax);
while(distToPlot) {
pMax -= distToPlot + buffer;
if(pMin > pMax) return;
distToPlot = getDistToPlot(pMax);
}

return {min: pMin, max: pMax, total: pTotal};
};
3 changes: 3 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ lib.apply2DTransform2 = matrixModule.apply2DTransform2;
var geom2dModule = require('./geometry2d');
lib.segmentsIntersect = geom2dModule.segmentsIntersect;
lib.segmentDistance = geom2dModule.segmentDistance;
lib.getTextLocation = geom2dModule.getTextLocation;
lib.clearLocationCache = geom2dModule.clearLocationCache;
lib.getVisibleSegment = geom2dModule.getVisibleSegment;

var extendModule = require('./extend');
lib.extendFlat = extendModule.extendFlat;
Expand Down
42 changes: 20 additions & 22 deletions src/traces/contour/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function plotOne(gd, plotinfo, cd) {
var plotGroup = makeContourGroup(plotinfo, cd, id);
makeBackground(plotGroup, perimeter, contours);
makeFills(plotGroup, pathinfo, perimeter, contours);
makeLines(plotGroup, pathinfo, gd, cd[0], contours, perimeter);
makeLinesAndLabels(plotGroup, pathinfo, gd, cd[0], contours, perimeter);
clipGaps(plotGroup, plotinfo, fullLayout._defs, cd[0], perimeter);
}

Expand Down Expand Up @@ -263,7 +263,7 @@ function joinAllPaths(pi, perimeter) {

var TRAILING_ZEROS = /\.?0+$/;

function makeLines(plotgroup, pathinfo, gd, cd0, contours, perimeter) {
function makeLinesAndLabels(plotgroup, pathinfo, gd, cd0, contours, perimeter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except for the innermost join content, it looks like mostly d3 operations so is probably okay, but this function comes out to 240 lines, which seems a bit on the high side.

var defs = gd._fullLayout._defs;

var smoothing = pathinfo[0].smoothing;
Expand Down Expand Up @@ -329,6 +329,9 @@ function makeLines(plotgroup, pathinfo, gd, cd0, contours, perimeter) {

var labelData = [];

// invalidate the getTextLocation cache in case paths changed
Lib.clearLocationCache();

var contourFormat;
if(contours.labelformat) {
contourFormat = d3.format(contours.labelformat);
Expand All @@ -352,8 +355,21 @@ function makeLines(plotgroup, pathinfo, gd, cd0, contours, perimeter) {
.attr('data-notex', 1)
.call(Drawing.font, contours.font);

var plotDiagonal = Math.sqrt(Math.pow(pathinfo[0].xaxis._length, 2) +
Math.pow(pathinfo[0].yaxis._length, 2));
var xLen = pathinfo[0].xaxis._length;
var yLen = pathinfo[0].yaxis._length;

// visible bounds of the contour trace (and the midpoints, to
// help with cost calculations)
var bounds = {
left: Math.max(perimeter[0][0], 0),
right: Math.min(perimeter[2][0], xLen),
top: Math.max(perimeter[0][1], 0),
bottom: Math.min(perimeter[2][1], yLen)
};
bounds.middle = (bounds.top + bounds.bottom) / 2;
bounds.center = (bounds.left + bounds.right) / 2;

var plotDiagonal = Math.sqrt(xLen * xLen + yLen * yLen);

// the path length to use to scale the number of labels to draw:
var normLength = plotDiagonal /
Expand Down Expand Up @@ -470,24 +486,6 @@ function addLabel(loc, textOpts, labelData) {
return straightClosedPath(bBoxPts);
}

function getLocation(path, pathLen, positionOnPath, textOpts) {
var halfWidth = textOpts.width / 2;

// for the angle, use points on the path separated by the text width
// even though due to curvature, the text will cover a bit more than that
var p0 = path.getPointAtLength(Lib.mod(positionOnPath - halfWidth, pathLen));
var p1 = path.getPointAtLength(Lib.mod(positionOnPath + halfWidth, pathLen));
// note: atan handles 1/0 nicely
var theta = Math.atan((p1.y - p0.y) / (p1.x - p0.x));
// center the text at 2/3 of the center position plus 1/3 the p0/p1 midpoint
// that's the average position of this segment, assuming it's roughly quadratic
var pCenter = path.getPointAtLength(positionOnPath);
var x = (pCenter.x * 4 + p0.x + p1.x) / 6;
var y = (pCenter.y * 4 + p0.y + p1.y) / 6;

return {x: x, y: y, theta: theta};
}

function clipGaps(plotGroup, plotinfo, defs, cd0, perimeter) {
var clipId = 'clip' + cd0.trace.uid;

Expand Down
0