MCPcopy Create free account
hub / github.com/chartjs/Chart.js / average

Function average

src/plugins/plugin.tooltip.js:55–87  ·  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

53 * Average mode places the tooltip at the average position of the elements shown
54 */
55 average(items) {
56 if (!items.length) {
57 return false;
58 }
59
60 const chartArea = getChartArea(this);
61 let i, len;
62 let xSet = new Set();
63 let y = 0;
64 let count = 0;
65
66 for (i = 0, len = items.length; i < len; ++i) {
67 const el = items[i].element;
68 if (el && el.hasValue()) {
69 const pos = getAnchorPosition(el, chartArea);
70 xSet.add(pos.x);
71 y += pos.y;
72 ++count;
73 }
74 }
75
76 // No visible items where found, return false so we don't have to divide by 0 which reduces in NaN
77 if (count === 0 || xSet.size === 0) {
78 return false;
79 }
80
81 const xAverage = [...xSet].reduce((a, b) => a + b) / xSet.size;
82
83 return {
84 x: xAverage,
85 y: y / count
86 };
87 },
88
89 /**
90 * Gets the tooltip position nearest of the item nearest to the event position

Callers

nothing calls this directly

Calls 4

getChartAreaFunction · 0.85
getAnchorPositionFunction · 0.85
hasValueMethod · 0.80
addMethod · 0.65

Tested by

no test coverage detected