MCPcopy
hub / github.com/chartjs/Chart.js / buildOrUpdateScales

Method buildOrUpdateScales

src/core/core.controller.js:316–380  ·  src/core/core.controller.js::Chart.buildOrUpdateScales

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

()

Source from the content-addressed store, hash-verified

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 === class="st">'r';
332 const isHorizontal = axis === class="st">'x';
333 return {
334 options: scaleOptions,
335 dposition: isRadial ? class="st">'chartArea' : isHorizontal ? class="st">'bottom' : class="st">'left',
336 dtype: isRadial ? class="st">'radialLinear' : isHorizontal ? class="st">'category' : class="st">'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 class="cm">// clear up discarded scales
370 each(updated, (hasUpdated, id) => {
371 if (!hasUpdated) {
372 delete scales[id];
373 }

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