Skip to content

Commit 5f101a8

Browse files
authored
Merge pull request #7937 from my-tien/7875-ticklabelindex-last-period
fix: Fix missing last period label with negative `ticklabelindex`
2 parents 795a7cb + 9b3cfb9 commit 5f101a8

8 files changed

Lines changed: 143 additions & 51 deletions

File tree

‎draftlogs/7937_fix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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!

‎src/plots/cartesian/axes.js‎

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1.44 KB
Loading
1.12 KB
Loading
22.2 KB
Loading
174 Bytes
Loading

‎test/image/mocks/ticklabelindex-2.json‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"data": [
3+
{
4+
"x": [
5+
"2020", "2021", "2022"
6+
],
7+
"y": [
8+
3, 2, 1
9+
]
10+
},
311
{
412
"mode": "markers+text",
513
"texttemplate": "%{x|Q%q %y}",
@@ -9,13 +17,36 @@
917
],
1018
"y": [
1119
2, 2, 2, 2, 2
12-
]
20+
],
21+
"yaxis": "y2",
22+
"xaxis": "x2"
1323
}
1424
],
1525
"layout": {
1626
"width": 600,
17-
"height": 400,
27+
"height": 800,
28+
"grid": {
29+
"rows": 2,
30+
"columns": 1,
31+
"pattern": "independent"
32+
},
1833
"xaxis": {
34+
"dtick": "M12",
35+
"insiderange": [
36+
"2020-03-27",
37+
"2022-07-21"
38+
],
39+
"tickformat": "%Y",
40+
"ticklabelindex": -1,
41+
"ticks": "outside",
42+
"ticklen": 20,
43+
"ticklabelmode": "period",
44+
"type": "date",
45+
"title": {
46+
"text": "Should display 2 major ticks and labels 2020, 2021, 2022"
47+
}
48+
},
49+
"xaxis2": {
1950
"dtick": "M24",
2051
"insiderange": [
2152
"2019-03-01",
@@ -28,9 +59,9 @@
2859
"ticklen": 20,
2960
"type": "date",
3061
"ticks": "outside",
31-
"minor": { "dtick": "M12" },
62+
"minor": { "dtick": "M12", "ticks": "outside", "ticklen": 5 },
3263
"title": {
33-
"text": "Should display 1 major tick and a label 2020."
64+
"text": "Should display 1 major tick, 1 minor tick and a label 2020 in between."
3465
}
3566
}
3667
}

‎test/jasmine/tests/axes_test.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7210,6 +7210,55 @@ describe('Test axes', function() {
72107210
.then(done, done.fail);
72117211
});
72127212
});
7213+
7214+
[
7215+
[-1, 1],
7216+
[-4, 4]
7217+
].forEach((ticklabelindex) => {
7218+
it(`with *ticklabelindex* ${ticklabelindex}, should label the partial periods at both range ends`, async () => {
7219+
await Plotly.newPlot(gd, [{ x: ['2017-01-01', '2017-12-31'] }], {
7220+
width: 1000,
7221+
xaxis: {
7222+
range: ['2017-02-05', '2017-09-25'],
7223+
dtick: 'M3',
7224+
tickformat: '%b',
7225+
ticklabelmode: 'period',
7226+
ticklabelindex,
7227+
minor: { dtick: 'M1' }
7228+
}
7229+
});
7230+
7231+
const labels = gd._fullLayout.xaxis._vals.filter((d) => d.text).map((d) => d.text);
7232+
expect(labels).toEqual(['Feb', 'Mar', 'May', 'Jun', 'Aug', 'Sep']);
7233+
});
7234+
});
7235+
7236+
it('with *ticklabelindex*, should keep weekly minor ticks on their tick0 when major ticks are monthly', async () => {
7237+
await Plotly.newPlot(gd, [{ x: ['2017-01-01', '2017-06-30'] }], {
7238+
width: 1000,
7239+
xaxis: {
7240+
range: ['2017-01-10', '2017-05-31'],
7241+
dtick: 'M1',
7242+
tickformat: '%b %d',
7243+
ticklabelmode: 'period',
7244+
ticklabelindex: -1,
7245+
minor: { ticks: 'outside', dtick: ONEWEEK, tick0: '2016-07-03' }
7246+
}
7247+
});
7248+
7249+
const ax = gd._fullLayout.xaxis;
7250+
const minorTicks = ax._vals.filter((d) => d.minor && !d.noTick);
7251+
expect(minorTicks.length).toBe(20);
7252+
minorTicks.forEach((d) => {
7253+
// 2016-07-03 is a Sunday
7254+
expect(new Date(d.x).getUTCDay()).withContext(ax.c2d(d.x)).toBe(0);
7255+
});
7256+
7257+
// The last period ends on the hidden major tick of June 1
7258+
const labels = ax._vals.filter((d) => d.text);
7259+
expect(labels.map((d) => d.text)).toEqual(['Jan 29', 'Feb 26', 'Mar 26', 'Apr 30', 'May 28']);
7260+
expect(ax.c2d(labels[4].periodX)).toBe('2017-05-30');
7261+
});
72137262
});
72147263

72157264
describe('minor ticks"', function() {

0 commit comments

Comments
 (0)