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