-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathchartist-plugin-tooltip.js
More file actions
87 lines (67 loc) · 2.77 KB
/
chartist-plugin-tooltip.js
File metadata and controls
87 lines (67 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['chartist', 'jquery'], function (chartist, jquery) {
return (root.returnExportsGlobal = factory(chartist, jquery));
});
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory(require('chartist'), require('jquery'));
} else {
root['Chartist.plugins.tooltip'] = factory(root.chartist, root.jquery);
}
}(this, function (Chartist, $) {
/**
* This Chartist tooltip plugin is a modified version of
* https://github.com/Globegitter/chartist-plugin-tooltip.
*
*/
'use strict';
var defaultOptions = {
valueTransform: Chartist.noop,
seriesName: true // Show name of series in tooltip.
};
Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.tooltip = function (options) {
options = Chartist.extend({}, defaultOptions, options);
return function tooltip(chart) {
var tooltipSelector = '.ct-point';
if (chart instanceof Chartist.Bar) {
tooltipSelector = '.ct-bar';
} else if (chart instanceof Chartist.Pie) {
tooltipSelector = '[class^=ct-slice]';
}
var $chart = $(chart.container),
$toolTip = $chart
.append('<div class="ct-tooltip"></div>')
.find('.ct-tooltip')
.hide();
$chart.on('mouseenter', tooltipSelector, function() {
var $point = $(this),
seriesName = $point.parent().attr('ct:series-name'),
tooltipText = '';
if (options.seriesName && seriesName) {
tooltipText += seriesName + '<br>';
}
if ($point.attr('ct:meta')) {
tooltipText += $point.attr('ct:meta') + '<br>';
}
var value = $point.attr('ct:value') || '0';
tooltipText += options.valueTransform(value);
$toolTip.html(tooltipText).show();
});
$chart.on('mouseleave', tooltipSelector, function() {
$toolTip.hide();
});
$chart.on('mousemove', function(event) {
$toolTip.css({
left: (event.offsetX || event.originalEvent.layerX) - $toolTip.width() / 2 - 10,
top: (event.offsetY || event.originalEvent.layerY) - $toolTip.height() - 40
});
});
};
};
return Chartist.plugins.tooltip;
}));