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

Function getRelativePosition

src/helpers/helpers.dom.ts:105–131  ·  view source on GitHub ↗
(
  event: Event | ChartEvent | TouchEvent | MouseEvent,
  chart: Chart | PrivateChart
)

Source from the content-addressed store, hash-verified

103 */
104
105export function getRelativePosition(
106 event: Event | ChartEvent | TouchEvent | MouseEvent,
107 chart: Chart | PrivateChart
108): { x: number; y: number } {
109 if ('native' in event) {
110 return event;
111 }
112
113 const {canvas, currentDevicePixelRatio} = chart;
114 const style = getComputedStyle(canvas);
115 const borderBox = style.boxSizing === 'border-box';
116 const paddings = getPositionedStyle(style, 'padding');
117 const borders = getPositionedStyle(style, 'border', 'width');
118 const {x, y, box} = getCanvasPosition(event, canvas);
119 const xOffset = paddings.left + (box && borders.left);
120 const yOffset = paddings.top + (box && borders.top);
121
122 let {width, height} = chart;
123 if (borderBox) {
124 width -= paddings.width + borders.width;
125 height -= paddings.height + borders.height;
126 }
127 return {
128 x: Math.round((x - xOffset) / width * canvas.width / currentDevicePixelRatio),
129 y: Math.round((y - yOffset) / height * canvas.height / currentDevicePixelRatio)
130 };
131}
132
133function getContainerSize(canvas: HTMLCanvasElement, width: number, height: number): Partial<Scale> {
134 let maxWidth: number, maxHeight: number;

Callers 8

dom.tsFile · 0.85
indexFunction · 0.85
datasetFunction · 0.85
pointFunction · 0.85
nearestFunction · 0.85
xFunction · 0.85
yFunction · 0.85
fromNativeEventFunction · 0.85

Calls 3

getComputedStyleFunction · 0.85
getPositionedStyleFunction · 0.85
getCanvasPositionFunction · 0.85

Tested by

no test coverage detected