@@ -82,7 +82,7 @@ function plotOne(gd, plotinfo, cd) {
82
82
var plotGroup = makeContourGroup ( plotinfo , cd , id ) ;
83
83
makeBackground ( plotGroup , perimeter , contours ) ;
84
84
makeFills ( plotGroup , pathinfo , perimeter , contours ) ;
85
- makeLines ( plotGroup , pathinfo , gd , cd [ 0 ] , contours , perimeter ) ;
85
+ makeLinesAndLabels ( plotGroup , pathinfo , gd , cd [ 0 ] , contours , perimeter ) ;
86
86
clipGaps ( plotGroup , plotinfo , fullLayout . _defs , cd [ 0 ] , perimeter ) ;
87
87
}
88
88
@@ -263,7 +263,7 @@ function joinAllPaths(pi, perimeter) {
263
263
264
264
var TRAILING_ZEROS = / \. ? 0 + $ / ;
265
265
266
- function makeLines ( plotgroup , pathinfo , gd , cd0 , contours , perimeter ) {
266
+ function makeLinesAndLabels ( plotgroup , pathinfo , gd , cd0 , contours , perimeter ) {
267
267
var defs = gd . _fullLayout . _defs ;
268
268
269
269
var smoothing = pathinfo [ 0 ] . smoothing ;
@@ -329,6 +329,9 @@ function makeLines(plotgroup, pathinfo, gd, cd0, contours, perimeter) {
329
329
330
330
var labelData = [ ] ;
331
331
332
+ // invalidate the getTextLocation cache in case paths changed
333
+ Lib . clearLocationCache ( ) ;
334
+
332
335
var contourFormat ;
333
336
if ( contours . labelformat ) {
334
337
contourFormat = d3 . format ( contours . labelformat ) ;
@@ -352,8 +355,21 @@ function makeLines(plotgroup, pathinfo, gd, cd0, contours, perimeter) {
352
355
. attr ( 'data-notex' , 1 )
353
356
. call ( Drawing . font , contours . font ) ;
354
357
355
- var plotDiagonal = Math . sqrt ( Math . pow ( pathinfo [ 0 ] . xaxis . _length , 2 ) +
356
- Math . pow ( pathinfo [ 0 ] . yaxis . _length , 2 ) ) ;
358
+ var xLen = pathinfo [ 0 ] . xaxis . _length ;
359
+ var yLen = pathinfo [ 0 ] . yaxis . _length ;
360
+
361
+ // visible bounds of the contour trace (and the midpoints, to
362
+ // help with cost calculations)
363
+ var bounds = {
364
+ left : Math . max ( perimeter [ 0 ] [ 0 ] , 0 ) ,
365
+ right : Math . min ( perimeter [ 2 ] [ 0 ] , xLen ) ,
366
+ top : Math . max ( perimeter [ 0 ] [ 1 ] , 0 ) ,
367
+ bottom : Math . min ( perimeter [ 2 ] [ 1 ] , yLen )
368
+ } ;
369
+ bounds . middle = ( bounds . top + bounds . bottom ) / 2 ;
370
+ bounds . center = ( bounds . left + bounds . right ) / 2 ;
371
+
372
+ var plotDiagonal = Math . sqrt ( xLen * xLen + yLen * yLen ) ;
357
373
358
374
// the path length to use to scale the number of labels to draw:
359
375
var normLength = plotDiagonal /
@@ -470,24 +486,6 @@ function addLabel(loc, textOpts, labelData) {
470
486
return straightClosedPath ( bBoxPts ) ;
471
487
}
472
488
473
- function getLocation ( path , pathLen , positionOnPath , textOpts ) {
474
- var halfWidth = textOpts . width / 2 ;
475
-
476
- // for the angle, use points on the path separated by the text width
477
- // even though due to curvature, the text will cover a bit more than that
478
- var p0 = path . getPointAtLength ( Lib . mod ( positionOnPath - halfWidth , pathLen ) ) ;
479
- var p1 = path . getPointAtLength ( Lib . mod ( positionOnPath + halfWidth , pathLen ) ) ;
480
- // note: atan handles 1/0 nicely
481
- var theta = Math . atan ( ( p1 . y - p0 . y ) / ( p1 . x - p0 . x ) ) ;
482
- // center the text at 2/3 of the center position plus 1/3 the p0/p1 midpoint
483
- // that's the average position of this segment, assuming it's roughly quadratic
484
- var pCenter = path . getPointAtLength ( positionOnPath ) ;
485
- var x = ( pCenter . x * 4 + p0 . x + p1 . x ) / 6 ;
486
- var y = ( pCenter . y * 4 + p0 . y + p1 . y ) / 6 ;
487
-
488
- return { x : x , y : y , theta : theta } ;
489
- }
490
-
491
489
function clipGaps ( plotGroup , plotinfo , defs , cd0 , perimeter ) {
492
490
var clipId = 'clip' + cd0 . trace . uid ;
493
491
0 commit comments