screen.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. $(function() {
  2. // screen搜索
  3. $('#searchinput').on('click', function(e) {
  4. e.preventDefault();
  5. e.stopPropagation();
  6. // console.log($(e.target));
  7. });
  8. $('#searchinput').on('keyup', debouncer(function(e) {
  9. var $el = $(e.target);
  10. var val = $el.val();
  11. // console.log('1', val);
  12. var screenEls = $el.parent().parent().nextAll();
  13. _.each(screenEls, function(e) {
  14. var text = $(e).find('small').text();
  15. // console.log('2', text);
  16. var re = new RegExp(val, 'i');
  17. // if (text.indexOf(val) !== -1) {
  18. if (re.test(text)) {
  19. $(e).show();
  20. } else {
  21. $(e).hide();
  22. }
  23. });
  24. // console.log('0', '------');
  25. // console.log('1', val);
  26. }, 300));
  27. function debouncer(func, timeout) {
  28. var timeoutID;
  29. timeout = timeout || 300;
  30. return function () {
  31. var scope = this , args = arguments;
  32. clearTimeout( timeoutID );
  33. timeoutID = setTimeout( function () {
  34. func.apply( scope , Array.prototype.slice.call( args ) );
  35. }, timeout);
  36. };
  37. }
  38. // 画图
  39. active();
  40. window.active = active;
  41. window.getData = getData;
  42. function getData(area, url) {
  43. $.getJSON(url, function(ret) {
  44. var data = parseData(ret);
  45. var title = ret.title;
  46. var $el = $(area).find('.chart-area');
  47. var $t = $(area).find('.chart-title');
  48. var $l = $(area).find('.legend');
  49. var href = $.query.load(window.location.href);
  50. var showLegend = href.get('legend');
  51. //if (showLegend && showLegend === 'off') {
  52. // $l.hide();
  53. //}
  54. $.plot($el, data, getConfig({legend: $l}));
  55. $t.html(title);
  56. $el.on('plothover', function (event, pos, item) {
  57. // console.log('1111');
  58. if (item) {
  59. // console.log('2222');
  60. // var x = item.datapoint[0].toFixed(2),
  61. var x = moment(item.datapoint[0]).format('YYYY-MM-DD HH:mm:ss'),
  62. y = item.datapoint[1].toFixed(2);
  63. $('#tooltip').html(item.series.label + '<br>' + x + '<br>' + y)
  64. .css({top: item.pageY+5, left: item.pageX+5})
  65. .fadeIn(200);
  66. // console.log('3333');
  67. } else {
  68. $('#tooltip').hide();
  69. }
  70. });
  71. });
  72. }
  73. function active() {
  74. var $containers = $('.chart-container');
  75. var href = $.query.load(window.location.href);
  76. var showLegend = href.get('legend') || "off";
  77. window.cacheData = {}; // 数据缓存
  78. window.cachePlot = {}; // plot缓存
  79. _.each($containers, function(c, i) {
  80. var url = $(c).data('u');
  81. var $el = $(c).find('.chart-area');
  82. var $t = $(c).find('.chart-title');
  83. var $l = $(c).find('.legend');
  84. $.getJSON(url, function(ret) {
  85. var data = parseData(ret);
  86. var title = ret.title;
  87. window.cacheData[i] = $.extend(true, [], data);
  88. if (showLegend && showLegend === 'off') {
  89. $l.hide();
  90. }
  91. cachePlot[i] = {};
  92. var flot = $.plot($el, data, getConfig({legend: $l}));
  93. cachePlot[i].plot = flot;
  94. cachePlot[i].$el = $el;
  95. cachePlot[i].$l = $l;
  96. $t.html(title);
  97. $el.on('plothover', function (event, pos, item) {
  98. // console.log('1111');
  99. if (item) {
  100. // console.log('2222');
  101. // var x = item.datapoint[0].toFixed(2),
  102. var x = moment(item.datapoint[0]).format('YYYY-MM-DD HH:mm:ss'),
  103. y = formatSizeOrigin(item.datapoint[1], "si", 3)
  104. $('#tooltip').html(item.series.label + '<br>' + x + '<br>' + y)
  105. .css({top: item.pageY+5, left: item.pageX+5})
  106. .fadeIn(200);
  107. // console.log('3333');
  108. } else {
  109. $('#tooltip').hide();
  110. }
  111. });
  112. $el.bind("plotselected", function (event, ranges) {
  113. // $("#selection").text(ranges.xaxis.from.toFixed(1) + " to " + ranges.xaxis.to.toFixed(1));
  114. $.each(flot.getXAxes(), function(_, axis) {
  115. var opts = axis.options;
  116. opts.min = ranges.xaxis.from;
  117. opts.max = ranges.xaxis.to;
  118. });
  119. flot.setupGrid();
  120. flot.draw();
  121. flot.clearSelection();
  122. });
  123. });
  124. });
  125. }
  126. // reset zoom
  127. // TODO: 切换求和的时候reset zoom 有问题
  128. $('body').on('click', '.reset-zoom', function(e) {
  129. var $el = $(e.target);
  130. var index = $el.data('index');
  131. var data = $.extend(true, [], window.cacheData[index]);
  132. window.cachePlot[index].plot.shutdown();
  133. $.plot(cachePlot[index].$el, data, getConfig({legend: cachePlot[index].$l}));
  134. });
  135. // sort data
  136. function sortData(data) {
  137. _.each(data, function(d) {
  138. d.data = d.data.sort(function(a, b) {
  139. return a[0] - b[0];
  140. });
  141. });
  142. return data;
  143. }
  144. // summary series
  145. function summary(series) {
  146. var d = series.data;
  147. // 去掉值中的null
  148. d = _.filter(d, function(i) {
  149. return i[1] !== null;
  150. });
  151. // 这里的数据结构是 [timestamp, value]
  152. var last = d[d.length-1][1];
  153. var max = _.max(d, function(i) {return i[1]})[1];
  154. var min = _.min(d, function(i) {return i[1]})[1]
  155. var sum = sum = _.reduce(d, function(memo, i) {return memo + i[1]}, 0);
  156. var avg = (sum/d.length);
  157. var sorted = d.sort(function(a,b) {return a[1]-b[1]});
  158. var th95 = sorted[parseInt(d.length*0.95, 10)][1];
  159. var th99 = sorted[parseInt(d.length*0.99, 10)][1];
  160. return {
  161. last: last,
  162. max: max,
  163. min: min,
  164. sum: sum,
  165. avg: avg,
  166. th95: th95,
  167. th99: th99
  168. };
  169. }
  170. // parse return data
  171. function parseData(ret) {
  172. var data = [];
  173. var len = ret.series.length;
  174. for (var i = 0, l = len; i < l; i ++) {
  175. var v = ret.series[i];
  176. data.push({label: v.name, data: v.data, check: true});
  177. }
  178. return data;
  179. }
  180. // flot config
  181. function getConfig(options) {
  182. return {
  183. lines: {
  184. show: true
  185. },
  186. points: {
  187. show: true,
  188. radius: 0.2,
  189. lineWidth: 1,
  190. fill:true,
  191. fillColor: "rgba(215, 234, 252, 0.8)"
  192. },
  193. xaxis: {
  194. mode: 'time',
  195. timezone: 'browser',
  196. color: "#EAEAEA",
  197. font: {
  198. size: 10,
  199. weight: "bold",
  200. color: "#ACABAB",
  201. family: "sans-serif",
  202. variant: "small-caps"
  203. }
  204. //tickDecimals: 0,
  205. //tickSize: 1
  206. },
  207. series: {
  208. shadowSize: 0 // Drawing is faster without shadows
  209. },
  210. // 使用深色色彩可选值, 避免因为绘图线条颜色太浅看不清楚
  211. colors: [
  212. "#FF6A6A", "#00BFFF", "#A52A2A",
  213. "#CDCD00", "#008878", "#FF0000",
  214. "#00FF00", "#7B68EE", "#FF00FF",
  215. "#EEAEEE", "#00AEEE", "#AEEEEE",
  216. "#FFB90F", "#00B90F", "#00FFFF",
  217. "#DC143C", "#BFEFFF", "#AA7500",
  218. "#F0E68C", "#00E68C", "#AAE68C",
  219. "#EE9A49", "#009A49", "#AA9A49",
  220. "#FFA54F", "#00A54F", "#AAA54F",
  221. "#8B4789", "#004789", "#AA4789",
  222. "#00CDCD", "#00CDCD", "#AACDCD",
  223. "#EE8262", "#008262", "#AA8262",
  224. "#FF8C00", "#008C00", "#AA8C00",
  225. "#8B3626", "#003626", "#AA3626",
  226. "#00BFFF", "#00E8AA", "#AAE8AA",
  227. "#EE7621", "#007621", "#AA7621",
  228. ],
  229. yaxis: {
  230. // min: -1
  231. // max: 100
  232. color: "#EAEAEA",
  233. font: {
  234. size: 10,
  235. weight: "bold",
  236. color: "#ACABAB",
  237. family: "sans-serif",
  238. variant: "small-caps"
  239. }
  240. },
  241. selection: {
  242. mode: "x"
  243. },
  244. grid: {
  245. hoverable: true,
  246. clickable: true,
  247. borderWidth: 0.5,
  248. borderColor: "#EAEAEA"
  249. },
  250. legend: {
  251. show: true,
  252. // position: 'nw',
  253. container: options.legend,
  254. labelFormatter: function(label, series) {
  255. return label;
  256. // return label + '<input type='checkbox' value='' + label + ''>';
  257. // return null;
  258. }
  259. }
  260. }
  261. }
  262. });