|
6 | 6 | (function (window, document, Chartist) { |
7 | 7 | 'use strict'; |
8 | 8 |
|
9 | | - var defaultOptions = { |
| 9 | + let defaultOptions = { |
10 | 10 | currency: undefined, |
11 | 11 | currencyFormatCallback: undefined, |
12 | 12 | tooltipOffset: { |
13 | 13 | x: 0, |
14 | | - y: -20, |
| 14 | + y: -20 |
15 | 15 | }, |
16 | 16 | anchorToPoint: false, |
17 | 17 | appendToBody: true, |
18 | 18 | class: undefined, |
19 | | - pointClass: 'ct-point', |
| 19 | + pointClass: 'ct-point' |
20 | 20 | }; |
21 | 21 |
|
22 | 22 | Chartist.plugins = Chartist.plugins || {}; |
|
25 | 25 |
|
26 | 26 | return function tooltip(chart) { |
27 | 27 | // Warning: If you are using npm link or yarn link, these instanceof checks will fail and you won't any tooltips |
28 | | - var tooltipSelector = options.pointClass; |
| 28 | + let tooltipSelector = options.pointClass; |
29 | 29 | if (chart instanceof Chartist.BarChart) { |
30 | 30 | tooltipSelector = 'ct-bar'; |
31 | 31 | } else if (chart instanceof Chartist.PieChart) { |
32 | 32 | // Added support for donut graph |
33 | 33 | if (chart.options.donut) { |
34 | 34 | // Added support for the solid donut graph |
35 | | - tooltipSelector = chart.options.donutSolid ? 'ct-slice-donut-solid' : 'ct-slice-donut'; |
| 35 | + tooltipSelector = chart.options.donutSolid |
| 36 | + ? 'ct-slice-donut-solid' |
| 37 | + : 'ct-slice-donut'; |
36 | 38 | } else { |
37 | 39 | tooltipSelector = 'ct-slice-pie'; |
38 | 40 | } |
39 | 41 | } |
40 | 42 |
|
41 | | - var $chart = chart.container; |
42 | | - var $toolTipIsShown = false; |
43 | | - var $tooltipOffsetParent = offsetParent($chart); |
44 | | - var $toolTip; |
| 43 | + let $chart = chart.container; |
| 44 | + let $toolTipIsShown = false; |
| 45 | + let $tooltipOffsetParent = offsetParent($chart); |
| 46 | + let $toolTip; |
45 | 47 |
|
46 | 48 | if (!options.appendToBody) { |
47 | 49 | // searching for existing tooltip in the chart, because appendToBody is disabled |
|
52 | 54 | } |
53 | 55 | if (!$toolTip) { |
54 | 56 | $toolTip = document.createElement('div'); |
55 | | - $toolTip.className = (!options.class) ? 'chartist-tooltip' : 'chartist-tooltip ' + options.class; |
| 57 | + $toolTip.className = !options.class |
| 58 | + ? 'chartist-tooltip' |
| 59 | + : 'chartist-tooltip ' + options.class; |
56 | 60 | if (!options.appendToBody) { |
57 | 61 | $chart.appendChild($toolTip); |
58 | 62 | } else { |
59 | 63 | document.body.appendChild($toolTip); |
60 | 64 | } |
61 | 65 | } |
62 | | - var height = $toolTip.offsetHeight; |
63 | | - var width = $toolTip.offsetWidth; |
| 66 | + let height = $toolTip.offsetHeight; |
| 67 | + let width = $toolTip.offsetWidth; |
64 | 68 |
|
65 | 69 | hide($toolTip); |
66 | 70 |
|
67 | 71 | function on(event, selector, callback) { |
68 | 72 | $chart.addEventListener(event, function (e) { |
69 | | - if (!selector || hasClass(e.target, selector)) |
| 73 | + if (!selector || hasClass(e.target, selector)) { |
70 | 74 | callback(e); |
| 75 | + } |
71 | 76 | }); |
72 | 77 | } |
73 | 78 |
|
74 | 79 | on('mouseover', tooltipSelector, function (event) { |
75 | | - var $point = event.target; |
76 | | - var tooltipText = ''; |
77 | | - |
78 | | - var isPieChart = (chart instanceof Chartist.PieChart) ? $point : $point.parentNode; |
79 | | - var seriesName = (isPieChart) ? $point.parentNode.getAttribute('ct:meta') || $point.parentNode.getAttribute('ct:series-name') : ''; |
80 | | - var meta = $point.getAttribute('ct:meta') || seriesName || ''; |
81 | | - var hasMeta = !!meta; |
82 | | - var value = $point.getAttribute('ct:value'); |
83 | | - |
84 | | - if (options.transformTooltipTextFnc && typeof options.transformTooltipTextFnc === 'function') { |
| 80 | + let $point = event.target; |
| 81 | + let tooltipText = ''; |
| 82 | + |
| 83 | + let isPieChart = |
| 84 | + chart instanceof Chartist.PieChart ? $point : $point.parentNode; |
| 85 | + let seriesName = isPieChart |
| 86 | + ? $point.parentNode.getAttribute('ct:meta') || |
| 87 | + $point.parentNode.getAttribute('ct:series-name') |
| 88 | + : ''; |
| 89 | + let meta = $point.getAttribute('ct:meta') || seriesName || ''; |
| 90 | + let hasMeta = !!meta; |
| 91 | + let value = $point.getAttribute('ct:value'); |
| 92 | + |
| 93 | + if ( |
| 94 | + options.transformTooltipTextFnc && |
| 95 | + typeof options.transformTooltipTextFnc === 'function' |
| 96 | + ) { |
85 | 97 | value = options.transformTooltipTextFnc(value); |
86 | 98 | } |
87 | 99 |
|
88 | 100 | if (options.tooltipFnc && typeof options.tooltipFnc === 'function') { |
89 | 101 | tooltipText = options.tooltipFnc(meta, value); |
90 | 102 | } else { |
91 | 103 | if (options.metaIsHTML) { |
92 | | - var txt = document.createElement('textarea'); |
| 104 | + let txt = document.createElement('textarea'); |
93 | 105 | txt.innerHTML = meta; |
94 | 106 | meta = txt.value; |
95 | 107 | } |
|
102 | 114 | // For Pie Charts also take the labels into account |
103 | 115 | // Could add support for more charts here as well! |
104 | 116 | if (chart instanceof Chartist.PieChart) { |
105 | | - var label = next($point, 'ct-label'); |
| 117 | + let label = next($point, 'ct-label'); |
106 | 118 | if (label) { |
107 | 119 | tooltipText += text(label) + '<br>'; |
108 | 120 | } |
|
111 | 123 |
|
112 | 124 | if (value) { |
113 | 125 | if (options.currency) { |
114 | | - if (options.currencyFormatCallback != undefined) { |
| 126 | + if (options.currencyFormatCallback !== undefined) { |
115 | 127 | value = options.currencyFormatCallback(value, options); |
116 | 128 | } else { |
117 | | - value = options.currency + value.replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, '$1,'); |
| 129 | + value = |
| 130 | + options.currency + |
| 131 | + value.replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, '$1,'); |
118 | 132 | } |
119 | 133 | } |
120 | 134 | value = '<span class="chartist-tooltip-value">' + value + '</span>'; |
|
157 | 171 | function setPosition(event) { |
158 | 172 | height = height || $toolTip.offsetHeight; |
159 | 173 | width = width || $toolTip.offsetWidth; |
160 | | - var offsetX = -width / 2 + options.tooltipOffset.x; |
161 | | - var offsetY = -height + options.tooltipOffset.y; |
| 174 | + let offsetX = -width / 2 + options.tooltipOffset.x; |
| 175 | + let offsetY = -height + options.tooltipOffset.y; |
162 | 176 |
|
163 | | - var anchor = options.anchorToPoint === true && event.target.x2 && event.target.y2; |
| 177 | + let anchor = |
| 178 | + options.anchorToPoint === true && event.target.x2 && event.target.y2; |
164 | 179 |
|
165 | 180 | if (options.appendToBody === true) { |
166 | 181 | if (anchor) { |
167 | | - var box = $chart.getBoundingClientRect(); |
168 | | - var left = event.target.x2.baseVal.value + box.left + window.pageXOffset; |
169 | | - var top = event.target.y2.baseVal.value + box.top + window.pageYOffset; |
| 182 | + const box = $chart.getBoundingClientRect(); |
| 183 | + const left = |
| 184 | + event.target.x2.baseVal.value + box.left + window.pageXOffset; |
| 185 | + const top = |
| 186 | + event.target.y2.baseVal.value + box.top + window.pageYOffset; |
170 | 187 |
|
171 | 188 | $toolTip.style.left = left + offsetX + 'px'; |
172 | 189 | $toolTip.style.top = top + offsetY + 'px'; |
|
175 | 192 | $toolTip.style.top = event.pageY + offsetY + 'px'; |
176 | 193 | } |
177 | 194 | } else { |
178 | | - var offsetBox = $tooltipOffsetParent.getBoundingClientRect(); |
179 | | - var allOffsetLeft = -offsetBox.left - window.pageXOffset + offsetX; |
180 | | - var allOffsetTop = -offsetBox.top - window.pageYOffset + offsetY; |
| 195 | + let offsetBox = $tooltipOffsetParent.getBoundingClientRect(); |
| 196 | + let allOffsetLeft = -offsetBox.left - window.pageXOffset + offsetX; |
| 197 | + let allOffsetTop = -offsetBox.top - window.pageYOffset + offsetY; |
181 | 198 |
|
182 | 199 | if (anchor) { |
183 | | - var box = $chart.getBoundingClientRect(); |
184 | | - var left = event.target.x2.baseVal.value + box.left + window.pageXOffset; |
185 | | - var top = event.target.y2.baseVal.value + box.top + window.pageYOffset; |
| 200 | + const box = $chart.getBoundingClientRect(); |
| 201 | + const left = |
| 202 | + event.target.x2.baseVal.value + box.left + window.pageXOffset; |
| 203 | + const top = |
| 204 | + event.target.y2.baseVal.value + box.top + window.pageYOffset; |
186 | 205 |
|
187 | 206 | $toolTip.style.left = left + allOffsetLeft + 'px'; |
188 | 207 | $toolTip.style.top = top + allOffsetTop + 'px'; |
|
210 | 229 | */ |
211 | 230 | function hide(element) { |
212 | 231 | $toolTipIsShown = false; |
213 | | - var regex = new RegExp('tooltip-show' + '\\s*', 'gi'); |
| 232 | + let regex = new RegExp('tooltip-show' + '\\s*', 'gi'); |
214 | 233 | element.className = element.className.replace(regex, '').trim(); |
215 | 234 | } |
216 | | - |
217 | 235 | }; |
218 | 236 | }; |
219 | 237 |
|
|
224 | 242 | * @return {boolean} |
225 | 243 | */ |
226 | 244 | function hasClass(element, className) { |
227 | | - return (' ' + element.getAttribute('class') + ' ').indexOf(' ' + className + ' ') > -1; |
| 245 | + return ( |
| 246 | + (' ' + element.getAttribute('class') + ' ').indexOf( |
| 247 | + ' ' + className + ' ' |
| 248 | + ) > -1 |
| 249 | + ); |
228 | 250 | } |
229 | 251 |
|
230 | 252 | function next(element, className) { |
|
250 | 272 | function offsetParent(elem) { |
251 | 273 | if (offsetParent in elem) { |
252 | 274 | // Using the native property if possible |
253 | | - var parent = elem.offsetParent; |
| 275 | + let parent = elem.offsetParent; |
254 | 276 |
|
255 | 277 | if (!parent) { |
256 | 278 | parent = document.body.parentElement; |
|
259 | 281 | return parent; |
260 | 282 | } |
261 | 283 |
|
262 | | - var parent = elem.parentNode; |
| 284 | + const parent = elem.parentNode; |
263 | 285 | if (!parent) { |
264 | 286 | return document.body.parentElement; |
265 | 287 | } |
|
272 | 294 | return offsetParent(parent); |
273 | 295 | } |
274 | 296 | } |
275 | | - |
276 | | -}(window, document, Chartist)); |
| 297 | +})(window, document, Chartist); |
0 commit comments