├── LICENSE ├── README.md └── jquery.graphite.js /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011, Preston Timmons 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## graphite.js 2 | 3 | Plugin to easily make graphs and update them on the fly using 4 | [Graphite's Render Url API](http://graphite.readthedocs.org/en/latest/render_api.html). 5 | 6 | ## How it works 7 | 8 | One. Adding a graph to a page: 9 | 10 | ```html 11 | 12 | ``` 13 | 14 | ```js 15 | $("#graph").graphite({ 16 | from: "-24hours", 17 | target: [ 18 | "server.web1.load", 19 | ], 20 | }); 21 | ``` 22 | 23 | Two. Setting custom options: 24 | 25 | ```html 26 | 27 | ``` 28 | 29 | ```js 30 | $("#graph").graphite({ 31 | from: "-24hours", 32 | colorList: "red,green", 33 | target: [ 34 | "alias(summarize(stats.site1.auth.login.error,'30min'),'Login Errors')", 35 | "alias(summarize(stats.site1.auth.login.user,'30min'),'Login Success')" 36 | ], 37 | title: "Login errors vs Success" 38 | }); 39 | ``` 40 | 41 | Three. Setting global defaults: 42 | 43 | ```js 44 | $.fn.graphite.defaults.width = "450" 45 | $.fn.graphite.defaults.height = "300" 46 | ``` 47 | 48 | Four. Updating existing graph: 49 | 50 | ```js 51 | $.fn.graphite.update($("#graph"), {from: "-3days", lineWidth: "2"}); 52 | ``` 53 | 54 | Five. Setting a custom api url--the default is "/render/": 55 | 56 | ```js 57 | $.fn.graphite.defaults.url = "http://myserver/render/" 58 | ``` 59 | 60 | or 61 | 62 | ```js 63 | $("#graph").graphite({ 64 | url: "http://myserver/render/" 65 | }); 66 | ``` 67 | 68 | ## $(img).graphite(options) 69 | 70 | You should probably specify a target. All other settings are optional. All 71 | settings will be passed through to the graphite api. 72 | -------------------------------------------------------------------------------- /jquery.graphite.js: -------------------------------------------------------------------------------- 1 | // graphite.js 2 | 3 | (function ($) { 4 | $.fn.graphite = function (options) { 5 | if (options === "update") { 6 | $.fn.graphite.update(this, arguments[1]); 7 | return this; 8 | } 9 | 10 | // Initialize plugin // 11 | options = options || {}; 12 | var settings = $.extend({}, $.fn.graphite.defaults, options); 13 | 14 | return this.each(function () { 15 | var $this = $(this); 16 | 17 | $this.data("graphOptions", settings); 18 | $.fn.graphite.render($this, settings); 19 | }); 20 | 21 | }; 22 | 23 | $.fn.graphite.geturl = function(rawOptions) { 24 | var src = rawOptions.url + "?"; 25 | 26 | // use random parameter to force image refresh 27 | var options = $.extend({}, rawOptions); 28 | 29 | options["_t"] = options["_t"] || Math.random(); 30 | 31 | $.each(options, function (key, value) { 32 | if (key === "target") { 33 | $.each(value, function (index, value) { 34 | src += "&target=" + value; 35 | }); 36 | } else if (value !== null && key !== "url") { 37 | src += "&" + key + "=" + value; 38 | } 39 | }); 40 | 41 | return src.replace(/\?&/, "?"); 42 | }; 43 | 44 | $.fn.graphite.render = function($img, options) { 45 | $img.attr("src", $.fn.graphite.geturl(options)); 46 | $img.attr("height", options.height); 47 | $img.attr("width", options.width); 48 | }; 49 | 50 | $.fn.graphite.update = function($img, options) { 51 | options = options || {}; 52 | $img.each(function () { 53 | var $this = $(this); 54 | var settings = $.extend({}, $this.data("graphOptions"), options); 55 | $this.data("graphOptions", settings); 56 | $.fn.graphite.render($this, settings); 57 | }); 58 | }; 59 | 60 | // Default settings. 61 | // Override with the options argument for per-case setup 62 | // or set $.fn.graphite.defaults. for global changes 63 | $.fn.graphite.defaults = { 64 | from: "-1hour", 65 | height: "300", 66 | until: "now", 67 | url: "/render/", 68 | width: "940" 69 | }; 70 | 71 | }(jQuery)); 72 | --------------------------------------------------------------------------------