diff --git a/draftlogs/7937_fix.md b/draftlogs/7937_fix.md new file mode 100644 index 00000000000..92ff947291f --- /dev/null +++ b/draftlogs/7937_fix.md @@ -0,0 +1 @@ +- Fix missing period labels at the range ends when using `ticklabelindex` [[#7937](https://github.com/plotly/plotly.js/pull/7937)], with thanks to @my-tien for the contribution! diff --git a/src/plots/cartesian/axes.js b/src/plots/cartesian/axes.js index 1661d8c115c..b1dd68bfaeb 100644 --- a/src/plots/cartesian/axes.js +++ b/src/plots/cartesian/axes.js @@ -1024,6 +1024,14 @@ axes.calcTicks = function calcTicks(ax, opts) { // the labels are placed at minor ticks (even if invisible) instead of major ticks. var calcMinor = hasMinor || ax._useTicklabelIndex; + // Number of hidden major steps past each range end. A label can sit |ticklabelindex| ticks + // away from its major tick, and its period needs one more tick to end. + // The cap keeps a huge ticklabelindex from exhausting the tick budget. + let helperSteps = 0; + if (isPeriod) { + const maxLabelIndex = ax._useTicklabelIndex ? Math.max(0, ...Array.from(ticklabelIndex, Math.abs)) : 0; + helperSteps = Math.min(maxLabelIndex + 1, Math.floor(maxTicks / 2)); + } // calc major first for(var major = 1; major >= (calcMinor ? 0 : 1); major--) { var isMinor = !major; @@ -1067,9 +1075,11 @@ axes.calcTicks = function calcTicks(ax, opts) { var exRng = expandRange(rng); var startTick = exRng[0]; var endTick = exRng[1]; + const visibleEndTick = endTick; // Ticks past this end are hidden helper ticks for period labels + var dtick = mockAx.dtick; - var numDtick = isNumeric(mockAx.dtick); - var isDLog = (type === 'log') && !(numDtick || mockAx.dtick.charAt(0) === 'L'); + const numDtick = isNumeric(dtick); + const isDLog = type === 'log' && !(numDtick || dtick.charAt(0) === 'L'); // find the first tick var x0 = axes.tickFirst(mockAx, opts); @@ -1113,7 +1123,6 @@ axes.calcTicks = function calcTicks(ax, opts) { ) / _dTick) - 1; } - var dtick = mockAx.dtick; var tick0l = (type === 'linear') ? mockAx.r2l(mockAx.tick0) : undefined; if(mockAx.rangebreaks && mockAx._tick0Init !== mockAx.tick0) { @@ -1124,13 +1133,20 @@ axes.calcTicks = function calcTicks(ax, opts) { } } - if((major || ax._useTicklabelIndex) && isPeriod) { - // if major: add one item to label period before tick0 - // if minor: add one item for ticklabelindex positioning. positionPeriodTicks requires - // at least 2 ticks to calculate the period length, so we add a dummy tick, ensuring - // that if a tick is labeled, there are always at least 2 ticks. - x = axes.tickIncrement(x, dtick, !axrev, calendar); - if (major) majorId--; + if (major && helperSteps) { + // Hidden helper ticks outside the range carry the labels of the periods that the range cuts + for (let k = 0; k < helperSteps; k++) { + x = axes.tickIncrement(x, dtick, !axrev, calendar); + if (ax._useTicklabelIndex) endTick = axes.tickIncrement(endTick, dtick, axrev, calendar); + } + majorId -= helperSteps; + } else if (isMinor && helperSteps && ax._useTicklabelIndex && tickVals.length) { + // Minor ticks span the major ticks, on their own grid + const firstMajor = tickVals[0].value; + while (axrev ? x < firstMajor : x > firstMajor) { + x = axes.tickIncrement(x, dtick, !axrev, calendar); + } + endTick = tickVals[tickVals.length - 1].value; } for(; @@ -1155,11 +1171,16 @@ axes.calcTicks = function calcTicks(ax, opts) { // prevent infinite loops - no more than one tick per pixel, // and make sure each value is different from the previous - if(tickVals.length > maxTicks || x === prevX) break; + if (tickVals.length > maxTicks + 2 * helperSteps || x === prevX) break; prevX = x; var obj = { value: snapToGrid(x, tick0l, dtick) }; + // mark ticks that were only added for period label positioning as "noTick" so they aren't drawn. + if (axrev ? x > startTick || x < visibleEndTick : x < startTick || x > visibleEndTick) { + obj.noTick = true; + } + if(major) { // mark first major tick, for showexponent/showtickprefix/showticksuffix 'first' if(x === x0) obj.first = true; @@ -1197,16 +1218,16 @@ axes.calcTicks = function calcTicks(ax, opts) { }; // check if ticklabelIndex makes sense, otherwise ignore it. - // It makes sense if in addition to the always present dummy, there are at least 2 minor ticks - // with the required distance to each other. - if(!minorTickVals || minorTickVals.length < 3) { - ax._useTicklabelIndex = false; - } else { - const diff = (minorTickVals[2].value - minorTickVals[1].value) * (isReversed ? -1 : 1); - if(!periodCompatibleWithTickformat(diff, ax.tickformat)) { + // It makes sense if there are at least 2 visible minor ticks with the required distance to each other. + if (ax._useTicklabelIndex) { + const visibleMinorTicks = minorTickVals.filter((minorTick) => !minorTick.noTick); + if (visibleMinorTicks.length < 2) { ax._useTicklabelIndex = false; - // remove previously added tick before tick0 for handling ticklabelindex positioning - minorTickVals = minorTickVals.slice(1); + } else { + const diff = (visibleMinorTicks[1].value - visibleMinorTicks[0].value) * (isReversed ? -1 : 1); + if(!periodCompatibleWithTickformat(diff, ax.tickformat)) { + ax._useTicklabelIndex = false; + } } } @@ -1223,10 +1244,17 @@ axes.calcTicks = function calcTicks(ax, opts) { const labelTickValsAscending = tickVals.concat(filteredMinors) .sort((a, b) => a.value - b.value); - if (isPeriod && tickVals.length) { - // first major tick was just added for period handling - tickVals[0].skipLabel = true; - } + // A hidden tick gets a label only if its period ends on a visible tick. + // Sub-hour periods get no periodX, so setTickLabel cannot hide their out-of-range labels. + const addTicklabel = (ticks, index) => { + const tickToLabel = ticks[index]; + const periodEndTick = isPeriod ? ticks[index + 1] : undefined; + if (tickToLabel.noTick && !(periodEndTick && !periodEndTick.noTick)) return; + + allTicklabelVals.push(tickToLabel); + if (periodEndTick) tickToLabel.periodEndTick = periodEndTick; + }; + tickVals.forEach(function(majorTick) { if (!majorTick.skipLabel) { ticklabelIndex.forEach((labelIndex) => { @@ -1235,20 +1263,12 @@ axes.calcTicks = function calcTicks(ax, opts) { const absLabelIndex = Math.abs(labelIndex); const labeledTickIndex = smallerTicks.length - absLabelIndex - 1; if (absLabelIndex < smallerTicks.length) { - const tickToLabel = smallerTicks[labeledTickIndex]; - allTicklabelVals.push(tickToLabel); - if (isPeriod) { - tickToLabel.periodEndTick = smallerTicks[labeledTickIndex + 1]; - } + addTicklabel(smallerTicks, labeledTickIndex); } } else { // labelIndex >= 0 const largerTicks = labelTickValsAscending.filter((minorTick) => minorTick.value >= majorTick.value); if (labelIndex < largerTicks.length) { - const tickToLabel = largerTicks[labelIndex]; - allTicklabelVals.push(tickToLabel); - if (isPeriod && largerTicks[labelIndex + 1]) { - tickToLabel.periodEndTick = largerTicks[labelIndex + 1]; - } + addTicklabel(largerTicks, labelIndex); } } }); @@ -1367,8 +1387,9 @@ axes.calcTicks = function calcTicks(ax, opts) { if(ax._useTicklabelIndex && allTicklabelVals.indexOf(tickVals[i]) !== -1) { t = setTickLabel(ax, tickVals[i]); } else { - t = { x: _value }; + t = { x: _value, text: '' }; } + t.noTick = tickVals[i].noTick; t.minor = true; minorTicks.push(t); } else { @@ -1377,24 +1398,14 @@ axes.calcTicks = function calcTicks(ax, opts) { if (tickVals[i].skipLabel) { hideLabel(t); } - + t.noTick = tickVals[i].noTick; ticksOut.push(t); } } - if(isPeriod && ax._useTicklabelIndex && minorTicks.length) { - // drop very first minor tick that we added to handle ticklabelindex - minorTicks[0].noTick = true; - } - ticksOut = ticksOut.concat(minorTicks); - ax._inCalcTicks = false; - if(isPeriod && ticksOut.length) { - // drop very first tick that we added to handle period - ticksOut[0].noTick = true; - } - + ticksOut = ticksOut.concat(minorTicks); return ticksOut; }; diff --git a/test/image/baselines/date_axes_period2_ticklabelindex.png b/test/image/baselines/date_axes_period2_ticklabelindex.png index dfac053357e..a42479ba0a5 100644 Binary files a/test/image/baselines/date_axes_period2_ticklabelindex.png and b/test/image/baselines/date_axes_period2_ticklabelindex.png differ diff --git a/test/image/baselines/date_axes_period_ticklabelindex.png b/test/image/baselines/date_axes_period_ticklabelindex.png index 3e36d32c27b..c8814a8c9a0 100644 Binary files a/test/image/baselines/date_axes_period_ticklabelindex.png and b/test/image/baselines/date_axes_period_ticklabelindex.png differ diff --git a/test/image/baselines/ticklabelindex-2.png b/test/image/baselines/ticklabelindex-2.png index 312a7f07709..9858e71441b 100644 Binary files a/test/image/baselines/ticklabelindex-2.png and b/test/image/baselines/ticklabelindex-2.png differ diff --git a/test/image/baselines/ticklabelindex.png b/test/image/baselines/ticklabelindex.png index 5cf10b8ec07..f83b04d6680 100644 Binary files a/test/image/baselines/ticklabelindex.png and b/test/image/baselines/ticklabelindex.png differ diff --git a/test/image/mocks/ticklabelindex-2.json b/test/image/mocks/ticklabelindex-2.json index 047fb3485a3..9321d4923ea 100644 --- a/test/image/mocks/ticklabelindex-2.json +++ b/test/image/mocks/ticklabelindex-2.json @@ -1,5 +1,13 @@ { "data": [ + { + "x": [ + "2020", "2021", "2022" + ], + "y": [ + 3, 2, 1 + ] + }, { "mode": "markers+text", "texttemplate": "%{x|Q%q %y}", @@ -9,13 +17,36 @@ ], "y": [ 2, 2, 2, 2, 2 - ] + ], + "yaxis": "y2", + "xaxis": "x2" } ], "layout": { "width": 600, - "height": 400, + "height": 800, + "grid": { + "rows": 2, + "columns": 1, + "pattern": "independent" + }, "xaxis": { + "dtick": "M12", + "insiderange": [ + "2020-03-27", + "2022-07-21" + ], + "tickformat": "%Y", + "ticklabelindex": -1, + "ticks": "outside", + "ticklen": 20, + "ticklabelmode": "period", + "type": "date", + "title": { + "text": "Should display 2 major ticks and labels 2020, 2021, 2022" + } + }, + "xaxis2": { "dtick": "M24", "insiderange": [ "2019-03-01", @@ -28,9 +59,9 @@ "ticklen": 20, "type": "date", "ticks": "outside", - "minor": { "dtick": "M12" }, + "minor": { "dtick": "M12", "ticks": "outside", "ticklen": 5 }, "title": { - "text": "Should display 1 major tick and a label 2020." + "text": "Should display 1 major tick, 1 minor tick and a label 2020 in between." } } } diff --git a/test/jasmine/tests/axes_test.js b/test/jasmine/tests/axes_test.js index e964c3c33da..8902360a0b4 100644 --- a/test/jasmine/tests/axes_test.js +++ b/test/jasmine/tests/axes_test.js @@ -7037,6 +7037,55 @@ describe('Test axes', function() { .then(done, done.fail); }); }); + + [ + [-1, 1], + [-4, 4] + ].forEach((ticklabelindex) => { + it(`with *ticklabelindex* ${ticklabelindex}, should label the partial periods at both range ends`, async () => { + await Plotly.newPlot(gd, [{ x: ['2017-01-01', '2017-12-31'] }], { + width: 1000, + xaxis: { + range: ['2017-02-05', '2017-09-25'], + dtick: 'M3', + tickformat: '%b', + ticklabelmode: 'period', + ticklabelindex, + minor: { dtick: 'M1' } + } + }); + + const labels = gd._fullLayout.xaxis._vals.filter((d) => d.text).map((d) => d.text); + expect(labels).toEqual(['Feb', 'Mar', 'May', 'Jun', 'Aug', 'Sep']); + }); + }); + + it('with *ticklabelindex*, should keep weekly minor ticks on their tick0 when major ticks are monthly', async () => { + await Plotly.newPlot(gd, [{ x: ['2017-01-01', '2017-06-30'] }], { + width: 1000, + xaxis: { + range: ['2017-01-10', '2017-05-31'], + dtick: 'M1', + tickformat: '%b %d', + ticklabelmode: 'period', + ticklabelindex: -1, + minor: { ticks: 'outside', dtick: ONEWEEK, tick0: '2016-07-03' } + } + }); + + const ax = gd._fullLayout.xaxis; + const minorTicks = ax._vals.filter((d) => d.minor && !d.noTick); + expect(minorTicks.length).toBe(20); + minorTicks.forEach((d) => { + // 2016-07-03 is a Sunday + expect(new Date(d.x).getUTCDay()).withContext(ax.c2d(d.x)).toBe(0); + }); + + // The last period ends on the hidden major tick of June 1 + const labels = ax._vals.filter((d) => d.text); + expect(labels.map((d) => d.text)).toEqual(['Jan 29', 'Feb 26', 'Mar 26', 'Apr 30', 'May 28']); + expect(ax.c2d(labels[4].periodX)).toBe('2017-05-30'); + }); }); describe('minor ticks"', function() {