forked from chartjs/chartjs-plugin-annotation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chart.Annotation.js
227 lines (186 loc) · 6.38 KB
/
Chart.Annotation.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*!
* Chart.Annotation.js
* http://chartjs.org/
* Version: 0.1.1
*
* Copyright 2016 Evert Timberg
* Released under the MIT license
* https://github.com/chartjs/Chart.Annotation.js/blob/master/LICENSE.md
*/
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
},{}],2:[function(require,module,exports){
// Box Annotation implementation
module.exports = function(Chart) {
var BoxAnnotation = Chart.Element.extend({
draw: function(ctx) {
var view = this._view;
// Canvas setup
ctx.lineWidth = view.borderWidth;
ctx.strokeStyle = view.borderColor;
ctx.fillStyle = view.backgroundColor;
// Draw
var width = view.right - view.left,
height = view.bottom - view.top;
ctx.fillRect(view.left, view.top, width, height);
ctx.strokeRect(view.left, view.top, width, height)
}
});
function isValid(num) {
return !isNaN(num) && isFinite(num);
}
// Function that updates a box annotation
function boxUpdate(obj, options, chartInstance) {
var model = obj._model = obj._model || {};
var xScale = chartInstance.scales[options.xScaleID];
var yScale = chartInstance.scales[options.yScaleID];
var chartArea = chartInstance.chartArea;
var left = chartArea.left,
top = chartArea.top,
right = chartArea.right,
bottom = chartArea.bottom;
var min,max;
if (xScale) {
min = isValid(options.xMin) ? xScale.getPixelForValue(options.xMin) : chartArea.left;
max = isValid(options.xMax) ? xScale.getPixelForValue(options.xMax) : chartArea.right;
left = Math.min(min, max);
right = Math.max(min, max);
}
if (yScale) {
min = isValid(options.yMin) ? yScale.getPixelForValue(options.yMin) : chartArea.bottom;
max = isValid(options.yMax) ? yScale.getPixelForValue(options.yMax) : chartArea.top;
top = Math.min(min, max);
bottom = Math.max(min, max);
}
// Ensure model has rect coordinates
model.left = left;
model.top = top;
model.right = right;
model.bottom = bottom;
// Stylistic options
model.borderColor = options.borderColor;
model.borderWidth = options.borderWidth;
model.backgroundColor = options.backgroundColor;
}
return {
Constructor: BoxAnnotation,
update: boxUpdate
};
}
},{}],3:[function(require,module,exports){
// Get the chart variable
var Chart = require('chart.js');
Chart = typeof(Chart) === 'function' ? Chart : window.Chart;
var helpers = Chart.helpers;
var isArray = helpers.isArray;
// Take the zoom namespace of Chart
Chart.Annotation = Chart.Annotation || {};
// Default options if none are provided
var defaultOptions = Chart.Annotation.defaults = {
annotations: [] // default to no annotations
};
var lineAnnotation = require('./line.js')(Chart);
var boxAnnotation = require('./box.js')(Chart);
// Map of all types
var annotationTypes = Chart.Annotation.annotationTypes = {
line: lineAnnotation.Constructor,
box: boxAnnotation.Constructor
};
// Map of all update functions
var updateFunctions = Chart.Annotation.updateFunctions = {
line: lineAnnotation.update,
box: boxAnnotation.update
};
// Chartjs Zoom Plugin
var AnnotationPlugin = Chart.PluginBase.extend({
beforeInit: function(chartInstance) {
var options = chartInstance.options;
options.annotation = helpers.configMerge(options.annotation, Chart.Annotation.defaults);
var annotationConfigs = options.annotation.annotations;
if (isArray(annotationConfigs)) {
var annotationObjects = chartInstance._annotationObjects = [];
annotationConfigs.forEach(function(configuration, i) {
var Constructor = annotationTypes[configuration.type];
if (Constructor) {
annotationObjects.push(new Constructor({
_index: i
}));
}
});
}
},
afterScaleUpdate: function(chartInstance) {
// Once scales are ready, update
var annotationObjects = chartInstance._annotationObjects;
var annotationOpts = chartInstance.options.annotation;
if (isArray(annotationObjects)) {
annotationObjects.forEach(function(annotationObject, i) {
var opts = annotationOpts.annotations[annotationObject._index];
var updateFunction = updateFunctions[opts.type];
if (updateFunction) {
updateFunction(annotationObject, opts, chartInstance);
}
});
}
},
afterDraw: function(chartInstance, easingDecimal) {
// If we have annotations, draw them
var annotationObjects = chartInstance._annotationObjects;
if (isArray(annotationObjects)) {
var ctx = chartInstance.chart.ctx;
annotationObjects.forEach(function(obj) {
obj.transition(easingDecimal).draw(ctx);
});
}
}
});
module.exports = AnnotationPlugin;
Chart.pluginService.register(new AnnotationPlugin());
},{"./box.js":2,"./line.js":4,"chart.js":1}],4:[function(require,module,exports){
// Line Annotation implementation
module.exports = function(Chart) {
var horizontalKeyword = 'horizontal';
var verticalKeyword = 'vertical';
var LineAnnotation = Chart.Element.extend({
draw: function(ctx) {
var view = this._view;
// Canvas setup
ctx.lineWidth = view.borderWidth;
ctx.strokeStyle = view.borderColor;
if (ctx.setLineDash) {
ctx.setLineDash(view.borderDash);
}
ctx.lineDashOffset = view.borderDashOffset;
// Draw
ctx.beginPath();
ctx.moveTo(view.x1, view.y1);
ctx.lineTo(view.x2, view.y2);
ctx.stroke();
}
});
function lineUpdate(obj, options, chartInstance) {
var model = obj._model = obj._model || {};
var scale = chartInstance.scales[options.scaleID];
var pixel = scale ? scale.getPixelForValue(options.value) : NaN;
var chartArea = chartInstance.chartArea;
if (!isNaN(pixel)) {
if (options.mode == horizontalKeyword) {
model.x1 = chartArea.left;
model.x2 = chartArea.right;
model.y1 = model.y2 = pixel;
} else {
model.y1 = chartArea.top;
model.y2 = chartArea.bottom;
model.x1 = model.x2 = pixel;
}
}
model.borderColor = options.borderColor;
model.borderWidth = options.borderWidth;
model.borderDash = options.borderDash || [];
model.borderDashOffset = options.borderDashOffset || 0;
}
return {
Constructor: LineAnnotation,
update: lineUpdate
};
};
},{}]},{},[3]);