* Builds a map of scale ID to scale object for future lookup.
()
| 323 | * Builds a map of scale ID to scale object for future lookup. |
| 324 | */ |
| 325 | buildOrUpdateScales() { |
| 326 | const options = this.options; |
| 327 | const scaleOpts = options.scales; |
| 328 | const scales = this.scales; |
| 329 | const updated = Object.keys(scales).reduce((obj, id) => { |
| 330 | obj[id] = false; |
| 331 | return obj; |
| 332 | }, {}); |
| 333 | let items = []; |
| 334 | |
| 335 | if (scaleOpts) { |
| 336 | items = items.concat( |
| 337 | Object.keys(scaleOpts).map((id) => { |
| 338 | const scaleOptions = scaleOpts[id]; |
| 339 | const axis = determineAxis(id, scaleOptions); |
| 340 | const isRadial = axis === 'r'; |
| 341 | const isHorizontal = axis === 'x'; |
| 342 | return { |
| 343 | options: scaleOptions, |
| 344 | dposition: isRadial ? 'chartArea' : isHorizontal ? 'bottom' : 'left', |
| 345 | dtype: isRadial ? 'radialLinear' : isHorizontal ? 'category' : 'linear' |
| 346 | }; |
| 347 | }) |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | each(items, (item) => { |
| 352 | const scaleOptions = item.options; |
| 353 | const id = scaleOptions.id; |
| 354 | const axis = determineAxis(id, scaleOptions); |
| 355 | const scaleType = valueOrDefault(scaleOptions.type, item.dtype); |
| 356 | |
| 357 | if (scaleOptions.position === undefined || positionIsHorizontal(scaleOptions.position, axis) !== positionIsHorizontal(item.dposition)) { |
| 358 | scaleOptions.position = item.dposition; |
| 359 | } |
| 360 | |
| 361 | updated[id] = true; |
| 362 | let scale = null; |
| 363 | if (id in scales && scales[id].type === scaleType) { |
| 364 | scale = scales[id]; |
| 365 | } else { |
| 366 | const scaleClass = registry.getScale(scaleType); |
| 367 | scale = new scaleClass({ |
| 368 | id, |
| 369 | type: scaleType, |
| 370 | ctx: this.ctx, |
| 371 | chart: this |
| 372 | }); |
| 373 | scales[scale.id] = scale; |
| 374 | } |
| 375 | |
| 376 | scale.init(scaleOptions, options); |
| 377 | }); |
| 378 | // clear up discarded scales |
| 379 | each(updated, (hasUpdated, id) => { |
| 380 | if (!hasUpdated) { |
| 381 | delete scales[id]; |
| 382 | } |
no test coverage detected