MCPcopy Create free account
hub / github.com/chartjs/Chart.js / buildOrUpdateScales

Method buildOrUpdateScales

src/core/core.controller.js:320–384  ·  view source on GitHub ↗

* Builds a map of scale ID to scale object for future lookup.

()

Source from the content-addressed store, hash-verified

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

Callers 1

_updateScalesMethod · 0.95

Calls 7

determineAxisFunction · 0.90
eachFunction · 0.85
valueOrDefaultFunction · 0.85
positionIsHorizontalFunction · 0.85
getScaleMethod · 0.65
initMethod · 0.65
configureMethod · 0.65

Tested by

no test coverage detected