* Average mode places the tooltip at the average position of the elements shown
(items)
| 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 |
nothing calls this directly
no test coverage detected