-
Notifications
You must be signed in to change notification settings - Fork 7
/
chartjs-plugin-watermark.js
196 lines (154 loc) · 5.67 KB
/
chartjs-plugin-watermark.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
/* chartjs-plugin-watermark | AlbinoDrought | MIT License | https://github.com/AlbinoDrought/chartjs-plugin-watermark/blob/master/LICENSE */
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
/**
* Chart.js Simple Watermark plugin
*
* Valid options:
*
* options: {
* watermark: {
* // required
* image: new Image(),
*
* x: 0,
* y: 0,
*
* width: 0,
* height: 0,
*
* alignX: "left"/"right"/"middle",
* alignY: "top"/"bottom"/"middle",
*
* position: "front"/"back/between",
*
* opacity: 0 to 1, // uses ctx.globalAlpha
* }
* }
*
* Created by Sean on 12/19/2016.
*/
var watermarkPlugin = {
id: 'watermark',
defaultOptions: {
x: 0,
y: 0,
height: false,
width: false,
alignX: "top",
alignY: "left",
alignToChartArea: false,
position: "front",
opacity: 1,
image: false,
},
isPercentage: function (value) {
return typeof(value) == "string" && value.charAt(value.length - 1) == "%";
},
calcPercentage: function (percentage, max) {
var value = percentage.substr(0, percentage.length - 1);
value = parseFloat(value);
return max * (value / 100);
},
autoPercentage: function (value, maxIfPercentage) {
if (this.isPercentage(value)) {
value = this.calcPercentage(value, maxIfPercentage);
}
return value;
},
imageFromString: function (imageSrc) {
// create the image object with this as our src
var imageObj = new Image();
imageObj.src = imageSrc;
return imageObj;
},
drawWatermark: function (chartInstance, position) {
var watermark = chartInstance.watermark;
// only draw watermarks meant for us
if (watermark.position != position) return;
if (watermark.image) {
var image = watermark.image;
var context = chartInstance.ctx;
var canvas = context.canvas;
var cHeight, cWidth;
var offsetX = 0, offsetY = 0;
if(watermark.alignToChartArea) {
var chartArea = chartInstance.chartArea;
cHeight = chartArea.bottom - chartArea.top;
cWidth = chartArea.right - chartArea.left;
offsetX = chartArea.left;
offsetY = chartArea.top;
} else {
cHeight = canvas.clientHeight || canvas.height;
cWidth = canvas.clientWidth || canvas.width;
}
var height = watermark.height || image.height;
height = this.autoPercentage(height, cHeight);
var width = watermark.width || image.width;
width = this.autoPercentage(width, cWidth);
var x = this.autoPercentage(watermark.x, cWidth);
var y = this.autoPercentage(watermark.y, cHeight);
switch (watermark.alignX) {
case "right":
x = cWidth - x - width;
break;
case "middle":
x = (cWidth / 2) - (width / 2) - x;
break;
}
switch (watermark.alignY) {
case "bottom":
y = cHeight - y - height;
break;
case "middle":
y = (cHeight / 2) - (height / 2) - y;
break;
}
context.save();
context.globalAlpha = watermark.opacity;
context.drawImage(image, offsetX + x, offsetY + y, width, height);
context.restore();
}
},
beforeInit: function (chartInstance) {
chartInstance.watermark = {};
var options = chartInstance.options;
if (options.watermark) {
var clonedDefaultOptions = Object.assign({}, this.defaultOptions),
watermark = Object.assign(clonedDefaultOptions, options.watermark);
if (watermark.image) {
var image = watermark.image;
if (typeof(image) == "string") {
image = this.imageFromString(image);
}
// automatically refresh the chart once the image has loaded (if necessary)
image.onload = function () {
if(chartInstance.ctx) {
chartInstance.update();
}
};
watermark.image = image;
}
chartInstance.watermark = watermark;
}
},
// draw the image behind most chart elements
beforeDraw: function (chartInstance) {
this.drawWatermark(chartInstance, "back");
},
// draw the image in front of most chart elements
afterDraw: function (chartInstance) {
this.drawWatermark(chartInstance, "front");
},
// draw the image in front of chart elements, but before tooltips
afterDatasetsDraw: function (chartInstance) {
this.drawWatermark(chartInstance, "between");
},
};
module.exports = watermarkPlugin;
// If used in browser, register globally
if (typeof window !== 'undefined') {
if (window.Chart) {
window.Chart.register(watermarkPlugin);
}
}
},{}]},{},[1]);