| 1 |
/*! |
| 2 |
* Chart.js v4.5.1 |
| 3 |
* https://www.chartjs.org |
| 4 |
* (c) 2025 Chart.js Contributors |
| 5 |
* Released under the MIT License |
| 6 |
*/ |
| 7 |
'use strict'; |
| 8 |
|
| 9 |
var color$1 = require('@kurkle/color'); |
| 10 |
|
| 11 |
/** |
| 12 |
* @namespace Chart.helpers |
| 13 |
*/ /** |
| 14 |
* An empty function that can be used, for example, for optional callback. |
| 15 |
*/ function noop() { |
| 16 |
/* noop */ } |
| 17 |
/** |
| 18 |
* Returns a unique id, sequentially generated from a global variable. |
| 19 |
*/ const uid = (()=>{ |
| 20 |
let id = 0; |
| 21 |
return ()=>id++; |
| 22 |
})(); |
| 23 |
/** |
| 24 |
* Returns true if `value` is neither null nor undefined, else returns false. |
| 25 |
* @param value - The value to test. |
| 26 |
* @since 2.7.0 |
| 27 |
*/ function isNullOrUndef(value) { |
| 28 |
return value === null || value === undefined; |
| 29 |
} |
| 30 |
/** |
| 31 |
* Returns true if `value` is an array (including typed arrays), else returns false. |
| 32 |
* @param value - The value to test. |
| 33 |
* @function |
| 34 |
*/ function isArray(value) { |
| 35 |
if (Array.isArray && Array.isArray(value)) { |
| 36 |
return true; |
| 37 |
} |
| 38 |
const type = Object.prototype.toString.call(value); |
| 39 |
if (type.slice(0, 7) === '[object' && type.slice(-6) === 'Array]') { |
| 40 |
return true; |
| 41 |
} |
| 42 |
return false; |
| 43 |
} |
| 44 |
/** |
| 45 |
* Returns true if `value` is an object (excluding null), else returns false. |
| 46 |
* @param value - The value to test. |
| 47 |
* @since 2.7.0 |
| 48 |
*/ function isObject(value) { |
| 49 |
return value !== null && Object.prototype.toString.call(value) === '[object Object]'; |
| 50 |
} |
| 51 |
/** |
| 52 |
* Returns true if `value` is a finite number, else returns false |
| 53 |
* @param value - The value to test. |
| 54 |
*/ function isNumberFinite(value) { |
| 55 |
return (typeof value === 'number' || value instanceof Number) && isFinite(+value); |
| 56 |
} |
| 57 |
/** |
| 58 |
* Returns `value` if finite, else returns `defaultValue`. |
| 59 |
* @param value - The value to return if defined. |
| 60 |
* @param defaultValue - The value to return if `value` is not finite. |
| 61 |
*/ function finiteOrDefault(value, defaultValue) { |
| 62 |
return isNumberFinite(value) ? value : defaultValue; |
| 63 |
} |
| 64 |
/** |
| 65 |
* Returns `value` if defined, else returns `defaultValue`. |
| 66 |
* @param value - The value to return if defined. |
| 67 |
* @param defaultValue - The value to return if `value` is undefined. |
| 68 |
*/ function valueOrDefault(value, defaultValue) { |
| 69 |
return typeof value === 'undefined' ? defaultValue : value; |
| 70 |
} |
| 71 |
const toPercentage = (value, dimension)=>typeof value === 'string' && value.endsWith('%') ? parseFloat(value) / 100 : +value / dimension; |
| 72 |
const toDimension = (value, dimension)=>typeof value === 'string' && value.endsWith('%') ? parseFloat(value) / 100 * dimension : +value; |
| 73 |
/** |
| 74 |
* Calls `fn` with the given `args` in the scope defined by `thisArg` and returns the |
| 75 |
* value returned by `fn`. If `fn` is not a function, this method returns undefined. |
| 76 |
* @param fn - The function to call. |
| 77 |
* @param args - The arguments with which `fn` should be called. |
| 78 |
* @param [thisArg] - The value of `this` provided for the call to `fn`. |
| 79 |
*/ function callback(fn, args, thisArg) { |
| 80 |
if (fn && typeof fn.call === 'function') { |
| 81 |
return fn.apply(thisArg, args); |
| 82 |
} |
| 83 |
} |
| 84 |
function each(loopable, fn, thisArg, reverse) { |
| 85 |
let i, len, keys; |
| 86 |
if (isArray(loopable)) { |
| 87 |
len = loopable.length; |
| 88 |
if (reverse) { |
| 89 |
for(i = len - 1; i >= 0; i--){ |
| 90 |
fn.call(thisArg, loopable[i], i); |
| 91 |
} |
| 92 |
} else { |
| 93 |
for(i = 0; i < len; i++){ |
| 94 |
fn.call(thisArg, loopable[i], i); |
| 95 |
} |
| 96 |
} |
| 97 |
} else if (isObject(loopable)) { |
| 98 |
keys = Object.keys(loopable); |
| 99 |
len = keys.length; |
| 100 |
for(i = 0; i < len; i++){ |
| 101 |
fn.call(thisArg, loopable[keys[i]], keys[i]); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
/** |
| 106 |
* Returns true if the `a0` and `a1` arrays have the same content, else returns false. |
| 107 |
* @param a0 - The array to compare |
| 108 |
* @param a1 - The array to compare |
| 109 |
* @private |
| 110 |
*/ function _elementsEqual(a0, a1) { |
| 111 |
let i, ilen, v0, v1; |
| 112 |
if (!a0 || !a1 || a0.length !== a1.length) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
for(i = 0, ilen = a0.length; i < ilen; ++i){ |
| 116 |
v0 = a0[i]; |
| 117 |
v1 = a1[i]; |
| 118 |
if (v0.datasetIndex !== v1.datasetIndex || v0.index !== v1.index) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
} |
| 122 |
return true; |
| 123 |
} |
| 124 |
/** |
| 125 |
* Returns a deep copy of `source` without keeping references on objects and arrays. |
| 126 |
* @param source - The value to clone. |
| 127 |
*/ function clone(source) { |
| 128 |
if (isArray(source)) { |
| 129 |
return source.map(clone); |
| 130 |
} |
| 131 |
if (isObject(source)) { |
| 132 |
const target = Object.create(null); |
| 133 |
const keys = Object.keys(source); |
| 134 |
const klen = keys.length; |
| 135 |
let k = 0; |
| 136 |
for(; k < klen; ++k){ |
| 137 |
target[keys[k]] = clone(source[keys[k]]); |
| 138 |
} |
| 139 |
return target; |
| 140 |
} |
| 141 |
return source; |
| 142 |
} |
| 143 |
function isValidKey(key) { |
| 144 |
return [ |
| 145 |
'__proto__', |
| 146 |
'prototype', |
| 147 |
'constructor' |
| 148 |
].indexOf(key) === -1; |
| 149 |
} |
| 150 |
/** |
| 151 |
* The default merger when Chart.helpers.merge is called without merger option. |
| 152 |
* Note(SB): also used by mergeConfig and mergeScaleConfig as fallback. |
| 153 |
* @private |
| 154 |
*/ function _merger(key, target, source, options) { |
| 155 |
if (!isValidKey(key)) { |
| 156 |
return; |
| 157 |
} |
| 158 |
const tval = target[key]; |
| 159 |
const sval = source[key]; |
| 160 |
if (isObject(tval) && isObject(sval)) { |
| 161 |
// eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 162 |
merge(tval, sval, options); |
| 163 |
} else { |
| 164 |
target[key] = clone(sval); |
| 165 |
} |
| 166 |
} |
| 167 |
function merge(target, source, options) { |
| 168 |
const sources = isArray(source) ? source : [ |
| 169 |
source |
| 170 |
]; |
| 171 |
const ilen = sources.length; |
| 172 |
if (!isObject(target)) { |
| 173 |
return target; |
| 174 |
} |
| 175 |
options = options || {}; |
| 176 |
const merger = options.merger || _merger; |
| 177 |
let current; |
| 178 |
for(let i = 0; i < ilen; ++i){ |
| 179 |
current = sources[i]; |
| 180 |
if (!isObject(current)) { |
| 181 |
continue; |
| 182 |
} |
| 183 |
const keys = Object.keys(current); |
| 184 |
for(let k = 0, klen = keys.length; k < klen; ++k){ |
| 185 |
merger(keys[k], target, current, options); |
| 186 |
} |
| 187 |
} |
| 188 |
return target; |
| 189 |
} |
| 190 |
function mergeIf(target, source) { |
| 191 |
// eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 192 |
return merge(target, source, { |
| 193 |
merger: _mergerIf |
| 194 |
}); |
| 195 |
} |
| 196 |
/** |
| 197 |
* Merges source[key] in target[key] only if target[key] is undefined. |
| 198 |
* @private |
| 199 |
*/ function _mergerIf(key, target, source) { |
| 200 |
if (!isValidKey(key)) { |
| 201 |
return; |
| 202 |
} |
| 203 |
const tval = target[key]; |
| 204 |
const sval = source[key]; |
| 205 |
if (isObject(tval) && isObject(sval)) { |
| 206 |
mergeIf(tval, sval); |
| 207 |
} else if (!Object.prototype.hasOwnProperty.call(target, key)) { |
| 208 |
target[key] = clone(sval); |
| 209 |
} |
| 210 |
} |
| 211 |
/** |
| 212 |
* @private |
| 213 |
*/ function _deprecated(scope, value, previous, current) { |
| 214 |
if (value !== undefined) { |
| 215 |
console.warn(scope + ': "' + previous + '" is deprecated. Please use "' + current + '" instead'); |
| 216 |
} |
| 217 |
} |
| 218 |
// resolveObjectKey resolver cache |
| 219 |
const keyResolvers = { |
| 220 |
// Chart.helpers.core resolveObjectKey should resolve empty key to root object |
| 221 |
'': (v)=>v, |
| 222 |
// default resolvers |
| 223 |
x: (o)=>o.x, |
| 224 |
y: (o)=>o.y |
| 225 |
}; |
| 226 |
/** |
| 227 |
* @private |
| 228 |
*/ function _splitKey(key) { |
| 229 |
const parts = key.split('.'); |
| 230 |
const keys = []; |
| 231 |
let tmp = ''; |
| 232 |
for (const part of parts){ |
| 233 |
tmp += part; |
| 234 |
if (tmp.endsWith('\\')) { |
| 235 |
tmp = tmp.slice(0, -1) + '.'; |
| 236 |
} else { |
| 237 |
keys.push(tmp); |
| 238 |
tmp = ''; |
| 239 |
} |
| 240 |
} |
| 241 |
return keys; |
| 242 |
} |
| 243 |
function _getKeyResolver(key) { |
| 244 |
const keys = _splitKey(key); |
| 245 |
return (obj)=>{ |
| 246 |
for (const k of keys){ |
| 247 |
if (k === '') { |
| 248 |
break; |
| 249 |
} |
| 250 |
obj = obj && obj[k]; |
| 251 |
} |
| 252 |
return obj; |
| 253 |
}; |
| 254 |
} |
| 255 |
function resolveObjectKey(obj, key) { |
| 256 |
const resolver = keyResolvers[key] || (keyResolvers[key] = _getKeyResolver(key)); |
| 257 |
return resolver(obj); |
| 258 |
} |
| 259 |
/** |
| 260 |
* @private |
| 261 |
*/ function _capitalize(str) { |
| 262 |
return str.charAt(0).toUpperCase() + str.slice(1); |
| 263 |
} |
| 264 |
const defined = (value)=>typeof value !== 'undefined'; |
| 265 |
const isFunction = (value)=>typeof value === 'function'; |
| 266 |
// Adapted from https://stackoverflow.com/questions/31128855/comparing-ecma6-sets-for-equality#31129384 |
| 267 |
const setsEqual = (a, b)=>{ |
| 268 |
if (a.size !== b.size) { |
| 269 |
return false; |
| 270 |
} |
| 271 |
for (const item of a){ |
| 272 |
if (!b.has(item)) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
} |
| 276 |
return true; |
| 277 |
}; |
| 278 |
/** |
| 279 |
* @param e - The event |
| 280 |
* @private |
| 281 |
*/ function _isClickEvent(e) { |
| 282 |
return e.type === 'mouseup' || e.type === 'click' || e.type === 'contextmenu'; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* @alias Chart.helpers.math |
| 287 |
* @namespace |
| 288 |
*/ const PI = Math.PI; |
| 289 |
const TAU = 2 * PI; |
| 290 |
const PITAU = TAU + PI; |
| 291 |
const INFINITY = Number.POSITIVE_INFINITY; |
| 292 |
const RAD_PER_DEG = PI / 180; |
| 293 |
const HALF_PI = PI / 2; |
| 294 |
const QUARTER_PI = PI / 4; |
| 295 |
const TWO_THIRDS_PI = PI * 2 / 3; |
| 296 |
const log10 = Math.log10; |
| 297 |
const sign = Math.sign; |
| 298 |
function almostEquals(x, y, epsilon) { |
| 299 |
return Math.abs(x - y) < epsilon; |
| 300 |
} |
| 301 |
/** |
| 302 |
* Implementation of the nice number algorithm used in determining where axis labels will go |
| 303 |
*/ function niceNum(range) { |
| 304 |
const roundedRange = Math.round(range); |
| 305 |
range = almostEquals(range, roundedRange, range / 1000) ? roundedRange : range; |
| 306 |
const niceRange = Math.pow(10, Math.floor(log10(range))); |
| 307 |
const fraction = range / niceRange; |
| 308 |
const niceFraction = fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 5 ? 5 : 10; |
| 309 |
return niceFraction * niceRange; |
| 310 |
} |
| 311 |
/** |
| 312 |
* Returns an array of factors sorted from 1 to sqrt(value) |
| 313 |
* @private |
| 314 |
*/ function _factorize(value) { |
| 315 |
const result = []; |
| 316 |
const sqrt = Math.sqrt(value); |
| 317 |
let i; |
| 318 |
for(i = 1; i < sqrt; i++){ |
| 319 |
if (value % i === 0) { |
| 320 |
result.push(i); |
| 321 |
result.push(value / i); |
| 322 |
} |
| 323 |
} |
| 324 |
if (sqrt === (sqrt | 0)) { |
| 325 |
result.push(sqrt); |
| 326 |
} |
| 327 |
result.sort((a, b)=>a - b).pop(); |
| 328 |
return result; |
| 329 |
} |
| 330 |
/** |
| 331 |
* Verifies that attempting to coerce n to string or number won't throw a TypeError. |
| 332 |
*/ function isNonPrimitive(n) { |
| 333 |
return typeof n === 'symbol' || typeof n === 'object' && n !== null && !(Symbol.toPrimitive in n || 'toString' in n || 'valueOf' in n); |
| 334 |
} |
| 335 |
function isNumber(n) { |
| 336 |
return !isNonPrimitive(n) && !isNaN(parseFloat(n)) && isFinite(n); |
| 337 |
} |
| 338 |
function almostWhole(x, epsilon) { |
| 339 |
const rounded = Math.round(x); |
| 340 |
return rounded - epsilon <= x && rounded + epsilon >= x; |
| 341 |
} |
| 342 |
/** |
| 343 |
* @private |
| 344 |
*/ function _setMinAndMaxByKey(array, target, property) { |
| 345 |
let i, ilen, value; |
| 346 |
for(i = 0, ilen = array.length; i < ilen; i++){ |
| 347 |
value = array[i][property]; |
| 348 |
if (!isNaN(value)) { |
| 349 |
target.min = Math.min(target.min, value); |
| 350 |
target.max = Math.max(target.max, value); |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
function toRadians(degrees) { |
| 355 |
return degrees * (PI / 180); |
| 356 |
} |
| 357 |
function toDegrees(radians) { |
| 358 |
return radians * (180 / PI); |
| 359 |
} |
| 360 |
/** |
| 361 |
* Returns the number of decimal places |
| 362 |
* i.e. the number of digits after the decimal point, of the value of this Number. |
| 363 |
* @param x - A number. |
| 364 |
* @returns The number of decimal places. |
| 365 |
* @private |
| 366 |
*/ function _decimalPlaces(x) { |
| 367 |
if (!isNumberFinite(x)) { |
| 368 |
return; |
| 369 |
} |
| 370 |
let e = 1; |
| 371 |
let p = 0; |
| 372 |
while(Math.round(x * e) / e !== x){ |
| 373 |
e *= 10; |
| 374 |
p++; |
| 375 |
} |
| 376 |
return p; |
| 377 |
} |
| 378 |
// Gets the angle from vertical upright to the point about a centre. |
| 379 |
function getAngleFromPoint(centrePoint, anglePoint) { |
| 380 |
const distanceFromXCenter = anglePoint.x - centrePoint.x; |
| 381 |
const distanceFromYCenter = anglePoint.y - centrePoint.y; |
| 382 |
const radialDistanceFromCenter = Math.sqrt(distanceFromXCenter * distanceFromXCenter + distanceFromYCenter * distanceFromYCenter); |
| 383 |
let angle = Math.atan2(distanceFromYCenter, distanceFromXCenter); |
| 384 |
if (angle < -0.5 * PI) { |
| 385 |
angle += TAU; // make sure the returned angle is in the range of (-PI/2, 3PI/2] |
| 386 |
} |
| 387 |
return { |
| 388 |
angle, |
| 389 |
distance: radialDistanceFromCenter |
| 390 |
}; |
| 391 |
} |
| 392 |
function distanceBetweenPoints(pt1, pt2) { |
| 393 |
return Math.sqrt(Math.pow(pt2.x - pt1.x, 2) + Math.pow(pt2.y - pt1.y, 2)); |
| 394 |
} |
| 395 |
/** |
| 396 |
* Shortest distance between angles, in either direction. |
| 397 |
* @private |
| 398 |
*/ function _angleDiff(a, b) { |
| 399 |
return (a - b + PITAU) % TAU - PI; |
| 400 |
} |
| 401 |
/** |
| 402 |
* Normalize angle to be between 0 and 2*PI |
| 403 |
* @private |
| 404 |
*/ function _normalizeAngle(a) { |
| 405 |
return (a % TAU + TAU) % TAU; |
| 406 |
} |
| 407 |
/** |
| 408 |
* @private |
| 409 |
*/ function _angleBetween(angle, start, end, sameAngleIsFullCircle) { |
| 410 |
const a = _normalizeAngle(angle); |
| 411 |
const s = _normalizeAngle(start); |
| 412 |
const e = _normalizeAngle(end); |
| 413 |
const angleToStart = _normalizeAngle(s - a); |
| 414 |
const angleToEnd = _normalizeAngle(e - a); |
| 415 |
const startToAngle = _normalizeAngle(a - s); |
| 416 |
const endToAngle = _normalizeAngle(a - e); |
| 417 |
return a === s || a === e || sameAngleIsFullCircle && s === e || angleToStart > angleToEnd && startToAngle < endToAngle; |
| 418 |
} |
| 419 |
/** |
| 420 |
* Limit `value` between `min` and `max` |
| 421 |
* @param value |
| 422 |
* @param min |
| 423 |
* @param max |
| 424 |
* @private |
| 425 |
*/ function _limitValue(value, min, max) { |
| 426 |
return Math.max(min, Math.min(max, value)); |
| 427 |
} |
| 428 |
/** |
| 429 |
* @param {number} value |
| 430 |
* @private |
| 431 |
*/ function _int16Range(value) { |
| 432 |
return _limitValue(value, -32768, 32767); |
| 433 |
} |
| 434 |
/** |
| 435 |
* @param value |
| 436 |
* @param start |
| 437 |
* @param end |
| 438 |
* @param [epsilon] |
| 439 |
* @private |
| 440 |
*/ function _isBetween(value, start, end, epsilon = 1e-6) { |
| 441 |
return value >= Math.min(start, end) - epsilon && value <= Math.max(start, end) + epsilon; |
| 442 |
} |
| 443 |
|
| 444 |
function _lookup(table, value, cmp) { |
| 445 |
cmp = cmp || ((index)=>table[index] < value); |
| 446 |
let hi = table.length - 1; |
| 447 |
let lo = 0; |
| 448 |
let mid; |
| 449 |
while(hi - lo > 1){ |
| 450 |
mid = lo + hi >> 1; |
| 451 |
if (cmp(mid)) { |
| 452 |
lo = mid; |
| 453 |
} else { |
| 454 |
hi = mid; |
| 455 |
} |
| 456 |
} |
| 457 |
return { |
| 458 |
lo, |
| 459 |
hi |
| 460 |
}; |
| 461 |
} |
| 462 |
/** |
| 463 |
* Binary search |
| 464 |
* @param table - the table search. must be sorted! |
| 465 |
* @param key - property name for the value in each entry |
| 466 |
* @param value - value to find |
| 467 |
* @param last - lookup last index |
| 468 |
* @private |
| 469 |
*/ const _lookupByKey = (table, key, value, last)=>_lookup(table, value, last ? (index)=>{ |
| 470 |
const ti = table[index][key]; |
| 471 |
return ti < value || ti === value && table[index + 1][key] === value; |
| 472 |
} : (index)=>table[index][key] < value); |
| 473 |
/** |
| 474 |
* Reverse binary search |
| 475 |
* @param table - the table search. must be sorted! |
| 476 |
* @param key - property name for the value in each entry |
| 477 |
* @param value - value to find |
| 478 |
* @private |
| 479 |
*/ const _rlookupByKey = (table, key, value)=>_lookup(table, value, (index)=>table[index][key] >= value); |
| 480 |
/** |
| 481 |
* Return subset of `values` between `min` and `max` inclusive. |
| 482 |
* Values are assumed to be in sorted order. |
| 483 |
* @param values - sorted array of values |
| 484 |
* @param min - min value |
| 485 |
* @param max - max value |
| 486 |
*/ function _filterBetween(values, min, max) { |
| 487 |
let start = 0; |
| 488 |
let end = values.length; |
| 489 |
while(start < end && values[start] < min){ |
| 490 |
start++; |
| 491 |
} |
| 492 |
while(end > start && values[end - 1] > max){ |
| 493 |
end--; |
| 494 |
} |
| 495 |
return start > 0 || end < values.length ? values.slice(start, end) : values; |
| 496 |
} |
| 497 |
const arrayEvents = [ |
| 498 |
'push', |
| 499 |
'pop', |
| 500 |
'shift', |
| 501 |
'splice', |
| 502 |
'unshift' |
| 503 |
]; |
| 504 |
function listenArrayEvents(array, listener) { |
| 505 |
if (array._chartjs) { |
| 506 |
array._chartjs.listeners.push(listener); |
| 507 |
return; |
| 508 |
} |
| 509 |
Object.defineProperty(array, '_chartjs', { |
| 510 |
configurable: true, |
| 511 |
enumerable: false, |
| 512 |
value: { |
| 513 |
listeners: [ |
| 514 |
listener |
| 515 |
] |
| 516 |
} |
| 517 |
}); |
| 518 |
arrayEvents.forEach((key)=>{ |
| 519 |
const method = '_onData' + _capitalize(key); |
| 520 |
const base = array[key]; |
| 521 |
Object.defineProperty(array, key, { |
| 522 |
configurable: true, |
| 523 |
enumerable: false, |
| 524 |
value (...args) { |
| 525 |
const res = base.apply(this, args); |
| 526 |
array._chartjs.listeners.forEach((object)=>{ |
| 527 |
if (typeof object[method] === 'function') { |
| 528 |
object[method](...args); |
| 529 |
} |
| 530 |
}); |
| 531 |
return res; |
| 532 |
} |
| 533 |
}); |
| 534 |
}); |
| 535 |
} |
| 536 |
function unlistenArrayEvents(array, listener) { |
| 537 |
const stub = array._chartjs; |
| 538 |
if (!stub) { |
| 539 |
return; |
| 540 |
} |
| 541 |
const listeners = stub.listeners; |
| 542 |
const index = listeners.indexOf(listener); |
| 543 |
if (index !== -1) { |
| 544 |
listeners.splice(index, 1); |
| 545 |
} |
| 546 |
if (listeners.length > 0) { |
| 547 |
return; |
| 548 |
} |
| 549 |
arrayEvents.forEach((key)=>{ |
| 550 |
delete array[key]; |
| 551 |
}); |
| 552 |
delete array._chartjs; |
| 553 |
} |
| 554 |
/** |
| 555 |
* @param items |
| 556 |
*/ function _arrayUnique(items) { |
| 557 |
const set = new Set(items); |
| 558 |
if (set.size === items.length) { |
| 559 |
return items; |
| 560 |
} |
| 561 |
return Array.from(set); |
| 562 |
} |
| 563 |
|
| 564 |
function fontString(pixelSize, fontStyle, fontFamily) { |
| 565 |
return fontStyle + ' ' + pixelSize + 'px ' + fontFamily; |
| 566 |
} |
| 567 |
/** |
| 568 |
* Request animation polyfill |
| 569 |
*/ const requestAnimFrame = function() { |
| 570 |
if (typeof window === 'undefined') { |
| 571 |
return function(callback) { |
| 572 |
return callback(); |
| 573 |
}; |
| 574 |
} |
| 575 |
return window.requestAnimationFrame; |
| 576 |
}(); |
| 577 |
/** |
| 578 |
* Throttles calling `fn` once per animation frame |
| 579 |
* Latest arguments are used on the actual call |
| 580 |
*/ function throttled(fn, thisArg) { |
| 581 |
let argsToUse = []; |
| 582 |
let ticking = false; |
| 583 |
return function(...args) { |
| 584 |
// Save the args for use later |
| 585 |
argsToUse = args; |
| 586 |
if (!ticking) { |
| 587 |
ticking = true; |
| 588 |
requestAnimFrame.call(window, ()=>{ |
| 589 |
ticking = false; |
| 590 |
fn.apply(thisArg, argsToUse); |
| 591 |
}); |
| 592 |
} |
| 593 |
}; |
| 594 |
} |
| 595 |
/** |
| 596 |
* Debounces calling `fn` for `delay` ms |
| 597 |
*/ function debounce(fn, delay) { |
| 598 |
let timeout; |
| 599 |
return function(...args) { |
| 600 |
if (delay) { |
| 601 |
clearTimeout(timeout); |
| 602 |
timeout = setTimeout(fn, delay, args); |
| 603 |
} else { |
| 604 |
fn.apply(this, args); |
| 605 |
} |
| 606 |
return delay; |
| 607 |
}; |
| 608 |
} |
| 609 |
/** |
| 610 |
* Converts 'start' to 'left', 'end' to 'right' and others to 'center' |
| 611 |
* @private |
| 612 |
*/ const _toLeftRightCenter = (align)=>align === 'start' ? 'left' : align === 'end' ? 'right' : 'center'; |
| 613 |
/** |
| 614 |
* Returns `start`, `end` or `(start + end) / 2` depending on `align`. Defaults to `center` |
| 615 |
* @private |
| 616 |
*/ const _alignStartEnd = (align, start, end)=>align === 'start' ? start : align === 'end' ? end : (start + end) / 2; |
| 617 |
/** |
| 618 |
* Returns `left`, `right` or `(left + right) / 2` depending on `align`. Defaults to `left` |
| 619 |
* @private |
| 620 |
*/ const _textX = (align, left, right, rtl)=>{ |
| 621 |
const check = rtl ? 'left' : 'right'; |
| 622 |
return align === check ? right : align === 'center' ? (left + right) / 2 : left; |
| 623 |
}; |
| 624 |
/** |
| 625 |
* Return start and count of visible points. |
| 626 |
* @private |
| 627 |
*/ function _getStartAndCountOfVisiblePoints(meta, points, animationsDisabled) { |
| 628 |
const pointCount = points.length; |
| 629 |
let start = 0; |
| 630 |
let count = pointCount; |
| 631 |
if (meta._sorted) { |
| 632 |
const { iScale , vScale , _parsed } = meta; |
| 633 |
const spanGaps = meta.dataset ? meta.dataset.options ? meta.dataset.options.spanGaps : null : null; |
| 634 |
const axis = iScale.axis; |
| 635 |
const { min , max , minDefined , maxDefined } = iScale.getUserBounds(); |
| 636 |
if (minDefined) { |
| 637 |
start = Math.min(// @ts-expect-error Need to type _parsed |
| 638 |
_lookupByKey(_parsed, axis, min).lo, // @ts-expect-error Need to fix types on _lookupByKey |
| 639 |
animationsDisabled ? pointCount : _lookupByKey(points, axis, iScale.getPixelForValue(min)).lo); |
| 640 |
if (spanGaps) { |
| 641 |
const distanceToDefinedLo = _parsed.slice(0, start + 1).reverse().findIndex((point)=>!isNullOrUndef(point[vScale.axis])); |
| 642 |
start -= Math.max(0, distanceToDefinedLo); |
| 643 |
} |
| 644 |
start = _limitValue(start, 0, pointCount - 1); |
| 645 |
} |
| 646 |
if (maxDefined) { |
| 647 |
let end = Math.max(// @ts-expect-error Need to type _parsed |
| 648 |
_lookupByKey(_parsed, iScale.axis, max, true).hi + 1, // @ts-expect-error Need to fix types on _lookupByKey |
| 649 |
animationsDisabled ? 0 : _lookupByKey(points, axis, iScale.getPixelForValue(max), true).hi + 1); |
| 650 |
if (spanGaps) { |
| 651 |
const distanceToDefinedHi = _parsed.slice(end - 1).findIndex((point)=>!isNullOrUndef(point[vScale.axis])); |
| 652 |
end += Math.max(0, distanceToDefinedHi); |
| 653 |
} |
| 654 |
count = _limitValue(end, start, pointCount) - start; |
| 655 |
} else { |
| 656 |
count = pointCount - start; |
| 657 |
} |
| 658 |
} |
| 659 |
return { |
| 660 |
start, |
| 661 |
count |
| 662 |
}; |
| 663 |
} |
| 664 |
/** |
| 665 |
* Checks if the scale ranges have changed. |
| 666 |
* @param {object} meta - dataset meta. |
| 667 |
* @returns {boolean} |
| 668 |
* @private |
| 669 |
*/ function _scaleRangesChanged(meta) { |
| 670 |
const { xScale , yScale , _scaleRanges } = meta; |
| 671 |
const newRanges = { |
| 672 |
xmin: xScale.min, |
| 673 |
xmax: xScale.max, |
| 674 |
ymin: yScale.min, |
| 675 |
ymax: yScale.max |
| 676 |
}; |
| 677 |
if (!_scaleRanges) { |
| 678 |
meta._scaleRanges = newRanges; |
| 679 |
return true; |
| 680 |
} |
| 681 |
const changed = _scaleRanges.xmin !== xScale.min || _scaleRanges.xmax !== xScale.max || _scaleRanges.ymin !== yScale.min || _scaleRanges.ymax !== yScale.max; |
| 682 |
Object.assign(_scaleRanges, newRanges); |
| 683 |
return changed; |
| 684 |
} |
| 685 |
|
| 686 |
const atEdge = (t)=>t === 0 || t === 1; |
| 687 |
const elasticIn = (t, s, p)=>-(Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * TAU / p)); |
| 688 |
const elasticOut = (t, s, p)=>Math.pow(2, -10 * t) * Math.sin((t - s) * TAU / p) + 1; |
| 689 |
/** |
| 690 |
* Easing functions adapted from Robert Penner's easing equations. |
| 691 |
* @namespace Chart.helpers.easing.effects |
| 692 |
* @see http://www.robertpenner.com/easing/ |
| 693 |
*/ const effects = { |
| 694 |
linear: (t)=>t, |
| 695 |
easeInQuad: (t)=>t * t, |
| 696 |
easeOutQuad: (t)=>-t * (t - 2), |
| 697 |
easeInOutQuad: (t)=>(t /= 0.5) < 1 ? 0.5 * t * t : -0.5 * (--t * (t - 2) - 1), |
| 698 |
easeInCubic: (t)=>t * t * t, |
| 699 |
easeOutCubic: (t)=>(t -= 1) * t * t + 1, |
| 700 |
easeInOutCubic: (t)=>(t /= 0.5) < 1 ? 0.5 * t * t * t : 0.5 * ((t -= 2) * t * t + 2), |
| 701 |
easeInQuart: (t)=>t * t * t * t, |
| 702 |
easeOutQuart: (t)=>-((t -= 1) * t * t * t - 1), |
| 703 |
easeInOutQuart: (t)=>(t /= 0.5) < 1 ? 0.5 * t * t * t * t : -0.5 * ((t -= 2) * t * t * t - 2), |
| 704 |
easeInQuint: (t)=>t * t * t * t * t, |
| 705 |
easeOutQuint: (t)=>(t -= 1) * t * t * t * t + 1, |
| 706 |
easeInOutQuint: (t)=>(t /= 0.5) < 1 ? 0.5 * t * t * t * t * t : 0.5 * ((t -= 2) * t * t * t * t + 2), |
| 707 |
easeInSine: (t)=>-Math.cos(t * HALF_PI) + 1, |
| 708 |
easeOutSine: (t)=>Math.sin(t * HALF_PI), |
| 709 |
easeInOutSine: (t)=>-0.5 * (Math.cos(PI * t) - 1), |
| 710 |
easeInExpo: (t)=>t === 0 ? 0 : Math.pow(2, 10 * (t - 1)), |
| 711 |
easeOutExpo: (t)=>t === 1 ? 1 : -Math.pow(2, -10 * t) + 1, |
| 712 |
easeInOutExpo: (t)=>atEdge(t) ? t : t < 0.5 ? 0.5 * Math.pow(2, 10 * (t * 2 - 1)) : 0.5 * (-Math.pow(2, -10 * (t * 2 - 1)) + 2), |
| 713 |
easeInCirc: (t)=>t >= 1 ? t : -(Math.sqrt(1 - t * t) - 1), |
| 714 |
easeOutCirc: (t)=>Math.sqrt(1 - (t -= 1) * t), |
| 715 |
easeInOutCirc: (t)=>(t /= 0.5) < 1 ? -0.5 * (Math.sqrt(1 - t * t) - 1) : 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1), |
| 716 |
easeInElastic: (t)=>atEdge(t) ? t : elasticIn(t, 0.075, 0.3), |
| 717 |
easeOutElastic: (t)=>atEdge(t) ? t : elasticOut(t, 0.075, 0.3), |
| 718 |
easeInOutElastic (t) { |
| 719 |
const s = 0.1125; |
| 720 |
const p = 0.45; |
| 721 |
return atEdge(t) ? t : t < 0.5 ? 0.5 * elasticIn(t * 2, s, p) : 0.5 + 0.5 * elasticOut(t * 2 - 1, s, p); |
| 722 |
}, |
| 723 |
easeInBack (t) { |
| 724 |
const s = 1.70158; |
| 725 |
return t * t * ((s + 1) * t - s); |
| 726 |
}, |
| 727 |
easeOutBack (t) { |
| 728 |
const s = 1.70158; |
| 729 |
return (t -= 1) * t * ((s + 1) * t + s) + 1; |
| 730 |
}, |
| 731 |
easeInOutBack (t) { |
| 732 |
let s = 1.70158; |
| 733 |
if ((t /= 0.5) < 1) { |
| 734 |
return 0.5 * (t * t * (((s *= 1.525) + 1) * t - s)); |
| 735 |
} |
| 736 |
return 0.5 * ((t -= 2) * t * (((s *= 1.525) + 1) * t + s) + 2); |
| 737 |
}, |
| 738 |
easeInBounce: (t)=>1 - effects.easeOutBounce(1 - t), |
| 739 |
easeOutBounce (t) { |
| 740 |
const m = 7.5625; |
| 741 |
const d = 2.75; |
| 742 |
if (t < 1 / d) { |
| 743 |
return m * t * t; |
| 744 |
} |
| 745 |
if (t < 2 / d) { |
| 746 |
return m * (t -= 1.5 / d) * t + 0.75; |
| 747 |
} |
| 748 |
if (t < 2.5 / d) { |
| 749 |
return m * (t -= 2.25 / d) * t + 0.9375; |
| 750 |
} |
| 751 |
return m * (t -= 2.625 / d) * t + 0.984375; |
| 752 |
}, |
| 753 |
easeInOutBounce: (t)=>t < 0.5 ? effects.easeInBounce(t * 2) * 0.5 : effects.easeOutBounce(t * 2 - 1) * 0.5 + 0.5 |
| 754 |
}; |
| 755 |
|
| 756 |
function isPatternOrGradient(value) { |
| 757 |
if (value && typeof value === 'object') { |
| 758 |
const type = value.toString(); |
| 759 |
return type === '[object CanvasPattern]' || type === '[object CanvasGradient]'; |
| 760 |
} |
| 761 |
return false; |
| 762 |
} |
| 763 |
function color(value) { |
| 764 |
return isPatternOrGradient(value) ? value : new color$1.Color(value); |
| 765 |
} |
| 766 |
function getHoverColor(value) { |
| 767 |
return isPatternOrGradient(value) ? value : new color$1.Color(value).saturate(0.5).darken(0.1).hexString(); |
| 768 |
} |
| 769 |
|
| 770 |
const numbers = [ |
| 771 |
'x', |
| 772 |
'y', |
| 773 |
'borderWidth', |
| 774 |
'radius', |
| 775 |
'tension' |
| 776 |
]; |
| 777 |
const colors = [ |
| 778 |
'color', |
| 779 |
'borderColor', |
| 780 |
'backgroundColor' |
| 781 |
]; |
| 782 |
function applyAnimationsDefaults(defaults) { |
| 783 |
defaults.set('animation', { |
| 784 |
delay: undefined, |
| 785 |
duration: 1000, |
| 786 |
easing: 'easeOutQuart', |
| 787 |
fn: undefined, |
| 788 |
from: undefined, |
| 789 |
loop: undefined, |
| 790 |
to: undefined, |
| 791 |
type: undefined |
| 792 |
}); |
| 793 |
defaults.describe('animation', { |
| 794 |
_fallback: false, |
| 795 |
_indexable: false, |
| 796 |
_scriptable: (name)=>name !== 'onProgress' && name !== 'onComplete' && name !== 'fn' |
| 797 |
}); |
| 798 |
defaults.set('animations', { |
| 799 |
colors: { |
| 800 |
type: 'color', |
| 801 |
properties: colors |
| 802 |
}, |
| 803 |
numbers: { |
| 804 |
type: 'number', |
| 805 |
properties: numbers |
| 806 |
} |
| 807 |
}); |
| 808 |
defaults.describe('animations', { |
| 809 |
_fallback: 'animation' |
| 810 |
}); |
| 811 |
defaults.set('transitions', { |
| 812 |
active: { |
| 813 |
animation: { |
| 814 |
duration: 400 |
| 815 |
} |
| 816 |
}, |
| 817 |
resize: { |
| 818 |
animation: { |
| 819 |
duration: 0 |
| 820 |
} |
| 821 |
}, |
| 822 |
show: { |
| 823 |
animations: { |
| 824 |
colors: { |
| 825 |
from: 'transparent' |
| 826 |
}, |
| 827 |
visible: { |
| 828 |
type: 'boolean', |
| 829 |
duration: 0 |
| 830 |
} |
| 831 |
} |
| 832 |
}, |
| 833 |
hide: { |
| 834 |
animations: { |
| 835 |
colors: { |
| 836 |
to: 'transparent' |
| 837 |
}, |
| 838 |
visible: { |
| 839 |
type: 'boolean', |
| 840 |
easing: 'linear', |
| 841 |
fn: (v)=>v | 0 |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
}); |
| 846 |
} |
| 847 |
|
| 848 |
function applyLayoutsDefaults(defaults) { |
| 849 |
defaults.set('layout', { |
| 850 |
autoPadding: true, |
| 851 |
padding: { |
| 852 |
top: 0, |
| 853 |
right: 0, |
| 854 |
bottom: 0, |
| 855 |
left: 0 |
| 856 |
} |
| 857 |
}); |
| 858 |
} |
| 859 |
|
| 860 |
const intlCache = new Map(); |
| 861 |
function getNumberFormat(locale, options) { |
| 862 |
options = options || {}; |
| 863 |
const cacheKey = locale + JSON.stringify(options); |
| 864 |
let formatter = intlCache.get(cacheKey); |
| 865 |
if (!formatter) { |
| 866 |
formatter = new Intl.NumberFormat(locale, options); |
| 867 |
intlCache.set(cacheKey, formatter); |
| 868 |
} |
| 869 |
return formatter; |
| 870 |
} |
| 871 |
function formatNumber(num, locale, options) { |
| 872 |
return getNumberFormat(locale, options).format(num); |
| 873 |
} |
| 874 |
|
| 875 |
const formatters = { |
| 876 |
values (value) { |
| 877 |
return isArray(value) ? value : '' + value; |
| 878 |
}, |
| 879 |
numeric (tickValue, index, ticks) { |
| 880 |
if (tickValue === 0) { |
| 881 |
return '0'; |
| 882 |
} |
| 883 |
const locale = this.chart.options.locale; |
| 884 |
let notation; |
| 885 |
let delta = tickValue; |
| 886 |
if (ticks.length > 1) { |
| 887 |
const maxTick = Math.max(Math.abs(ticks[0].value), Math.abs(ticks[ticks.length - 1].value)); |
| 888 |
if (maxTick < 1e-4 || maxTick > 1e+15) { |
| 889 |
notation = 'scientific'; |
| 890 |
} |
| 891 |
delta = calculateDelta(tickValue, ticks); |
| 892 |
} |
| 893 |
const logDelta = log10(Math.abs(delta)); |
| 894 |
const numDecimal = isNaN(logDelta) ? 1 : Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0); |
| 895 |
const options = { |
| 896 |
notation, |
| 897 |
minimumFractionDigits: numDecimal, |
| 898 |
maximumFractionDigits: numDecimal |
| 899 |
}; |
| 900 |
Object.assign(options, this.options.ticks.format); |
| 901 |
return formatNumber(tickValue, locale, options); |
| 902 |
}, |
| 903 |
logarithmic (tickValue, index, ticks) { |
| 904 |
if (tickValue === 0) { |
| 905 |
return '0'; |
| 906 |
} |
| 907 |
const remain = ticks[index].significand || tickValue / Math.pow(10, Math.floor(log10(tickValue))); |
| 908 |
if ([ |
| 909 |
1, |
| 910 |
2, |
| 911 |
3, |
| 912 |
5, |
| 913 |
10, |
| 914 |
15 |
| 915 |
].includes(remain) || index > 0.8 * ticks.length) { |
| 916 |
return formatters.numeric.call(this, tickValue, index, ticks); |
| 917 |
} |
| 918 |
return ''; |
| 919 |
} |
| 920 |
}; |
| 921 |
function calculateDelta(tickValue, ticks) { |
| 922 |
let delta = ticks.length > 3 ? ticks[2].value - ticks[1].value : ticks[1].value - ticks[0].value; |
| 923 |
if (Math.abs(delta) >= 1 && tickValue !== Math.floor(tickValue)) { |
| 924 |
delta = tickValue - Math.floor(tickValue); |
| 925 |
} |
| 926 |
return delta; |
| 927 |
} |
| 928 |
var Ticks = { |
| 929 |
formatters |
| 930 |
}; |
| 931 |
|
| 932 |
function applyScaleDefaults(defaults) { |
| 933 |
defaults.set('scale', { |
| 934 |
display: true, |
| 935 |
offset: false, |
| 936 |
reverse: false, |
| 937 |
beginAtZero: false, |
| 938 |
bounds: 'ticks', |
| 939 |
clip: true, |
| 940 |
grace: 0, |
| 941 |
grid: { |
| 942 |
display: true, |
| 943 |
lineWidth: 1, |
| 944 |
drawOnChartArea: true, |
| 945 |
drawTicks: true, |
| 946 |
tickLength: 8, |
| 947 |
tickWidth: (_ctx, options)=>options.lineWidth, |
| 948 |
tickColor: (_ctx, options)=>options.color, |
| 949 |
offset: false |
| 950 |
}, |
| 951 |
border: { |
| 952 |
display: true, |
| 953 |
dash: [], |
| 954 |
dashOffset: 0.0, |
| 955 |
width: 1 |
| 956 |
}, |
| 957 |
title: { |
| 958 |
display: false, |
| 959 |
text: '', |
| 960 |
padding: { |
| 961 |
top: 4, |
| 962 |
bottom: 4 |
| 963 |
} |
| 964 |
}, |
| 965 |
ticks: { |
| 966 |
minRotation: 0, |
| 967 |
maxRotation: 50, |
| 968 |
mirror: false, |
| 969 |
textStrokeWidth: 0, |
| 970 |
textStrokeColor: '', |
| 971 |
padding: 3, |
| 972 |
display: true, |
| 973 |
autoSkip: true, |
| 974 |
autoSkipPadding: 3, |
| 975 |
labelOffset: 0, |
| 976 |
callback: Ticks.formatters.values, |
| 977 |
minor: {}, |
| 978 |
major: {}, |
| 979 |
align: 'center', |
| 980 |
crossAlign: 'near', |
| 981 |
showLabelBackdrop: false, |
| 982 |
backdropColor: 'rgba(255, 255, 255, 0.75)', |
| 983 |
backdropPadding: 2 |
| 984 |
} |
| 985 |
}); |
| 986 |
defaults.route('scale.ticks', 'color', '', 'color'); |
| 987 |
defaults.route('scale.grid', 'color', '', 'borderColor'); |
| 988 |
defaults.route('scale.border', 'color', '', 'borderColor'); |
| 989 |
defaults.route('scale.title', 'color', '', 'color'); |
| 990 |
defaults.describe('scale', { |
| 991 |
_fallback: false, |
| 992 |
_scriptable: (name)=>!name.startsWith('before') && !name.startsWith('after') && name !== 'callback' && name !== 'parser', |
| 993 |
_indexable: (name)=>name !== 'borderDash' && name !== 'tickBorderDash' && name !== 'dash' |
| 994 |
}); |
| 995 |
defaults.describe('scales', { |
| 996 |
_fallback: 'scale' |
| 997 |
}); |
| 998 |
defaults.describe('scale.ticks', { |
| 999 |
_scriptable: (name)=>name !== 'backdropPadding' && name !== 'callback', |
| 1000 |
_indexable: (name)=>name !== 'backdropPadding' |
| 1001 |
}); |
| 1002 |
} |
| 1003 |
|
| 1004 |
const overrides = Object.create(null); |
| 1005 |
const descriptors = Object.create(null); |
| 1006 |
function getScope$1(node, key) { |
| 1007 |
if (!key) { |
| 1008 |
return node; |
| 1009 |
} |
| 1010 |
const keys = key.split('.'); |
| 1011 |
for(let i = 0, n = keys.length; i < n; ++i){ |
| 1012 |
const k = keys[i]; |
| 1013 |
node = node[k] || (node[k] = Object.create(null)); |
| 1014 |
} |
| 1015 |
return node; |
| 1016 |
} |
| 1017 |
function set(root, scope, values) { |
| 1018 |
if (typeof scope === 'string') { |
| 1019 |
return merge(getScope$1(root, scope), values); |
| 1020 |
} |
| 1021 |
return merge(getScope$1(root, ''), scope); |
| 1022 |
} |
| 1023 |
class Defaults { |
| 1024 |
constructor(_descriptors, _appliers){ |
| 1025 |
this.animation = undefined; |
| 1026 |
this.backgroundColor = 'rgba(0,0,0,0.1)'; |
| 1027 |
this.borderColor = 'rgba(0,0,0,0.1)'; |
| 1028 |
this.color = '#666'; |
| 1029 |
this.datasets = {}; |
| 1030 |
this.devicePixelRatio = (context)=>context.chart.platform.getDevicePixelRatio(); |
| 1031 |
this.elements = {}; |
| 1032 |
this.events = [ |
| 1033 |
'mousemove', |
| 1034 |
'mouseout', |
| 1035 |
'click', |
| 1036 |
'touchstart', |
| 1037 |
'touchmove' |
| 1038 |
]; |
| 1039 |
this.font = { |
| 1040 |
family: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", |
| 1041 |
size: 12, |
| 1042 |
style: 'normal', |
| 1043 |
lineHeight: 1.2, |
| 1044 |
weight: null |
| 1045 |
}; |
| 1046 |
this.hover = {}; |
| 1047 |
this.hoverBackgroundColor = (ctx, options)=>getHoverColor(options.backgroundColor); |
| 1048 |
this.hoverBorderColor = (ctx, options)=>getHoverColor(options.borderColor); |
| 1049 |
this.hoverColor = (ctx, options)=>getHoverColor(options.color); |
| 1050 |
this.indexAxis = 'x'; |
| 1051 |
this.interaction = { |
| 1052 |
mode: 'nearest', |
| 1053 |
intersect: true, |
| 1054 |
includeInvisible: false |
| 1055 |
}; |
| 1056 |
this.maintainAspectRatio = true; |
| 1057 |
this.onHover = null; |
| 1058 |
this.onClick = null; |
| 1059 |
this.parsing = true; |
| 1060 |
this.plugins = {}; |
| 1061 |
this.responsive = true; |
| 1062 |
this.scale = undefined; |
| 1063 |
this.scales = {}; |
| 1064 |
this.showLine = true; |
| 1065 |
this.drawActiveElementsOnTop = true; |
| 1066 |
this.describe(_descriptors); |
| 1067 |
this.apply(_appliers); |
| 1068 |
} |
| 1069 |
set(scope, values) { |
| 1070 |
return set(this, scope, values); |
| 1071 |
} |
| 1072 |
get(scope) { |
| 1073 |
return getScope$1(this, scope); |
| 1074 |
} |
| 1075 |
describe(scope, values) { |
| 1076 |
return set(descriptors, scope, values); |
| 1077 |
} |
| 1078 |
override(scope, values) { |
| 1079 |
return set(overrides, scope, values); |
| 1080 |
} |
| 1081 |
route(scope, name, targetScope, targetName) { |
| 1082 |
const scopeObject = getScope$1(this, scope); |
| 1083 |
const targetScopeObject = getScope$1(this, targetScope); |
| 1084 |
const privateName = '_' + name; |
| 1085 |
Object.defineProperties(scopeObject, { |
| 1086 |
[privateName]: { |
| 1087 |
value: scopeObject[name], |
| 1088 |
writable: true |
| 1089 |
}, |
| 1090 |
[name]: { |
| 1091 |
enumerable: true, |
| 1092 |
get () { |
| 1093 |
const local = this[privateName]; |
| 1094 |
const target = targetScopeObject[targetName]; |
| 1095 |
if (isObject(local)) { |
| 1096 |
return Object.assign({}, target, local); |
| 1097 |
} |
| 1098 |
return valueOrDefault(local, target); |
| 1099 |
}, |
| 1100 |
set (value) { |
| 1101 |
this[privateName] = value; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
}); |
| 1105 |
} |
| 1106 |
apply(appliers) { |
| 1107 |
appliers.forEach((apply)=>apply(this)); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
var defaults = /* #__PURE__ */ new Defaults({ |
| 1111 |
_scriptable: (name)=>!name.startsWith('on'), |
| 1112 |
_indexable: (name)=>name !== 'events', |
| 1113 |
hover: { |
| 1114 |
_fallback: 'interaction' |
| 1115 |
}, |
| 1116 |
interaction: { |
| 1117 |
_scriptable: false, |
| 1118 |
_indexable: false |
| 1119 |
} |
| 1120 |
}, [ |
| 1121 |
applyAnimationsDefaults, |
| 1122 |
applyLayoutsDefaults, |
| 1123 |
applyScaleDefaults |
| 1124 |
]); |
| 1125 |
|
| 1126 |
/** |
| 1127 |
* Converts the given font object into a CSS font string. |
| 1128 |
* @param font - A font object. |
| 1129 |
* @return The CSS font string. See https://developer.mozilla.org/en-US/docs/Web/CSS/font |
| 1130 |
* @private |
| 1131 |
*/ function toFontString(font) { |
| 1132 |
if (!font || isNullOrUndef(font.size) || isNullOrUndef(font.family)) { |
| 1133 |
return null; |
| 1134 |
} |
| 1135 |
return (font.style ? font.style + ' ' : '') + (font.weight ? font.weight + ' ' : '') + font.size + 'px ' + font.family; |
| 1136 |
} |
| 1137 |
/** |
| 1138 |
* @private |
| 1139 |
*/ function _measureText(ctx, data, gc, longest, string) { |
| 1140 |
let textWidth = data[string]; |
| 1141 |
if (!textWidth) { |
| 1142 |
textWidth = data[string] = ctx.measureText(string).width; |
| 1143 |
gc.push(string); |
| 1144 |
} |
| 1145 |
if (textWidth > longest) { |
| 1146 |
longest = textWidth; |
| 1147 |
} |
| 1148 |
return longest; |
| 1149 |
} |
| 1150 |
/** |
| 1151 |
* @private |
| 1152 |
*/ // eslint-disable-next-line complexity |
| 1153 |
function _longestText(ctx, font, arrayOfThings, cache) { |
| 1154 |
cache = cache || {}; |
| 1155 |
let data = cache.data = cache.data || {}; |
| 1156 |
let gc = cache.garbageCollect = cache.garbageCollect || []; |
| 1157 |
if (cache.font !== font) { |
| 1158 |
data = cache.data = {}; |
| 1159 |
gc = cache.garbageCollect = []; |
| 1160 |
cache.font = font; |
| 1161 |
} |
| 1162 |
ctx.save(); |
| 1163 |
ctx.font = font; |
| 1164 |
let longest = 0; |
| 1165 |
const ilen = arrayOfThings.length; |
| 1166 |
let i, j, jlen, thing, nestedThing; |
| 1167 |
for(i = 0; i < ilen; i++){ |
| 1168 |
thing = arrayOfThings[i]; |
| 1169 |
// Undefined strings and arrays should not be measured |
| 1170 |
if (thing !== undefined && thing !== null && !isArray(thing)) { |
| 1171 |
longest = _measureText(ctx, data, gc, longest, thing); |
| 1172 |
} else if (isArray(thing)) { |
| 1173 |
// if it is an array lets measure each element |
| 1174 |
// to do maybe simplify this function a bit so we can do this more recursively? |
| 1175 |
for(j = 0, jlen = thing.length; j < jlen; j++){ |
| 1176 |
nestedThing = thing[j]; |
| 1177 |
// Undefined strings and arrays should not be measured |
| 1178 |
if (nestedThing !== undefined && nestedThing !== null && !isArray(nestedThing)) { |
| 1179 |
longest = _measureText(ctx, data, gc, longest, nestedThing); |
| 1180 |
} |
| 1181 |
} |
| 1182 |
} |
| 1183 |
} |
| 1184 |
ctx.restore(); |
| 1185 |
const gcLen = gc.length / 2; |
| 1186 |
if (gcLen > arrayOfThings.length) { |
| 1187 |
for(i = 0; i < gcLen; i++){ |
| 1188 |
delete data[gc[i]]; |
| 1189 |
} |
| 1190 |
gc.splice(0, gcLen); |
| 1191 |
} |
| 1192 |
return longest; |
| 1193 |
} |
| 1194 |
/** |
| 1195 |
* Returns the aligned pixel value to avoid anti-aliasing blur |
| 1196 |
* @param chart - The chart instance. |
| 1197 |
* @param pixel - A pixel value. |
| 1198 |
* @param width - The width of the element. |
| 1199 |
* @returns The aligned pixel value. |
| 1200 |
* @private |
| 1201 |
*/ function _alignPixel(chart, pixel, width) { |
| 1202 |
const devicePixelRatio = chart.currentDevicePixelRatio; |
| 1203 |
const halfWidth = width !== 0 ? Math.max(width / 2, 0.5) : 0; |
| 1204 |
return Math.round((pixel - halfWidth) * devicePixelRatio) / devicePixelRatio + halfWidth; |
| 1205 |
} |
| 1206 |
/** |
| 1207 |
* Clears the entire canvas. |
| 1208 |
*/ function clearCanvas(canvas, ctx) { |
| 1209 |
if (!ctx && !canvas) { |
| 1210 |
return; |
| 1211 |
} |
| 1212 |
ctx = ctx || canvas.getContext('2d'); |
| 1213 |
ctx.save(); |
| 1214 |
// canvas.width and canvas.height do not consider the canvas transform, |
| 1215 |
// while clearRect does |
| 1216 |
ctx.resetTransform(); |
| 1217 |
ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 1218 |
ctx.restore(); |
| 1219 |
} |
| 1220 |
function drawPoint(ctx, options, x, y) { |
| 1221 |
// eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 1222 |
drawPointLegend(ctx, options, x, y, null); |
| 1223 |
} |
| 1224 |
// eslint-disable-next-line complexity |
| 1225 |
function drawPointLegend(ctx, options, x, y, w) { |
| 1226 |
let type, xOffset, yOffset, size, cornerRadius, width, xOffsetW, yOffsetW; |
| 1227 |
const style = options.pointStyle; |
| 1228 |
const rotation = options.rotation; |
| 1229 |
const radius = options.radius; |
| 1230 |
let rad = (rotation || 0) * RAD_PER_DEG; |
| 1231 |
if (style && typeof style === 'object') { |
| 1232 |
type = style.toString(); |
| 1233 |
if (type === '[object HTMLImageElement]' || type === '[object HTMLCanvasElement]') { |
| 1234 |
ctx.save(); |
| 1235 |
ctx.translate(x, y); |
| 1236 |
ctx.rotate(rad); |
| 1237 |
ctx.drawImage(style, -style.width / 2, -style.height / 2, style.width, style.height); |
| 1238 |
ctx.restore(); |
| 1239 |
return; |
| 1240 |
} |
| 1241 |
} |
| 1242 |
if (isNaN(radius) || radius <= 0) { |
| 1243 |
return; |
| 1244 |
} |
| 1245 |
ctx.beginPath(); |
| 1246 |
switch(style){ |
| 1247 |
// Default includes circle |
| 1248 |
default: |
| 1249 |
if (w) { |
| 1250 |
ctx.ellipse(x, y, w / 2, radius, 0, 0, TAU); |
| 1251 |
} else { |
| 1252 |
ctx.arc(x, y, radius, 0, TAU); |
| 1253 |
} |
| 1254 |
ctx.closePath(); |
| 1255 |
break; |
| 1256 |
case 'triangle': |
| 1257 |
width = w ? w / 2 : radius; |
| 1258 |
ctx.moveTo(x + Math.sin(rad) * width, y - Math.cos(rad) * radius); |
| 1259 |
rad += TWO_THIRDS_PI; |
| 1260 |
ctx.lineTo(x + Math.sin(rad) * width, y - Math.cos(rad) * radius); |
| 1261 |
rad += TWO_THIRDS_PI; |
| 1262 |
ctx.lineTo(x + Math.sin(rad) * width, y - Math.cos(rad) * radius); |
| 1263 |
ctx.closePath(); |
| 1264 |
break; |
| 1265 |
case 'rectRounded': |
| 1266 |
// NOTE: the rounded rect implementation changed to use `arc` instead of |
| 1267 |
// `quadraticCurveTo` since it generates better results when rect is |
| 1268 |
// almost a circle. 0.516 (instead of 0.5) produces results with visually |
| 1269 |
// closer proportion to the previous impl and it is inscribed in the |
| 1270 |
// circle with `radius`. For more details, see the following PRs: |
| 1271 |
// https://github.com/chartjs/Chart.js/issues/5597 |
| 1272 |
// https://github.com/chartjs/Chart.js/issues/5858 |
| 1273 |
cornerRadius = radius * 0.516; |
| 1274 |
size = radius - cornerRadius; |
| 1275 |
xOffset = Math.cos(rad + QUARTER_PI) * size; |
| 1276 |
xOffsetW = Math.cos(rad + QUARTER_PI) * (w ? w / 2 - cornerRadius : size); |
| 1277 |
yOffset = Math.sin(rad + QUARTER_PI) * size; |
| 1278 |
yOffsetW = Math.sin(rad + QUARTER_PI) * (w ? w / 2 - cornerRadius : size); |
| 1279 |
ctx.arc(x - xOffsetW, y - yOffset, cornerRadius, rad - PI, rad - HALF_PI); |
| 1280 |
ctx.arc(x + yOffsetW, y - xOffset, cornerRadius, rad - HALF_PI, rad); |
| 1281 |
ctx.arc(x + xOffsetW, y + yOffset, cornerRadius, rad, rad + HALF_PI); |
| 1282 |
ctx.arc(x - yOffsetW, y + xOffset, cornerRadius, rad + HALF_PI, rad + PI); |
| 1283 |
ctx.closePath(); |
| 1284 |
break; |
| 1285 |
case 'rect': |
| 1286 |
if (!rotation) { |
| 1287 |
size = Math.SQRT1_2 * radius; |
| 1288 |
width = w ? w / 2 : size; |
| 1289 |
ctx.rect(x - width, y - size, 2 * width, 2 * size); |
| 1290 |
break; |
| 1291 |
} |
| 1292 |
rad += QUARTER_PI; |
| 1293 |
/* falls through */ case 'rectRot': |
| 1294 |
xOffsetW = Math.cos(rad) * (w ? w / 2 : radius); |
| 1295 |
xOffset = Math.cos(rad) * radius; |
| 1296 |
yOffset = Math.sin(rad) * radius; |
| 1297 |
yOffsetW = Math.sin(rad) * (w ? w / 2 : radius); |
| 1298 |
ctx.moveTo(x - xOffsetW, y - yOffset); |
| 1299 |
ctx.lineTo(x + yOffsetW, y - xOffset); |
| 1300 |
ctx.lineTo(x + xOffsetW, y + yOffset); |
| 1301 |
ctx.lineTo(x - yOffsetW, y + xOffset); |
| 1302 |
ctx.closePath(); |
| 1303 |
break; |
| 1304 |
case 'crossRot': |
| 1305 |
rad += QUARTER_PI; |
| 1306 |
/* falls through */ case 'cross': |
| 1307 |
xOffsetW = Math.cos(rad) * (w ? w / 2 : radius); |
| 1308 |
xOffset = Math.cos(rad) * radius; |
| 1309 |
yOffset = Math.sin(rad) * radius; |
| 1310 |
yOffsetW = Math.sin(rad) * (w ? w / 2 : radius); |
| 1311 |
ctx.moveTo(x - xOffsetW, y - yOffset); |
| 1312 |
ctx.lineTo(x + xOffsetW, y + yOffset); |
| 1313 |
ctx.moveTo(x + yOffsetW, y - xOffset); |
| 1314 |
ctx.lineTo(x - yOffsetW, y + xOffset); |
| 1315 |
break; |
| 1316 |
case 'star': |
| 1317 |
xOffsetW = Math.cos(rad) * (w ? w / 2 : radius); |
| 1318 |
xOffset = Math.cos(rad) * radius; |
| 1319 |
yOffset = Math.sin(rad) * radius; |
| 1320 |
yOffsetW = Math.sin(rad) * (w ? w / 2 : radius); |
| 1321 |
ctx.moveTo(x - xOffsetW, y - yOffset); |
| 1322 |
ctx.lineTo(x + xOffsetW, y + yOffset); |
| 1323 |
ctx.moveTo(x + yOffsetW, y - xOffset); |
| 1324 |
ctx.lineTo(x - yOffsetW, y + xOffset); |
| 1325 |
rad += QUARTER_PI; |
| 1326 |
xOffsetW = Math.cos(rad) * (w ? w / 2 : radius); |
| 1327 |
xOffset = Math.cos(rad) * radius; |
| 1328 |
yOffset = Math.sin(rad) * radius; |
| 1329 |
yOffsetW = Math.sin(rad) * (w ? w / 2 : radius); |
| 1330 |
ctx.moveTo(x - xOffsetW, y - yOffset); |
| 1331 |
ctx.lineTo(x + xOffsetW, y + yOffset); |
| 1332 |
ctx.moveTo(x + yOffsetW, y - xOffset); |
| 1333 |
ctx.lineTo(x - yOffsetW, y + xOffset); |
| 1334 |
break; |
| 1335 |
case 'line': |
| 1336 |
xOffset = w ? w / 2 : Math.cos(rad) * radius; |
| 1337 |
yOffset = Math.sin(rad) * radius; |
| 1338 |
ctx.moveTo(x - xOffset, y - yOffset); |
| 1339 |
ctx.lineTo(x + xOffset, y + yOffset); |
| 1340 |
break; |
| 1341 |
case 'dash': |
| 1342 |
ctx.moveTo(x, y); |
| 1343 |
ctx.lineTo(x + Math.cos(rad) * (w ? w / 2 : radius), y + Math.sin(rad) * radius); |
| 1344 |
break; |
| 1345 |
case false: |
| 1346 |
ctx.closePath(); |
| 1347 |
break; |
| 1348 |
} |
| 1349 |
ctx.fill(); |
| 1350 |
if (options.borderWidth > 0) { |
| 1351 |
ctx.stroke(); |
| 1352 |
} |
| 1353 |
} |
| 1354 |
/** |
| 1355 |
* Returns true if the point is inside the rectangle |
| 1356 |
* @param point - The point to test |
| 1357 |
* @param area - The rectangle |
| 1358 |
* @param margin - allowed margin |
| 1359 |
* @private |
| 1360 |
*/ function _isPointInArea(point, area, margin) { |
| 1361 |
margin = margin || 0.5; // margin - default is to match rounded decimals |
| 1362 |
return !area || point && point.x > area.left - margin && point.x < area.right + margin && point.y > area.top - margin && point.y < area.bottom + margin; |
| 1363 |
} |
| 1364 |
function clipArea(ctx, area) { |
| 1365 |
ctx.save(); |
| 1366 |
ctx.beginPath(); |
| 1367 |
ctx.rect(area.left, area.top, area.right - area.left, area.bottom - area.top); |
| 1368 |
ctx.clip(); |
| 1369 |
} |
| 1370 |
function unclipArea(ctx) { |
| 1371 |
ctx.restore(); |
| 1372 |
} |
| 1373 |
/** |
| 1374 |
* @private |
| 1375 |
*/ function _steppedLineTo(ctx, previous, target, flip, mode) { |
| 1376 |
if (!previous) { |
| 1377 |
return ctx.lineTo(target.x, target.y); |
| 1378 |
} |
| 1379 |
if (mode === 'middle') { |
| 1380 |
const midpoint = (previous.x + target.x) / 2.0; |
| 1381 |
ctx.lineTo(midpoint, previous.y); |
| 1382 |
ctx.lineTo(midpoint, target.y); |
| 1383 |
} else if (mode === 'after' !== !!flip) { |
| 1384 |
ctx.lineTo(previous.x, target.y); |
| 1385 |
} else { |
| 1386 |
ctx.lineTo(target.x, previous.y); |
| 1387 |
} |
| 1388 |
ctx.lineTo(target.x, target.y); |
| 1389 |
} |
| 1390 |
/** |
| 1391 |
* @private |
| 1392 |
*/ function _bezierCurveTo(ctx, previous, target, flip) { |
| 1393 |
if (!previous) { |
| 1394 |
return ctx.lineTo(target.x, target.y); |
| 1395 |
} |
| 1396 |
ctx.bezierCurveTo(flip ? previous.cp1x : previous.cp2x, flip ? previous.cp1y : previous.cp2y, flip ? target.cp2x : target.cp1x, flip ? target.cp2y : target.cp1y, target.x, target.y); |
| 1397 |
} |
| 1398 |
function setRenderOpts(ctx, opts) { |
| 1399 |
if (opts.translation) { |
| 1400 |
ctx.translate(opts.translation[0], opts.translation[1]); |
| 1401 |
} |
| 1402 |
if (!isNullOrUndef(opts.rotation)) { |
| 1403 |
ctx.rotate(opts.rotation); |
| 1404 |
} |
| 1405 |
if (opts.color) { |
| 1406 |
ctx.fillStyle = opts.color; |
| 1407 |
} |
| 1408 |
if (opts.textAlign) { |
| 1409 |
ctx.textAlign = opts.textAlign; |
| 1410 |
} |
| 1411 |
if (opts.textBaseline) { |
| 1412 |
ctx.textBaseline = opts.textBaseline; |
| 1413 |
} |
| 1414 |
} |
| 1415 |
function decorateText(ctx, x, y, line, opts) { |
| 1416 |
if (opts.strikethrough || opts.underline) { |
| 1417 |
/** |
| 1418 |
* Now that IE11 support has been dropped, we can use more |
| 1419 |
* of the TextMetrics object. The actual bounding boxes |
| 1420 |
* are unflagged in Chrome, Firefox, Edge, and Safari so they |
| 1421 |
* can be safely used. |
| 1422 |
* See https://developer.mozilla.org/en-US/docs/Web/API/TextMetrics#Browser_compatibility |
| 1423 |
*/ const metrics = ctx.measureText(line); |
| 1424 |
const left = x - metrics.actualBoundingBoxLeft; |
| 1425 |
const right = x + metrics.actualBoundingBoxRight; |
| 1426 |
const top = y - metrics.actualBoundingBoxAscent; |
| 1427 |
const bottom = y + metrics.actualBoundingBoxDescent; |
| 1428 |
const yDecoration = opts.strikethrough ? (top + bottom) / 2 : bottom; |
| 1429 |
ctx.strokeStyle = ctx.fillStyle; |
| 1430 |
ctx.beginPath(); |
| 1431 |
ctx.lineWidth = opts.decorationWidth || 2; |
| 1432 |
ctx.moveTo(left, yDecoration); |
| 1433 |
ctx.lineTo(right, yDecoration); |
| 1434 |
ctx.stroke(); |
| 1435 |
} |
| 1436 |
} |
| 1437 |
function drawBackdrop(ctx, opts) { |
| 1438 |
const oldColor = ctx.fillStyle; |
| 1439 |
ctx.fillStyle = opts.color; |
| 1440 |
ctx.fillRect(opts.left, opts.top, opts.width, opts.height); |
| 1441 |
ctx.fillStyle = oldColor; |
| 1442 |
} |
| 1443 |
/** |
| 1444 |
* Render text onto the canvas |
| 1445 |
*/ function renderText(ctx, text, x, y, font, opts = {}) { |
| 1446 |
const lines = isArray(text) ? text : [ |
| 1447 |
text |
| 1448 |
]; |
| 1449 |
const stroke = opts.strokeWidth > 0 && opts.strokeColor !== ''; |
| 1450 |
let i, line; |
| 1451 |
ctx.save(); |
| 1452 |
ctx.font = font.string; |
| 1453 |
setRenderOpts(ctx, opts); |
| 1454 |
for(i = 0; i < lines.length; ++i){ |
| 1455 |
line = lines[i]; |
| 1456 |
if (opts.backdrop) { |
| 1457 |
drawBackdrop(ctx, opts.backdrop); |
| 1458 |
} |
| 1459 |
if (stroke) { |
| 1460 |
if (opts.strokeColor) { |
| 1461 |
ctx.strokeStyle = opts.strokeColor; |
| 1462 |
} |
| 1463 |
if (!isNullOrUndef(opts.strokeWidth)) { |
| 1464 |
ctx.lineWidth = opts.strokeWidth; |
| 1465 |
} |
| 1466 |
ctx.strokeText(line, x, y, opts.maxWidth); |
| 1467 |
} |
| 1468 |
ctx.fillText(line, x, y, opts.maxWidth); |
| 1469 |
decorateText(ctx, x, y, line, opts); |
| 1470 |
y += Number(font.lineHeight); |
| 1471 |
} |
| 1472 |
ctx.restore(); |
| 1473 |
} |
| 1474 |
/** |
| 1475 |
* Add a path of a rectangle with rounded corners to the current sub-path |
| 1476 |
* @param ctx - Context |
| 1477 |
* @param rect - Bounding rect |
| 1478 |
*/ function addRoundedRectPath(ctx, rect) { |
| 1479 |
const { x , y , w , h , radius } = rect; |
| 1480 |
// top left arc |
| 1481 |
ctx.arc(x + radius.topLeft, y + radius.topLeft, radius.topLeft, 1.5 * PI, PI, true); |
| 1482 |
// line from top left to bottom left |
| 1483 |
ctx.lineTo(x, y + h - radius.bottomLeft); |
| 1484 |
// bottom left arc |
| 1485 |
ctx.arc(x + radius.bottomLeft, y + h - radius.bottomLeft, radius.bottomLeft, PI, HALF_PI, true); |
| 1486 |
// line from bottom left to bottom right |
| 1487 |
ctx.lineTo(x + w - radius.bottomRight, y + h); |
| 1488 |
// bottom right arc |
| 1489 |
ctx.arc(x + w - radius.bottomRight, y + h - radius.bottomRight, radius.bottomRight, HALF_PI, 0, true); |
| 1490 |
// line from bottom right to top right |
| 1491 |
ctx.lineTo(x + w, y + radius.topRight); |
| 1492 |
// top right arc |
| 1493 |
ctx.arc(x + w - radius.topRight, y + radius.topRight, radius.topRight, 0, -HALF_PI, true); |
| 1494 |
// line from top right to top left |
| 1495 |
ctx.lineTo(x + radius.topLeft, y); |
| 1496 |
} |
| 1497 |
|
| 1498 |
const LINE_HEIGHT = /^(normal|(\d+(?:\.\d+)?)(px|em|%)?)$/; |
| 1499 |
const FONT_STYLE = /^(normal|italic|initial|inherit|unset|(oblique( -?[0-9]?[0-9]deg)?))$/; |
| 1500 |
/** |
| 1501 |
* @alias Chart.helpers.options |
| 1502 |
* @namespace |
| 1503 |
*/ /** |
| 1504 |
* Converts the given line height `value` in pixels for a specific font `size`. |
| 1505 |
* @param value - The lineHeight to parse (eg. 1.6, '14px', '75%', '1.6em'). |
| 1506 |
* @param size - The font size (in pixels) used to resolve relative `value`. |
| 1507 |
* @returns The effective line height in pixels (size * 1.2 if value is invalid). |
| 1508 |
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/line-height |
| 1509 |
* @since 2.7.0 |
| 1510 |
*/ function toLineHeight(value, size) { |
| 1511 |
const matches = ('' + value).match(LINE_HEIGHT); |
| 1512 |
if (!matches || matches[1] === 'normal') { |
| 1513 |
return size * 1.2; |
| 1514 |
} |
| 1515 |
value = +matches[2]; |
| 1516 |
switch(matches[3]){ |
| 1517 |
case 'px': |
| 1518 |
return value; |
| 1519 |
case '%': |
| 1520 |
value /= 100; |
| 1521 |
break; |
| 1522 |
} |
| 1523 |
return size * value; |
| 1524 |
} |
| 1525 |
const numberOrZero = (v)=>+v || 0; |
| 1526 |
function _readValueToProps(value, props) { |
| 1527 |
const ret = {}; |
| 1528 |
const objProps = isObject(props); |
| 1529 |
const keys = objProps ? Object.keys(props) : props; |
| 1530 |
const read = isObject(value) ? objProps ? (prop)=>valueOrDefault(value[prop], value[props[prop]]) : (prop)=>value[prop] : ()=>value; |
| 1531 |
for (const prop of keys){ |
| 1532 |
ret[prop] = numberOrZero(read(prop)); |
| 1533 |
} |
| 1534 |
return ret; |
| 1535 |
} |
| 1536 |
/** |
| 1537 |
* Converts the given value into a TRBL object. |
| 1538 |
* @param value - If a number, set the value to all TRBL component, |
| 1539 |
* else, if an object, use defined properties and sets undefined ones to 0. |
| 1540 |
* x / y are shorthands for same value for left/right and top/bottom. |
| 1541 |
* @returns The padding values (top, right, bottom, left) |
| 1542 |
* @since 3.0.0 |
| 1543 |
*/ function toTRBL(value) { |
| 1544 |
return _readValueToProps(value, { |
| 1545 |
top: 'y', |
| 1546 |
right: 'x', |
| 1547 |
bottom: 'y', |
| 1548 |
left: 'x' |
| 1549 |
}); |
| 1550 |
} |
| 1551 |
/** |
| 1552 |
* Converts the given value into a TRBL corners object (similar with css border-radius). |
| 1553 |
* @param value - If a number, set the value to all TRBL corner components, |
| 1554 |
* else, if an object, use defined properties and sets undefined ones to 0. |
| 1555 |
* @returns The TRBL corner values (topLeft, topRight, bottomLeft, bottomRight) |
| 1556 |
* @since 3.0.0 |
| 1557 |
*/ function toTRBLCorners(value) { |
| 1558 |
return _readValueToProps(value, [ |
| 1559 |
'topLeft', |
| 1560 |
'topRight', |
| 1561 |
'bottomLeft', |
| 1562 |
'bottomRight' |
| 1563 |
]); |
| 1564 |
} |
| 1565 |
/** |
| 1566 |
* Converts the given value into a padding object with pre-computed width/height. |
| 1567 |
* @param value - If a number, set the value to all TRBL component, |
| 1568 |
* else, if an object, use defined properties and sets undefined ones to 0. |
| 1569 |
* x / y are shorthands for same value for left/right and top/bottom. |
| 1570 |
* @returns The padding values (top, right, bottom, left, width, height) |
| 1571 |
* @since 2.7.0 |
| 1572 |
*/ function toPadding(value) { |
| 1573 |
const obj = toTRBL(value); |
| 1574 |
obj.width = obj.left + obj.right; |
| 1575 |
obj.height = obj.top + obj.bottom; |
| 1576 |
return obj; |
| 1577 |
} |
| 1578 |
/** |
| 1579 |
* Parses font options and returns the font object. |
| 1580 |
* @param options - A object that contains font options to be parsed. |
| 1581 |
* @param fallback - A object that contains fallback font options. |
| 1582 |
* @return The font object. |
| 1583 |
* @private |
| 1584 |
*/ function toFont(options, fallback) { |
| 1585 |
options = options || {}; |
| 1586 |
fallback = fallback || defaults.font; |
| 1587 |
let size = valueOrDefault(options.size, fallback.size); |
| 1588 |
if (typeof size === 'string') { |
| 1589 |
size = parseInt(size, 10); |
| 1590 |
} |
| 1591 |
let style = valueOrDefault(options.style, fallback.style); |
| 1592 |
if (style && !('' + style).match(FONT_STYLE)) { |
| 1593 |
console.warn('Invalid font style specified: "' + style + '"'); |
| 1594 |
style = undefined; |
| 1595 |
} |
| 1596 |
const font = { |
| 1597 |
family: valueOrDefault(options.family, fallback.family), |
| 1598 |
lineHeight: toLineHeight(valueOrDefault(options.lineHeight, fallback.lineHeight), size), |
| 1599 |
size, |
| 1600 |
style, |
| 1601 |
weight: valueOrDefault(options.weight, fallback.weight), |
| 1602 |
string: '' |
| 1603 |
}; |
| 1604 |
font.string = toFontString(font); |
| 1605 |
return font; |
| 1606 |
} |
| 1607 |
/** |
| 1608 |
* Evaluates the given `inputs` sequentially and returns the first defined value. |
| 1609 |
* @param inputs - An array of values, falling back to the last value. |
| 1610 |
* @param context - If defined and the current value is a function, the value |
| 1611 |
* is called with `context` as first argument and the result becomes the new input. |
| 1612 |
* @param index - If defined and the current value is an array, the value |
| 1613 |
* at `index` become the new input. |
| 1614 |
* @param info - object to return information about resolution in |
| 1615 |
* @param info.cacheable - Will be set to `false` if option is not cacheable. |
| 1616 |
* @since 2.7.0 |
| 1617 |
*/ function resolve(inputs, context, index, info) { |
| 1618 |
let cacheable = true; |
| 1619 |
let i, ilen, value; |
| 1620 |
for(i = 0, ilen = inputs.length; i < ilen; ++i){ |
| 1621 |
value = inputs[i]; |
| 1622 |
if (value === undefined) { |
| 1623 |
continue; |
| 1624 |
} |
| 1625 |
if (context !== undefined && typeof value === 'function') { |
| 1626 |
value = value(context); |
| 1627 |
cacheable = false; |
| 1628 |
} |
| 1629 |
if (index !== undefined && isArray(value)) { |
| 1630 |
value = value[index % value.length]; |
| 1631 |
cacheable = false; |
| 1632 |
} |
| 1633 |
if (value !== undefined) { |
| 1634 |
if (info && !cacheable) { |
| 1635 |
info.cacheable = false; |
| 1636 |
} |
| 1637 |
return value; |
| 1638 |
} |
| 1639 |
} |
| 1640 |
} |
| 1641 |
/** |
| 1642 |
* @param minmax |
| 1643 |
* @param grace |
| 1644 |
* @param beginAtZero |
| 1645 |
* @private |
| 1646 |
*/ function _addGrace(minmax, grace, beginAtZero) { |
| 1647 |
const { min , max } = minmax; |
| 1648 |
const change = toDimension(grace, (max - min) / 2); |
| 1649 |
const keepZero = (value, add)=>beginAtZero && value === 0 ? 0 : value + add; |
| 1650 |
return { |
| 1651 |
min: keepZero(min, -Math.abs(change)), |
| 1652 |
max: keepZero(max, change) |
| 1653 |
}; |
| 1654 |
} |
| 1655 |
function createContext(parentContext, context) { |
| 1656 |
return Object.assign(Object.create(parentContext), context); |
| 1657 |
} |
| 1658 |
|
| 1659 |
/** |
| 1660 |
* Creates a Proxy for resolving raw values for options. |
| 1661 |
* @param scopes - The option scopes to look for values, in resolution order |
| 1662 |
* @param prefixes - The prefixes for values, in resolution order. |
| 1663 |
* @param rootScopes - The root option scopes |
| 1664 |
* @param fallback - Parent scopes fallback |
| 1665 |
* @param getTarget - callback for getting the target for changed values |
| 1666 |
* @returns Proxy |
| 1667 |
* @private |
| 1668 |
*/ function _createResolver(scopes, prefixes = [ |
| 1669 |
'' |
| 1670 |
], rootScopes, fallback, getTarget = ()=>scopes[0]) { |
| 1671 |
const finalRootScopes = rootScopes || scopes; |
| 1672 |
if (typeof fallback === 'undefined') { |
| 1673 |
fallback = _resolve('_fallback', scopes); |
| 1674 |
} |
| 1675 |
const cache = { |
| 1676 |
[Symbol.toStringTag]: 'Object', |
| 1677 |
_cacheable: true, |
| 1678 |
_scopes: scopes, |
| 1679 |
_rootScopes: finalRootScopes, |
| 1680 |
_fallback: fallback, |
| 1681 |
_getTarget: getTarget, |
| 1682 |
override: (scope)=>_createResolver([ |
| 1683 |
scope, |
| 1684 |
...scopes |
| 1685 |
], prefixes, finalRootScopes, fallback) |
| 1686 |
}; |
| 1687 |
return new Proxy(cache, { |
| 1688 |
/** |
| 1689 |
* A trap for the delete operator. |
| 1690 |
*/ deleteProperty (target, prop) { |
| 1691 |
delete target[prop]; // remove from cache |
| 1692 |
delete target._keys; // remove cached keys |
| 1693 |
delete scopes[0][prop]; // remove from top level scope |
| 1694 |
return true; |
| 1695 |
}, |
| 1696 |
/** |
| 1697 |
* A trap for getting property values. |
| 1698 |
*/ get (target, prop) { |
| 1699 |
return _cached(target, prop, ()=>_resolveWithPrefixes(prop, prefixes, scopes, target)); |
| 1700 |
}, |
| 1701 |
/** |
| 1702 |
* A trap for Object.getOwnPropertyDescriptor. |
| 1703 |
* Also used by Object.hasOwnProperty. |
| 1704 |
*/ getOwnPropertyDescriptor (target, prop) { |
| 1705 |
return Reflect.getOwnPropertyDescriptor(target._scopes[0], prop); |
| 1706 |
}, |
| 1707 |
/** |
| 1708 |
* A trap for Object.getPrototypeOf. |
| 1709 |
*/ getPrototypeOf () { |
| 1710 |
return Reflect.getPrototypeOf(scopes[0]); |
| 1711 |
}, |
| 1712 |
/** |
| 1713 |
* A trap for the in operator. |
| 1714 |
*/ has (target, prop) { |
| 1715 |
return getKeysFromAllScopes(target).includes(prop); |
| 1716 |
}, |
| 1717 |
/** |
| 1718 |
* A trap for Object.getOwnPropertyNames and Object.getOwnPropertySymbols. |
| 1719 |
*/ ownKeys (target) { |
| 1720 |
return getKeysFromAllScopes(target); |
| 1721 |
}, |
| 1722 |
/** |
| 1723 |
* A trap for setting property values. |
| 1724 |
*/ set (target, prop, value) { |
| 1725 |
const storage = target._storage || (target._storage = getTarget()); |
| 1726 |
target[prop] = storage[prop] = value; // set to top level scope + cache |
| 1727 |
delete target._keys; // remove cached keys |
| 1728 |
return true; |
| 1729 |
} |
| 1730 |
}); |
| 1731 |
} |
| 1732 |
/** |
| 1733 |
* Returns an Proxy for resolving option values with context. |
| 1734 |
* @param proxy - The Proxy returned by `_createResolver` |
| 1735 |
* @param context - Context object for scriptable/indexable options |
| 1736 |
* @param subProxy - The proxy provided for scriptable options |
| 1737 |
* @param descriptorDefaults - Defaults for descriptors |
| 1738 |
* @private |
| 1739 |
*/ function _attachContext(proxy, context, subProxy, descriptorDefaults) { |
| 1740 |
const cache = { |
| 1741 |
_cacheable: false, |
| 1742 |
_proxy: proxy, |
| 1743 |
_context: context, |
| 1744 |
_subProxy: subProxy, |
| 1745 |
_stack: new Set(), |
| 1746 |
_descriptors: _descriptors(proxy, descriptorDefaults), |
| 1747 |
setContext: (ctx)=>_attachContext(proxy, ctx, subProxy, descriptorDefaults), |
| 1748 |
override: (scope)=>_attachContext(proxy.override(scope), context, subProxy, descriptorDefaults) |
| 1749 |
}; |
| 1750 |
return new Proxy(cache, { |
| 1751 |
/** |
| 1752 |
* A trap for the delete operator. |
| 1753 |
*/ deleteProperty (target, prop) { |
| 1754 |
delete target[prop]; // remove from cache |
| 1755 |
delete proxy[prop]; // remove from proxy |
| 1756 |
return true; |
| 1757 |
}, |
| 1758 |
/** |
| 1759 |
* A trap for getting property values. |
| 1760 |
*/ get (target, prop, receiver) { |
| 1761 |
return _cached(target, prop, ()=>_resolveWithContext(target, prop, receiver)); |
| 1762 |
}, |
| 1763 |
/** |
| 1764 |
* A trap for Object.getOwnPropertyDescriptor. |
| 1765 |
* Also used by Object.hasOwnProperty. |
| 1766 |
*/ getOwnPropertyDescriptor (target, prop) { |
| 1767 |
return target._descriptors.allKeys ? Reflect.has(proxy, prop) ? { |
| 1768 |
enumerable: true, |
| 1769 |
configurable: true |
| 1770 |
} : undefined : Reflect.getOwnPropertyDescriptor(proxy, prop); |
| 1771 |
}, |
| 1772 |
/** |
| 1773 |
* A trap for Object.getPrototypeOf. |
| 1774 |
*/ getPrototypeOf () { |
| 1775 |
return Reflect.getPrototypeOf(proxy); |
| 1776 |
}, |
| 1777 |
/** |
| 1778 |
* A trap for the in operator. |
| 1779 |
*/ has (target, prop) { |
| 1780 |
return Reflect.has(proxy, prop); |
| 1781 |
}, |
| 1782 |
/** |
| 1783 |
* A trap for Object.getOwnPropertyNames and Object.getOwnPropertySymbols. |
| 1784 |
*/ ownKeys () { |
| 1785 |
return Reflect.ownKeys(proxy); |
| 1786 |
}, |
| 1787 |
/** |
| 1788 |
* A trap for setting property values. |
| 1789 |
*/ set (target, prop, value) { |
| 1790 |
proxy[prop] = value; // set to proxy |
| 1791 |
delete target[prop]; // remove from cache |
| 1792 |
return true; |
| 1793 |
} |
| 1794 |
}); |
| 1795 |
} |
| 1796 |
/** |
| 1797 |
* @private |
| 1798 |
*/ function _descriptors(proxy, defaults = { |
| 1799 |
scriptable: true, |
| 1800 |
indexable: true |
| 1801 |
}) { |
| 1802 |
const { _scriptable =defaults.scriptable , _indexable =defaults.indexable , _allKeys =defaults.allKeys } = proxy; |
| 1803 |
return { |
| 1804 |
allKeys: _allKeys, |
| 1805 |
scriptable: _scriptable, |
| 1806 |
indexable: _indexable, |
| 1807 |
isScriptable: isFunction(_scriptable) ? _scriptable : ()=>_scriptable, |
| 1808 |
isIndexable: isFunction(_indexable) ? _indexable : ()=>_indexable |
| 1809 |
}; |
| 1810 |
} |
| 1811 |
const readKey = (prefix, name)=>prefix ? prefix + _capitalize(name) : name; |
| 1812 |
const needsSubResolver = (prop, value)=>isObject(value) && prop !== 'adapters' && (Object.getPrototypeOf(value) === null || value.constructor === Object); |
| 1813 |
function _cached(target, prop, resolve) { |
| 1814 |
if (Object.prototype.hasOwnProperty.call(target, prop) || prop === 'constructor') { |
| 1815 |
return target[prop]; |
| 1816 |
} |
| 1817 |
const value = resolve(); |
| 1818 |
// cache the resolved value |
| 1819 |
target[prop] = value; |
| 1820 |
return value; |
| 1821 |
} |
| 1822 |
function _resolveWithContext(target, prop, receiver) { |
| 1823 |
const { _proxy , _context , _subProxy , _descriptors: descriptors } = target; |
| 1824 |
let value = _proxy[prop]; // resolve from proxy |
| 1825 |
// resolve with context |
| 1826 |
if (isFunction(value) && descriptors.isScriptable(prop)) { |
| 1827 |
value = _resolveScriptable(prop, value, target, receiver); |
| 1828 |
} |
| 1829 |
if (isArray(value) && value.length) { |
| 1830 |
value = _resolveArray(prop, value, target, descriptors.isIndexable); |
| 1831 |
} |
| 1832 |
if (needsSubResolver(prop, value)) { |
| 1833 |
// if the resolved value is an object, create a sub resolver for it |
| 1834 |
value = _attachContext(value, _context, _subProxy && _subProxy[prop], descriptors); |
| 1835 |
} |
| 1836 |
return value; |
| 1837 |
} |
| 1838 |
function _resolveScriptable(prop, getValue, target, receiver) { |
| 1839 |
const { _proxy , _context , _subProxy , _stack } = target; |
| 1840 |
if (_stack.has(prop)) { |
| 1841 |
throw new Error('Recursion detected: ' + Array.from(_stack).join('->') + '->' + prop); |
| 1842 |
} |
| 1843 |
_stack.add(prop); |
| 1844 |
let value = getValue(_context, _subProxy || receiver); |
| 1845 |
_stack.delete(prop); |
| 1846 |
if (needsSubResolver(prop, value)) { |
| 1847 |
// When scriptable option returns an object, create a resolver on that. |
| 1848 |
value = createSubResolver(_proxy._scopes, _proxy, prop, value); |
| 1849 |
} |
| 1850 |
return value; |
| 1851 |
} |
| 1852 |
function _resolveArray(prop, value, target, isIndexable) { |
| 1853 |
const { _proxy , _context , _subProxy , _descriptors: descriptors } = target; |
| 1854 |
if (typeof _context.index !== 'undefined' && isIndexable(prop)) { |
| 1855 |
return value[_context.index % value.length]; |
| 1856 |
} else if (isObject(value[0])) { |
| 1857 |
// Array of objects, return array or resolvers |
| 1858 |
const arr = value; |
| 1859 |
const scopes = _proxy._scopes.filter((s)=>s !== arr); |
| 1860 |
value = []; |
| 1861 |
for (const item of arr){ |
| 1862 |
const resolver = createSubResolver(scopes, _proxy, prop, item); |
| 1863 |
value.push(_attachContext(resolver, _context, _subProxy && _subProxy[prop], descriptors)); |
| 1864 |
} |
| 1865 |
} |
| 1866 |
return value; |
| 1867 |
} |
| 1868 |
function resolveFallback(fallback, prop, value) { |
| 1869 |
return isFunction(fallback) ? fallback(prop, value) : fallback; |
| 1870 |
} |
| 1871 |
const getScope = (key, parent)=>key === true ? parent : typeof key === 'string' ? resolveObjectKey(parent, key) : undefined; |
| 1872 |
function addScopes(set, parentScopes, key, parentFallback, value) { |
| 1873 |
for (const parent of parentScopes){ |
| 1874 |
const scope = getScope(key, parent); |
| 1875 |
if (scope) { |
| 1876 |
set.add(scope); |
| 1877 |
const fallback = resolveFallback(scope._fallback, key, value); |
| 1878 |
if (typeof fallback !== 'undefined' && fallback !== key && fallback !== parentFallback) { |
| 1879 |
// When we reach the descriptor that defines a new _fallback, return that. |
| 1880 |
// The fallback will resume to that new scope. |
| 1881 |
return fallback; |
| 1882 |
} |
| 1883 |
} else if (scope === false && typeof parentFallback !== 'undefined' && key !== parentFallback) { |
| 1884 |
// Fallback to `false` results to `false`, when falling back to different key. |
| 1885 |
// For example `interaction` from `hover` or `plugins.tooltip` and `animation` from `animations` |
| 1886 |
return null; |
| 1887 |
} |
| 1888 |
} |
| 1889 |
return false; |
| 1890 |
} |
| 1891 |
function createSubResolver(parentScopes, resolver, prop, value) { |
| 1892 |
const rootScopes = resolver._rootScopes; |
| 1893 |
const fallback = resolveFallback(resolver._fallback, prop, value); |
| 1894 |
const allScopes = [ |
| 1895 |
...parentScopes, |
| 1896 |
...rootScopes |
| 1897 |
]; |
| 1898 |
const set = new Set(); |
| 1899 |
set.add(value); |
| 1900 |
let key = addScopesFromKey(set, allScopes, prop, fallback || prop, value); |
| 1901 |
if (key === null) { |
| 1902 |
return false; |
| 1903 |
} |
| 1904 |
if (typeof fallback !== 'undefined' && fallback !== prop) { |
| 1905 |
key = addScopesFromKey(set, allScopes, fallback, key, value); |
| 1906 |
if (key === null) { |
| 1907 |
return false; |
| 1908 |
} |
| 1909 |
} |
| 1910 |
return _createResolver(Array.from(set), [ |
| 1911 |
'' |
| 1912 |
], rootScopes, fallback, ()=>subGetTarget(resolver, prop, value)); |
| 1913 |
} |
| 1914 |
function addScopesFromKey(set, allScopes, key, fallback, item) { |
| 1915 |
while(key){ |
| 1916 |
key = addScopes(set, allScopes, key, fallback, item); |
| 1917 |
} |
| 1918 |
return key; |
| 1919 |
} |
| 1920 |
function subGetTarget(resolver, prop, value) { |
| 1921 |
const parent = resolver._getTarget(); |
| 1922 |
if (!(prop in parent)) { |
| 1923 |
parent[prop] = {}; |
| 1924 |
} |
| 1925 |
const target = parent[prop]; |
| 1926 |
if (isArray(target) && isObject(value)) { |
| 1927 |
// For array of objects, the object is used to store updated values |
| 1928 |
return value; |
| 1929 |
} |
| 1930 |
return target || {}; |
| 1931 |
} |
| 1932 |
function _resolveWithPrefixes(prop, prefixes, scopes, proxy) { |
| 1933 |
let value; |
| 1934 |
for (const prefix of prefixes){ |
| 1935 |
value = _resolve(readKey(prefix, prop), scopes); |
| 1936 |
if (typeof value !== 'undefined') { |
| 1937 |
return needsSubResolver(prop, value) ? createSubResolver(scopes, proxy, prop, value) : value; |
| 1938 |
} |
| 1939 |
} |
| 1940 |
} |
| 1941 |
function _resolve(key, scopes) { |
| 1942 |
for (const scope of scopes){ |
| 1943 |
if (!scope) { |
| 1944 |
continue; |
| 1945 |
} |
| 1946 |
const value = scope[key]; |
| 1947 |
if (typeof value !== 'undefined') { |
| 1948 |
return value; |
| 1949 |
} |
| 1950 |
} |
| 1951 |
} |
| 1952 |
function getKeysFromAllScopes(target) { |
| 1953 |
let keys = target._keys; |
| 1954 |
if (!keys) { |
| 1955 |
keys = target._keys = resolveKeysFromAllScopes(target._scopes); |
| 1956 |
} |
| 1957 |
return keys; |
| 1958 |
} |
| 1959 |
function resolveKeysFromAllScopes(scopes) { |
| 1960 |
const set = new Set(); |
| 1961 |
for (const scope of scopes){ |
| 1962 |
for (const key of Object.keys(scope).filter((k)=>!k.startsWith('_'))){ |
| 1963 |
set.add(key); |
| 1964 |
} |
| 1965 |
} |
| 1966 |
return Array.from(set); |
| 1967 |
} |
| 1968 |
function _parseObjectDataRadialScale(meta, data, start, count) { |
| 1969 |
const { iScale } = meta; |
| 1970 |
const { key ='r' } = this._parsing; |
| 1971 |
const parsed = new Array(count); |
| 1972 |
let i, ilen, index, item; |
| 1973 |
for(i = 0, ilen = count; i < ilen; ++i){ |
| 1974 |
index = i + start; |
| 1975 |
item = data[index]; |
| 1976 |
parsed[i] = { |
| 1977 |
r: iScale.parse(resolveObjectKey(item, key), index) |
| 1978 |
}; |
| 1979 |
} |
| 1980 |
return parsed; |
| 1981 |
} |
| 1982 |
|
| 1983 |
const EPSILON = Number.EPSILON || 1e-14; |
| 1984 |
const getPoint = (points, i)=>i < points.length && !points[i].skip && points[i]; |
| 1985 |
const getValueAxis = (indexAxis)=>indexAxis === 'x' ? 'y' : 'x'; |
| 1986 |
function splineCurve(firstPoint, middlePoint, afterPoint, t) { |
| 1987 |
// Props to Rob Spencer at scaled innovation for his post on splining between points |
| 1988 |
// http://scaledinnovation.com/analytics/splines/aboutSplines.html |
| 1989 |
// This function must also respect "skipped" points |
| 1990 |
const previous = firstPoint.skip ? middlePoint : firstPoint; |
| 1991 |
const current = middlePoint; |
| 1992 |
const next = afterPoint.skip ? middlePoint : afterPoint; |
| 1993 |
const d01 = distanceBetweenPoints(current, previous); |
| 1994 |
const d12 = distanceBetweenPoints(next, current); |
| 1995 |
let s01 = d01 / (d01 + d12); |
| 1996 |
let s12 = d12 / (d01 + d12); |
| 1997 |
// If all points are the same, s01 & s02 will be inf |
| 1998 |
s01 = isNaN(s01) ? 0 : s01; |
| 1999 |
s12 = isNaN(s12) ? 0 : s12; |
| 2000 |
const fa = t * s01; // scaling factor for triangle Ta |
| 2001 |
const fb = t * s12; |
| 2002 |
return { |
| 2003 |
previous: { |
| 2004 |
x: current.x - fa * (next.x - previous.x), |
| 2005 |
y: current.y - fa * (next.y - previous.y) |
| 2006 |
}, |
| 2007 |
next: { |
| 2008 |
x: current.x + fb * (next.x - previous.x), |
| 2009 |
y: current.y + fb * (next.y - previous.y) |
| 2010 |
} |
| 2011 |
}; |
| 2012 |
} |
| 2013 |
/** |
| 2014 |
* Adjust tangents to ensure monotonic properties |
| 2015 |
*/ function monotoneAdjust(points, deltaK, mK) { |
| 2016 |
const pointsLen = points.length; |
| 2017 |
let alphaK, betaK, tauK, squaredMagnitude, pointCurrent; |
| 2018 |
let pointAfter = getPoint(points, 0); |
| 2019 |
for(let i = 0; i < pointsLen - 1; ++i){ |
| 2020 |
pointCurrent = pointAfter; |
| 2021 |
pointAfter = getPoint(points, i + 1); |
| 2022 |
if (!pointCurrent || !pointAfter) { |
| 2023 |
continue; |
| 2024 |
} |
| 2025 |
if (almostEquals(deltaK[i], 0, EPSILON)) { |
| 2026 |
mK[i] = mK[i + 1] = 0; |
| 2027 |
continue; |
| 2028 |
} |
| 2029 |
alphaK = mK[i] / deltaK[i]; |
| 2030 |
betaK = mK[i + 1] / deltaK[i]; |
| 2031 |
squaredMagnitude = Math.pow(alphaK, 2) + Math.pow(betaK, 2); |
| 2032 |
if (squaredMagnitude <= 9) { |
| 2033 |
continue; |
| 2034 |
} |
| 2035 |
tauK = 3 / Math.sqrt(squaredMagnitude); |
| 2036 |
mK[i] = alphaK * tauK * deltaK[i]; |
| 2037 |
mK[i + 1] = betaK * tauK * deltaK[i]; |
| 2038 |
} |
| 2039 |
} |
| 2040 |
function monotoneCompute(points, mK, indexAxis = 'x') { |
| 2041 |
const valueAxis = getValueAxis(indexAxis); |
| 2042 |
const pointsLen = points.length; |
| 2043 |
let delta, pointBefore, pointCurrent; |
| 2044 |
let pointAfter = getPoint(points, 0); |
| 2045 |
for(let i = 0; i < pointsLen; ++i){ |
| 2046 |
pointBefore = pointCurrent; |
| 2047 |
pointCurrent = pointAfter; |
| 2048 |
pointAfter = getPoint(points, i + 1); |
| 2049 |
if (!pointCurrent) { |
| 2050 |
continue; |
| 2051 |
} |
| 2052 |
const iPixel = pointCurrent[indexAxis]; |
| 2053 |
const vPixel = pointCurrent[valueAxis]; |
| 2054 |
if (pointBefore) { |
| 2055 |
delta = (iPixel - pointBefore[indexAxis]) / 3; |
| 2056 |
pointCurrent[`cp1${indexAxis}`] = iPixel - delta; |
| 2057 |
pointCurrent[`cp1${valueAxis}`] = vPixel - delta * mK[i]; |
| 2058 |
} |
| 2059 |
if (pointAfter) { |
| 2060 |
delta = (pointAfter[indexAxis] - iPixel) / 3; |
| 2061 |
pointCurrent[`cp2${indexAxis}`] = iPixel + delta; |
| 2062 |
pointCurrent[`cp2${valueAxis}`] = vPixel + delta * mK[i]; |
| 2063 |
} |
| 2064 |
} |
| 2065 |
} |
| 2066 |
/** |
| 2067 |
* This function calculates Bézier control points in a similar way than |splineCurve|, |
| 2068 |
* but preserves monotonicity of the provided data and ensures no local extremums are added |
| 2069 |
* between the dataset discrete points due to the interpolation. |
| 2070 |
* See : https://en.wikipedia.org/wiki/Monotone_cubic_interpolation |
| 2071 |
*/ function splineCurveMonotone(points, indexAxis = 'x') { |
| 2072 |
const valueAxis = getValueAxis(indexAxis); |
| 2073 |
const pointsLen = points.length; |
| 2074 |
const deltaK = Array(pointsLen).fill(0); |
| 2075 |
const mK = Array(pointsLen); |
| 2076 |
// Calculate slopes (deltaK) and initialize tangents (mK) |
| 2077 |
let i, pointBefore, pointCurrent; |
| 2078 |
let pointAfter = getPoint(points, 0); |
| 2079 |
for(i = 0; i < pointsLen; ++i){ |
| 2080 |
pointBefore = pointCurrent; |
| 2081 |
pointCurrent = pointAfter; |
| 2082 |
pointAfter = getPoint(points, i + 1); |
| 2083 |
if (!pointCurrent) { |
| 2084 |
continue; |
| 2085 |
} |
| 2086 |
if (pointAfter) { |
| 2087 |
const slopeDelta = pointAfter[indexAxis] - pointCurrent[indexAxis]; |
| 2088 |
// In the case of two points that appear at the same x pixel, slopeDeltaX is 0 |
| 2089 |
deltaK[i] = slopeDelta !== 0 ? (pointAfter[valueAxis] - pointCurrent[valueAxis]) / slopeDelta : 0; |
| 2090 |
} |
| 2091 |
mK[i] = !pointBefore ? deltaK[i] : !pointAfter ? deltaK[i - 1] : sign(deltaK[i - 1]) !== sign(deltaK[i]) ? 0 : (deltaK[i - 1] + deltaK[i]) / 2; |
| 2092 |
} |
| 2093 |
monotoneAdjust(points, deltaK, mK); |
| 2094 |
monotoneCompute(points, mK, indexAxis); |
| 2095 |
} |
| 2096 |
function capControlPoint(pt, min, max) { |
| 2097 |
return Math.max(Math.min(pt, max), min); |
| 2098 |
} |
| 2099 |
function capBezierPoints(points, area) { |
| 2100 |
let i, ilen, point, inArea, inAreaPrev; |
| 2101 |
let inAreaNext = _isPointInArea(points[0], area); |
| 2102 |
for(i = 0, ilen = points.length; i < ilen; ++i){ |
| 2103 |
inAreaPrev = inArea; |
| 2104 |
inArea = inAreaNext; |
| 2105 |
inAreaNext = i < ilen - 1 && _isPointInArea(points[i + 1], area); |
| 2106 |
if (!inArea) { |
| 2107 |
continue; |
| 2108 |
} |
| 2109 |
point = points[i]; |
| 2110 |
if (inAreaPrev) { |
| 2111 |
point.cp1x = capControlPoint(point.cp1x, area.left, area.right); |
| 2112 |
point.cp1y = capControlPoint(point.cp1y, area.top, area.bottom); |
| 2113 |
} |
| 2114 |
if (inAreaNext) { |
| 2115 |
point.cp2x = capControlPoint(point.cp2x, area.left, area.right); |
| 2116 |
point.cp2y = capControlPoint(point.cp2y, area.top, area.bottom); |
| 2117 |
} |
| 2118 |
} |
| 2119 |
} |
| 2120 |
/** |
| 2121 |
* @private |
| 2122 |
*/ function _updateBezierControlPoints(points, options, area, loop, indexAxis) { |
| 2123 |
let i, ilen, point, controlPoints; |
| 2124 |
// Only consider points that are drawn in case the spanGaps option is used |
| 2125 |
if (options.spanGaps) { |
| 2126 |
points = points.filter((pt)=>!pt.skip); |
| 2127 |
} |
| 2128 |
if (options.cubicInterpolationMode === 'monotone') { |
| 2129 |
splineCurveMonotone(points, indexAxis); |
| 2130 |
} else { |
| 2131 |
let prev = loop ? points[points.length - 1] : points[0]; |
| 2132 |
for(i = 0, ilen = points.length; i < ilen; ++i){ |
| 2133 |
point = points[i]; |
| 2134 |
controlPoints = splineCurve(prev, point, points[Math.min(i + 1, ilen - (loop ? 0 : 1)) % ilen], options.tension); |
| 2135 |
point.cp1x = controlPoints.previous.x; |
| 2136 |
point.cp1y = controlPoints.previous.y; |
| 2137 |
point.cp2x = controlPoints.next.x; |
| 2138 |
point.cp2y = controlPoints.next.y; |
| 2139 |
prev = point; |
| 2140 |
} |
| 2141 |
} |
| 2142 |
if (options.capBezierPoints) { |
| 2143 |
capBezierPoints(points, area); |
| 2144 |
} |
| 2145 |
} |
| 2146 |
|
| 2147 |
/** |
| 2148 |
* @private |
| 2149 |
*/ function _isDomSupported() { |
| 2150 |
return typeof window !== 'undefined' && typeof document !== 'undefined'; |
| 2151 |
} |
| 2152 |
/** |
| 2153 |
* @private |
| 2154 |
*/ function _getParentNode(domNode) { |
| 2155 |
let parent = domNode.parentNode; |
| 2156 |
if (parent && parent.toString() === '[object ShadowRoot]') { |
| 2157 |
parent = parent.host; |
| 2158 |
} |
| 2159 |
return parent; |
| 2160 |
} |
| 2161 |
/** |
| 2162 |
* convert max-width/max-height values that may be percentages into a number |
| 2163 |
* @private |
| 2164 |
*/ function parseMaxStyle(styleValue, node, parentProperty) { |
| 2165 |
let valueInPixels; |
| 2166 |
if (typeof styleValue === 'string') { |
| 2167 |
valueInPixels = parseInt(styleValue, 10); |
| 2168 |
if (styleValue.indexOf('%') !== -1) { |
| 2169 |
// percentage * size in dimension |
| 2170 |
valueInPixels = valueInPixels / 100 * node.parentNode[parentProperty]; |
| 2171 |
} |
| 2172 |
} else { |
| 2173 |
valueInPixels = styleValue; |
| 2174 |
} |
| 2175 |
return valueInPixels; |
| 2176 |
} |
| 2177 |
const getComputedStyle = (element)=>element.ownerDocument.defaultView.getComputedStyle(element, null); |
| 2178 |
function getStyle(el, property) { |
| 2179 |
return getComputedStyle(el).getPropertyValue(property); |
| 2180 |
} |
| 2181 |
const positions = [ |
| 2182 |
'top', |
| 2183 |
'right', |
| 2184 |
'bottom', |
| 2185 |
'left' |
| 2186 |
]; |
| 2187 |
function getPositionedStyle(styles, style, suffix) { |
| 2188 |
const result = {}; |
| 2189 |
suffix = suffix ? '-' + suffix : ''; |
| 2190 |
for(let i = 0; i < 4; i++){ |
| 2191 |
const pos = positions[i]; |
| 2192 |
result[pos] = parseFloat(styles[style + '-' + pos + suffix]) || 0; |
| 2193 |
} |
| 2194 |
result.width = result.left + result.right; |
| 2195 |
result.height = result.top + result.bottom; |
| 2196 |
return result; |
| 2197 |
} |
| 2198 |
const useOffsetPos = (x, y, target)=>(x > 0 || y > 0) && (!target || !target.shadowRoot); |
| 2199 |
/** |
| 2200 |
* @param e |
| 2201 |
* @param canvas |
| 2202 |
* @returns Canvas position |
| 2203 |
*/ function getCanvasPosition(e, canvas) { |
| 2204 |
const touches = e.touches; |
| 2205 |
const source = touches && touches.length ? touches[0] : e; |
| 2206 |
const { offsetX , offsetY } = source; |
| 2207 |
let box = false; |
| 2208 |
let x, y; |
| 2209 |
if (useOffsetPos(offsetX, offsetY, e.target)) { |
| 2210 |
x = offsetX; |
| 2211 |
y = offsetY; |
| 2212 |
} else { |
| 2213 |
const rect = canvas.getBoundingClientRect(); |
| 2214 |
x = source.clientX - rect.left; |
| 2215 |
y = source.clientY - rect.top; |
| 2216 |
box = true; |
| 2217 |
} |
| 2218 |
return { |
| 2219 |
x, |
| 2220 |
y, |
| 2221 |
box |
| 2222 |
}; |
| 2223 |
} |
| 2224 |
/** |
| 2225 |
* Gets an event's x, y coordinates, relative to the chart area |
| 2226 |
* @param event |
| 2227 |
* @param chart |
| 2228 |
* @returns x and y coordinates of the event |
| 2229 |
*/ function getRelativePosition(event, chart) { |
| 2230 |
if ('native' in event) { |
| 2231 |
return event; |
| 2232 |
} |
| 2233 |
const { canvas , currentDevicePixelRatio } = chart; |
| 2234 |
const style = getComputedStyle(canvas); |
| 2235 |
const borderBox = style.boxSizing === 'border-box'; |
| 2236 |
const paddings = getPositionedStyle(style, 'padding'); |
| 2237 |
const borders = getPositionedStyle(style, 'border', 'width'); |
| 2238 |
const { x , y , box } = getCanvasPosition(event, canvas); |
| 2239 |
const xOffset = paddings.left + (box && borders.left); |
| 2240 |
const yOffset = paddings.top + (box && borders.top); |
| 2241 |
let { width , height } = chart; |
| 2242 |
if (borderBox) { |
| 2243 |
width -= paddings.width + borders.width; |
| 2244 |
height -= paddings.height + borders.height; |
| 2245 |
} |
| 2246 |
return { |
| 2247 |
x: Math.round((x - xOffset) / width * canvas.width / currentDevicePixelRatio), |
| 2248 |
y: Math.round((y - yOffset) / height * canvas.height / currentDevicePixelRatio) |
| 2249 |
}; |
| 2250 |
} |
| 2251 |
function getContainerSize(canvas, width, height) { |
| 2252 |
let maxWidth, maxHeight; |
| 2253 |
if (width === undefined || height === undefined) { |
| 2254 |
const container = canvas && _getParentNode(canvas); |
| 2255 |
if (!container) { |
| 2256 |
width = canvas.clientWidth; |
| 2257 |
height = canvas.clientHeight; |
| 2258 |
} else { |
| 2259 |
const rect = container.getBoundingClientRect(); // this is the border box of the container |
| 2260 |
const containerStyle = getComputedStyle(container); |
| 2261 |
const containerBorder = getPositionedStyle(containerStyle, 'border', 'width'); |
| 2262 |
const containerPadding = getPositionedStyle(containerStyle, 'padding'); |
| 2263 |
width = rect.width - containerPadding.width - containerBorder.width; |
| 2264 |
height = rect.height - containerPadding.height - containerBorder.height; |
| 2265 |
maxWidth = parseMaxStyle(containerStyle.maxWidth, container, 'clientWidth'); |
| 2266 |
maxHeight = parseMaxStyle(containerStyle.maxHeight, container, 'clientHeight'); |
| 2267 |
} |
| 2268 |
} |
| 2269 |
return { |
| 2270 |
width, |
| 2271 |
height, |
| 2272 |
maxWidth: maxWidth || INFINITY, |
| 2273 |
maxHeight: maxHeight || INFINITY |
| 2274 |
}; |
| 2275 |
} |
| 2276 |
const round1 = (v)=>Math.round(v * 10) / 10; |
| 2277 |
// eslint-disable-next-line complexity |
| 2278 |
function getMaximumSize(canvas, bbWidth, bbHeight, aspectRatio) { |
| 2279 |
const style = getComputedStyle(canvas); |
| 2280 |
const margins = getPositionedStyle(style, 'margin'); |
| 2281 |
const maxWidth = parseMaxStyle(style.maxWidth, canvas, 'clientWidth') || INFINITY; |
| 2282 |
const maxHeight = parseMaxStyle(style.maxHeight, canvas, 'clientHeight') || INFINITY; |
| 2283 |
const containerSize = getContainerSize(canvas, bbWidth, bbHeight); |
| 2284 |
let { width , height } = containerSize; |
| 2285 |
if (style.boxSizing === 'content-box') { |
| 2286 |
const borders = getPositionedStyle(style, 'border', 'width'); |
| 2287 |
const paddings = getPositionedStyle(style, 'padding'); |
| 2288 |
width -= paddings.width + borders.width; |
| 2289 |
height -= paddings.height + borders.height; |
| 2290 |
} |
| 2291 |
width = Math.max(0, width - margins.width); |
| 2292 |
height = Math.max(0, aspectRatio ? width / aspectRatio : height - margins.height); |
| 2293 |
width = round1(Math.min(width, maxWidth, containerSize.maxWidth)); |
| 2294 |
height = round1(Math.min(height, maxHeight, containerSize.maxHeight)); |
| 2295 |
if (width && !height) { |
| 2296 |
// https://github.com/chartjs/Chart.js/issues/4659 |
| 2297 |
// If the canvas has width, but no height, default to aspectRatio of 2 (canvas default) |
| 2298 |
height = round1(width / 2); |
| 2299 |
} |
| 2300 |
const maintainHeight = bbWidth !== undefined || bbHeight !== undefined; |
| 2301 |
if (maintainHeight && aspectRatio && containerSize.height && height > containerSize.height) { |
| 2302 |
height = containerSize.height; |
| 2303 |
width = round1(Math.floor(height * aspectRatio)); |
| 2304 |
} |
| 2305 |
return { |
| 2306 |
width, |
| 2307 |
height |
| 2308 |
}; |
| 2309 |
} |
| 2310 |
/** |
| 2311 |
* @param chart |
| 2312 |
* @param forceRatio |
| 2313 |
* @param forceStyle |
| 2314 |
* @returns True if the canvas context size or transformation has changed. |
| 2315 |
*/ function retinaScale(chart, forceRatio, forceStyle) { |
| 2316 |
const pixelRatio = forceRatio || 1; |
| 2317 |
const deviceHeight = round1(chart.height * pixelRatio); |
| 2318 |
const deviceWidth = round1(chart.width * pixelRatio); |
| 2319 |
chart.height = round1(chart.height); |
| 2320 |
chart.width = round1(chart.width); |
| 2321 |
const canvas = chart.canvas; |
| 2322 |
// If no style has been set on the canvas, the render size is used as display size, |
| 2323 |
// making the chart visually bigger, so let's enforce it to the "correct" values. |
| 2324 |
// See https://github.com/chartjs/Chart.js/issues/3575 |
| 2325 |
if (canvas.style && (forceStyle || !canvas.style.height && !canvas.style.width)) { |
| 2326 |
canvas.style.height = `${chart.height}px`; |
| 2327 |
canvas.style.width = `${chart.width}px`; |
| 2328 |
} |
| 2329 |
if (chart.currentDevicePixelRatio !== pixelRatio || canvas.height !== deviceHeight || canvas.width !== deviceWidth) { |
| 2330 |
chart.currentDevicePixelRatio = pixelRatio; |
| 2331 |
canvas.height = deviceHeight; |
| 2332 |
canvas.width = deviceWidth; |
| 2333 |
chart.ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0); |
| 2334 |
return true; |
| 2335 |
} |
| 2336 |
return false; |
| 2337 |
} |
| 2338 |
/** |
| 2339 |
* Detects support for options object argument in addEventListener. |
| 2340 |
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support |
| 2341 |
* @private |
| 2342 |
*/ const supportsEventListenerOptions = function() { |
| 2343 |
let passiveSupported = false; |
| 2344 |
try { |
| 2345 |
const options = { |
| 2346 |
get passive () { |
| 2347 |
passiveSupported = true; |
| 2348 |
return false; |
| 2349 |
} |
| 2350 |
}; |
| 2351 |
if (_isDomSupported()) { |
| 2352 |
window.addEventListener('test', null, options); |
| 2353 |
window.removeEventListener('test', null, options); |
| 2354 |
} |
| 2355 |
} catch (e) { |
| 2356 |
// continue regardless of error |
| 2357 |
} |
| 2358 |
return passiveSupported; |
| 2359 |
}(); |
| 2360 |
/** |
| 2361 |
* The "used" size is the final value of a dimension property after all calculations have |
| 2362 |
* been performed. This method uses the computed style of `element` but returns undefined |
| 2363 |
* if the computed style is not expressed in pixels. That can happen in some cases where |
| 2364 |
* `element` has a size relative to its parent and this last one is not yet displayed, |
| 2365 |
* for example because of `display: none` on a parent node. |
| 2366 |
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/used_value |
| 2367 |
* @returns Size in pixels or undefined if unknown. |
| 2368 |
*/ function readUsedSize(element, property) { |
| 2369 |
const value = getStyle(element, property); |
| 2370 |
const matches = value && value.match(/^(\d+)(\.\d+)?px$/); |
| 2371 |
return matches ? +matches[1] : undefined; |
| 2372 |
} |
| 2373 |
|
| 2374 |
/** |
| 2375 |
* @private |
| 2376 |
*/ function _pointInLine(p1, p2, t, mode) { |
| 2377 |
return { |
| 2378 |
x: p1.x + t * (p2.x - p1.x), |
| 2379 |
y: p1.y + t * (p2.y - p1.y) |
| 2380 |
}; |
| 2381 |
} |
| 2382 |
/** |
| 2383 |
* @private |
| 2384 |
*/ function _steppedInterpolation(p1, p2, t, mode) { |
| 2385 |
return { |
| 2386 |
x: p1.x + t * (p2.x - p1.x), |
| 2387 |
y: mode === 'middle' ? t < 0.5 ? p1.y : p2.y : mode === 'after' ? t < 1 ? p1.y : p2.y : t > 0 ? p2.y : p1.y |
| 2388 |
}; |
| 2389 |
} |
| 2390 |
/** |
| 2391 |
* @private |
| 2392 |
*/ function _bezierInterpolation(p1, p2, t, mode) { |
| 2393 |
const cp1 = { |
| 2394 |
x: p1.cp2x, |
| 2395 |
y: p1.cp2y |
| 2396 |
}; |
| 2397 |
const cp2 = { |
| 2398 |
x: p2.cp1x, |
| 2399 |
y: p2.cp1y |
| 2400 |
}; |
| 2401 |
const a = _pointInLine(p1, cp1, t); |
| 2402 |
const b = _pointInLine(cp1, cp2, t); |
| 2403 |
const c = _pointInLine(cp2, p2, t); |
| 2404 |
const d = _pointInLine(a, b, t); |
| 2405 |
const e = _pointInLine(b, c, t); |
| 2406 |
return _pointInLine(d, e, t); |
| 2407 |
} |
| 2408 |
|
| 2409 |
const getRightToLeftAdapter = function(rectX, width) { |
| 2410 |
return { |
| 2411 |
x (x) { |
| 2412 |
return rectX + rectX + width - x; |
| 2413 |
}, |
| 2414 |
setWidth (w) { |
| 2415 |
width = w; |
| 2416 |
}, |
| 2417 |
textAlign (align) { |
| 2418 |
if (align === 'center') { |
| 2419 |
return align; |
| 2420 |
} |
| 2421 |
return align === 'right' ? 'left' : 'right'; |
| 2422 |
}, |
| 2423 |
xPlus (x, value) { |
| 2424 |
return x - value; |
| 2425 |
}, |
| 2426 |
leftForLtr (x, itemWidth) { |
| 2427 |
return x - itemWidth; |
| 2428 |
} |
| 2429 |
}; |
| 2430 |
}; |
| 2431 |
const getLeftToRightAdapter = function() { |
| 2432 |
return { |
| 2433 |
x (x) { |
| 2434 |
return x; |
| 2435 |
}, |
| 2436 |
setWidth (w) {}, |
| 2437 |
textAlign (align) { |
| 2438 |
return align; |
| 2439 |
}, |
| 2440 |
xPlus (x, value) { |
| 2441 |
return x + value; |
| 2442 |
}, |
| 2443 |
leftForLtr (x, _itemWidth) { |
| 2444 |
return x; |
| 2445 |
} |
| 2446 |
}; |
| 2447 |
}; |
| 2448 |
function getRtlAdapter(rtl, rectX, width) { |
| 2449 |
return rtl ? getRightToLeftAdapter(rectX, width) : getLeftToRightAdapter(); |
| 2450 |
} |
| 2451 |
function overrideTextDirection(ctx, direction) { |
| 2452 |
let style, original; |
| 2453 |
if (direction === 'ltr' || direction === 'rtl') { |
| 2454 |
style = ctx.canvas.style; |
| 2455 |
original = [ |
| 2456 |
style.getPropertyValue('direction'), |
| 2457 |
style.getPropertyPriority('direction') |
| 2458 |
]; |
| 2459 |
style.setProperty('direction', direction, 'important'); |
| 2460 |
ctx.prevTextDirection = original; |
| 2461 |
} |
| 2462 |
} |
| 2463 |
function restoreTextDirection(ctx, original) { |
| 2464 |
if (original !== undefined) { |
| 2465 |
delete ctx.prevTextDirection; |
| 2466 |
ctx.canvas.style.setProperty('direction', original[0], original[1]); |
| 2467 |
} |
| 2468 |
} |
| 2469 |
|
| 2470 |
function propertyFn(property) { |
| 2471 |
if (property === 'angle') { |
| 2472 |
return { |
| 2473 |
between: _angleBetween, |
| 2474 |
compare: _angleDiff, |
| 2475 |
normalize: _normalizeAngle |
| 2476 |
}; |
| 2477 |
} |
| 2478 |
return { |
| 2479 |
between: _isBetween, |
| 2480 |
compare: (a, b)=>a - b, |
| 2481 |
normalize: (x)=>x |
| 2482 |
}; |
| 2483 |
} |
| 2484 |
function normalizeSegment({ start , end , count , loop , style }) { |
| 2485 |
return { |
| 2486 |
start: start % count, |
| 2487 |
end: end % count, |
| 2488 |
loop: loop && (end - start + 1) % count === 0, |
| 2489 |
style |
| 2490 |
}; |
| 2491 |
} |
| 2492 |
function getSegment(segment, points, bounds) { |
| 2493 |
const { property , start: startBound , end: endBound } = bounds; |
| 2494 |
const { between , normalize } = propertyFn(property); |
| 2495 |
const count = points.length; |
| 2496 |
let { start , end , loop } = segment; |
| 2497 |
let i, ilen; |
| 2498 |
if (loop) { |
| 2499 |
start += count; |
| 2500 |
end += count; |
| 2501 |
for(i = 0, ilen = count; i < ilen; ++i){ |
| 2502 |
if (!between(normalize(points[start % count][property]), startBound, endBound)) { |
| 2503 |
break; |
| 2504 |
} |
| 2505 |
start--; |
| 2506 |
end--; |
| 2507 |
} |
| 2508 |
start %= count; |
| 2509 |
end %= count; |
| 2510 |
} |
| 2511 |
if (end < start) { |
| 2512 |
end += count; |
| 2513 |
} |
| 2514 |
return { |
| 2515 |
start, |
| 2516 |
end, |
| 2517 |
loop, |
| 2518 |
style: segment.style |
| 2519 |
}; |
| 2520 |
} |
| 2521 |
function _boundSegment(segment, points, bounds) { |
| 2522 |
if (!bounds) { |
| 2523 |
return [ |
| 2524 |
segment |
| 2525 |
]; |
| 2526 |
} |
| 2527 |
const { property , start: startBound , end: endBound } = bounds; |
| 2528 |
const count = points.length; |
| 2529 |
const { compare , between , normalize } = propertyFn(property); |
| 2530 |
const { start , end , loop , style } = getSegment(segment, points, bounds); |
| 2531 |
const result = []; |
| 2532 |
let inside = false; |
| 2533 |
let subStart = null; |
| 2534 |
let value, point, prevValue; |
| 2535 |
const startIsBefore = ()=>between(startBound, prevValue, value) && compare(startBound, prevValue) !== 0; |
| 2536 |
const endIsBefore = ()=>compare(endBound, value) === 0 || between(endBound, prevValue, value); |
| 2537 |
const shouldStart = ()=>inside || startIsBefore(); |
| 2538 |
const shouldStop = ()=>!inside || endIsBefore(); |
| 2539 |
for(let i = start, prev = start; i <= end; ++i){ |
| 2540 |
point = points[i % count]; |
| 2541 |
if (point.skip) { |
| 2542 |
continue; |
| 2543 |
} |
| 2544 |
value = normalize(point[property]); |
| 2545 |
if (value === prevValue) { |
| 2546 |
continue; |
| 2547 |
} |
| 2548 |
inside = between(value, startBound, endBound); |
| 2549 |
if (subStart === null && shouldStart()) { |
| 2550 |
subStart = compare(value, startBound) === 0 ? i : prev; |
| 2551 |
} |
| 2552 |
if (subStart !== null && shouldStop()) { |
| 2553 |
result.push(normalizeSegment({ |
| 2554 |
start: subStart, |
| 2555 |
end: i, |
| 2556 |
loop, |
| 2557 |
count, |
| 2558 |
style |
| 2559 |
})); |
| 2560 |
subStart = null; |
| 2561 |
} |
| 2562 |
prev = i; |
| 2563 |
prevValue = value; |
| 2564 |
} |
| 2565 |
if (subStart !== null) { |
| 2566 |
result.push(normalizeSegment({ |
| 2567 |
start: subStart, |
| 2568 |
end, |
| 2569 |
loop, |
| 2570 |
count, |
| 2571 |
style |
| 2572 |
})); |
| 2573 |
} |
| 2574 |
return result; |
| 2575 |
} |
| 2576 |
function _boundSegments(line, bounds) { |
| 2577 |
const result = []; |
| 2578 |
const segments = line.segments; |
| 2579 |
for(let i = 0; i < segments.length; i++){ |
| 2580 |
const sub = _boundSegment(segments[i], line.points, bounds); |
| 2581 |
if (sub.length) { |
| 2582 |
result.push(...sub); |
| 2583 |
} |
| 2584 |
} |
| 2585 |
return result; |
| 2586 |
} |
| 2587 |
function findStartAndEnd(points, count, loop, spanGaps) { |
| 2588 |
let start = 0; |
| 2589 |
let end = count - 1; |
| 2590 |
if (loop && !spanGaps) { |
| 2591 |
while(start < count && !points[start].skip){ |
| 2592 |
start++; |
| 2593 |
} |
| 2594 |
} |
| 2595 |
while(start < count && points[start].skip){ |
| 2596 |
start++; |
| 2597 |
} |
| 2598 |
start %= count; |
| 2599 |
if (loop) { |
| 2600 |
end += start; |
| 2601 |
} |
| 2602 |
while(end > start && points[end % count].skip){ |
| 2603 |
end--; |
| 2604 |
} |
| 2605 |
end %= count; |
| 2606 |
return { |
| 2607 |
start, |
| 2608 |
end |
| 2609 |
}; |
| 2610 |
} |
| 2611 |
function solidSegments(points, start, max, loop) { |
| 2612 |
const count = points.length; |
| 2613 |
const result = []; |
| 2614 |
let last = start; |
| 2615 |
let prev = points[start]; |
| 2616 |
let end; |
| 2617 |
for(end = start + 1; end <= max; ++end){ |
| 2618 |
const cur = points[end % count]; |
| 2619 |
if (cur.skip || cur.stop) { |
| 2620 |
if (!prev.skip) { |
| 2621 |
loop = false; |
| 2622 |
result.push({ |
| 2623 |
start: start % count, |
| 2624 |
end: (end - 1) % count, |
| 2625 |
loop |
| 2626 |
}); |
| 2627 |
start = last = cur.stop ? end : null; |
| 2628 |
} |
| 2629 |
} else { |
| 2630 |
last = end; |
| 2631 |
if (prev.skip) { |
| 2632 |
start = end; |
| 2633 |
} |
| 2634 |
} |
| 2635 |
prev = cur; |
| 2636 |
} |
| 2637 |
if (last !== null) { |
| 2638 |
result.push({ |
| 2639 |
start: start % count, |
| 2640 |
end: last % count, |
| 2641 |
loop |
| 2642 |
}); |
| 2643 |
} |
| 2644 |
return result; |
| 2645 |
} |
| 2646 |
function _computeSegments(line, segmentOptions) { |
| 2647 |
const points = line.points; |
| 2648 |
const spanGaps = line.options.spanGaps; |
| 2649 |
const count = points.length; |
| 2650 |
if (!count) { |
| 2651 |
return []; |
| 2652 |
} |
| 2653 |
const loop = !!line._loop; |
| 2654 |
const { start , end } = findStartAndEnd(points, count, loop, spanGaps); |
| 2655 |
if (spanGaps === true) { |
| 2656 |
return splitByStyles(line, [ |
| 2657 |
{ |
| 2658 |
start, |
| 2659 |
end, |
| 2660 |
loop |
| 2661 |
} |
| 2662 |
], points, segmentOptions); |
| 2663 |
} |
| 2664 |
const max = end < start ? end + count : end; |
| 2665 |
const completeLoop = !!line._fullLoop && start === 0 && end === count - 1; |
| 2666 |
return splitByStyles(line, solidSegments(points, start, max, completeLoop), points, segmentOptions); |
| 2667 |
} |
| 2668 |
function splitByStyles(line, segments, points, segmentOptions) { |
| 2669 |
if (!segmentOptions || !segmentOptions.setContext || !points) { |
| 2670 |
return segments; |
| 2671 |
} |
| 2672 |
return doSplitByStyles(line, segments, points, segmentOptions); |
| 2673 |
} |
| 2674 |
function doSplitByStyles(line, segments, points, segmentOptions) { |
| 2675 |
const chartContext = line._chart.getContext(); |
| 2676 |
const baseStyle = readStyle(line.options); |
| 2677 |
const { _datasetIndex: datasetIndex , options: { spanGaps } } = line; |
| 2678 |
const count = points.length; |
| 2679 |
const result = []; |
| 2680 |
let prevStyle = baseStyle; |
| 2681 |
let start = segments[0].start; |
| 2682 |
let i = start; |
| 2683 |
function addStyle(s, e, l, st) { |
| 2684 |
const dir = spanGaps ? -1 : 1; |
| 2685 |
if (s === e) { |
| 2686 |
return; |
| 2687 |
} |
| 2688 |
s += count; |
| 2689 |
while(points[s % count].skip){ |
| 2690 |
s -= dir; |
| 2691 |
} |
| 2692 |
while(points[e % count].skip){ |
| 2693 |
e += dir; |
| 2694 |
} |
| 2695 |
if (s % count !== e % count) { |
| 2696 |
result.push({ |
| 2697 |
start: s % count, |
| 2698 |
end: e % count, |
| 2699 |
loop: l, |
| 2700 |
style: st |
| 2701 |
}); |
| 2702 |
prevStyle = st; |
| 2703 |
start = e % count; |
| 2704 |
} |
| 2705 |
} |
| 2706 |
for (const segment of segments){ |
| 2707 |
start = spanGaps ? start : segment.start; |
| 2708 |
let prev = points[start % count]; |
| 2709 |
let style; |
| 2710 |
for(i = start + 1; i <= segment.end; i++){ |
| 2711 |
const pt = points[i % count]; |
| 2712 |
style = readStyle(segmentOptions.setContext(createContext(chartContext, { |
| 2713 |
type: 'segment', |
| 2714 |
p0: prev, |
| 2715 |
p1: pt, |
| 2716 |
p0DataIndex: (i - 1) % count, |
| 2717 |
p1DataIndex: i % count, |
| 2718 |
datasetIndex |
| 2719 |
}))); |
| 2720 |
if (styleChanged(style, prevStyle)) { |
| 2721 |
addStyle(start, i - 1, segment.loop, prevStyle); |
| 2722 |
} |
| 2723 |
prev = pt; |
| 2724 |
prevStyle = style; |
| 2725 |
} |
| 2726 |
if (start < i - 1) { |
| 2727 |
addStyle(start, i - 1, segment.loop, prevStyle); |
| 2728 |
} |
| 2729 |
} |
| 2730 |
return result; |
| 2731 |
} |
| 2732 |
function readStyle(options) { |
| 2733 |
return { |
| 2734 |
backgroundColor: options.backgroundColor, |
| 2735 |
borderCapStyle: options.borderCapStyle, |
| 2736 |
borderDash: options.borderDash, |
| 2737 |
borderDashOffset: options.borderDashOffset, |
| 2738 |
borderJoinStyle: options.borderJoinStyle, |
| 2739 |
borderWidth: options.borderWidth, |
| 2740 |
borderColor: options.borderColor |
| 2741 |
}; |
| 2742 |
} |
| 2743 |
function styleChanged(style, prevStyle) { |
| 2744 |
if (!prevStyle) { |
| 2745 |
return false; |
| 2746 |
} |
| 2747 |
const cache = []; |
| 2748 |
const replacer = function(key, value) { |
| 2749 |
if (!isPatternOrGradient(value)) { |
| 2750 |
return value; |
| 2751 |
} |
| 2752 |
if (!cache.includes(value)) { |
| 2753 |
cache.push(value); |
| 2754 |
} |
| 2755 |
return cache.indexOf(value); |
| 2756 |
}; |
| 2757 |
return JSON.stringify(style, replacer) !== JSON.stringify(prevStyle, replacer); |
| 2758 |
} |
| 2759 |
|
| 2760 |
function getSizeForArea(scale, chartArea, field) { |
| 2761 |
return scale.options.clip ? scale[field] : chartArea[field]; |
| 2762 |
} |
| 2763 |
function getDatasetArea(meta, chartArea) { |
| 2764 |
const { xScale , yScale } = meta; |
| 2765 |
if (xScale && yScale) { |
| 2766 |
return { |
| 2767 |
left: getSizeForArea(xScale, chartArea, 'left'), |
| 2768 |
right: getSizeForArea(xScale, chartArea, 'right'), |
| 2769 |
top: getSizeForArea(yScale, chartArea, 'top'), |
| 2770 |
bottom: getSizeForArea(yScale, chartArea, 'bottom') |
| 2771 |
}; |
| 2772 |
} |
| 2773 |
return chartArea; |
| 2774 |
} |
| 2775 |
function getDatasetClipArea(chart, meta) { |
| 2776 |
const clip = meta._clip; |
| 2777 |
if (clip.disabled) { |
| 2778 |
return false; |
| 2779 |
} |
| 2780 |
const area = getDatasetArea(meta, chart.chartArea); |
| 2781 |
return { |
| 2782 |
left: clip.left === false ? 0 : area.left - (clip.left === true ? 0 : clip.left), |
| 2783 |
right: clip.right === false ? chart.width : area.right + (clip.right === true ? 0 : clip.right), |
| 2784 |
top: clip.top === false ? 0 : area.top - (clip.top === true ? 0 : clip.top), |
| 2785 |
bottom: clip.bottom === false ? chart.height : area.bottom + (clip.bottom === true ? 0 : clip.bottom) |
| 2786 |
}; |
| 2787 |
} |
| 2788 |
|
| 2789 |
exports.HALF_PI = HALF_PI; |
| 2790 |
exports.INFINITY = INFINITY; |
| 2791 |
exports.PI = PI; |
| 2792 |
exports.PITAU = PITAU; |
| 2793 |
exports.QUARTER_PI = QUARTER_PI; |
| 2794 |
exports.RAD_PER_DEG = RAD_PER_DEG; |
| 2795 |
exports.TAU = TAU; |
| 2796 |
exports.TWO_THIRDS_PI = TWO_THIRDS_PI; |
| 2797 |
exports.Ticks = Ticks; |
| 2798 |
exports._addGrace = _addGrace; |
| 2799 |
exports._alignPixel = _alignPixel; |
| 2800 |
exports._alignStartEnd = _alignStartEnd; |
| 2801 |
exports._angleBetween = _angleBetween; |
| 2802 |
exports._angleDiff = _angleDiff; |
| 2803 |
exports._arrayUnique = _arrayUnique; |
| 2804 |
exports._attachContext = _attachContext; |
| 2805 |
exports._bezierCurveTo = _bezierCurveTo; |
| 2806 |
exports._bezierInterpolation = _bezierInterpolation; |
| 2807 |
exports._boundSegment = _boundSegment; |
| 2808 |
exports._boundSegments = _boundSegments; |
| 2809 |
exports._capitalize = _capitalize; |
| 2810 |
exports._computeSegments = _computeSegments; |
| 2811 |
exports._createResolver = _createResolver; |
| 2812 |
exports._decimalPlaces = _decimalPlaces; |
| 2813 |
exports._deprecated = _deprecated; |
| 2814 |
exports._descriptors = _descriptors; |
| 2815 |
exports._elementsEqual = _elementsEqual; |
| 2816 |
exports._factorize = _factorize; |
| 2817 |
exports._filterBetween = _filterBetween; |
| 2818 |
exports._getParentNode = _getParentNode; |
| 2819 |
exports._getStartAndCountOfVisiblePoints = _getStartAndCountOfVisiblePoints; |
| 2820 |
exports._int16Range = _int16Range; |
| 2821 |
exports._isBetween = _isBetween; |
| 2822 |
exports._isClickEvent = _isClickEvent; |
| 2823 |
exports._isDomSupported = _isDomSupported; |
| 2824 |
exports._isPointInArea = _isPointInArea; |
| 2825 |
exports._limitValue = _limitValue; |
| 2826 |
exports._longestText = _longestText; |
| 2827 |
exports._lookup = _lookup; |
| 2828 |
exports._lookupByKey = _lookupByKey; |
| 2829 |
exports._measureText = _measureText; |
| 2830 |
exports._merger = _merger; |
| 2831 |
exports._mergerIf = _mergerIf; |
| 2832 |
exports._normalizeAngle = _normalizeAngle; |
| 2833 |
exports._parseObjectDataRadialScale = _parseObjectDataRadialScale; |
| 2834 |
exports._pointInLine = _pointInLine; |
| 2835 |
exports._readValueToProps = _readValueToProps; |
| 2836 |
exports._rlookupByKey = _rlookupByKey; |
| 2837 |
exports._scaleRangesChanged = _scaleRangesChanged; |
| 2838 |
exports._setMinAndMaxByKey = _setMinAndMaxByKey; |
| 2839 |
exports._splitKey = _splitKey; |
| 2840 |
exports._steppedInterpolation = _steppedInterpolation; |
| 2841 |
exports._steppedLineTo = _steppedLineTo; |
| 2842 |
exports._textX = _textX; |
| 2843 |
exports._toLeftRightCenter = _toLeftRightCenter; |
| 2844 |
exports._updateBezierControlPoints = _updateBezierControlPoints; |
| 2845 |
exports.addRoundedRectPath = addRoundedRectPath; |
| 2846 |
exports.almostEquals = almostEquals; |
| 2847 |
exports.almostWhole = almostWhole; |
| 2848 |
exports.callback = callback; |
| 2849 |
exports.clearCanvas = clearCanvas; |
| 2850 |
exports.clipArea = clipArea; |
| 2851 |
exports.clone = clone; |
| 2852 |
exports.color = color; |
| 2853 |
exports.createContext = createContext; |
| 2854 |
exports.debounce = debounce; |
| 2855 |
exports.defaults = defaults; |
| 2856 |
exports.defined = defined; |
| 2857 |
exports.descriptors = descriptors; |
| 2858 |
exports.distanceBetweenPoints = distanceBetweenPoints; |
| 2859 |
exports.drawPoint = drawPoint; |
| 2860 |
exports.drawPointLegend = drawPointLegend; |
| 2861 |
exports.each = each; |
| 2862 |
exports.effects = effects; |
| 2863 |
exports.finiteOrDefault = finiteOrDefault; |
| 2864 |
exports.fontString = fontString; |
| 2865 |
exports.formatNumber = formatNumber; |
| 2866 |
exports.getAngleFromPoint = getAngleFromPoint; |
| 2867 |
exports.getDatasetClipArea = getDatasetClipArea; |
| 2868 |
exports.getHoverColor = getHoverColor; |
| 2869 |
exports.getMaximumSize = getMaximumSize; |
| 2870 |
exports.getRelativePosition = getRelativePosition; |
| 2871 |
exports.getRtlAdapter = getRtlAdapter; |
| 2872 |
exports.getStyle = getStyle; |
| 2873 |
exports.isArray = isArray; |
| 2874 |
exports.isFunction = isFunction; |
| 2875 |
exports.isNullOrUndef = isNullOrUndef; |
| 2876 |
exports.isNumber = isNumber; |
| 2877 |
exports.isNumberFinite = isNumberFinite; |
| 2878 |
exports.isObject = isObject; |
| 2879 |
exports.isPatternOrGradient = isPatternOrGradient; |
| 2880 |
exports.listenArrayEvents = listenArrayEvents; |
| 2881 |
exports.log10 = log10; |
| 2882 |
exports.merge = merge; |
| 2883 |
exports.mergeIf = mergeIf; |
| 2884 |
exports.niceNum = niceNum; |
| 2885 |
exports.noop = noop; |
| 2886 |
exports.overrideTextDirection = overrideTextDirection; |
| 2887 |
exports.overrides = overrides; |
| 2888 |
exports.readUsedSize = readUsedSize; |
| 2889 |
exports.renderText = renderText; |
| 2890 |
exports.requestAnimFrame = requestAnimFrame; |
| 2891 |
exports.resolve = resolve; |
| 2892 |
exports.resolveObjectKey = resolveObjectKey; |
| 2893 |
exports.restoreTextDirection = restoreTextDirection; |
| 2894 |
exports.retinaScale = retinaScale; |
| 2895 |
exports.setsEqual = setsEqual; |
| 2896 |
exports.sign = sign; |
| 2897 |
exports.splineCurve = splineCurve; |
| 2898 |
exports.splineCurveMonotone = splineCurveMonotone; |
| 2899 |
exports.supportsEventListenerOptions = supportsEventListenerOptions; |
| 2900 |
exports.throttled = throttled; |
| 2901 |
exports.toDegrees = toDegrees; |
| 2902 |
exports.toDimension = toDimension; |
| 2903 |
exports.toFont = toFont; |
| 2904 |
exports.toFontString = toFontString; |
| 2905 |
exports.toLineHeight = toLineHeight; |
| 2906 |
exports.toPadding = toPadding; |
| 2907 |
exports.toPercentage = toPercentage; |
| 2908 |
exports.toRadians = toRadians; |
| 2909 |
exports.toTRBL = toTRBL; |
| 2910 |
exports.toTRBLCorners = toTRBLCorners; |
| 2911 |
exports.uid = uid; |
| 2912 |
exports.unclipArea = unclipArea; |
| 2913 |
exports.unlistenArrayEvents = unlistenArrayEvents; |
| 2914 |
exports.valueOrDefault = valueOrDefault; |
| 2915 |
//# sourceMappingURL=helpers.dataset.cjs.map |