-
Notifications
You must be signed in to change notification settings - Fork 1
/
chartist-plugin-barlabels.js
61 lines (54 loc) · 1.83 KB
/
chartist-plugin-barlabels.js
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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], function () {
return (root.returnExportsGlobal = factory());
});
} 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();
} else {
root['Chartist.plugins.ctBarLabels'] = factory();
}
}(this, function () {
/**
* Chartist.js plugin to display a data label left or right to a bar chart column.
*
*/
/* global Chartist */
(function(window, document, Chartist) {
'use strict';
var defaultOptions = {
labelClass: 'ct-label',
labelOffset: {
x: 20,
y: -4
},
barAnchor: 'left',
labelPrefix: '',
labelSuffix: ''
};
Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.ctBarLabels = function(options) {
options = Chartist.extend({}, defaultOptions, options);
return function ctBarLabels(chart) {
// bar labels
if(chart instanceof Chartist.Bar) {
chart.on('draw', function(data) {
var centerPos = (data.y1 + data.y2)/2;
if(data.type === 'bar') {
data.group.elem('text', {
x: data.x1 + options.labelOffset.x,
y: centerPos - options.labelOffset.y,
style: 'text-anchor: ' + options.barAnchor
}, options.labelClass).text(options.labelPrefix + data.value + options.labelSuffix);
}
});
}
};
};
}(window, document, Chartist));
return Chartist.plugins.ctBarLabels;
}));