Skip to content

tickvals / ticktext edge cases #1191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ function autoTickRound(ax) {
// not necessarily *all* the information in tick0 though, if it's really odd
// minimal string length for tick0: 'd' is 10, 'M' is 16, 'S' is 19
// take off a leading minus (year < 0 so length is consistent)
var tick0ms = Lib.dateTime2ms(ax.tick0) || 0,
var tick0ms = Lib.dateTime2ms(ax.tick0),
tick0str = Lib.ms2DateTime(tick0ms).replace(/^-/, ''),
tick0len = tick0str.length;

Expand Down Expand Up @@ -1106,7 +1106,6 @@ function tickTextObj(ax, x, text) {
function formatDate(ax, out, hover, extraPrecision) {
var x = out.x,
tr = ax._tickround,
trOriginal = tr,
d = new Date(x),
// suffix completes the full date info, to be included
// with only the first tick or if any info before what's
Expand Down Expand Up @@ -1145,17 +1144,13 @@ function formatDate(ax, out, hover, extraPrecision) {
.substr(1);
}
}
else if(trOriginal === 'd') {
// for hover on axes with day ticks, minuteFormat (which
// only includes %H:%M) isn't enough, you want the date too
tt = dayFormat(d) + ' ' + tt;
}
}
}
}
if(ax.tickmode === 'array') {
// we get extra precision in array mode, but it may be useless, strip it off
if(tt === '00:00:00') {
if(hover || ax.tickmode === 'array') {
// we get extra precision in array mode or hover,
// but it may be useless, strip it off
if(tt === '00:00:00' || tt === '00:00') {
tt = suffix;
suffix = '';
}
Expand All @@ -1169,7 +1164,9 @@ function formatDate(ax, out, hover, extraPrecision) {
if(suffix) {
if(hover) {
// hover puts it all on one line, so suffix works best up front
tt = suffix + (tt ? ' ' + tt : '');
// except for year suffix: turn this into "Jan 1, 2000" etc.
if(tr === 'd') tt += ', ' + suffix;
else tt = suffix + (tt ? ', ' + tt : '');
}
else if(!ax._inCalcTicks || (suffix !== ax._prevSuffix)) {
tt += '<br>' + suffix;
Expand Down
160 changes: 144 additions & 16 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,22 +1393,36 @@ describe('Test axes', function() {
});
});

describe('calcTicks', function() {
describe('calcTicks and tickText', function() {
function mockCalc(ax) {
Axes.setConvert(ax);
ax.tickfont = {};
ax._gd = {_fullLayout: {separators: '.,'}};
return Axes.calcTicks(ax).map(function(v) { return v.text; });
}

function mockHoverText(ax, x) {
var xCalc = (ax.d2l_noadd || ax.d2c)(x);
var tickTextObj = Axes.tickText(ax, xCalc, true);
return tickTextObj.text;
}

function checkHovers(ax, specArray) {
specArray.forEach(function(v) {
expect(mockHoverText(ax, v[0]))
.toBe(v[1], ax.dtick + ' - ' + v[0]);
});
}

it('provides a new date suffix whenever the suffix changes', function() {
var textOut = mockCalc({
var ax = {
type: 'date',
tickmode: 'linear',
tick0: '2000-01-01',
dtick: 14 * 24 * 3600 * 1000, // 14 days
range: ['1999-12-01', '2000-02-15']
});
};
var textOut = mockCalc(ax);

var expectedText = [
'Dec 4<br>1999',
Expand All @@ -1419,14 +1433,17 @@ describe('Test axes', function() {
'Feb 12'
];
expect(textOut).toEqual(expectedText);
expect(mockHoverText(ax, ax.d2c('1999-12-18 15:34:33.3')))
.toBe('Dec 18, 1999, 15:34');

textOut = mockCalc({
ax = {
type: 'date',
tickmode: 'linear',
tick0: '2000-01-01',
dtick: 12 * 3600 * 1000, // 12 hours
range: ['2000-01-03 11:00', '2000-01-06']
});
};
textOut = mockCalc(ax);

expectedText = [
'12:00<br>Jan 3, 2000',
Expand All @@ -1437,14 +1454,17 @@ describe('Test axes', function() {
'00:00<br>Jan 6, 2000'
];
expect(textOut).toEqual(expectedText);
expect(mockHoverText(ax, ax.d2c('2000-01-04 15:34:33.3')))
.toBe('Jan 4, 2000, 15:34:33');

textOut = mockCalc({
ax = {
type: 'date',
tickmode: 'linear',
tick0: '2000-01-01',
dtick: 1000, // 1 sec
range: ['2000-02-03 23:59:57', '2000-02-04 00:00:02']
});
};
textOut = mockCalc(ax);

expectedText = [
'23:59:57<br>Feb 3, 2000',
Expand All @@ -1455,16 +1475,21 @@ describe('Test axes', function() {
'00:00:02'
];
expect(textOut).toEqual(expectedText);
expect(mockHoverText(ax, ax.d2c('2000-02-04 00:00:00.123456')))
.toBe('Feb 4, 2000, 00:00:00.1235');
expect(mockHoverText(ax, ax.d2c('2000-02-04 00:00:00')))
.toBe('Feb 4, 2000');
});

it('should give dates extra precision if tick0 is weird', function() {
var textOut = mockCalc({
var ax = {
type: 'date',
tickmode: 'linear',
tick0: '2000-01-01 00:05',
dtick: 14 * 24 * 3600 * 1000, // 14 days
range: ['1999-12-01', '2000-02-15']
});
};
var textOut = mockCalc(ax);

var expectedText = [
'00:05<br>Dec 4, 1999',
Expand All @@ -1475,16 +1500,21 @@ describe('Test axes', function() {
'00:05<br>Feb 12, 2000'
];
expect(textOut).toEqual(expectedText);
expect(mockHoverText(ax, ax.d2c('2000-02-04 00:00:00.123456')))
.toBe('Feb 4, 2000');
expect(mockHoverText(ax, ax.d2c('2000-02-04 00:00:05.123456')))
.toBe('Feb 4, 2000, 00:00:05');
});

it('should never give dates more than 100 microsecond precision', function() {
var textOut = mockCalc({
var ax = {
type: 'date',
tickmode: 'linear',
tick0: '2000-01-01',
dtick: 1.1333,
range: ['2000-01-01', '2000-01-01 00:00:00.01']
});
};
var textOut = mockCalc(ax);

var expectedText = [
'00:00:00<br>Jan 1, 2000',
Expand All @@ -1501,7 +1531,7 @@ describe('Test axes', function() {
});

it('should handle edge cases with dates and tickvals', function() {
var textOut = mockCalc({
var ax = {
type: 'date',
tickmode: 'array',
tickvals: [
Expand All @@ -1518,7 +1548,8 @@ describe('Test axes', function() {
// required to get calcTicks to run
range: ['2011-12-10', '2013-01-23'],
nticks: 10
});
};
var textOut = mockCalc(ax);

var expectedText = [
'New year',
Expand All @@ -1528,19 +1559,24 @@ describe('Test axes', function() {
'00:00:01<br>Jan 1, 2013'
];
expect(textOut).toEqual(expectedText);
expect(mockHoverText(ax, ax.d2c('2012-01-01')))
.toBe('New year');
expect(mockHoverText(ax, ax.d2c('2012-01-01 12:34:56.1234')))
.toBe('Jan 1, 2012, 12:34:56');
});

it('should handle tickvals edge cases with linear and log axes', function() {
['linear', 'log'].forEach(function(axType) {
var textOut = mockCalc({
var ax = {
type: axType,
tickmode: 'array',
tickvals: [1, 1.5, 2.6999999, 30, 39.999, 100, 0.1],
ticktext: ['One', '...and a half'],
// I'll be so happy when I can finally get rid of this switch!
range: axType === 'log' ? [-0.2, 1.8] : [0.5, 50],
nticks: 10
});
};
var textOut = mockCalc(ax);

var expectedText = [
'One',
Expand All @@ -1551,6 +1587,8 @@ describe('Test axes', function() {
// 10 and 0.1 are off scale
];
expect(textOut).toEqual(expectedText, axType);
expect(mockHoverText(ax, ax.c2l(1))).toBe('One');
expect(mockHoverText(ax, ax.c2l(19.999))).toBe('19.999');
});
});

Expand Down Expand Up @@ -1578,8 +1616,98 @@ describe('Test axes', function() {
// 'e', 5, -2: bad category and numbers out of range: omitted
];
expect(textOut).toEqual(expectedText);
// make sure we didn't add any more axes accidentally
expect(mockHoverText(ax, 0)).toBe('A!');
expect(mockHoverText(ax, 2)).toBe('c');
expect(mockHoverText(ax, 4)).toBe('');

// make sure we didn't add any more categories accidentally
expect(ax._categories).toEqual(['a', 'b', 'c', 'd']);
});

it('should always start at year for date axis hover', function() {
var ax = {
type: 'date',
tickmode: 'linear',
tick0: '2000-01-01',
dtick: 'M1200',
range: ['1000-01-01', '3000-01-01'],
nticks: 10
};
mockCalc(ax);

checkHovers(ax, [
['2000-01-01', 'Jan 2000'],
['2000-01-01 11:00', 'Jan 2000'],
['2000-01-01 11:14', 'Jan 2000'],
['2000-01-01 11:00:15', 'Jan 2000'],
['2000-01-01 11:00:00.1', 'Jan 2000'],
['2000-01-01 11:00:00.0001', 'Jan 2000']
]);

ax.dtick = 'M1';
ax.range = ['1999-06-01', '2000-06-01'];
mockCalc(ax);

checkHovers(ax, [
['2000-01-01', 'Jan 1, 2000'],
['2000-01-01 11:00', 'Jan 1, 2000'],
['2000-01-01 11:14', 'Jan 1, 2000'],
['2000-01-01 11:00:15', 'Jan 1, 2000'],
['2000-01-01 11:00:00.1', 'Jan 1, 2000'],
['2000-01-01 11:00:00.0001', 'Jan 1, 2000']
]);

ax.dtick = 24 * 3600000; // one day
ax.range = ['1999-12-15', '2000-01-15'];
mockCalc(ax);

checkHovers(ax, [
['2000-01-01', 'Jan 1, 2000'],
['2000-01-01 11:00', 'Jan 1, 2000, 11:00'],
['2000-01-01 11:14', 'Jan 1, 2000, 11:14'],
['2000-01-01 11:00:15', 'Jan 1, 2000, 11:00'],
['2000-01-01 11:00:00.1', 'Jan 1, 2000, 11:00'],
['2000-01-01 11:00:00.0001', 'Jan 1, 2000, 11:00']
]);

ax.dtick = 3600000; // one hour
ax.range = ['1999-12-31', '2000-01-02'];
mockCalc(ax);

checkHovers(ax, [
['2000-01-01', 'Jan 1, 2000'],
['2000-01-01 11:00', 'Jan 1, 2000, 11:00'],
['2000-01-01 11:14', 'Jan 1, 2000, 11:14'],
['2000-01-01 11:00:15', 'Jan 1, 2000, 11:00:15'],
['2000-01-01 11:00:00.1', 'Jan 1, 2000, 11:00'],
['2000-01-01 11:00:00.0001', 'Jan 1, 2000, 11:00']
]);

ax.dtick = 60000; // one minute
ax.range = ['1999-12-31 23:00', '2000-01-01 01:00'];
mockCalc(ax);

checkHovers(ax, [
['2000-01-01', 'Jan 1, 2000'],
['2000-01-01 11:00', 'Jan 1, 2000, 11:00'],
['2000-01-01 11:14', 'Jan 1, 2000, 11:14'],
['2000-01-01 11:00:15', 'Jan 1, 2000, 11:00:15'],
['2000-01-01 11:00:00.1', 'Jan 1, 2000, 11:00'],
['2000-01-01 11:00:00.0001', 'Jan 1, 2000, 11:00']
]);

ax.dtick = 1000; // one second
ax.range = ['1999-12-31 23:59', '2000-01-01 00:01'];
mockCalc(ax);

checkHovers(ax, [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

['2000-01-01', 'Jan 1, 2000'],
['2000-01-01 11:00', 'Jan 1, 2000, 11:00'],
['2000-01-01 11:14', 'Jan 1, 2000, 11:14'],
['2000-01-01 11:00:15', 'Jan 1, 2000, 11:00:15'],
['2000-01-01 11:00:00.1', 'Jan 1, 2000, 11:00:00.1'],
['2000-01-01 11:00:00.0001', 'Jan 1, 2000, 11:00:00.0001']
]);
});
});
});