@@ -1024,7 +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- var majorDtick = ax . dtick ;
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+ }
10281035 // calc major first
10291036 for ( var major = 1 ; major >= ( calcMinor ? 0 : 1 ) ; major -- ) {
10301037 var isMinor = ! major ;
@@ -1042,7 +1049,6 @@ axes.calcTicks = function calcTicks(ax, opts) {
10421049 axes . prepMinorTicks ( mockAx , ax , opts ) ;
10431050 } else {
10441051 axes . prepTicks ( mockAx , opts ) ;
1045- majorDtick = mockAx . dtick ;
10461052 }
10471053
10481054 // now that we've figured out the auto values for formatting
@@ -1069,11 +1075,11 @@ axes.calcTicks = function calcTicks(ax, opts) {
10691075 var exRng = expandRange ( rng ) ;
10701076 var startTick = exRng [ 0 ] ;
10711077 var endTick = exRng [ 1 ] ;
1072- var visibleEndTick = endTick ; // for period axis, there will be an additional major tick after that
1078+ const visibleEndTick = endTick ; // Ticks past this end are hidden helper ticks for period labels
10731079 var dtick = mockAx . dtick ;
10741080
1075- var numDtick = isNumeric ( dtick ) ;
1076- var isDLog = ( type === 'log' ) && ! ( numDtick || dtick . charAt ( 0 ) === 'L' ) ;
1081+ const numDtick = isNumeric ( dtick ) ;
1082+ const isDLog = type === 'log' && ! ( numDtick || dtick . charAt ( 0 ) === 'L' ) ;
10771083
10781084 // find the first tick
10791085 var x0 = axes . tickFirst ( mockAx , opts ) ;
@@ -1127,13 +1133,20 @@ axes.calcTicks = function calcTicks(ax, opts) {
11271133 }
11281134 }
11291135
1130- if ( ( major || ax . _useTicklabelIndex ) && isPeriod ) {
1131- // add an additional major tick and correspondingly many minor ticks
1132- // before the first major tick to be able to label the period before the first visible ticks.
1133- x = axes . tickIncrement ( x , majorDtick , ! axrev , calendar ) ;
1134- // same after the last major tick to be able to label the period after the last visible tick.
1135- if ( ax . _useTicklabelIndex ) endTick = axes . tickIncrement ( endTick , majorDtick , axrev , calendar ) ;
1136- 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 ;
11371150 }
11381151
11391152 for ( ;
@@ -1158,13 +1171,13 @@ axes.calcTicks = function calcTicks(ax, opts) {
11581171
11591172 // prevent infinite loops - no more than one tick per pixel,
11601173 // and make sure each value is different from the previous
1161- if ( tickVals . length > maxTicks || x === prevX ) break ;
1174+ if ( tickVals . length > maxTicks + 2 * helperSteps || x === prevX ) break ;
11621175 prevX = x ;
11631176
11641177 var obj = { value : snapToGrid ( x , tick0l , dtick ) } ;
11651178
11661179 // mark ticks that were only added for period label positioning as "noTick" so they aren't drawn.
1167- if ( axrev ? ( x > startTick || x < visibleEndTick ) : ( x < startTick || x > visibleEndTick ) ) {
1180+ if ( axrev ? x > startTick || x < visibleEndTick : x < startTick || x > visibleEndTick ) {
11681181 obj . noTick = true ;
11691182 }
11701183
@@ -1205,8 +1218,7 @@ axes.calcTicks = function calcTicks(ax, opts) {
12051218 } ;
12061219
12071220 // check if ticklabelIndex makes sense, otherwise ignore it.
1208- // It makes sense if in addition to the always present dummy, there are at least 2 minor ticks
1209- // with the required distance to each other.
1221+ // It makes sense if there are at least 2 visible minor ticks with the required distance to each other.
12101222 if ( ax . _useTicklabelIndex ) {
12111223 const visibleMinorTicks = minorTickVals . filter ( ( minorTick ) => ! minorTick . noTick ) ;
12121224 if ( visibleMinorTicks . length < 2 ) {
@@ -1232,6 +1244,17 @@ axes.calcTicks = function calcTicks(ax, opts) {
12321244 const labelTickValsAscending = tickVals . concat ( filteredMinors )
12331245 . sort ( ( a , b ) => a . value - b . value ) ;
12341246
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+
12351258 tickVals . forEach ( function ( majorTick ) {
12361259 if ( ! majorTick . skipLabel ) {
12371260 ticklabelIndex . forEach ( ( labelIndex ) => {
@@ -1240,28 +1263,12 @@ axes.calcTicks = function calcTicks(ax, opts) {
12401263 const absLabelIndex = Math . abs ( labelIndex ) ;
12411264 const labeledTickIndex = smallerTicks . length - absLabelIndex - 1 ;
12421265 if ( absLabelIndex < smallerTicks . length ) {
1243- const tickToLabel = smallerTicks [ labeledTickIndex ] ;
1244- const periodEndTick = isPeriod ? smallerTicks [ labeledTickIndex + 1 ] : undefined ;
1245- const labelVisible = ! tickToLabel . noTick || ( periodEndTick && ! periodEndTick . noTick ) ;
1246- if ( labelVisible ) {
1247- allTicklabelVals . push ( tickToLabel ) ;
1248- if ( periodEndTick && ! periodEndTick . noTick ) {
1249- tickToLabel . periodEndTick = periodEndTick ;
1250- }
1251- }
1266+ addTicklabel ( smallerTicks , labeledTickIndex ) ;
12521267 }
12531268 } else { // labelIndex >= 0
12541269 const largerTicks = labelTickValsAscending . filter ( ( minorTick ) => minorTick . value >= majorTick . value ) ;
1255- if ( labelIndex < largerTicks . length && ! largerTicks [ labelIndex ] . noTick ) {
1256- const tickToLabel = largerTicks [ labelIndex ] ;
1257- const periodEndTick = isPeriod ? largerTicks [ labelIndex + 1 ] : undefined ;
1258- const labelVisible = ! tickToLabel . noTick || ( periodEndTick && ! periodEndTick . noTick ) ;
1259- if ( labelVisible ) {
1260- allTicklabelVals . push ( tickToLabel ) ;
1261- if ( periodEndTick && ! periodEndTick . noTick ) {
1262- tickToLabel . periodEndTick = periodEndTick ;
1263- }
1264- }
1270+ if ( labelIndex < largerTicks . length ) {
1271+ addTicklabel ( largerTicks , labelIndex ) ;
12651272 }
12661273 }
12671274 } ) ;
@@ -1380,7 +1387,7 @@ axes.calcTicks = function calcTicks(ax, opts) {
13801387 if ( ax . _useTicklabelIndex && allTicklabelVals . indexOf ( tickVals [ i ] ) !== - 1 ) {
13811388 t = setTickLabel ( ax , tickVals [ i ] ) ;
13821389 } else {
1383- t = { x : _value , text : '' } ;
1390+ t = { x : _value , text : '' } ;
13841391 }
13851392 t . noTick = tickVals [ i ] . noTick ;
13861393 t . minor = true ;
0 commit comments