forked from lsiden/export-jqplot-to-png
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-jqplot-to-png.js
78 lines (66 loc) · 2.4 KB
/
export-jqplot-to-png.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
$(function() {
$.fn.jqplotToImage =
function(x_offset, y_offset) {
if ($(this).width() == 0 || $(this).height() == 0) {
return null;
}
var newCanvas = document.createElement("canvas");
newCanvas.width = $(this).outerWidth() + Number(x_offset);
newCanvas.height = $(this).outerHeight() + Number(y_offset);
if (!newCanvas.getContext) return null;
var newContext = newCanvas.getContext("2d");
newContext.textAlign = 'left';
newContext.textBaseline = 'top';
function _jqpToImage(el, x_offset, y_offset) {
var tagname = el.tagName.toLowerCase();
var p = $(el).position();
var css = getComputedStyle(el);
var left = x_offset + p.left + parseInt(css.marginLeft) + parseInt(css.borderLeftWidth) + parseInt(css.paddingLeft);
var top = y_offset + p.top + parseInt(css.marginTop) + parseInt(css.borderTopWidth)+ parseInt(css.paddingTop);
if ((tagname == 'div' || tagname == 'span') && !$(el).hasClass('jqplot-highlighter-tooltip')) {
$(el).children().each(function() {
_jqpToImage(this, left, top);
});
var text = $(el).childText();
if (text) {
var metrics = newContext.measureText(text);
newContext.font = $(el).getComputedFontStyle();
newContext.fillText(text, left, top);
// For debugging.
//newContext.strokeRect(left, top, $(el).width(), $(el).height());
}
}
else if (tagname == 'canvas') {
newContext.drawImage(el, left, top);
}
}
$(this).children().each(function() {
_jqpToImage(this, x_offset, y_offset);
});
return newCanvas;
};
$.fn.css2 = jQuery.fn.css;
$.fn.css = function() {
if (arguments.length) return jQuery.fn.css2.apply(this, arguments);
return window.getComputedStyle(this[0]);
};
// Returns font style as abbreviation for "font" property.
$.fn.getComputedFontStyle = function() {
var css = this.css();
var attr = ['font-style', 'font-weight', 'font-size', 'font-family'];
var style = [];
for (var i=0 ; i < attr.length; ++i) {
var attr = String(css[attr[i]]);
if (attr && attr != 'normal') {
style.push(attr);
}
}
return style.join(' ');
}
$.fn.childText =
function() {
return $(this).contents().filter(function() {
return this.nodeType == 3; // Node.TEXT_NODE not defined in I7
}).text();
};
});