-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighres.js
194 lines (175 loc) · 6.16 KB
/
highres.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
function ChartHighRes(chart, canvas) {
// See https://stackoverflow.com/a/50300880/336311
var button = document.createElement("img");
button.src = "button.download.0.svg";
button.setAttribute("alt", "⇓");
button.style.width = "30px";
button.style.display = "block";
var link = document.createElement('a');
link.download = 'chart.png';
link.style.position = "absolute";
link.style.display = "block";
link.style.border = "1px solid #CCCCCC";
link.style.borderRadius = "5px";
link.style.background = "#EEEEEE";
link.style.padding = "3px";
link.style.opacity = 0;
link.append(button);
var container = canvas.offsetParent;
container.append(link);
var canvasWidth = canvas.offsetWidth;
var canvasHeight = canvas.offsetHeight;
/**
* Report mouse position relative to the page.
* @param {Event} evt
* @return undefined|Object
*/
function getMousePos(evt) {
// IE events
if (!evt) {
evt = window.event;
}
if (!evt) {
return;
}
var mouseX;
var mouseY;
// Store position
if (evt.touches && evt.touches[0]) {
mouseX = evt.touches[0].pageX;
mouseY = evt.touches[0].pageY;
} else if (evt.changedTouches && evt.changedTouches[0]) {
mouseX = evt.changedTouches[0].pageX;
mouseY = evt.changedTouches[0].pageY;
} else if (evt.pageX) {
// evt.pageX returns a wrong value on Samsung Galaxy III/Android?!
mouseX = evt.pageX;
mouseY = evt.pageY;
} else if (evt.clientX) {
mouseX = evt.clientX + document.body.scrollLeft;
mouseY = evt.clientY + document.body.scrollTop;
} else {
return;
}
return {
x: mouseX,
y: mouseY
};
};
function getNodePos(node) {
if ((node === null) || (node === undefined)) {
throw new Error("No HTML node specified for getNodePosition");
}
if (node.getBoundingClientRect) {
// The body may tell a wrong value for top in Firefox
// var root = document.body.getBoundingClientRect();
var rect = node.getBoundingClientRect();
var sx = -(window.scrollX ? window.scrollX : window.pageXOffset);
var sy = -(window.scrollY ? window.scrollY : window.pageYOffset);
return {
x: rect.left - sx,
y: rect.top - sy,
w: rect.right - rect.left,
h: rect.bottom - rect.top
};
}
var px = node.offsetLeft;
var py = node.offsetTop;
// Find relative to page, not to offsetParent
while (node.offsetParent) {
node = node.offsetParent;
px += node.offsetLeft - node.scrollLeft;
py += node.offsetTop - node.scrollTop;
}
return {
x: px,
y: py,
w: node.offsetWidth,
h: node.offsetHeight
};
};
/**
* Try and change each size definition in the chart
*/
function changeFontSize(options, factor, level) {
if ((level > 10) || (typeof options !== 'object')) {
return options;
}
for (var key in options) {
if ((key === "font") && ("size" in options[key])) {
options.font.size = options.font.size * factor;
} else if ((key === "gridLines") && ("tickMarkLength" in options[key])) {
options.gridLines.tickMarkLength = options.gridLines.tickMarkLength * factor;
} else if (key === "borderWidth") {
options.borderWidth = options.borderWidth * factor;
} else {
options[key] = changeFontSize(options[key], factor, level + 1);
}
}
return options;
}
// Create a larger canvas for the high resolution output
var canvas2 = document.createElement("canvas");
canvas2.setAttribute("width", 2048);
canvas2.setAttribute("height", Math.round(2048 / canvasWidth * canvasHeight));
canvas2.style.position = "absolute";
canvas2.style.zIndex = -1;
canvas2.style.width = canvasWidth + "px";
canvas2.style.height = canvasHeight + "px";
document.body.append(canvas2);
var options = changeFontSize(chart.config.options, 2048 / canvasWidth, 0);
var data = changeFontSize(chart.config.data, 2048 / canvasWidth, 0);
options.responsive = false;
var chartCopy = new Chart(
canvas2.getContext("2d"),
{
type: chart.config.type,
data: data,
options: options
}
);
function onMouseOver() {
link.href = canvas2.toDataURL();
link.style.left = (canvas.offsetLeft + canvas.offsetWidth - 32) + "px";
link.style.top = (canvas.offsetTop + 1) + "px";
if ("Velocity" in window) {
Velocity(link, "stop");
Velocity(
link,
{ opacity: 1 },
{ duration: 150 }
);
} else {
link.style.opacity = 1;
}
}
function onMouseOut(evt) {
window.setTimeout(function() {
var mPos = getMousePos(evt);
var cPos = getNodePos(canvas);
if ((mPos.x < cPos.x ) || (mPos.x > cPos.x + cPos.w) || (mPos.y < cPos.y ) || (mPos.y > cPos.y + cPos.h)) {
if ("Velocity" in window) {
Velocity(link, "stop");
Velocity(
link,
{ opacity: 0 },
{ duration: 250, delay: 100 }
);
} else {
link.style.opacity = 0;
}
}
}, 50);
}
function onLinkOver() {
button.src = "button.download.1.svg";
}
function onLinkOut(evt) {
button.src = "button.download.0.svg";
onMouseOut(evt);
}
canvas.addEventListener("mouseover", onMouseOver);
canvas.addEventListener("mouseout", onMouseOut);
link.addEventListener("mouseover", onLinkOver);
link.addEventListener("mouseout", onLinkOut);
}