graph_specs.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. expect(axisAutoscale.tickDecimals).to.be(3);
  123. var axisFixedscale = ctx.plotOptions.yaxes[1];
  124. expect(axisFixedscale.min).to.be(0.05);
  125. expect(axisFixedscale.max).to.be(1500);
  126. expect(axisFixedscale.ticks.length).to.be(5);
  127. expect(axisFixedscale.ticks[0]).to.be(0.1);
  128. expect(axisFixedscale.ticks[4]).to.be(1000);
  129. expect(axisFixedscale.tickDecimals).to.be(1);
  130. });
  131. });
  132. graphScenario('when logBase is log 10 and data points contain only zeroes', function(ctx) {
  133. ctx.setup(function(ctrl, data) {
  134. ctrl.panel.yaxes[0].logBase = 10;
  135. data[0] = new TimeSeries({
  136. datapoints: [[0,1],[0,2],[0,3],[0,4]],
  137. alias: 'seriesAutoscale',
  138. });
  139. data[0].yaxis = 1;
  140. });
  141. it('should not set min and max and should create some fake ticks', function() {
  142. var axisAutoscale = ctx.plotOptions.yaxes[0];
  143. expect(axisAutoscale.transform(100)).to.be(2);
  144. expect(axisAutoscale.inverseTransform(-3)).to.be(0.001);
  145. expect(axisAutoscale.min).to.be(undefined);
  146. expect(axisAutoscale.max).to.be(undefined);
  147. expect(axisAutoscale.ticks.length).to.be(2);
  148. expect(axisAutoscale.ticks[0]).to.be(1);
  149. expect(axisAutoscale.ticks[1]).to.be(2);
  150. expect(axisAutoscale.tickDecimals).to.be(undefined);
  151. });
  152. });
  153. // y-min set 0 is a special case for log scale,
  154. // this approximates it by setting min to 0.1
  155. graphScenario('when logBase is log 10 and y-min is set to 0 and auto min is > 0.1', function(ctx) {
  156. ctx.setup(function(ctrl, data) {
  157. ctrl.panel.yaxes[0].logBase = 10;
  158. ctrl.panel.yaxes[0].min = '0';
  159. data[0] = new TimeSeries({
  160. datapoints: [[2000,1],[4 ,2],[500,3],[3000,4]],
  161. alias: 'seriesAutoscale',
  162. });
  163. data[0].yaxis = 1;
  164. });
  165. it('should set min to 0.1 and add a tick for 0.1 and tickDecimals to be 0', function() {
  166. var axisAutoscale = ctx.plotOptions.yaxes[0];
  167. expect(axisAutoscale.transform(100)).to.be(2);
  168. expect(axisAutoscale.inverseTransform(-3)).to.be(0.001);
  169. expect(axisAutoscale.min).to.be(0.1);
  170. expect(axisAutoscale.max).to.be(10000);
  171. expect(axisAutoscale.ticks.length).to.be(6);
  172. expect(axisAutoscale.ticks[0]).to.be(0.1);
  173. expect(axisAutoscale.ticks[5]).to.be(10000);
  174. expect(axisAutoscale.tickDecimals).to.be(0);
  175. });
  176. });
  177. graphScenario('when logBase is log 2 and y-min is set to 0 and num of ticks exceeds max', function(ctx) {
  178. ctx.setup(function(ctrl, data) {
  179. const heightForApprox5Ticks = 125;
  180. ctrl.height = heightForApprox5Ticks;
  181. ctrl.panel.yaxes[0].logBase = 2;
  182. ctrl.panel.yaxes[0].min = '0';
  183. data[0] = new TimeSeries({
  184. datapoints: [[2000,1],[4 ,2],[500,3],[3000,4], [10000,5], [100000,6]],
  185. alias: 'seriesAutoscale',
  186. });
  187. data[0].yaxis = 1;
  188. });
  189. it('should regenerate ticks so that if fits on the y-axis', function() {
  190. var axisAutoscale = ctx.plotOptions.yaxes[0];
  191. expect(axisAutoscale.min).to.be(0.1);
  192. expect(axisAutoscale.ticks.length).to.be(8);
  193. expect(axisAutoscale.ticks[0]).to.be(0.1);
  194. expect(axisAutoscale.ticks[7]).to.be(262144);
  195. expect(axisAutoscale.max).to.be(262144);
  196. expect(axisAutoscale.tickDecimals).to.be(0);
  197. });
  198. it('should set axis max to be max tick value', function() {
  199. expect(ctx.plotOptions.yaxes[0].max).to.be(262144);
  200. });
  201. });
  202. graphScenario('dashed lines options', function(ctx) {
  203. ctx.setup(function(ctrl) {
  204. ctrl.panel.lines = true;
  205. ctrl.panel.linewidth = 2;
  206. ctrl.panel.dashes = true;
  207. });
  208. it('should configure dashed plot with correct options', function() {
  209. expect(ctx.plotOptions.series.lines.show).to.be(true);
  210. expect(ctx.plotOptions.series.dashes.lineWidth).to.be(2);
  211. expect(ctx.plotOptions.series.dashes.show).to.be(true);
  212. });
  213. });
  214. graphScenario('should use timeStep for barWidth', function(ctx) {
  215. ctx.setup(function(ctrl, data) {
  216. ctrl.panel.bars = true;
  217. data[0] = new TimeSeries({
  218. datapoints: [[1,10],[2,20]],
  219. alias: 'series1',
  220. });
  221. });
  222. it('should set barWidth', function() {
  223. expect(ctx.plotOptions.series.bars.barWidth).to.be(1/1.5);
  224. });
  225. });
  226. graphScenario('series option overrides, fill & points', function(ctx) {
  227. ctx.setup(function(ctrl, data) {
  228. ctrl.panel.lines = true;
  229. ctrl.panel.fill = 5;
  230. data[0].zindex = 10;
  231. data[1].alias = 'test';
  232. data[1].lines = {fill: 0.001};
  233. data[1].points = {show: true};
  234. });
  235. it('should match second series and fill zero, and enable points', function() {
  236. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  237. expect(ctx.plotData[1].lines.fill).to.be(0.001);
  238. expect(ctx.plotData[1].points.show).to.be(true);
  239. });
  240. });
  241. graphScenario('should order series order according to zindex', function(ctx) {
  242. ctx.setup(function(ctrl, data) {
  243. data[1].zindex = 1;
  244. data[0].zindex = 10;
  245. });
  246. it('should move zindex 2 last', function() {
  247. expect(ctx.plotData[0].alias).to.be('series2');
  248. expect(ctx.plotData[1].alias).to.be('series1');
  249. });
  250. });
  251. graphScenario('when series is hidden', function(ctx) {
  252. ctx.setup(function(ctrl) {
  253. ctrl.hiddenSeries = {'series2': true};
  254. });
  255. it('should remove datapoints and disable stack', function() {
  256. expect(ctx.plotData[0].alias).to.be('series1');
  257. expect(ctx.plotData[1].data.length).to.be(0);
  258. expect(ctx.plotData[1].stack).to.be(false);
  259. });
  260. });
  261. graphScenario('when stack and percent', function(ctx) {
  262. ctx.setup(function(ctrl) {
  263. ctrl.panel.percentage = true;
  264. ctrl.panel.stack = true;
  265. });
  266. it('should show percentage', function() {
  267. var axis = ctx.plotOptions.yaxes[0];
  268. expect(axis.tickFormatter(100, axis)).to.be("100%");
  269. });
  270. });
  271. graphScenario('when panel too narrow to show x-axis dates in same granularity as wide panels', function(ctx) {
  272. describe('and the range is less than 24 hours', function() {
  273. ctx.setup(function(ctrl) {
  274. ctrl.range.from = moment([2015, 1, 1, 10]);
  275. ctrl.range.to = moment([2015, 1, 1, 22]);
  276. });
  277. it('should format dates as hours minutes', function() {
  278. var axis = ctx.plotOptions.xaxis;
  279. expect(axis.timeformat).to.be('%H:%M');
  280. });
  281. });
  282. describe('and the range is less than one year', function() {
  283. ctx.setup(function(scope) {
  284. scope.range.from = moment([2015, 1, 1]);
  285. scope.range.to = moment([2015, 11, 20]);
  286. });
  287. it('should format dates as month days', function() {
  288. var axis = ctx.plotOptions.xaxis;
  289. expect(axis.timeformat).to.be('%m/%d');
  290. });
  291. });
  292. }, 10);
  293. // graphScenario('when using flexible Y-Min and Y-Max settings', function(ctx) {
  294. // describe('and Y-Min is <100 and Y-Max is >200 and values within range', function() {
  295. // ctx.setup(function(ctrl, data) {
  296. // ctrl.panel.yaxes[0].min = '<100';
  297. // ctrl.panel.yaxes[0].max = '>200';
  298. // data[0] = new TimeSeries({
  299. // datapoints: [[120,10],[160,20]],
  300. // alias: 'series1',
  301. // });
  302. // });
  303. //
  304. // it('should set min to 100 and max to 200', function() {
  305. // expect(ctx.plotOptions.yaxes[0].min).to.be(100);
  306. // expect(ctx.plotOptions.yaxes[0].max).to.be(200);
  307. // });
  308. // });
  309. // describe('and Y-Min is <100 and Y-Max is >200 and values outside range', function() {
  310. // ctx.setup(function(ctrl, data) {
  311. // ctrl.panel.yaxes[0].min = '<100';
  312. // ctrl.panel.yaxes[0].max = '>200';
  313. // data[0] = new TimeSeries({
  314. // datapoints: [[99,10],[201,20]],
  315. // alias: 'series1',
  316. // });
  317. // });
  318. //
  319. // it('should set min to auto and max to auto', function() {
  320. // expect(ctx.plotOptions.yaxes[0].min).to.be(null);
  321. // expect(ctx.plotOptions.yaxes[0].max).to.be(null);
  322. // });
  323. // });
  324. // describe('and Y-Min is =10.5 and Y-Max is =10.5', function() {
  325. // ctx.setup(function(ctrl, data) {
  326. // ctrl.panel.yaxes[0].min = '=10.5';
  327. // ctrl.panel.yaxes[0].max = '=10.5';
  328. // data[0] = new TimeSeries({
  329. // datapoints: [[100,10],[120,20], [110,30]],
  330. // alias: 'series1',
  331. // });
  332. // });
  333. //
  334. // it('should set min to last value + 10.5 and max to last value + 10.5', function() {
  335. // expect(ctx.plotOptions.yaxes[0].min).to.be(99.5);
  336. // expect(ctx.plotOptions.yaxes[0].max).to.be(120.5);
  337. // });
  338. // });
  339. // describe('and Y-Min is ~10.5 and Y-Max is ~10.5', function() {
  340. // ctx.setup(function(ctrl, data) {
  341. // ctrl.panel.yaxes[0].min = '~10.5';
  342. // ctrl.panel.yaxes[0].max = '~10.5';
  343. // data[0] = new TimeSeries({
  344. // datapoints: [[102,10],[104,20], [110,30]], //Also checks precision
  345. // alias: 'series1',
  346. // });
  347. // });
  348. //
  349. // it('should set min to average value + 10.5 and max to average value + 10.5', function() {
  350. // expect(ctx.plotOptions.yaxes[0].min).to.be(94.8);
  351. // expect(ctx.plotOptions.yaxes[0].max).to.be(115.8);
  352. // });
  353. // });
  354. // });
  355. // graphScenario('when using regular Y-Min and Y-Max settings', function(ctx) {
  356. // describe('and Y-Min is 100 and Y-Max is 200', function() {
  357. // ctx.setup(function(ctrl, data) {
  358. // ctrl.panel.yaxes[0].min = '100';
  359. // ctrl.panel.yaxes[0].max = '200';
  360. // data[0] = new TimeSeries({
  361. // datapoints: [[120,10],[160,20]],
  362. // alias: 'series1',
  363. // });
  364. // });
  365. //
  366. // it('should set min to 100 and max to 200', function() {
  367. // expect(ctx.plotOptions.yaxes[0].min).to.be(100);
  368. // expect(ctx.plotOptions.yaxes[0].max).to.be(200);
  369. // });
  370. // });
  371. // describe('and Y-Min is 0 and Y-Max is 0', function() {
  372. // ctx.setup(function(ctrl, data) {
  373. // ctrl.panel.yaxes[0].min = '0';
  374. // ctrl.panel.yaxes[0].max = '0';
  375. // data[0] = new TimeSeries({
  376. // datapoints: [[120,10],[160,20]],
  377. // alias: 'series1',
  378. // });
  379. // });
  380. //
  381. // it('should set min to 0 and max to 0', function() {
  382. // expect(ctx.plotOptions.yaxes[0].min).to.be(0);
  383. // expect(ctx.plotOptions.yaxes[0].max).to.be(0);
  384. // });
  385. // });
  386. // describe('and negative values used', function() {
  387. // ctx.setup(function(ctrl, data) {
  388. // ctrl.panel.yaxes[0].min = '-10';
  389. // ctrl.panel.yaxes[0].max = '-13.14';
  390. // data[0] = new TimeSeries({
  391. // datapoints: [[120,10],[160,20]],
  392. // alias: 'series1',
  393. // });
  394. // });
  395. //
  396. // it('should set min and max to negative', function() {
  397. // expect(ctx.plotOptions.yaxes[0].min).to.be(-10);
  398. // expect(ctx.plotOptions.yaxes[0].max).to.be(-13.14);
  399. // });
  400. // });
  401. // });
  402. // graphScenario('when using Y-Min and Y-Max settings stored as number', function(ctx) {
  403. // describe('and Y-Min is 0 and Y-Max is 100', function() {
  404. // ctx.setup(function(ctrl, data) {
  405. // ctrl.panel.yaxes[0].min = 0;
  406. // ctrl.panel.yaxes[0].max = 100;
  407. // data[0] = new TimeSeries({
  408. // datapoints: [[120,10],[160,20]],
  409. // alias: 'series1',
  410. // });
  411. // });
  412. //
  413. // it('should set min to 0 and max to 100', function() {
  414. // expect(ctx.plotOptions.yaxes[0].min).to.be(0);
  415. // expect(ctx.plotOptions.yaxes[0].max).to.be(100);
  416. // });
  417. // });
  418. // describe('and Y-Min is -100 and Y-Max is -10.5', function() {
  419. // ctx.setup(function(ctrl, data) {
  420. // ctrl.panel.yaxes[0].min = -100;
  421. // ctrl.panel.yaxes[0].max = -10.5;
  422. // data[0] = new TimeSeries({
  423. // datapoints: [[120,10],[160,20]],
  424. // alias: 'series1',
  425. // });
  426. // });
  427. //
  428. // it('should set min to -100 and max to -10.5', function() {
  429. // expect(ctx.plotOptions.yaxes[0].min).to.be(-100);
  430. // expect(ctx.plotOptions.yaxes[0].max).to.be(-10.5);
  431. // });
  432. // });
  433. // });
  434. });