From 0163faefef02207ad0ea3330af688103633293ff Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Fri, 10 May 2013 21:36:54 +0200 Subject: import js directory from jsrrdgraph for client side graph rendering Source: https://github.com/manuelluis/jsrrdgraph@276b880 --- js/RrdJson.js | 588 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 588 insertions(+) create mode 100644 js/RrdJson.js (limited to 'js/RrdJson.js') diff --git a/js/RrdJson.js b/js/RrdJson.js new file mode 100644 index 0000000..85b7f11 --- /dev/null +++ b/js/RrdJson.js @@ -0,0 +1,588 @@ +/** + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + + * + * Manuel Sanmartin + **/ + +"use strict"; + +/** + * RrdJson + * @constructor + */ +var RrdJson = function() { + if (arguments.length == 1) { + this.init1.apply(this, arguments); + } else if (arguments.length == 2) { + this.init2.apply(this, arguments); + } else if (arguments.length == 3) { + this.init3.apply(this, arguments); + } +}; + +RrdJson.prototype = { + graph: null, + json: null, + + init1: function (rrdgraph) + { + this.graph = rrdgraph + }, + init2: function (rrdgraph, jsonstr) + { + this.json = JSON.parse(jsonstr); + this.graph = rrdgraph + }, + init3: function (gfx, fetch, jsonstr) + { + this.json = JSON.parse(jsonstr); + this.graph = new RrdGraph(gfx, fetch); + }, + parse: function() + { + for (var option in this.json) { + switch(option) { + case 'alt_autoscale': + this.graph.alt_autoscale = this.json.alt_autoscale; + break; + case 'base': + this.graph.base = parseInt(this.json.base, 10); + if (this.graph.base !== 1000 && this.graph.base !== 1024) + throw 'the only sensible value for base apart from 1000 is 1024'; + break; + case 'color': + for (var color in this.json.color) { + if (color in this.graph.GRC) { + this.graph.GRC[color] = this.json.color[color]; + } else { + throw "invalid color name '"+name+"'"; + } + } + break; + case 'full_size_mode': + this.graph.full_size_mode = this.json.full_size_mode; + break; + case 'slope_mode': + this.graph.slopemode = this.json.slope_mode; + break; + case 'end': + this.graph.end_t = new RrdTime(this.json.end); + break; + case 'force_rules_legend': + this.graph.force_rules_legend = this.json.force_rules_legend; + break; + case 'no_legend': + this.graph.no_legend = this.json.no_legend; + break; + case 'height': + this.graph.ysize = this.json.height; + break; + case 'no_minor': + this.graph.no_minor = this.json.no_minor; + break; + case 'alt_autoscale_min': + this.graph.alt_autoscale_min = this.json.alt_autoscale_min; + break; + case 'only_graph': + this.graph.only_graph = this.json.only_graph; + break; + case 'units_length': + this.graph.unitslength = this.json.units_length; // FIXME + this.graph.forceleftspace = true; + break; + case 'lower_limit': + if (this.json.lower_limit === null) this.graph.setminval = Number.NaN; + else this.graph.setminval = this.json.lower_limit; + break; + case 'alt_autoscale_max': + this.graph.alt_autoscale_max = this.json.alt_autoscale_max; + break; + case 'zoom': + this.graph.zoom = this.json.zoom; + if (this.graph.zoom <= 0.0) + throw "zoom factor must be > 0"; + break; + case 'no_gridfit': + this.graph.gridfit = this.json.no_gridfit; + break; + case 'font': + for (var font in this.json.font) { + if (font in this.graph.TEXT) { + if (this.json.font[font].size !== undefined) + this.graph.TEXT[font].size = this.json.font[font].size; + if (this.json.font[font].font !== undefined) + this.graph.TEXT[font].font = this.json.font[font].font; + } else { + throw "invalid text property name"; + } + } + break; + case 'logarithmic': + this.graph.logarithmic = this.json.logarithmic; + break; + case 'rigid': + this.graph.rigid = this.json.rigid; + break; + case 'step': + this.graph.step = this.json.step; + break; + case 'start': + this.graph.start_t = new RrdTime(this.json.start); + break; + case 'tabwidth': + this.graph.tabwidth = this.json.tabwidth; + break; + case 'title': + this.graph.title = this.json.title; + break; + case 'upper_limit': + if (this.json.upper_limit === null) this.graph.setmaxval = Number.NaN; + else this.graph.setmaxval = this.json.upper_limit; + break; + case 'vertical_label': + this.graph.ylegend = this.json.vertical_label; + break; + case 'watermark': + this.graph.watermark = this.json.watermark; + break; + case 'width': + this.graph.xsize = this.json.width; + if (this.graph.xsize < 10) + throw "width below 10 pixels"; + break; + case 'units_exponent': + this.graph.unitsexponent = this.json.units_exponent; + break; + case 'x_grid': + break; + case 'alt_ygrid': + this.graph.alt_ygrid = this.json.alt_ygrid; + break; + case 'y_grid': + break; + case 'lazy': + this.graph.lazy = this.json.lazy; + break; + case 'units': + break; + case 'disable_rrdtool_tag': + this.graph.no_rrdtool_tag = this.json.disable_rrdtool_tag; + break; + case 'right_axis': + break; + case 'right_axis_label': + this.graph.second_axis_legend = this.json.right_axis_label; + break; + case 'right_axis_format': + this.graph.second_axis_format = this.json.right_axis_format; + break; + case 'legend_position': + if (this.json.legend_position === "north") { + this.graph.legendposition = this.graph.LEGEND_POS.NORTH; + } else if (this.json.legend_position === "west") { + this.graph.legendposition = this.graph.LEGEND_POS.WEST; + } else if (this.json.legend_position === "south") { + this.graph.legendposition = this.graph.LEGEND_POS.SOUTH; + } else if (this.json.legend_position === "east") { + this.graph.legendposition = this.graph.LEGEND_POS.EAST; + } else { + throw "unknown legend-position '"+value+"'"; + } + break; + case 'legend_direction': + if (this.json.legend_direction === "topdown") { + this.graph.legenddirection = this.graph.LEGEND_DIR.TOP_DOWN; + } else if (this.json.legend_direction === "bottomup") { + this.graph.legenddirection = this.graph.LEGEND_DIR.BOTTOM_UP; + } else { + throw "unknown legend-position '"+value+"'"; + } + break; + case 'border': + this.graph.draw_3d_border = this.json.border; + break; + case 'grid_dash': + if (this.json.grid_dash.length !== 2) + throw "expected grid-dash format float:float"; + this.graph.grid_dash_on = this.json.grid_dash[0]; + this.graph.grid_dash_off = this.json.grid_dash[1]; + break; + case 'dynamic_labels': + this.graph.dynamic_labels = this.json.dynamic_labels; + break; + case 'gdes': + this.parse_gdes(this.json.gdes); + break; + default: + throw 'Unknow option "'+option+'"'; + } + } + var start_end = RrdTime.proc_start_end(this.graph.start_t, this.graph.end_t); // FIXME here? + this.graph.start = start_end[0]; + this.graph.end = start_end[1]; + }, + parse_gdes: function (gdes) + { + for (var i = 0, gdes_c = gdes.length; i < gdes_c; i++) { + switch (gdes[i].type) { +// GPRINT:vname:format + case 'GPRINT': + this.graph.gdes_add_gprint(gdes[i].vname, gdes[i].cf, gdes[i].format, gdes[i].strftm); + break; +// LINE[width]:value[#color][:[legend][:STACK]][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]] + case 'LINE': + this.graph.gdes_add_line(gdes[i].width, gdes[i].value, gdes[i].color, gdes[i].legend, gdes[i].stack); + break; +// AREA:value[#color][:[legend][:STACK]] + case 'AREA': + this.graph.gdes_add_area(gdes[i].value, gdes[i].color, gdes[i].legend, gdes[i].stack); + break; +// TICK:vname#rrggbb[aa][:fraction[:legend]] + case 'TICK': + this.graph.gdes_add_tick(gdes[i].vname, gdes[i].color, gdes[i].fraction, gdes[i].legend); + break; +// HRULE:value#color[:legend][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]] + case 'HRULE': + this.graph.gdes_add_hrule(gdes[i].value, gdes[i].color, gdes[i].legend); + break; +// VRULE:time#color[:legend][:dashes[=on_s[,off_s[,on_s,off_s]...]][:dash-offset=offset]] + case 'VRULE': + this.graph.gdes_add_vrule(gdes[i].time, gdes[i].color, gdes[i].legend); + break; +// COMMENT:text + case 'COMMENT': + this.graph.gdes_add_comment(gdes[i].legend); + break; +// TEXTALIGN:{left|right|justified|center} + case 'TEXTALIGN': + switch (gdes[i].align) { + case 'left': + this.graph.gdes_add_textaling(RrdGraphDesc.TXA_LEFT); + break + case 'right': + this.graph.gdes_add_textaling(RrdGraphDesc.TXA_RIGHT); + break + case 'justified': + this.graph.gdes_add_textaling(RrdGraphDesc.TXA_JUSTIFIED); + break + case 'center': + this.graph.gdes_add_textaling(RrdGraphDesc.TXA_CENTER); + break + } + break; +// DEF:=::[:step=][:start=