graph_specs.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. ///<reference path="../../../../headers/common.d.ts" />
  2. import {describe, beforeEach, it, sinon, expect, angularMocks} from '../../../../../test/lib/common';
  3. import '../module';
  4. import angular from 'angular';
  5. import $ from 'jquery';
  6. import helpers from 'test/specs/helpers';
  7. import TimeSeries from 'app/core/time_series2';
  8. import moment from 'moment';
  9. import {Emitter} from 'app/core/core';
  10. describe('grafanaGraph', function() {
  11. beforeEach(angularMocks.module('grafana.core'));
  12. function graphScenario(desc, func, elementWidth = 500) {
  13. describe(desc, function() {
  14. var ctx: any = {};
  15. ctx.setup = function(setupFunc) {
  16. beforeEach(angularMocks.module(function($provide) {
  17. $provide.value("timeSrv", new helpers.TimeSrvStub());
  18. }));
  19. beforeEach(angularMocks.inject(function($rootScope, $compile) {
  20. var ctrl: any = {
  21. events: new Emitter(),
  22. height: 200,
  23. panel: {
  24. legend: {},
  25. grid: { },
  26. yaxes: [
  27. {
  28. min: null,
  29. max: null,
  30. format: 'short',
  31. logBase: 1
  32. },
  33. {
  34. min: null,
  35. max: null,
  36. format: 'short',
  37. logBase: 1
  38. }
  39. ],
  40. thresholds: [],
  41. xaxis: {},
  42. seriesOverrides: [],
  43. tooltip: {
  44. shared: true
  45. }
  46. },
  47. renderingCompleted: sinon.spy(),
  48. hiddenSeries: {},
  49. dashboard: {
  50. getTimezone: sinon.stub().returns('browser')
  51. },
  52. range: {
  53. from: moment([2015, 1, 1, 10]),
  54. to: moment([2015, 1, 1, 22]),
  55. },
  56. };
  57. var scope = $rootScope.$new();
  58. scope.ctrl = ctrl;
  59. $rootScope.onAppEvent = sinon.spy();
  60. ctx.data = [];
  61. ctx.data.push(new TimeSeries({
  62. datapoints: [[1,1],[2,2]],
  63. alias: 'series1'
  64. }));
  65. ctx.data.push(new TimeSeries({
  66. datapoints: [[1,1],[2,2]],
  67. alias: 'series2'
  68. }));
  69. setupFunc(ctrl, ctx.data);
  70. var element = angular.element("<div style='width:" + elementWidth + "px' grafana-graph><div>");
  71. $compile(element)(scope);
  72. scope.$digest();
  73. $.plot = ctx.plotSpy = sinon.spy();
  74. ctrl.events.emit('render', ctx.data);
  75. ctx.plotData = ctx.plotSpy.getCall(0).args[1];
  76. ctx.plotOptions = ctx.plotSpy.getCall(0).args[2];
  77. }));
  78. };
  79. func(ctx);
  80. });
  81. }
  82. graphScenario('simple lines options', function(ctx) {
  83. ctx.setup(function(ctrl) {
  84. ctrl.panel.lines = true;
  85. ctrl.panel.fill = 5;
  86. ctrl.panel.linewidth = 3;
  87. ctrl.panel.steppedLine = true;
  88. });
  89. it('should configure plot with correct options', function() {
  90. expect(ctx.plotOptions.series.lines.show).to.be(true);
  91. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  92. expect(ctx.plotOptions.series.lines.lineWidth).to.be(3);
  93. expect(ctx.plotOptions.series.lines.steps).to.be(true);
  94. });
  95. });
  96. graphScenario('when logBase is log 10', function(ctx) {
  97. ctx.setup(function(ctrl, data) {
  98. ctrl.panel.yaxes[0].logBase = 10;
  99. data[0] = new TimeSeries({
  100. datapoints: [[2000,1],[0.002,2],[0,3],[-1,4]],
  101. alias: 'seriesAutoscale',
  102. });
  103. data[0].yaxis = 1;
  104. ctrl.panel.yaxes[1].logBase = 10;
  105. ctrl.panel.yaxes[1].min = 0.05;
  106. ctrl.panel.yaxes[1].max = 1500;
  107. data[1] = new TimeSeries({
  108. datapoints: [[2000,1],[0.002,2],[0,3],[-1,4]],
  109. alias: 'seriesFixedscale',
  110. });
  111. data[1].yaxis = 2;
  112. });
  113. it('should apply axis transform, autoscaling (if necessary) and ticks', function() {
  114. var axisAutoscale = ctx.plotOptions.yaxes[0];
  115. expect(axisAutoscale.transform(100)).to.be(2);
  116. expect(axisAutoscale.inverseTransform(-3)).to.be(0.001);
  117. expect(axisAutoscale.min).to.be(0.001);
  118. expect(axisAutoscale.max).to.be(10000);
  119. expect(axisAutoscale.ticks.length).to.be(8);
  120. expect(axisAutoscale.ticks[0]).to.be(0.001);
  121. expect(axisAutoscale.ticks[7]).to.be(10000);
  122. var axisFixedscale = ctx.plotOptions.yaxes[1];
  123. expect(axisFixedscale.min).to.be(0.05);
  124. expect(axisFixedscale.max).to.be(1500);
  125. expect(axisFixedscale.ticks.length).to.be(5);
  126. expect(axisFixedscale.ticks[0]).to.be(0.1);
  127. expect(axisFixedscale.ticks[4]).to.be(1000);
  128. });
  129. });
  130. graphScenario('should use timeStep for barWidth', function(ctx) {
  131. ctx.setup(function(ctrl, data) {
  132. ctrl.panel.bars = true;
  133. data[0] = new TimeSeries({
  134. datapoints: [[1,10],[2,20]],
  135. alias: 'series1',
  136. });
  137. });
  138. it('should set barWidth', function() {
  139. expect(ctx.plotOptions.series.bars.barWidth).to.be(1/1.5);
  140. });
  141. });
  142. graphScenario('series option overrides, fill & points', function(ctx) {
  143. ctx.setup(function(ctrl, data) {
  144. ctrl.panel.lines = true;
  145. ctrl.panel.fill = 5;
  146. data[0].zindex = 10;
  147. data[1].alias = 'test';
  148. data[1].lines = {fill: 0.001};
  149. data[1].points = {show: true};
  150. });
  151. it('should match second series and fill zero, and enable points', function() {
  152. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  153. expect(ctx.plotData[1].lines.fill).to.be(0.001);
  154. expect(ctx.plotData[1].points.show).to.be(true);
  155. });
  156. });
  157. graphScenario('should order series order according to zindex', function(ctx) {
  158. ctx.setup(function(ctrl, data) {
  159. data[1].zindex = 1;
  160. data[0].zindex = 10;
  161. });
  162. it('should move zindex 2 last', function() {
  163. expect(ctx.plotData[0].alias).to.be('series2');
  164. expect(ctx.plotData[1].alias).to.be('series1');
  165. });
  166. });
  167. graphScenario('when series is hidden', function(ctx) {
  168. ctx.setup(function(ctrl) {
  169. ctrl.hiddenSeries = {'series2': true};
  170. });
  171. it('should remove datapoints and disable stack', function() {
  172. expect(ctx.plotData[0].alias).to.be('series1');
  173. expect(ctx.plotData[1].data.length).to.be(0);
  174. expect(ctx.plotData[1].stack).to.be(false);
  175. });
  176. });
  177. graphScenario('when stack and percent', function(ctx) {
  178. ctx.setup(function(ctrl) {
  179. ctrl.panel.percentage = true;
  180. ctrl.panel.stack = true;
  181. });
  182. it('should show percentage', function() {
  183. var axis = ctx.plotOptions.yaxes[0];
  184. expect(axis.tickFormatter(100, axis)).to.be("100%");
  185. });
  186. });
  187. graphScenario('when panel too narrow to show x-axis dates in same granularity as wide panels', function(ctx) {
  188. describe('and the range is less than 24 hours', function() {
  189. ctx.setup(function(ctrl) {
  190. ctrl.range.from = moment([2015, 1, 1, 10]);
  191. ctrl.range.to = moment([2015, 1, 1, 22]);
  192. });
  193. it('should format dates as hours minutes', function() {
  194. var axis = ctx.plotOptions.xaxis;
  195. expect(axis.timeformat).to.be('%H:%M');
  196. });
  197. });
  198. describe('and the range is less than one year', function() {
  199. ctx.setup(function(scope) {
  200. scope.range.from = moment([2015, 1, 1]);
  201. scope.range.to = moment([2015, 11, 20]);
  202. });
  203. it('should format dates as month days', function() {
  204. var axis = ctx.plotOptions.xaxis;
  205. expect(axis.timeformat).to.be('%m/%d');
  206. });
  207. });
  208. }, 10);
  209. // graphScenario('when using flexible Y-Min and Y-Max settings', function(ctx) {
  210. // describe('and Y-Min is <100 and Y-Max is >200 and values within range', function() {
  211. // ctx.setup(function(ctrl, data) {
  212. // ctrl.panel.yaxes[0].min = '<100';
  213. // ctrl.panel.yaxes[0].max = '>200';
  214. // data[0] = new TimeSeries({
  215. // datapoints: [[120,10],[160,20]],
  216. // alias: 'series1',
  217. // });
  218. // });
  219. //
  220. // it('should set min to 100 and max to 200', function() {
  221. // expect(ctx.plotOptions.yaxes[0].min).to.be(100);
  222. // expect(ctx.plotOptions.yaxes[0].max).to.be(200);
  223. // });
  224. // });
  225. // describe('and Y-Min is <100 and Y-Max is >200 and values outside range', function() {
  226. // ctx.setup(function(ctrl, data) {
  227. // ctrl.panel.yaxes[0].min = '<100';
  228. // ctrl.panel.yaxes[0].max = '>200';
  229. // data[0] = new TimeSeries({
  230. // datapoints: [[99,10],[201,20]],
  231. // alias: 'series1',
  232. // });
  233. // });
  234. //
  235. // it('should set min to auto and max to auto', function() {
  236. // expect(ctx.plotOptions.yaxes[0].min).to.be(null);
  237. // expect(ctx.plotOptions.yaxes[0].max).to.be(null);
  238. // });
  239. // });
  240. // describe('and Y-Min is =10.5 and Y-Max is =10.5', function() {
  241. // ctx.setup(function(ctrl, data) {
  242. // ctrl.panel.yaxes[0].min = '=10.5';
  243. // ctrl.panel.yaxes[0].max = '=10.5';
  244. // data[0] = new TimeSeries({
  245. // datapoints: [[100,10],[120,20], [110,30]],
  246. // alias: 'series1',
  247. // });
  248. // });
  249. //
  250. // it('should set min to last value + 10.5 and max to last value + 10.5', function() {
  251. // expect(ctx.plotOptions.yaxes[0].min).to.be(99.5);
  252. // expect(ctx.plotOptions.yaxes[0].max).to.be(120.5);
  253. // });
  254. // });
  255. // describe('and Y-Min is ~10.5 and Y-Max is ~10.5', function() {
  256. // ctx.setup(function(ctrl, data) {
  257. // ctrl.panel.yaxes[0].min = '~10.5';
  258. // ctrl.panel.yaxes[0].max = '~10.5';
  259. // data[0] = new TimeSeries({
  260. // datapoints: [[102,10],[104,20], [110,30]], //Also checks precision
  261. // alias: 'series1',
  262. // });
  263. // });
  264. //
  265. // it('should set min to average value + 10.5 and max to average value + 10.5', function() {
  266. // expect(ctx.plotOptions.yaxes[0].min).to.be(94.8);
  267. // expect(ctx.plotOptions.yaxes[0].max).to.be(115.8);
  268. // });
  269. // });
  270. // });
  271. // graphScenario('when using regular Y-Min and Y-Max settings', function(ctx) {
  272. // describe('and Y-Min is 100 and Y-Max is 200', function() {
  273. // ctx.setup(function(ctrl, data) {
  274. // ctrl.panel.yaxes[0].min = '100';
  275. // ctrl.panel.yaxes[0].max = '200';
  276. // data[0] = new TimeSeries({
  277. // datapoints: [[120,10],[160,20]],
  278. // alias: 'series1',
  279. // });
  280. // });
  281. //
  282. // it('should set min to 100 and max to 200', function() {
  283. // expect(ctx.plotOptions.yaxes[0].min).to.be(100);
  284. // expect(ctx.plotOptions.yaxes[0].max).to.be(200);
  285. // });
  286. // });
  287. // describe('and Y-Min is 0 and Y-Max is 0', function() {
  288. // ctx.setup(function(ctrl, data) {
  289. // ctrl.panel.yaxes[0].min = '0';
  290. // ctrl.panel.yaxes[0].max = '0';
  291. // data[0] = new TimeSeries({
  292. // datapoints: [[120,10],[160,20]],
  293. // alias: 'series1',
  294. // });
  295. // });
  296. //
  297. // it('should set min to 0 and max to 0', function() {
  298. // expect(ctx.plotOptions.yaxes[0].min).to.be(0);
  299. // expect(ctx.plotOptions.yaxes[0].max).to.be(0);
  300. // });
  301. // });
  302. // describe('and negative values used', function() {
  303. // ctx.setup(function(ctrl, data) {
  304. // ctrl.panel.yaxes[0].min = '-10';
  305. // ctrl.panel.yaxes[0].max = '-13.14';
  306. // data[0] = new TimeSeries({
  307. // datapoints: [[120,10],[160,20]],
  308. // alias: 'series1',
  309. // });
  310. // });
  311. //
  312. // it('should set min and max to negative', function() {
  313. // expect(ctx.plotOptions.yaxes[0].min).to.be(-10);
  314. // expect(ctx.plotOptions.yaxes[0].max).to.be(-13.14);
  315. // });
  316. // });
  317. // });
  318. // graphScenario('when using Y-Min and Y-Max settings stored as number', function(ctx) {
  319. // describe('and Y-Min is 0 and Y-Max is 100', function() {
  320. // ctx.setup(function(ctrl, data) {
  321. // ctrl.panel.yaxes[0].min = 0;
  322. // ctrl.panel.yaxes[0].max = 100;
  323. // data[0] = new TimeSeries({
  324. // datapoints: [[120,10],[160,20]],
  325. // alias: 'series1',
  326. // });
  327. // });
  328. //
  329. // it('should set min to 0 and max to 100', function() {
  330. // expect(ctx.plotOptions.yaxes[0].min).to.be(0);
  331. // expect(ctx.plotOptions.yaxes[0].max).to.be(100);
  332. // });
  333. // });
  334. // describe('and Y-Min is -100 and Y-Max is -10.5', function() {
  335. // ctx.setup(function(ctrl, data) {
  336. // ctrl.panel.yaxes[0].min = -100;
  337. // ctrl.panel.yaxes[0].max = -10.5;
  338. // data[0] = new TimeSeries({
  339. // datapoints: [[120,10],[160,20]],
  340. // alias: 'series1',
  341. // });
  342. // });
  343. //
  344. // it('should set min to -100 and max to -10.5', function() {
  345. // expect(ctx.plotOptions.yaxes[0].min).to.be(-100);
  346. // expect(ctx.plotOptions.yaxes[0].max).to.be(-10.5);
  347. // });
  348. // });
  349. // });
  350. });