31 (function (window, document, Chartist) {
34 var defaultOptions = {
36 currencyFormatCallback: undefined,
44 pointClass: 'ct-point'
47 Chartist.plugins = Chartist.plugins || {};
48 Chartist.plugins.tooltip = function (options) {
49 options = Chartist.extend({}, defaultOptions, options);
51 return function tooltip(chart) {
52 var tooltipSelector = options.pointClass;
53 if (chart instanceof Chartist.Bar) {
54 tooltipSelector = 'ct-bar';
55 } else if (chart instanceof Chartist.Pie) {
56 // Added support for donut graph
57 if (chart.options.donut) {
58 tooltipSelector = 'ct-slice-donut';
60 tooltipSelector = 'ct-slice-pie';
64 var $chart = chart.container;
65 var $toolTip = $chart.querySelector('.chartist-tooltip');
67 $toolTip = document.createElement('div');
68 $toolTip.className = (!options.class) ? 'chartist-tooltip' : 'chartist-tooltip ' + options.class;
69 if (!options.appendToBody) {
70 $chart.appendChild($toolTip);
72 document.body.appendChild($toolTip);
75 var height = $toolTip.offsetHeight;
76 var width = $toolTip.offsetWidth;
80 function on(event, selector, callback) {
81 $chart.addEventListener(event, function (e) {
82 if (!selector || hasClass(e.target, selector))
87 on('mouseover', tooltipSelector, function (event) {
88 var $point = event.target;
91 var isPieChart = (chart instanceof Chartist.Pie) ? $point : $point.parentNode;
92 var seriesName = (isPieChart) ? $point.parentNode.getAttribute('ct:meta') || $point.parentNode.getAttribute('ct:series-name') : '';
93 var meta = $point.getAttribute('ct:meta') || seriesName || '';
95 var value = $point.getAttribute('ct:value');
96 if (options.transformTooltipTextFnc && typeof options.transformTooltipTextFnc === 'function') {
97 value = options.transformTooltipTextFnc(value);
100 if (options.tooltipFnc && typeof options.tooltipFnc === 'function') {
101 tooltipText = options.tooltipFnc(meta, value);
103 if (options.metaIsHTML) {
104 var txt = document.createElement('textarea');
105 txt.innerHTML = meta;
109 meta = '<span class="chartist-tooltip-meta">' + meta + '</span>';
112 tooltipText += meta + '<br>';
114 // For Pie Charts also take the labels into account
115 // Could add support for more charts here as well!
116 if (chart instanceof Chartist.Pie) {
117 var label = next($point, 'ct-label');
119 tooltipText += text(label) + '<br>';
125 if (value % 1 === 0) {
126 value = '<span class="chartist-tooltip-value">' + minify_numbers(value) + '</span>';
127 tooltipText += value;
129 value = '<span class="chartist-tooltip-value">' + humanise_time(parseFloat(value)) + '</span>';
130 tooltipText += value;
136 $toolTip.innerHTML = tooltipText;
140 // Remember height and width to avoid wrong position in IE
141 height = $toolTip.offsetHeight;
142 width = $toolTip.offsetWidth;
146 on('mouseout', tooltipSelector, function () {
150 on('mousemove', null, function (event) {
151 if (false === options.anchorToPoint)
155 function setPosition(event) {
156 height = height || $toolTip.offsetHeight;
157 width = width || $toolTip.offsetWidth;
158 var offsetX = - width / 2 + options.tooltipOffset.x;
159 var offsetY = - height + options.tooltipOffset.y;
160 var anchorX, anchorY;
162 if (!options.appendToBody) {
163 var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
164 var top = event.pageY - window.pageYOffset + scrollTop ;
165 var left = event.pageX- window.pageXOffset;
167 if (true === options.anchorToPoint && event.target.x2 && event.target.y2) {
168 anchorX = parseInt(event.target.x2.baseVal.value);
169 anchorY = parseInt(event.target.y2.baseVal.value);
172 $toolTip.style.top = (anchorY || top) + offsetY + 'px';
173 $toolTip.style.left = (anchorX || left) + offsetX + 'px';
175 $toolTip.style.top = event.pageY + offsetY + 'px';
176 $toolTip.style.left = event.pageX + offsetX + 'px';
182 function show(element) {
183 if(!hasClass(element, 'tooltip-show')) {
184 element.className = element.className + ' tooltip-show';
188 function hide(element) {
189 var regex = new RegExp('tooltip-show' + '\\s*', 'gi');
190 element.className = element.className.replace(regex, '').trim();
193 function hasClass(element, className) {
194 return (' ' + element.getAttribute('class') + ' ').indexOf(' ' + className + ' ') > -1;
197 function next(element, className) {
199 element = element.nextSibling;
200 } while (element && !hasClass(element, className));
204 function text(element) {
205 return element.innerText || element.textContent;
208 } (window, document, Chartist));