linear_time.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. /**
  19. * @class
  20. *
  21. * This is a view which GETs data from a URL and shows it as a time based line
  22. * graph. Time is shown on the X axis with data series shown on Y axis. It
  23. * optionally also has the ability to auto refresh itself over a given time
  24. * interval.
  25. *
  26. * This is an abstract class which is meant to be extended.
  27. *
  28. * Extending classes should override the following:
  29. * <ul>
  30. * <li>url - from where the data can be retrieved
  31. * <li>title - Title to be displayed when showing the chart
  32. * <li>id - which uniquely identifies this chart in any page
  33. * <li>#transformToSeries(jsonData) - function to map server data into graph
  34. * series
  35. * </ul>
  36. *
  37. * Extending classes could optionally override the following:
  38. * <ul>
  39. * <li>#colorForSeries(series) - function to get custom colors per series
  40. * </ul>
  41. *
  42. * @extends Ember.Object
  43. * @extends Ember.View
  44. */
  45. App.ChartLinearTimeView = Ember.View
  46. .extend({
  47. templateName: require('templates/main/charts/linear_time'),
  48. /**
  49. * The URL from which data can be retrieved.
  50. *
  51. * This property must be provided for the graph to show properly.
  52. *
  53. * @type String
  54. * @default null
  55. */
  56. url: null,
  57. /**
  58. * A unique ID for this chart.
  59. *
  60. * @type String
  61. * @default null
  62. */
  63. id: null,
  64. /**
  65. * Title to be shown under the chart.
  66. *
  67. * @type String
  68. * @default null
  69. */
  70. title: null,
  71. /**
  72. * @private
  73. *
  74. * @type Rickshaw.Graph
  75. * @default null
  76. */
  77. _graph: null,
  78. /**
  79. * Color palette used for this chart
  80. *
  81. * @private
  82. * @type String[]
  83. */
  84. _paletteScheme: [ 'rgba(181,182,169,0.4)', 'rgba(133,135,114,0.4)',
  85. 'rgba(120,95,67,0.4)', 'rgba(150,85,126,0.4)',
  86. 'rgba(70,130,180,0.4)', 'rgba(0,255,204,0.4)',
  87. 'rgba(255,105,180,0.4)', 'rgba(101,185,172,0.4)',
  88. 'rgba(115,192,58,0.4)', 'rgba(203,81,58,0.4)' ].reverse(),
  89. init: function () {
  90. this._super();
  91. },
  92. selector: function () {
  93. return '#' + this.get('elementId');
  94. }.property('elementId'),
  95. didInsertElement: function () {
  96. this._super();
  97. if (this.url != null) {
  98. var hash = {};
  99. hash.url = this.url;
  100. hash.type = 'GET';
  101. hash.dataType = 'json';
  102. hash.contentType = 'application/json; charset=utf-8';
  103. hash.context = this;
  104. hash.success = this._refreshGraph;
  105. jQuery.ajax(hash);
  106. }
  107. },
  108. /**
  109. * Transforms the JSON data retrieved from the server into the series
  110. * format that Rickshaw.Graph understands.
  111. *
  112. * The series object is generally in the following format: [ { name :
  113. * "Series 1", data : [ { x : 0, y : 0 }, { x : 1, y : 1 } ] } ]
  114. *
  115. * Extending classes should override this method.
  116. *
  117. * @param jsonData
  118. * Data retrieved from the server
  119. * @type: Function
  120. *
  121. */
  122. transformToSeries: function (jsonData) {
  123. return [ {
  124. name: "Series 1",
  125. data: [ {
  126. x: 0,
  127. y: 0
  128. }, {
  129. x: 1,
  130. y: 1
  131. } ]
  132. } ]
  133. },
  134. /**
  135. * Provides the formatter to use in displaying Y axis.
  136. *
  137. * The default is Rickshaw.Fixtures.Number.formatKMBT which shows 10K,
  138. * 300M etc.
  139. *
  140. * @type Function
  141. */
  142. yAxisFormatter: Rickshaw.Fixtures.Number.formatKMBT,
  143. /**
  144. * Provides the color (in any HTML color format) to use for a particular
  145. * series.
  146. *
  147. * @param series
  148. * Series for which color is being requested
  149. * @return color String. Returning null allows this chart to pick a color
  150. * from palette.
  151. * @default null
  152. * @type Function
  153. */
  154. colorForSeries: function (series) {
  155. return null;
  156. },
  157. /**
  158. * @private
  159. *
  160. * Refreshes the graph with the latest JSON data.
  161. *
  162. * @type Function
  163. */
  164. _refreshGraph: function (jsonData) {
  165. var seriesData = this.transformToSeries(jsonData);
  166. if (seriesData instanceof Array) {
  167. var palette = new Rickshaw.Color.Palette({
  168. scheme: this._paletteScheme
  169. });
  170. seriesData.forEach(function (series) {
  171. series.color = this.colorForSeries(series) || palette.color();
  172. series.stroke = 'rgba(0,0,0,0.3)';
  173. }.bind(this));
  174. }
  175. if (this._graph == null) {
  176. var chartId = "#" + this.id + "-chart";
  177. var chartOverlayId = "#" + this.id + "-overlay";
  178. var xaxisElementId = "#" + this.id + "-xaxis";
  179. var yaxisElementId = "#" + this.id + "-yaxis";
  180. var chartElement = document.querySelector(chartId);
  181. var overlayElement = document.querySelector(chartOverlayId);
  182. var xaxisElement = document.querySelector(xaxisElementId);
  183. var yaxisElement = document.querySelector(yaxisElementId);
  184. this._graph = new Rickshaw.Graph({
  185. height: 150,
  186. element: chartElement,
  187. series: seriesData,
  188. interpolation: 'step-after',
  189. stroke: true,
  190. renderer: 'area',
  191. strokeWidth: 1
  192. });
  193. this._graph.renderer.unstack = true;
  194. xAxis = new Rickshaw.Graph.Axis.Time({
  195. graph: this._graph
  196. });
  197. yAxis = new Rickshaw.Graph.Axis.Y({
  198. tickFormat: this.yAxisFormatter,
  199. element: yaxisElement,
  200. graph: this._graph
  201. });
  202. overlayElement.addEventListener('mousemove', function () {
  203. $(xaxisElement).removeClass('hide');
  204. $(yaxisElement).removeClass('hide');
  205. $(chartElement).children("div").removeClass('hide');
  206. });
  207. overlayElement.addEventListener('mouseout', function () {
  208. $(xaxisElement).addClass('hide');
  209. $(yaxisElement).addClass('hide');
  210. $(chartElement).children("div").addClass('hide');
  211. });
  212. // Hide axes
  213. this._graph.onUpdate(function () {
  214. $(xaxisElement).addClass('hide');
  215. $(yaxisElement).addClass('hide');
  216. $(chartElement).children('div').addClass('hide');
  217. });
  218. new Rickshaw.Graph.Legend({
  219. graph: this._graph,
  220. element: xaxisElement
  221. });
  222. // The below code will be needed if we ever use curve
  223. // smoothing in our graphs. (see rickshaw defect below)
  224. // this._graph.onUpdate(jQuery.proxy(function () {
  225. // this._adjustSVGHeight();
  226. // }, this));
  227. }
  228. this._graph.render();
  229. },
  230. /**
  231. * @private
  232. *
  233. * When a graph is given a particular width and height,the lines are drawn
  234. * in a slightly bigger area thereby chopping off some of the UI. Hence
  235. * after the rendering, we adjust the SVGs size in the DOM to compensate.
  236. *
  237. * Opened https://github.com/shutterstock/rickshaw/issues/141
  238. *
  239. * @type Function
  240. */
  241. _adjustSVGHeight: function () {
  242. if (this._graph && this._graph.element
  243. && this._graph.element.firstChild) {
  244. var svgElement = this._graph.element.firstChild;
  245. svgElement.setAttribute('height', $(this._graph.element).height()
  246. + "px");
  247. svgElement.setAttribute('width', $(this._graph.element).width()
  248. + "px");
  249. }
  250. }
  251. });
  252. /**
  253. * A formatter which will turn a number into computer storage sizes of the
  254. * format '23 GB' etc.
  255. *
  256. * @type Function
  257. */
  258. App.ChartLinearTimeView.BytesFormatter = function (y) {
  259. var value = Rickshaw.Fixtures.Number.formatBase1024KMGTP(y);
  260. if (!y || y.length < 1) {
  261. value = '';
  262. } else {
  263. if ("number" == typeof value) {
  264. value = String(value);
  265. }
  266. if ("string" == typeof value) {
  267. value = value.replace(/\.\d+/, ''); // Remove decimal part
  268. // Either it ends with digit or ends with character
  269. value = value.replace(/(\d$)/, '$1 '); // Ends with digit like '120'
  270. value = value.replace(/([a-zA-Z]$)/, ' $1'); // Ends with character like
  271. // '120M'
  272. value = value + 'B'; // Append B to make B, MB, GB etc.
  273. }
  274. }
  275. return value;
  276. };
  277. /**
  278. * A formatter which will turn a number into percentage display like '42%'
  279. *
  280. * @type Function
  281. */
  282. App.ChartLinearTimeView.PercentageFormatter = function (percentage) {
  283. var value = percentage;
  284. if (!value || value.length < 1) {
  285. value = '';
  286. } else {
  287. value = value + '%';
  288. }
  289. return value;
  290. };
  291. /**
  292. * A formatter which will turn elapsed time into display time like '50 ms',
  293. * '5s', '10 m', '3 hr' etc. Time is expected to be provided in milliseconds.
  294. *
  295. * @type Function
  296. */
  297. App.ChartLinearTimeView.TimeElapsedFormatter = function (millis) {
  298. var value = millis;
  299. if (!value || value.length < 1) {
  300. value = '';
  301. } else if ("number" == typeof millis) {
  302. var seconds = millis > 1000 ? Math.round(millis / 1000) : 0;
  303. var minutes = seconds > 60 ? Math.round(seconds / 60) : 0;
  304. var hours = minutes > 60 ? Math.round(minutes / 60) : 0;
  305. var days = hours > 24 ? Math.round(hours / 24) : 0;
  306. if (days > 0) {
  307. value = days + ' d';
  308. } else if (hours > 0) {
  309. value = hours + ' hr';
  310. } else if (minutes > 0) {
  311. value = minutes + ' m';
  312. } else if (seconds > 0) {
  313. value = seconds + ' s';
  314. } else if (millis > 0) {
  315. value = millis + ' ms';
  316. } else {
  317. value = millis + ' ms';
  318. }
  319. }
  320. return value;
  321. };