diff --git a/src/annotation.js b/src/annotation.js index 8bf7b2b02..cc1612323 100644 --- a/src/annotation.js +++ b/src/annotation.js @@ -1,6 +1,7 @@ import {Animations, Chart} from 'chart.js'; -import {clipArea, unclipArea, isFinite, valueOrDefault, isObject, isArray} from 'chart.js/helpers'; +import {clipArea, unclipArea, isFinite, valueOrDefault, isObject, isArray, defined, isFunction} from 'chart.js/helpers'; import {handleEvent, hooks, updateListeners} from './events'; +import {verifyScaleOptions} from './helpers'; import {annotationTypes} from './types'; import {version} from '../package.json'; @@ -245,19 +246,15 @@ function draw(chart, caller, clip) { function adjustScaleRange(chart, scale, annotations) { const range = getScaleLimits(scale, annotations); let changed = false; - if (isFinite(range.min) && - typeof scale.options.min === 'undefined' && - typeof scale.options.suggestedMin === 'undefined') { + if (isFinite(range.min) && !defined(scale.options.min) && !defined(scale.options.suggestedMin)) { changed = scale.min !== range.min; scale.min = range.min; } - if (isFinite(range.max) && - typeof scale.options.max === 'undefined' && - typeof scale.options.suggestedMax === 'undefined') { + if (isFinite(range.max) && !defined(scale.options.max) && !defined(scale.options.suggestedMax)) { changed = scale.max !== range.max; scale.max = range.max; } - if (changed && typeof scale.handleTickRangeOptions === 'function') { + if (changed && isFunction(scale.handleTickRangeOptions)) { scale.handleTickRangeOptions(); } } @@ -291,13 +288,3 @@ function getScaleLimits(scale, annotations) { } return {min, max}; } - -function verifyScaleOptions(annotations, scales) { - for (const annotation of annotations) { - for (const key of ['scaleID', 'xScaleID', 'yScaleID']) { - if (annotation[key] && !scales[annotation[key]]) { - console.warn(`No scale found with id '${annotation[key]}' for annotation '${annotation.id}'`); - } - } - } -} diff --git a/src/helpers/helpers.options.js b/src/helpers/helpers.options.js index 592d5dfb0..83aaa35e6 100644 --- a/src/helpers/helpers.options.js +++ b/src/helpers/helpers.options.js @@ -69,3 +69,13 @@ export function toPosition(value) { export function isBoundToPoint(options) { return options && (defined(options.xValue) || defined(options.yValue)); } + +export function verifyScaleOptions(annotations, scales) { + for (const annotation of annotations) { + for (const key of ['scaleID', 'xScaleID', 'yScaleID']) { + if (annotation[key] && !scales[annotation[key]]) { + console.warn(`No scale found with id '${annotation[key]}' for annotation '${annotation.id}'`); + } + } + } +}