Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Histogram autobin #3044

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 10 commits into from
Oct 4, 2018
Prev Previous commit
Next Next commit
pull out tick0/dtick validation logic for reuse
  • Loading branch information
alexcjohnson committed Sep 24, 2018
commit ab20fae9a57d7b418accb1af3d93d66f4046a195
87 changes: 87 additions & 0 deletions 87 src/plots/cartesian/clean_ticks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var ONEDAY = require('../../constants/numerical').ONEDAY;

/**
* Return a validated dtick value for this axis
*
* @param {any} dtick: the candidate dtick. valid values are numbers and strings,
* and further constrained depending on the axis type.
* @param {string} axType: the axis type
*/
exports.dtick = function(dtick, axType) {
var isLog = axType === 'log';
var isDate = axType === 'date';
var isCat = axType === 'category';
var dtickDflt = isDate ? ONEDAY : 1;

if(!dtick) return dtickDflt;

if(isNumeric(dtick)) {
dtick = Number(dtick);
if(dtick <= 0) return dtickDflt;
if(isCat) {
// category dtick must be positive integers
return Math.max(1, Math.round(dtick));
}
if(isDate) {
// date dtick must be at least 0.1ms (our current precision)
return Math.max(0.1, dtick);
}
return dtick;
}

if(typeof dtick !== 'string' || !(isDate || isLog)) {
return dtickDflt;
}

var prefix = dtick.charAt(0);
var dtickNum = dtick.substr(1);
dtickNum = isNumeric(dtickNum) ? Number(dtickNum) : 0;

if((dtickNum <= 0) || !(
// "M<n>" gives ticks every (integer) n months
(isDate && prefix === 'M' && dtickNum === Math.round(dtickNum)) ||
// "L<f>" gives ticks linearly spaced in data (not in position) every (float) f
(isLog && prefix === 'L') ||
// "D1" gives powers of 10 with all small digits between, "D2" gives only 2 and 5
(isLog && prefix === 'D' && (dtickNum === 1 || dtickNum === 2))
)) {
return dtickDflt;
}

return dtick;
};

/**
* Return a validated tick0 for this axis
*
* @param {any} tick0: the candidate tick0. Valid values are numbers and strings,
* further constrained depending on the axis type
* @param {string} axType: the axis type
* @param {string} calendar: for date axes, the calendar to validate/convert with
* @param {any} dtick: an already valid dtick. Only used for D1 and D2 log dticks,
* which do not support tick0 at all.
*/
exports.tick0 = function(tick0, axType, calendar, dtick) {
if(axType === 'date') {
return Lib.cleanDate(tick0, Lib.dateTick0(calendar));
}
if(dtick === 'D1' || dtick === 'D2') {
// D1 and D2 modes ignore tick0 entirely
return undefined;
}
// Aside from date axes, tick0 must be numeric
return isNumeric(tick0) ? Number(tick0) : 0;
};
50 changes: 6 additions & 44 deletions 50 src/plots/cartesian/tick_value_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

'use strict';

var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var ONEDAY = require('../../constants/numerical').ONEDAY;
var cleanTicks = require('./clean_ticks');


module.exports = function handleTickValueDefaults(containerIn, containerOut, coerce, axType) {
Expand All @@ -33,47 +31,11 @@ module.exports = function handleTickValueDefaults(containerIn, containerOut, coe
else if(tickmode === 'linear') {
// dtick is usually a positive number, but there are some
// special strings available for log or date axes
// default is 1 day for dates, otherwise 1
var dtickDflt = (axType === 'date') ? ONEDAY : 1;
var dtick = coerce('dtick', dtickDflt);
if(isNumeric(dtick)) {
containerOut.dtick = (dtick > 0) ? Number(dtick) : dtickDflt;
}
else if(typeof dtick !== 'string') {
containerOut.dtick = dtickDflt;
}
else {
// date and log special cases are all one character plus a number
var prefix = dtick.charAt(0),
dtickNum = dtick.substr(1);

dtickNum = isNumeric(dtickNum) ? Number(dtickNum) : 0;
if((dtickNum <= 0) || !(
// "M<n>" gives ticks every (integer) n months
(axType === 'date' && prefix === 'M' && dtickNum === Math.round(dtickNum)) ||
// "L<f>" gives ticks linearly spaced in data (not in position) every (float) f
(axType === 'log' && prefix === 'L') ||
// "D1" gives powers of 10 with all small digits between, "D2" gives only 2 and 5
(axType === 'log' && prefix === 'D' && (dtickNum === 1 || dtickNum === 2))
)) {
containerOut.dtick = dtickDflt;
}
}

// tick0 can have different valType for different axis types, so
// validate that now. Also for dates, change milliseconds to date strings
var tick0Dflt = (axType === 'date') ? Lib.dateTick0(containerOut.calendar) : 0;
var tick0 = coerce('tick0', tick0Dflt);
if(axType === 'date') {
containerOut.tick0 = Lib.cleanDate(tick0, tick0Dflt);
}
// Aside from date axes, dtick must be numeric; D1 and D2 modes ignore tick0 entirely
else if(isNumeric(tick0) && dtick !== 'D1' && dtick !== 'D2') {
containerOut.tick0 = Number(tick0);
}
else {
containerOut.tick0 = tick0Dflt;
}
// tick0 also has special logic
var dtick = containerOut.dtick = cleanTicks.dtick(
containerIn.dtick, axType);
containerOut.tick0 = cleanTicks.tick0(
containerIn.tick0, axType, containerOut.calendar, dtick);
}
else {
var tickvals = coerce('tickvals');
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.