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

Function average

src/plugins/plugin.tooltip.js:21–52  ·  view source on GitHub ↗

* Average mode places the tooltip at the average position of the elements shown

(items)

Source from the content-addressed store, hash-verified

19 * Average mode places the tooltip at the average position of the elements shown
20 */
21 average(items) {
22 if (!items.length) {
23 return false;
24 }
25
26 let i, len;
27 let xSet = new Set();
28 let y = 0;
29 let count = 0;
30
31 for (i = 0, len = items.length; i < len; ++i) {
32 const el = items[i].element;
33 if (el && el.hasValue()) {
34 const pos = el.tooltipPosition();
35 xSet.add(pos.x);
36 y += pos.y;
37 ++count;
38 }
39 }
40
41 // No visible items where found, return false so we don't have to divide by 0 which reduces in NaN
42 if (count === 0 || xSet.size === 0) {
43 return false;
44 }
45
46 const xAverage = [...xSet].reduce((a, b) => a + b) / xSet.size;
47
48 return {
49 x: xAverage,
50 y: y / count
51 };
52 },
53
54 /**
55 * Gets the tooltip position nearest of the item nearest to the event position

Callers

nothing calls this directly

Calls 3

hasValueMethod · 0.80
addMethod · 0.65
tooltipPositionMethod · 0.45

Tested by

no test coverage detected