-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathjquery.graphite.js
71 lines (57 loc) · 2 KB
/
jquery.graphite.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
// graphite.js
(function ($) {
$.fn.graphite = function (options) {
if (options === "update") {
$.fn.graphite.update(this, arguments[1]);
return this;
}
// Initialize plugin //
options = options || {};
var settings = $.extend({}, $.fn.graphite.defaults, options);
return this.each(function () {
var $this = $(this);
$this.data("graphOptions", settings);
$.fn.graphite.render($this, settings);
});
};
$.fn.graphite.geturl = function(rawOptions) {
var src = rawOptions.url + "?";
// use random parameter to force image refresh
var options = $.extend({}, rawOptions);
options["_t"] = options["_t"] || Math.random();
$.each(options, function (key, value) {
if (key === "target") {
$.each(value, function (index, value) {
src += "&target=" + value;
});
} else if (value !== null && key !== "url") {
src += "&" + key + "=" + value;
}
});
return src.replace(/\?&/, "?");
};
$.fn.graphite.render = function($img, options) {
$img.attr("src", $.fn.graphite.geturl(options));
$img.attr("height", options.height);
$img.attr("width", options.width);
};
$.fn.graphite.update = function($img, options) {
options = options || {};
$img.each(function () {
var $this = $(this);
var settings = $.extend({}, $this.data("graphOptions"), options);
$this.data("graphOptions", settings);
$.fn.graphite.render($this, settings);
});
};
// Default settings.
// Override with the options argument for per-case setup
// or set $.fn.graphite.defaults.<value> for global changes
$.fn.graphite.defaults = {
from: "-1hour",
height: "300",
until: "now",
url: "/render/",
width: "940"
};
}(jQuery));