Skip to content

Commit ae355c5

Browse files
committed
Label partial periods at both range ends for any ticklabelindex
1 parent 8cd6850 commit ae355c5

2 files changed

Lines changed: 92 additions & 36 deletions

File tree

‎src/plots/cartesian/axes.js‎

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

‎test/jasmine/tests/axes_test.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7037,6 +7037,55 @@ describe('Test axes', function() {
70377037
.then(done, done.fail);
70387038
});
70397039
});
7040+
7041+
[
7042+
[-1, 1],
7043+
[-4, 4]
7044+
].forEach((ticklabelindex) => {
7045+
it(`with *ticklabelindex* ${ticklabelindex}, should label the partial periods at both range ends`, async () => {
7046+
await Plotly.newPlot(gd, [{ x: ['2017-01-01', '2017-12-31'] }], {
7047+
width: 1000,
7048+
xaxis: {
7049+
range: ['2017-02-05', '2017-09-25'],
7050+
dtick: 'M3',
7051+
tickformat: '%b',
7052+
ticklabelmode: 'period',
7053+
ticklabelindex,
7054+
minor: { dtick: 'M1' }
7055+
}
7056+
});
7057+
7058+
const labels = gd._fullLayout.xaxis._vals.filter((d) => d.text).map((d) => d.text);
7059+
expect(labels).toEqual(['Feb', 'Mar', 'May', 'Jun', 'Aug', 'Sep']);
7060+
});
7061+
});
7062+
7063+
it('with *ticklabelindex*, should keep weekly minor ticks on their tick0 when major ticks are monthly', async () => {
7064+
await Plotly.newPlot(gd, [{ x: ['2017-01-01', '2017-06-30'] }], {
7065+
width: 1000,
7066+
xaxis: {
7067+
range: ['2017-01-10', '2017-05-31'],
7068+
dtick: 'M1',
7069+
tickformat: '%b %d',
7070+
ticklabelmode: 'period',
7071+
ticklabelindex: -1,
7072+
minor: { ticks: 'outside', dtick: ONEWEEK, tick0: '2016-07-03' }
7073+
}
7074+
});
7075+
7076+
const ax = gd._fullLayout.xaxis;
7077+
const minorTicks = ax._vals.filter((d) => d.minor && !d.noTick);
7078+
expect(minorTicks.length).toBe(20);
7079+
minorTicks.forEach((d) => {
7080+
// 2016-07-03 is a Sunday
7081+
expect(new Date(d.x).getUTCDay()).withContext(ax.c2d(d.x)).toBe(0);
7082+
});
7083+
7084+
// The last period ends on the hidden major tick of June 1
7085+
const labels = ax._vals.filter((d) => d.text);
7086+
expect(labels.map((d) => d.text)).toEqual(['Jan 29', 'Feb 26', 'Mar 26', 'Apr 30', 'May 28']);
7087+
expect(ax.c2d(labels[4].periodX)).toBe('2017-05-30');
7088+
});
70407089
});
70417090

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

0 commit comments

Comments
 (0)