Skip to content

Commit 50a3451

Browse files
committed
skip labels with identical text as previous label
1 parent 350a10a commit 50a3451

File tree

2 files changed

+80
-23
lines changed

2 files changed

+80
-23
lines changed

src/plots/cartesian/axes.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -752,35 +752,47 @@ axes.calcTicks = function calcTicks(ax, opts) {
752752
}
753753
}
754754

755-
var removedPreTick0Label = false;
756-
var ticksOut = new Array(tickVals.length);
755+
var ticksOut = [];
757756
var i;
757+
var prevText;
758758
for(i = 0; i < tickVals.length; i++) {
759759
var _minor = tickVals[i].minor;
760760
var _value = tickVals[i].value;
761761

762-
ticksOut[i] = axes.tickText(
762+
var t = axes.tickText(
763763
ax,
764764
_value,
765765
false, // hover
766766
_minor // noSuffixPrefix
767767
);
768768

769-
if(isPeriod) {
770-
var v = tickVals[i].value;
769+
if(isPeriod && prevText === t.text) continue;
770+
prevText = t.text;
771+
772+
ticksOut.push(t);
773+
}
774+
775+
if(isPeriod) {
776+
var removedPreTick0Label = false;
777+
778+
for(i = 0; i < ticksOut.length; i++) {
779+
var v = ticksOut[i].x;
771780

772781
var a = i;
773782
var b = i + 1;
774-
if(i < tickVals.length - 1) {
783+
if(i < ticksOut.length - 1) {
775784
a = i;
776785
b = i + 1;
777-
} else {
786+
} else if(i > 0) {
778787
a = i - 1;
779788
b = i;
789+
} else {
790+
a = i;
791+
b = i;
780792
}
781793

782-
var A = tickVals[a].value;
783-
var B = tickVals[b].value;
794+
var A = ticksOut[a].x;
795+
var B = ticksOut[b].x;
784796
var actualDelta = Math.abs(B - A);
785797
var delta = definedDelta || actualDelta;
786798
var periodLength = 0;
@@ -828,15 +840,15 @@ axes.calcTicks = function calcTicks(ax, opts) {
828840
removedPreTick0Label = true;
829841
}
830842
}
831-
}
832843

833-
if(removedPreTick0Label) {
834-
for(i = 0; i < ticksOut.length; i++) {
835-
if(ticksOut[i].periodX <= maxRange && ticksOut[i].periodX >= minRange) {
836-
// redo first visible tick
837-
ax._prevDateHead = '';
838-
ticksOut[i].text = axes.tickText(ax, tickVals[i].value).text;
839-
break;
844+
if(removedPreTick0Label) {
845+
for(i = 0; i < ticksOut.length; i++) {
846+
if(ticksOut[i].periodX <= maxRange && ticksOut[i].periodX >= minRange) {
847+
// redo first visible tick
848+
ax._prevDateHead = '';
849+
ticksOut[i].text = axes.tickText(ax, ticksOut[i].x).text;
850+
break;
851+
}
840852
}
841853
}
842854
}

test/jasmine/tests/axes_test.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5524,7 +5524,7 @@ describe('Test axes', function() {
55245524
});
55255525
});
55265526

5527-
['%f', '%L', '%Q', '%s', '%S', '%M', '%H', '%I', '%p', '%X'].forEach(function(formatter, i) {
5527+
['%Q', '%s', '%H', '%I', '%X'].forEach(function(formatter, i) {
55285528
it('should respect daily tickformat that includes ' + formatter, function(done) {
55295529
Plotly.newPlot(gd, {
55305530
data: [{
@@ -5552,15 +5552,10 @@ describe('Test axes', function() {
55525552
'2020-01-01 21:00',
55535553
'2020-01-02'
55545554
], [
5555-
['', 'Wed-0', 'Wed-0', 'Wed-0', 'Wed-0', 'Wed-0', 'Wed-0', 'Wed-0', 'Wed-0', 'Thu-0'],
5556-
['', 'Wed-000', 'Wed-000', 'Wed-000', 'Wed-000', 'Wed-000', 'Wed-000', 'Wed-000', 'Wed-000', 'Thu-000'],
55575555
['', 'Wed-1577836800000', 'Wed-1577847600000', 'Wed-1577858400000', 'Wed-1577869200000', 'Wed-1577880000000', 'Wed-1577890800000', 'Wed-1577901600000', 'Wed-1577912400000', 'Thu-1577923200000'],
55585556
['', 'Wed-1577836800', 'Wed-1577847600', 'Wed-1577858400', 'Wed-1577869200', 'Wed-1577880000', 'Wed-1577890800', 'Wed-1577901600', 'Wed-1577912400', 'Thu-1577923200'],
5559-
['', 'Wed-00', 'Wed-00', 'Wed-00', 'Wed-00', 'Wed-00', 'Wed-00', 'Wed-00', 'Wed-00', 'Thu-00'],
5560-
['', 'Wed-00', 'Wed-00', 'Wed-00', 'Wed-00', 'Wed-00', 'Wed-00', 'Wed-00', 'Wed-00', 'Thu-00'],
55615557
['', 'Wed-00', 'Wed-03', 'Wed-06', 'Wed-09', 'Wed-12', 'Wed-15', 'Wed-18', 'Wed-21', 'Thu-00'],
55625558
['', 'Wed-12', 'Wed-03', 'Wed-06', 'Wed-09', 'Wed-12', 'Wed-03', 'Wed-06', 'Wed-09', 'Thu-12'],
5563-
['', 'Wed-AM', 'Wed-AM', 'Wed-AM', 'Wed-AM', 'Wed-PM', 'Wed-PM', 'Wed-PM', 'Wed-PM', 'Thu-AM'],
55645559
['', 'Wed-00:00:00', 'Wed-03:00:00', 'Wed-06:00:00', 'Wed-09:00:00', 'Wed-12:00:00', 'Wed-15:00:00', 'Wed-18:00:00', 'Wed-21:00:00', 'Thu-00:00:00']
55655560
][i]);
55665561
})
@@ -5569,6 +5564,56 @@ describe('Test axes', function() {
55695564
});
55705565
});
55715566

5567+
5568+
[
5569+
{
5570+
formatter: '%p',
5571+
positions: ['2019-12-31 21:00', '2020-01-01', '2020-01-01 12:00', '2020-01-02'],
5572+
labels: ['', 'Wed-AM', 'Wed-PM', 'Thu-AM']
5573+
},
5574+
{
5575+
formatter: '%M',
5576+
positions: ['2019-12-31 21:00', '2020-01-01 12:00', '2020-01-02 12:00'],
5577+
labels: ['', 'Wed-00', '']
5578+
},
5579+
{
5580+
formatter: '%S',
5581+
positions: ['2019-12-31 21:00', '2020-01-01 12:00', '2020-01-02 12:00'],
5582+
labels: ['', 'Wed-00', '']
5583+
},
5584+
{
5585+
formatter: '%L',
5586+
positions: ['2019-12-31 21:00', '2020-01-01 12:00', '2020-01-02 12:00'],
5587+
labels: ['', 'Wed-000', '']
5588+
},
5589+
{
5590+
formatter: '%f',
5591+
positions: ['2019-12-31 21:00', '2020-01-01 12:00', '2020-01-02 12:00'],
5592+
labels: ['', 'Wed-0', '']
5593+
}
5594+
].forEach(function(t) {
5595+
it('should respect time tickformat that includes ' + t.formatter, function(done) {
5596+
Plotly.newPlot(gd, {
5597+
data: [{
5598+
hovertemplate: hovertemplate,
5599+
x: ['2020-01-01', '2020-01-02']
5600+
}],
5601+
layout: {
5602+
width: 1000,
5603+
xaxis: {
5604+
ticklabelmode: 'period',
5605+
tickformat: '%a-' + t.formatter
5606+
}
5607+
}
5608+
})
5609+
.then(function() {
5610+
_assert('', t.positions, t.labels);
5611+
})
5612+
.catch(failTest)
5613+
.then(done);
5614+
});
5615+
});
5616+
55725617
[
55735618
{
55745619
range: ['2020-12-15', '2084-12-15'],

0 commit comments

Comments
 (0)