import {_parseFont, resolve, toPadding} from '../helpers/helpers.options';
import Ticks from './core.ticks';
+/**
+ * @typedef { import("./core.controller").default } Chart
+ */
+
defaults.set('scale', {
display: true,
offset: false,
/** @type {string} */
this.id = cfg.id;
+ /** @type {string} */
this.type = cfg.type;
/** @type {object} */
this.options = cfg.options;
+ /** @type {CanvasRenderingContext2D} */
this.ctx = cfg.ctx;
+ /** @type {Chart} */
this.chart = cfg.chart;
// implements box
this.min = undefined;
this.max = undefined;
/** @type {object[]} */
- this.ticks = null;
+ this.ticks = [];
/** @type {object[]|null} */
this._gridLineItems = null;
/** @type {object[]|null} */
this._labelItems = null;
/** @type {object|null} */
this._labelSizes = null;
- /** @type {number} */
- this._length = undefined;
- /** @type {object} */
+ this._length = 0;
this._longestTextCache = {};
/** @type {number} */
this._startPixel = undefined;
/** @type {number} */
this._endPixel = undefined;
- this._reversePixels = undefined;
+ this._reversePixels = false;
this._userMax = undefined;
this._userMin = undefined;
- this._ticksLength = undefined;
- this._borderValue = undefined;
+ this._ticksLength = 0;
+ this._borderValue = 0;
}
/**
const position = options.position;
const isReverse = me.options.reverse;
let rotation = 0;
- let scaleLabelX, scaleLabelY, textAlign;
+ /** @type CanvasTextAlign */
+ let textAlign;
+ let scaleLabelX, scaleLabelY;
if (me.isHorizontal()) {
switch (scaleLabelAlign) {
function readUsedSize(element, property) {
const value = helpers.dom.getStyle(element, property);
const matches = value && value.match(/^(\d+)(\.\d+)?px$/);
- return matches ? Number(matches[1]) : undefined;
+ return matches ? +matches[1] : undefined;
}
/**
* @private
*/
const supportsEventListenerOptions = (function() {
- let supports = false;
+ let passiveSupported = false;
try {
- const options = Object.defineProperty({}, 'passive', {
- // eslint-disable-next-line getter-return
- get() {
- supports = true;
+ const options = {
+ get passive() { // This function will be called when the browser attempts to access the passive property.
+ passiveSupported = true;
+ return false;
}
- });
- window.addEventListener('e', null, options);
+ };
+ // @ts-ignore
+ window.addEventListener('test', null, options);
+ // @ts-ignore
+ window.removeEventListener('test', null, options);
} catch (e) {
// continue regardless of error
}
- return supports;
+ return passiveSupported;
}());
// Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.
/**
* @typedef { import("../core/core.adapters").Unit } Unit
+ * @typedef {{common: boolean, size: number, steps?: number}} Interval
*/
// Integer constants are from the ES6 spec.
const MAX_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
/**
- * @type {Map<Unit, {common: boolean, size: number, steps?: number}>}
+ * @type {Object<Unit, Interval>}
*/
-const INTERVALS = new Map();
-INTERVALS.set('millisecond', {common: true, size: 1, steps: 1000});
-INTERVALS.set('second', {common: true, size: 1000, steps: 60});
-INTERVALS.set('minute', {common: true, size: 60000, steps: 60});
-INTERVALS.set('hour', {common: true, size: 3600000, steps: 24});
-INTERVALS.set('day', {common: true, size: 86400000, steps: 30});
-INTERVALS.set('week', {common: false, size: 604800000, steps: 4});
-INTERVALS.set('month', {common: true, size: 2.628e9, steps: 12});
-INTERVALS.set('quarter', {common: false, size: 7.884e9, steps: 4});
-INTERVALS.set('year', {common: true, size: 3.154e10});
+const INTERVALS = {
+ millisecond: {common: true, size: 1, steps: 1000},
+ second: {common: true, size: 1000, steps: 60},
+ minute: {common: true, size: 60000, steps: 60},
+ hour: {common: true, size: 3600000, steps: 24},
+ day: {common: true, size: 86400000, steps: 30},
+ week: {common: false, size: 604800000, steps: 4},
+ month: {common: true, size: 2.628e9, steps: 12},
+ quarter: {common: false, size: 7.884e9, steps: 4},
+ year: {common: true, size: 3.154e10}
+};
/**
* @type {Unit[]}
*/
-const UNITS = [];
-INTERVALS.forEach((v, k) => UNITS.push(k));
+const UNITS = /** @type Unit[] */(Object.keys(INTERVALS));
/**
* @param {number} a
/**
* @param {TimeScale} scale
- * {*} input
+ * @param {*} input
*/
function parse(scale, input) {
if (isNullOrUndef(input)) {
*/
function determineUnitForAutoTicks(minUnit, min, max, capacity) {
const ilen = UNITS.length;
- let i, interval, factor;
- for (i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) {
- interval = INTERVALS.get(UNITS[i]);
- factor = interval.steps ? interval.steps : MAX_INTEGER;
+ for (let i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) {
+ const interval = INTERVALS[UNITS[i]];
+ const factor = interval.steps ? interval.steps : MAX_INTEGER;
if (interval.common && Math.ceil((max - min) / (factor * interval.size)) <= capacity) {
return UNITS[i];
function determineUnitForFormatting(scale, numTicks, minUnit, min, max) {
for (let i = UNITS.length - 1; i >= UNITS.indexOf(minUnit); i--) {
const unit = UNITS[i];
- if (INTERVALS.get(unit).common && scale._adapter.diff(max, min, unit) >= numTicks - 1) {
+ if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= numTicks - 1) {
return unit;
}
}
*/
function determineMajorUnit(unit) {
for (let i = UNITS.indexOf(unit) + 1, ilen = UNITS.length; i < ilen; ++i) {
- if (INTERVALS.get(UNITS[i]).common) {
+ if (INTERVALS[UNITS[i]].common) {
return UNITS[i];
}
}
*/
function ticksFromTimestamps(scale, values, majorUnit) {
const ticks = [];
+ /** @type {Object<number,object>} */
const map = {};
const ilen = values.length;
let i, value;
const time = options.time || (options.time = {});
const adapter = this._adapter = new adapters._date(options.adapters.date);
-
+ /** @type {{data: number[], labels: number[], all: number[]}} */
this._cache = {
data: [],
labels: [],
/** @type {Unit} */
this._unit = 'day';
- /** @type {Unit | undefined} */
+ /** @type {Unit=} */
this._majorUnit = undefined;
/** @type {object} */
this._offsets = {};