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

Function getPixelForGridLine

src/core/core.scale.js:40–65  ·  view source on GitHub ↗

* @param {Scale} scale * @param {number} index * @param {boolean} offsetGridLines

(scale, index, offsetGridLines)

Source from the content-addressed store, hash-verified

38 * @param {boolean} offsetGridLines
39 */
40function getPixelForGridLine(scale, index, offsetGridLines) {
41 const length = scale.ticks.length;
42 const validIndex = Math.min(index, length - 1);
43 const start = scale._startPixel;
44 const end = scale._endPixel;
45 const epsilon = 1e-6; // 1e-6 is margin in pixels for accumulated error.
46 let lineValue = scale.getPixelForTick(validIndex);
47 let offset;
48
49 if (offsetGridLines) {
50 if (length === 1) {
51 offset = Math.max(lineValue - start, end - lineValue);
52 } else if (index === 0) {
53 offset = (scale.getPixelForTick(1) - lineValue) / 2;
54 } else {
55 offset = (lineValue - scale.getPixelForTick(validIndex - 1)) / 2;
56 }
57 lineValue += validIndex < index ? offset : -offset;
58
59 // Return undefined if the pixel is out of the range
60 if (lineValue < start - epsilon || lineValue > end + epsilon) {
61 return;
62 }
63 }
64 return lineValue;
65}
66
67/**
68 * @param {object} caches

Callers 1

_computeGridLineItemsMethod · 0.85

Calls 1

getPixelForTickMethod · 0.65

Tested by

no test coverage detected