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

Method _calculateBarValuePixels

src/controllers/controller.bar.js:563–631  ·  view source on GitHub ↗

* Note: pixel values are not clamped to the scale area. * @private

(index)

Source from the content-addressed store, hash-verified

561 * @private
562 */
563 _calculateBarValuePixels(index) {
564 const {_cachedMeta: {vScale, _stacked, index: datasetIndex}, options: {base: baseValue, minBarLength}} = this;
565 const actualBase = baseValue || 0;
566 const parsed = this.getParsed(index);
567 const custom = parsed._custom;
568 const floating = isFloatBar(custom);
569 let value = parsed[vScale.axis];
570 let start = 0;
571 let length = _stacked ? this.applyStack(vScale, parsed, _stacked) : value;
572 let head, size;
573
574 if (length !== value) {
575 start = length - value;
576 length = value;
577 }
578
579 if (floating) {
580 value = custom.barStart;
581 length = custom.barEnd - custom.barStart;
582 // bars crossing origin are not stacked
583 if (value !== 0 && sign(value) !== sign(custom.barEnd)) {
584 start = 0;
585 }
586 start += value;
587 }
588
589 const startValue = !isNullOrUndef(baseValue) && !floating ? baseValue : start;
590 let base = vScale.getPixelForValue(startValue);
591
592 if (this.chart.getDataVisibility(index)) {
593 head = vScale.getPixelForValue(start + length);
594 } else {
595 // When not visible, no height
596 head = base;
597 }
598
599 size = head - base;
600
601 if (Math.abs(size) < minBarLength) {
602 size = barSign(size, vScale, actualBase) * minBarLength;
603 if (value === actualBase) {
604 base -= size / 2;
605 }
606 const startPixel = vScale.getPixelForDecimal(0);
607 const endPixel = vScale.getPixelForDecimal(1);
608 const min = Math.min(startPixel, endPixel);
609 const max = Math.max(startPixel, endPixel);
610 base = Math.max(Math.min(base, max), min);
611 head = base + size;
612
613 if (_stacked && !floating) {
614 // visual data coordinates after applying minBarLength
615 parsed._stacks[vScale.axis]._visualValues[datasetIndex] = vScale.getValueForPixel(head) - vScale.getValueForPixel(base);
616 }
617 }
618
619 if (base === vScale.getPixelForValue(actualBase)) {
620 const halfGrid = sign(size) * vScale.getLineWidthForValue(actualBase) / 2;

Callers 1

updateElementsMethod · 0.95

Calls 10

isFloatBarFunction · 0.85
isNullOrUndefFunction · 0.85
barSignFunction · 0.85
getParsedMethod · 0.80
applyStackMethod · 0.80
getDataVisibilityMethod · 0.80
getPixelForValueMethod · 0.65
getPixelForDecimalMethod · 0.65
getValueForPixelMethod · 0.65
getLineWidthForValueMethod · 0.65

Tested by

no test coverage detected