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