graph_specs.ts 18 KB

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