@@ -1024,6 +1024,14 @@ axes.calcTicks = function calcTicks(ax, opts) {
10241024 // the labels are placed at minor ticks (even if invisible) instead of major ticks.
10251025 var calcMinor = hasMinor || ax . _useTicklabelIndex ;
10261026
1027+ // Number of hidden major steps past each range end. A label can sit |ticklabelindex| ticks
1028+ // away from its major tick, and its period needs one more tick to end.
1029+ // The cap keeps a huge ticklabelindex from exhausting the tick budget.
1030+ let helperSteps = 0 ;
1031+ if ( isPeriod ) {
1032+ const maxLabelIndex = ax . _useTicklabelIndex ? Math . max ( 0 , ...Array . from ( ticklabelIndex , Math . abs ) ) : 0 ;
1033+ helperSteps = Math . min ( maxLabelIndex + 1 , Math . floor ( maxTicks / 2 ) ) ;
1034+ }
10271035 // calc major first
10281036 for ( var major = 1 ; major >= ( calcMinor ? 0 : 1 ) ; major -- ) {
10291037 var isMinor = ! major ;
@@ -1067,9 +1075,11 @@ axes.calcTicks = function calcTicks(ax, opts) {
10671075 var exRng = expandRange ( rng ) ;
10681076 var startTick = exRng [ 0 ] ;
10691077 var endTick = exRng [ 1 ] ;
1078+ const visibleEndTick = endTick ; // Ticks past this end are hidden helper ticks for period labels
1079+ var dtick = mockAx . dtick ;
10701080
1071- var numDtick = isNumeric ( mockAx . dtick ) ;
1072- var isDLog = ( type === 'log' ) && ! ( numDtick || mockAx . dtick . charAt ( 0 ) === 'L' ) ;
1081+ const numDtick = isNumeric ( dtick ) ;
1082+ const isDLog = type === 'log' && ! ( numDtick || dtick . charAt ( 0 ) === 'L' ) ;
10731083
10741084 // find the first tick
10751085 var x0 = axes . tickFirst ( mockAx , opts ) ;
@@ -1113,7 +1123,6 @@ axes.calcTicks = function calcTicks(ax, opts) {
11131123 ) / _dTick ) - 1 ;
11141124 }
11151125
1116- var dtick = mockAx . dtick ;
11171126 var tick0l = ( type === 'linear' ) ? mockAx . r2l ( mockAx . tick0 ) : undefined ;
11181127
11191128 if ( mockAx . rangebreaks && mockAx . _tick0Init !== mockAx . tick0 ) {
@@ -1124,13 +1133,20 @@ axes.calcTicks = function calcTicks(ax, opts) {
11241133 }
11251134 }
11261135
1127- if ( ( major || ax . _useTicklabelIndex ) && isPeriod ) {
1128- // if major: add one item to label period before tick0
1129- // if minor: add one item for ticklabelindex positioning. positionPeriodTicks requires
1130- // at least 2 ticks to calculate the period length, so we add a dummy tick, ensuring
1131- // that if a tick is labeled, there are always at least 2 ticks.
1132- x = axes . tickIncrement ( x , dtick , ! axrev , calendar ) ;
1133- if ( major ) majorId -- ;
1136+ if ( major && helperSteps ) {
1137+ // Hidden helper ticks outside the range carry the labels of the periods that the range cuts
1138+ for ( let k = 0 ; k < helperSteps ; k ++ ) {
1139+ x = axes . tickIncrement ( x , dtick , ! axrev , calendar ) ;
1140+ if ( ax . _useTicklabelIndex ) endTick = axes . tickIncrement ( endTick , dtick , axrev , calendar ) ;
1141+ }
1142+ majorId -= helperSteps ;
1143+ } else if ( isMinor && helperSteps && ax . _useTicklabelIndex && tickVals . length ) {
1144+ // Minor ticks span the major ticks, on their own grid
1145+ const firstMajor = tickVals [ 0 ] . value ;
1146+ while ( axrev ? x < firstMajor : x > firstMajor ) {
1147+ x = axes . tickIncrement ( x , dtick , ! axrev , calendar ) ;
1148+ }
1149+ endTick = tickVals [ tickVals . length - 1 ] . value ;
11341150 }
11351151
11361152 for ( ;
@@ -1155,11 +1171,16 @@ axes.calcTicks = function calcTicks(ax, opts) {
11551171
11561172 // prevent infinite loops - no more than one tick per pixel,
11571173 // and make sure each value is different from the previous
1158- if ( tickVals . length > maxTicks || x === prevX ) break ;
1174+ if ( tickVals . length > maxTicks + 2 * helperSteps || x === prevX ) break ;
11591175 prevX = x ;
11601176
11611177 var obj = { value : snapToGrid ( x , tick0l , dtick ) } ;
11621178
1179+ // mark ticks that were only added for period label positioning as "noTick" so they aren't drawn.
1180+ if ( axrev ? x > startTick || x < visibleEndTick : x < startTick || x > visibleEndTick ) {
1181+ obj . noTick = true ;
1182+ }
1183+
11631184 if ( major ) {
11641185 // mark first major tick, for showexponent/showtickprefix/showticksuffix 'first'
11651186 if ( x === x0 ) obj . first = true ;
@@ -1197,16 +1218,16 @@ axes.calcTicks = function calcTicks(ax, opts) {
11971218 } ;
11981219
11991220 // check if ticklabelIndex makes sense, otherwise ignore it.
1200- // It makes sense if in addition to the always present dummy, there are at least 2 minor ticks
1201- // with the required distance to each other.
1202- if ( ! minorTickVals || minorTickVals . length < 3 ) {
1203- ax . _useTicklabelIndex = false ;
1204- } else {
1205- const diff = ( minorTickVals [ 2 ] . value - minorTickVals [ 1 ] . value ) * ( isReversed ? - 1 : 1 ) ;
1206- if ( ! periodCompatibleWithTickformat ( diff , ax . tickformat ) ) {
1221+ // It makes sense if there are at least 2 visible minor ticks with the required distance to each other.
1222+ if ( ax . _useTicklabelIndex ) {
1223+ const visibleMinorTicks = minorTickVals . filter ( ( minorTick ) => ! minorTick . noTick ) ;
1224+ if ( visibleMinorTicks . length < 2 ) {
12071225 ax . _useTicklabelIndex = false ;
1208- // remove previously added tick before tick0 for handling ticklabelindex positioning
1209- minorTickVals = minorTickVals . slice ( 1 ) ;
1226+ } else {
1227+ const diff = ( visibleMinorTicks [ 1 ] . value - visibleMinorTicks [ 0 ] . value ) * ( isReversed ? - 1 : 1 ) ;
1228+ if ( ! periodCompatibleWithTickformat ( diff , ax . tickformat ) ) {
1229+ ax . _useTicklabelIndex = false ;
1230+ }
12101231 }
12111232 }
12121233
@@ -1223,10 +1244,17 @@ axes.calcTicks = function calcTicks(ax, opts) {
12231244 const labelTickValsAscending = tickVals . concat ( filteredMinors )
12241245 . sort ( ( a , b ) => a . value - b . value ) ;
12251246
1226- if ( isPeriod && tickVals . length ) {
1227- // first major tick was just added for period handling
1228- tickVals [ 0 ] . skipLabel = true ;
1229- }
1247+ // A hidden tick gets a label only if its period ends on a visible tick.
1248+ // Sub-hour periods get no periodX, so setTickLabel cannot hide their out-of-range labels.
1249+ const addTicklabel = ( ticks , index ) => {
1250+ const tickToLabel = ticks [ index ] ;
1251+ const periodEndTick = isPeriod ? ticks [ index + 1 ] : undefined ;
1252+ if ( tickToLabel . noTick && ! ( periodEndTick && ! periodEndTick . noTick ) ) return ;
1253+
1254+ allTicklabelVals . push ( tickToLabel ) ;
1255+ if ( periodEndTick ) tickToLabel . periodEndTick = periodEndTick ;
1256+ } ;
1257+
12301258 tickVals . forEach ( function ( majorTick ) {
12311259 if ( ! majorTick . skipLabel ) {
12321260 ticklabelIndex . forEach ( ( labelIndex ) => {
@@ -1235,20 +1263,12 @@ axes.calcTicks = function calcTicks(ax, opts) {
12351263 const absLabelIndex = Math . abs ( labelIndex ) ;
12361264 const labeledTickIndex = smallerTicks . length - absLabelIndex - 1 ;
12371265 if ( absLabelIndex < smallerTicks . length ) {
1238- const tickToLabel = smallerTicks [ labeledTickIndex ] ;
1239- allTicklabelVals . push ( tickToLabel ) ;
1240- if ( isPeriod ) {
1241- tickToLabel . periodEndTick = smallerTicks [ labeledTickIndex + 1 ] ;
1242- }
1266+ addTicklabel ( smallerTicks , labeledTickIndex ) ;
12431267 }
12441268 } else { // labelIndex >= 0
12451269 const largerTicks = labelTickValsAscending . filter ( ( minorTick ) => minorTick . value >= majorTick . value ) ;
12461270 if ( labelIndex < largerTicks . length ) {
1247- const tickToLabel = largerTicks [ labelIndex ] ;
1248- allTicklabelVals . push ( tickToLabel ) ;
1249- if ( isPeriod && largerTicks [ labelIndex + 1 ] ) {
1250- tickToLabel . periodEndTick = largerTicks [ labelIndex + 1 ] ;
1251- }
1271+ addTicklabel ( largerTicks , labelIndex ) ;
12521272 }
12531273 }
12541274 } ) ;
@@ -1367,8 +1387,9 @@ axes.calcTicks = function calcTicks(ax, opts) {
13671387 if ( ax . _useTicklabelIndex && allTicklabelVals . indexOf ( tickVals [ i ] ) !== - 1 ) {
13681388 t = setTickLabel ( ax , tickVals [ i ] ) ;
13691389 } else {
1370- t = { x : _value } ;
1390+ t = { x : _value , text : '' } ;
13711391 }
1392+ t . noTick = tickVals [ i ] . noTick ;
13721393 t . minor = true ;
13731394 minorTicks . push ( t ) ;
13741395 } else {
@@ -1377,24 +1398,14 @@ axes.calcTicks = function calcTicks(ax, opts) {
13771398 if ( tickVals [ i ] . skipLabel ) {
13781399 hideLabel ( t ) ;
13791400 }
1380-
1401+ t . noTick = tickVals [ i ] . noTick ;
13811402 ticksOut . push ( t ) ;
13821403 }
13831404 }
13841405
1385- if ( isPeriod && ax . _useTicklabelIndex && minorTicks . length ) {
1386- // drop very first minor tick that we added to handle ticklabelindex
1387- minorTicks [ 0 ] . noTick = true ;
1388- }
1389- ticksOut = ticksOut . concat ( minorTicks ) ;
1390-
13911406 ax . _inCalcTicks = false ;
13921407
1393- if ( isPeriod && ticksOut . length ) {
1394- // drop very first tick that we added to handle period
1395- ticksOut [ 0 ] . noTick = true ;
1396- }
1397-
1408+ ticksOut = ticksOut . concat ( minorTicks ) ;
13981409 return ticksOut ;
13991410} ;
14001411
0 commit comments