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
50 changes: 39 additions & 11 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,6 @@ axes.autoBin = function(data, ax, nbins, is2d) {
// in any case, set tickround to # of digits to round tick labels to,
// or codes to this effect for log and date scales
axes.calcTicks = function calcTicks(ax) {
if(ax.tickmode === 'array') return arrayTicks(ax);

var rng = ax.range.map(ax.r2l);

// calculate max number of (auto) ticks to display based on plot size
Expand All @@ -629,6 +627,11 @@ axes.calcTicks = function calcTicks(ax) {
nt = Lib.constrain(ax._length / minPx, 4, 9) + 1;
}
}

// add a couple of extra digits for filling in ticks when we
// have explicit tickvals without tick text
if(ax.tickmode === 'array') nt *= 100;

axes.autoTicks(ax, Math.abs(rng[1] - rng[0]) / nt);
// check for a forced minimum dtick
if(ax._minDtick > 0 && ax.dtick < ax._minDtick * 2) {
Expand All @@ -645,6 +648,10 @@ axes.calcTicks = function calcTicks(ax) {
// now figure out rounding of tick values
autoTickRound(ax);

// now that we've figured out the auto values for formatting
// in case we're missing some ticktext, we can break out for array ticks
if(ax.tickmode === 'array') return arrayTicks(ax);
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for that comment. Very useful.


// find the first tick
ax._tmin = axes.tickFirst(ax);

Expand Down Expand Up @@ -704,8 +711,11 @@ function arrayTicks(ax) {
// except with more precision to the numbers
if(!Array.isArray(text)) text = [];

// make sure showing ticks doesn't accidentally add new categories
var tickVal2l = ax.type === 'category' ? ax.d2l_noadd : ax.d2l;
Copy link
Contributor

Choose a reason for hiding this comment

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

non- 🚫 . As far as I know, we only need to add new categories during the trace modules' calc step (via ax.makeCalcdata). So perhaps, ax.d2l should become ax.d2l_noadd and categories could then be added inside ax.makeCalcdata.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

right, like #1191 (comment) - there are a bunch of uses outside makeCalcdata, particularly in gl code that we need to sort out first.


for(i = 0; i < vals.length; i++) {
vali = ax.d2l(vals[i]);
vali = tickVal2l(vals[i]);
if(vali > tickMin && vali < tickMax) {
if(text[i] === undefined) ticksOut[j] = axes.tickText(ax, vali);
else ticksOut[j] = tickTextObj(ax, vali, String(text[i]));
Expand Down Expand Up @@ -856,7 +866,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),
var tick0ms = Lib.dateTime2ms(ax.tick0) || 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is that || 0 fallback testing somewhere? It looks fragile.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, that's an artifact of an older iteration... not needed anymore.

tick0str = Lib.ms2DateTime(tick0ms).replace(/^-/, ''),
tick0len = tick0str.length;

Expand Down Expand Up @@ -1030,13 +1040,14 @@ axes.tickText = function(ax, x, hover) {
hideexp,
arrayMode = ax.tickmode === 'array',
extraPrecision = hover || arrayMode,
i;
i,
tickVal2l = ax.type === 'category' ? ax.d2l_noadd : ax.d2l;

if(arrayMode && Array.isArray(ax.ticktext)) {
var rng = ax.range.map(ax.r2l),
minDiff = Math.abs(rng[1] - rng[0]) / 10000;
for(i = 0; i < ax.ticktext.length; i++) {
if(Math.abs(x - ax.d2l(ax.tickvals[i])) < minDiff) break;
if(Math.abs(x - tickVal2l(ax.tickvals[i])) < minDiff) break;
}
if(i < ax.ticktext.length) {
out.text = String(ax.ticktext[i]);
Expand Down Expand Up @@ -1113,12 +1124,12 @@ function formatDate(ax, out, hover, extraPrecision) {
else if(tr === 'm') tt = monthFormat(d);
else {
if(tr === 'd') {
if(!hover) suffix = '<br>' + yearFormat(d);
suffix = yearFormat(d);

tt = dayFormat(d);
}
else {
if(!hover) suffix = '<br>' + yearMonthDayFormat(d);
suffix = yearMonthDayFormat(d);

tt = minuteFormat(d);
if(tr !== 'M') {
Expand All @@ -1136,9 +1147,26 @@ function formatDate(ax, out, hover, extraPrecision) {
}
}
}
if(suffix && (!ax._inCalcTicks || (suffix !== ax._prevSuffix))) {
tt += suffix;
ax._prevSuffix = suffix;
if(ax.tickmode === 'array') {
// we get extra precision in array mode, but it may be useless, strip it off
if(tt === '00:00:00') {
tt = suffix;
suffix = '';
}
else {
tt = tt.replace(/:00$/, '');
}
}

if(suffix) {
if(hover) {
// hover puts it all on one line, so suffix works best up front
tt = suffix + (tt ? ' ' + tt : '');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Previously, hover text on date axes would not get the suffix at all. This changes it for all date axes, not just those with array ticks, to include the suffix (as a prefix... hmm, maybe we should change this name...) which is sometimes more information than you need to interpret it, but it's useful often enough that I think this is the right way to go about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, maybe we should change this name.

That would be nice. suffix can be easily confused with axis.ticksuffix.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm.

Looks we're getting a little too much information on regular date axis (e.g. the date_axis mock)

gifrecord_2016-11-22_170028

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah... yep, I see where that's coming from. Looks like I need to add some more explicit tests of Axes.tickText with hover; the regular (non-hover) cases should all be covered by the calcTicks test cases.

}
else if(!ax._inCalcTicks || (suffix !== ax._prevSuffix)) {
tt += '<br>' + suffix;
ax._prevSuffix = suffix;
}
}
out.text = tt;
}
Expand Down
8 changes: 8 additions & 0 deletions src/plots/cartesian/set_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ module.exports = function setConvert(ax) {
return c === -1 ? BADNUM : c;
};

ax.d2l_noadd = function(v) {
// d2c variant that that won't add categories but will also
// allow numbers to be mapped to the linearized axis positions
var index = ax._categories.indexOf(v);
if(index !== -1) return index;
if(typeof v === 'number') return v;
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

before this change, sometimes tickvals could add categories to the list, but it happened after autorange calculations so they didn't show up (until you zoom out on the axis). It was weird. Now that can't happen. At some point we should probably look through all other uses of ax.d2l (or ax.d2c and ax.d2r, which are all the same function for category axes) and make sure they shouldn't also be using d2l_noadd... but so far I haven't seen any other ill effects.


ax.d2l = ax.d2c;
ax.r2l = num;
ax.l2r = num;
Expand Down
79 changes: 79 additions & 0 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1499,5 +1499,84 @@ describe('Test axes', function() {
];
expect(textOut).toEqual(expectedText);
});

it('should handle edge cases with dates and tickvals', function() {
var textOut = mockCalc({
type: 'date',
tickmode: 'array',
tickvals: [
'2012-01-01',
new Date(2012, 2, 1).getTime(),
'2012-08-01 00:00:00',
'2012-10-01 12:00:00',
new Date(2013, 0, 1, 0, 0, 1).getTime(),
'2010-01-01', '2014-01-01' // off the axis
],
// only the first two have text
ticktext: ['New year', 'February'],
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks pretty useful. Nice feature to have in the 🏦


// required to get calcTicks to run
range: ['2011-12-10', '2013-01-23'],
nticks: 10
});

var expectedText = [
'New year',
'February',
'Aug 1, 2012',
'12:00<br>Oct 1, 2012',
'00:00:01<br>Jan 1, 2013'
];
expect(textOut).toEqual(expectedText);
});

it('should handle tickvals edge cases with linear and log axes', function() {
['linear', 'log'].forEach(function(axType) {
var textOut = mockCalc({
type: axType,
tickmode: 'array',
tickvals: [1, 1.5, 2.6999999, 3, 3.999, 10, 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, 0.8] : [0.5, 5],
nticks: 10
});

var expectedText = [
'One',
'...and a half', // the first two get explicit labels
'2.7', // 2.6999999 gets rounded to 2.7
'3',
'3.999' // 3.999 does not get rounded
// 10 and 0.1 are off scale
];
expect(textOut).toEqual(expectedText, axType);
});
});

it('should handle tickvals edge cases with category axes', function() {
var textOut = mockCalc({
type: 'category',
_categories: ['a', 'b', 'c', 'd'],
tickmode: 'array',
tickvals: ['a', 1, 1.5, 'c', 2.7, 3, 'e', 4, 5, -2],
ticktext: ['A!', 'B?', 'B->C'],
range: [-0.5, 4.5],
nticks: 10
});

var expectedText = [
'A!', // category position, explicit text
'B?', // integer position, explicit text
'B->C', // non-integer position, explicit text
'c', // category position, no text: use category
'd', // non-integer position, no text: use closest category
'd', // integer position, no text: use category
'' // 4: number with no close category: leave blank
// but still include it so we get a tick mark & grid
// 'e', 5, -2: bad category and numbers out of range: omitted
];
expect(textOut).toEqual(expectedText);
});
});
});