-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchartist-plugin-averageline.js
More file actions
96 lines (84 loc) · 3.34 KB
/
chartist-plugin-averageline.js
File metadata and controls
96 lines (84 loc) · 3.34 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
88
89
90
91
92
93
94
95
96
/* global Chartist */
(function(window, document, Chartist) {
'use strict';
var defaultOptions = {
value: null,
averageCircleRadius: 11,
averageCircleClass: 'ct-circle-average',
averageCircleXOffset: 30,
averageLabelClass: 'ct-label-average',
averageLabelOffset: {
x: 0,
y: 0
},
averageLineClass: 'ct-line-average',
highlightPointClass: 'ct-point-highlight',
warningPointClass: 'ct-point-warning',
textAnchor: 'middle'
};
Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.ctAverageLine = function(options) {
options = Chartist.extend({}, defaultOptions, options);
return function ctAverageLine(chart) {
if(chart instanceof Chartist.Line) {
chart.on('draw', function(data) {
if (data.type == "point")
{
// highlight points equal or over the average
if (options.value !== null && data.value >= options.value)
{
var element = data.element._node;
element.setAttribute('class', element.getAttribute('class') + ' ' + options.highlightPointClass);
}
// warn zero points
if (data.value == 0)
{
var element = data.element._node;
element.setAttribute('class', element.getAttribute('class') + ' ' + options.warningPointClass);
}
}
});
chart.on('created', function(data) {
// Initiate Y axis
var axisY = new Chartist.LinearScaleAxis(
Chartist.Axis.units.y,
data.chartRect,
function yAxisTransform(projectedValue) {
projectedValue.pos = data.chartRect.y1 - projectedValue.pos;
return projectedValue;
},
{
x: data.options.chartPadding + data.options.axisY.labelOffset.x + (Chartist.Svg.isSupported('Extensibility') ? -10 : 0),
y: data.options.axisY.labelOffset.y + (Chartist.Svg.isSupported('Extensibility') ? -15 : 0)
},
{
highLow: { high: data.bounds.high, low: data.bounds.low },
scaleMinSpace: data.options.axisY.scaleMinSpace
}
);
// Project average value on y axis
var yavg = data.chartRect.y1 - axisY.projectValue(options.value).pos;
// Draw label
data.svg.elem('text', {
x: data.chartRect.x1 - options.averageCircleXOffset + options.averageLabelOffset.x,
y: yavg + options.averageLabelOffset.y,
style: 'text-anchor: ' + options.textAnchor
}, options.averageLabelClass, true).text(options.value);
// circle label
data.svg.elem('circle',{
cx: data.chartRect.x1 - options.averageCircleXOffset,
cy: yavg,
r: options.averageCircleRadius
}, options.averageCircleClass, true);
// draw average line behind the graph
data.svg.elem('line',{
x1: data.chartRect.x1 - options.averageCircleXOffset,
y1: yavg,
x2: data.chartRect.x2,
y2: yavg
}, options.averageLineClass, true);
});
}
};
};
}(window, document, Chartist));