graph.test.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. jest.mock('app/features/annotations/all', () => ({
  2. EventManager: () => {
  3. return {
  4. on: () => {},
  5. addFlotEvents: () => {},
  6. };
  7. },
  8. }));
  9. jest.mock('app/core/core', () => ({
  10. coreModule: {
  11. directive: () => {},
  12. },
  13. appEvents: {
  14. on: () => {},
  15. },
  16. }));
  17. import '../module';
  18. import { GraphCtrl } from '../module';
  19. import { MetricsPanelCtrl } from 'app/features/panel/metrics_panel_ctrl';
  20. import { PanelCtrl } from 'app/features/panel/panel_ctrl';
  21. import config from 'app/core/config';
  22. import TimeSeries from 'app/core/time_series2';
  23. import $ from 'jquery';
  24. import { graphDirective } from '../graph';
  25. import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
  26. const ctx = {} as any;
  27. let ctrl;
  28. const scope = {
  29. ctrl: {},
  30. range: {
  31. from: dateTime([2015, 1, 1]),
  32. to: dateTime([2015, 11, 20]),
  33. },
  34. $on: () => {},
  35. };
  36. let link;
  37. describe('grafanaGraph', () => {
  38. const setupCtx = (beforeRender?) => {
  39. config.bootData = {
  40. user: {
  41. lightTheme: false,
  42. },
  43. };
  44. GraphCtrl.prototype = {
  45. ...MetricsPanelCtrl.prototype,
  46. ...PanelCtrl.prototype,
  47. ...GraphCtrl.prototype,
  48. height: 200,
  49. panel: {
  50. events: {
  51. on: () => {},
  52. },
  53. legend: {},
  54. grid: {},
  55. yaxes: [
  56. {
  57. min: null,
  58. max: null,
  59. format: 'short',
  60. logBase: 1,
  61. },
  62. {
  63. min: null,
  64. max: null,
  65. format: 'short',
  66. logBase: 1,
  67. },
  68. ],
  69. thresholds: [],
  70. xaxis: {},
  71. seriesOverrides: [],
  72. tooltip: {
  73. shared: true,
  74. },
  75. },
  76. renderingCompleted: jest.fn(),
  77. hiddenSeries: {},
  78. dashboard: {
  79. getTimezone: () => 'browser',
  80. },
  81. range: {
  82. from: dateTime([2015, 1, 1, 10]),
  83. to: dateTime([2015, 1, 1, 22]),
  84. },
  85. } as any;
  86. ctx.data = [];
  87. ctx.data.push(
  88. new TimeSeries({
  89. datapoints: [[1, 1], [2, 2]],
  90. alias: 'series1',
  91. })
  92. );
  93. ctx.data.push(
  94. new TimeSeries({
  95. datapoints: [[10, 1], [20, 2]],
  96. alias: 'series2',
  97. })
  98. );
  99. ctrl = new GraphCtrl(
  100. {
  101. $on: () => {},
  102. },
  103. {
  104. get: () => {},
  105. },
  106. {}
  107. );
  108. // @ts-ignore
  109. $.plot = ctrl.plot = jest.fn();
  110. scope.ctrl = ctrl;
  111. link = graphDirective({}, {}, {}).link(scope, { width: () => 500, mouseleave: () => {}, bind: () => {} });
  112. if (typeof beforeRender === 'function') {
  113. beforeRender();
  114. }
  115. link.data = ctx.data;
  116. //Emulate functions called by event listeners
  117. link.buildFlotPairs(link.data);
  118. link.renderPanel();
  119. ctx.plotData = ctrl.plot.mock.calls[0][1];
  120. ctx.plotOptions = ctrl.plot.mock.calls[0][2];
  121. };
  122. describe('simple lines options', () => {
  123. beforeEach(() => {
  124. setupCtx(() => {
  125. ctrl.panel.lines = true;
  126. ctrl.panel.fill = 5;
  127. ctrl.panel.linewidth = 3;
  128. ctrl.panel.steppedLine = true;
  129. });
  130. });
  131. it('should configure plot with correct options', () => {
  132. expect(ctx.plotOptions.series.lines.show).toBe(true);
  133. expect(ctx.plotOptions.series.lines.fill).toBe(0.5);
  134. expect(ctx.plotOptions.series.lines.lineWidth).toBe(3);
  135. expect(ctx.plotOptions.series.lines.steps).toBe(true);
  136. });
  137. });
  138. describe('sorting stacked series as legend. disabled', () => {
  139. beforeEach(() => {
  140. setupCtx(() => {
  141. ctrl.panel.legend.sort = undefined;
  142. ctrl.panel.stack = false;
  143. });
  144. });
  145. it('should not modify order of time series', () => {
  146. expect(ctx.plotData[0].alias).toBe('series1');
  147. expect(ctx.plotData[1].alias).toBe('series2');
  148. });
  149. });
  150. describe('sorting stacked series as legend. min descending order', () => {
  151. beforeEach(() => {
  152. setupCtx(() => {
  153. const sortKey = 'min';
  154. ctrl.panel.legend.sort = sortKey;
  155. ctrl.panel.legend.sortDesc = true;
  156. ctrl.panel.legend.alignAsTable = true;
  157. ctrl.panel.legend[sortKey] = true;
  158. ctrl.panel.stack = true;
  159. });
  160. });
  161. it('highest value should be first', () => {
  162. expect(ctx.plotData[0].alias).toBe('series2');
  163. expect(ctx.plotData[1].alias).toBe('series1');
  164. });
  165. });
  166. describe('sorting stacked series as legend. min ascending order', () => {
  167. beforeEach(() => {
  168. setupCtx(() => {
  169. ctrl.panel.legend.sort = 'min';
  170. ctrl.panel.legend.sortDesc = false;
  171. ctrl.panel.stack = true;
  172. });
  173. });
  174. it('lowest value should be first', () => {
  175. expect(ctx.plotData[0].alias).toBe('series1');
  176. expect(ctx.plotData[1].alias).toBe('series2');
  177. });
  178. });
  179. describe('sorting stacked series as legend. stacking disabled', () => {
  180. beforeEach(() => {
  181. setupCtx(() => {
  182. ctrl.panel.legend.sort = 'min';
  183. ctrl.panel.legend.sortDesc = true;
  184. ctrl.panel.stack = false;
  185. });
  186. });
  187. it('highest value should be first', () => {
  188. expect(ctx.plotData[0].alias).toBe('series1');
  189. expect(ctx.plotData[1].alias).toBe('series2');
  190. });
  191. });
  192. describe('sorting stacked series as legend. current descending order', () => {
  193. beforeEach(() => {
  194. setupCtx(() => {
  195. const sortKey = 'current';
  196. ctrl.panel.legend.sort = sortKey;
  197. ctrl.panel.legend.sortDesc = true;
  198. ctrl.panel.legend.alignAsTable = true;
  199. ctrl.panel.legend[sortKey] = true;
  200. ctrl.panel.stack = true;
  201. });
  202. });
  203. it('highest last value should be first', () => {
  204. expect(ctx.plotData[0].alias).toBe('series2');
  205. expect(ctx.plotData[1].alias).toBe('series1');
  206. });
  207. });
  208. describe('stacked series should not sort if legend is not as table or sort key column is not visible', () => {
  209. beforeEach(() => {
  210. setupCtx(() => {
  211. const sortKey = 'min';
  212. ctrl.panel.legend.sort = sortKey;
  213. ctrl.panel.legend.sortDesc = true;
  214. ctrl.panel.legend.alignAsTable = false;
  215. ctrl.panel.legend[sortKey] = false;
  216. ctrl.panel.stack = true;
  217. });
  218. });
  219. it('highest value should be first', () => {
  220. expect(ctx.plotData[0].alias).toBe('series1');
  221. expect(ctx.plotData[1].alias).toBe('series2');
  222. });
  223. });
  224. describe('when logBase is log 10', () => {
  225. beforeEach(() => {
  226. setupCtx(() => {
  227. ctx.data[0] = new TimeSeries({
  228. datapoints: [[2000, 1], [0.002, 2], [0, 3], [-1, 4]],
  229. alias: 'seriesAutoscale',
  230. });
  231. ctx.data[0].yaxis = 1;
  232. ctx.data[1] = new TimeSeries({
  233. datapoints: [[2000, 1], [0.002, 2], [0, 3], [-1, 4]],
  234. alias: 'seriesFixedscale',
  235. });
  236. ctx.data[1].yaxis = 2;
  237. ctrl.panel.yaxes[0].logBase = 10;
  238. ctrl.panel.yaxes[1].logBase = 10;
  239. ctrl.panel.yaxes[1].min = '0.05';
  240. ctrl.panel.yaxes[1].max = '1500';
  241. });
  242. });
  243. it('should apply axis transform, autoscaling (if necessary) and ticks', () => {
  244. const axisAutoscale = ctx.plotOptions.yaxes[0];
  245. expect(axisAutoscale.transform(100)).toBe(2);
  246. expect(axisAutoscale.inverseTransform(-3)).toBeCloseTo(0.001);
  247. expect(axisAutoscale.min).toBeCloseTo(0.001);
  248. expect(axisAutoscale.max).toBe(10000);
  249. expect(axisAutoscale.ticks.length).toBeCloseTo(8);
  250. expect(axisAutoscale.ticks[0]).toBeCloseTo(0.001);
  251. if (axisAutoscale.ticks.length === 7) {
  252. expect(axisAutoscale.ticks[axisAutoscale.ticks.length - 1]).toBeCloseTo(1000);
  253. } else {
  254. expect(axisAutoscale.ticks[axisAutoscale.ticks.length - 1]).toBe(10000);
  255. }
  256. const axisFixedscale = ctx.plotOptions.yaxes[1];
  257. expect(axisFixedscale.min).toBe(0.05);
  258. expect(axisFixedscale.max).toBe(1500);
  259. expect(axisFixedscale.ticks.length).toBe(5);
  260. expect(axisFixedscale.ticks[0]).toBe(0.1);
  261. expect(axisFixedscale.ticks[4]).toBe(1000);
  262. });
  263. });
  264. describe('when logBase is log 10 and data points contain only zeroes', () => {
  265. beforeEach(() => {
  266. setupCtx(() => {
  267. ctrl.panel.yaxes[0].logBase = 10;
  268. ctx.data[0] = new TimeSeries({
  269. datapoints: [[0, 1], [0, 2], [0, 3], [0, 4]],
  270. alias: 'seriesAutoscale',
  271. });
  272. ctx.data[0].yaxis = 1;
  273. });
  274. });
  275. it('should not set min and max and should create some fake ticks', () => {
  276. const axisAutoscale = ctx.plotOptions.yaxes[0];
  277. expect(axisAutoscale.transform(100)).toBe(2);
  278. expect(axisAutoscale.inverseTransform(-3)).toBeCloseTo(0.001);
  279. expect(axisAutoscale.min).toBe(undefined);
  280. expect(axisAutoscale.max).toBe(undefined);
  281. expect(axisAutoscale.ticks.length).toBe(2);
  282. expect(axisAutoscale.ticks[0]).toBe(1);
  283. expect(axisAutoscale.ticks[1]).toBe(2);
  284. });
  285. });
  286. // y-min set 0 is a special case for log scale,
  287. // this approximates it by setting min to 0.1
  288. describe('when logBase is log 10 and y-min is set to 0 and auto min is > 0.1', () => {
  289. beforeEach(() => {
  290. setupCtx(() => {
  291. ctrl.panel.yaxes[0].logBase = 10;
  292. ctrl.panel.yaxes[0].min = '0';
  293. ctx.data[0] = new TimeSeries({
  294. datapoints: [[2000, 1], [4, 2], [500, 3], [3000, 4]],
  295. alias: 'seriesAutoscale',
  296. });
  297. ctx.data[0].yaxis = 1;
  298. });
  299. });
  300. it('should set min to 0.1 and add a tick for 0.1', () => {
  301. const axisAutoscale = ctx.plotOptions.yaxes[0];
  302. expect(axisAutoscale.transform(100)).toBe(2);
  303. expect(axisAutoscale.inverseTransform(-3)).toBeCloseTo(0.001);
  304. expect(axisAutoscale.min).toBe(0.1);
  305. expect(axisAutoscale.max).toBe(10000);
  306. expect(axisAutoscale.ticks.length).toBe(6);
  307. expect(axisAutoscale.ticks[0]).toBe(0.1);
  308. expect(axisAutoscale.ticks[5]).toBe(10000);
  309. });
  310. });
  311. describe('when logBase is log 2 and y-min is set to 0 and num of ticks exceeds max', () => {
  312. beforeEach(() => {
  313. setupCtx(() => {
  314. const heightForApprox5Ticks = 125;
  315. ctrl.height = heightForApprox5Ticks;
  316. ctrl.panel.yaxes[0].logBase = 2;
  317. ctrl.panel.yaxes[0].min = '0';
  318. ctx.data[0] = new TimeSeries({
  319. datapoints: [[2000, 1], [4, 2], [500, 3], [3000, 4], [10000, 5], [100000, 6]],
  320. alias: 'seriesAutoscale',
  321. });
  322. ctx.data[0].yaxis = 1;
  323. });
  324. });
  325. it('should regenerate ticks so that if fits on the y-axis', () => {
  326. const axisAutoscale = ctx.plotOptions.yaxes[0];
  327. expect(axisAutoscale.min).toBe(0.1);
  328. expect(axisAutoscale.ticks.length).toBe(8);
  329. expect(axisAutoscale.ticks[0]).toBe(0.1);
  330. expect(axisAutoscale.ticks[7]).toBe(262144);
  331. expect(axisAutoscale.max).toBe(262144);
  332. });
  333. it('should set axis max to be max tick value', () => {
  334. expect(ctx.plotOptions.yaxes[0].max).toBe(262144);
  335. });
  336. });
  337. describe('dashed lines options', () => {
  338. beforeEach(() => {
  339. setupCtx(() => {
  340. ctrl.panel.lines = true;
  341. ctrl.panel.linewidth = 2;
  342. ctrl.panel.dashes = true;
  343. });
  344. });
  345. it('should configure dashed plot with correct options', () => {
  346. expect(ctx.plotOptions.series.lines.show).toBe(true);
  347. expect(ctx.plotOptions.series.dashes.lineWidth).toBe(2);
  348. expect(ctx.plotOptions.series.dashes.show).toBe(true);
  349. });
  350. });
  351. describe('should use timeStep for barWidth', () => {
  352. beforeEach(() => {
  353. setupCtx(() => {
  354. ctrl.panel.bars = true;
  355. ctx.data[0] = new TimeSeries({
  356. datapoints: [[1, 10], [2, 20]],
  357. alias: 'series1',
  358. });
  359. });
  360. });
  361. it('should set barWidth', () => {
  362. expect(ctx.plotOptions.series.bars.barWidth).toBe(1 / 1.5);
  363. });
  364. });
  365. describe('series option overrides, fill & points', () => {
  366. beforeEach(() => {
  367. setupCtx(() => {
  368. ctrl.panel.lines = true;
  369. ctrl.panel.fill = 5;
  370. ctx.data[0].zindex = 10;
  371. ctx.data[1].alias = 'test';
  372. ctx.data[1].lines = { fill: 0.001 };
  373. ctx.data[1].points = { show: true };
  374. });
  375. });
  376. it('should match second series and fill zero, and enable points', () => {
  377. expect(ctx.plotOptions.series.lines.fill).toBe(0.5);
  378. expect(ctx.plotData[1].lines.fill).toBe(0.001);
  379. expect(ctx.plotData[1].points.show).toBe(true);
  380. });
  381. });
  382. describe('should order series order according to zindex', () => {
  383. beforeEach(() => {
  384. setupCtx(() => {
  385. ctx.data[1].zindex = 1;
  386. ctx.data[0].zindex = 10;
  387. });
  388. });
  389. it('should move zindex 2 last', () => {
  390. expect(ctx.plotData[0].alias).toBe('series2');
  391. expect(ctx.plotData[1].alias).toBe('series1');
  392. });
  393. });
  394. describe('when series is hidden', () => {
  395. beforeEach(() => {
  396. setupCtx(() => {
  397. ctrl.hiddenSeries = { series2: true };
  398. });
  399. });
  400. it('should remove datapoints and disable stack', () => {
  401. expect(ctx.plotData[0].alias).toBe('series1');
  402. expect(ctx.plotData[1].data.length).toBe(0);
  403. expect(ctx.plotData[1].stack).toBe(false);
  404. });
  405. });
  406. describe('when stack and percent', () => {
  407. beforeEach(() => {
  408. setupCtx(() => {
  409. ctrl.panel.percentage = true;
  410. ctrl.panel.stack = true;
  411. });
  412. });
  413. it('should show percentage', () => {
  414. const axis = ctx.plotOptions.yaxes[0];
  415. expect(axis.tickFormatter(100, axis)).toBe('100%');
  416. });
  417. });
  418. describe('when panel too narrow to show x-axis dates in same granularity as wide panels', () => {
  419. //Set width to 10px
  420. describe('and the range is less than 24 hours', () => {
  421. beforeEach(() => {
  422. setupCtx(() => {
  423. ctrl.range.from = dateTime([2015, 1, 1, 10]);
  424. ctrl.range.to = dateTime([2015, 1, 1, 22]);
  425. });
  426. });
  427. it('should format dates as hours minutes', () => {
  428. const axis = ctx.plotOptions.xaxis;
  429. expect(axis.timeformat).toBe('%H:%M');
  430. });
  431. });
  432. describe('and the range is less than one year', () => {
  433. beforeEach(() => {
  434. setupCtx(() => {
  435. ctrl.range.from = dateTime([2015, 1, 1]);
  436. ctrl.range.to = dateTime([2015, 11, 20]);
  437. });
  438. });
  439. it('should format dates as month days', () => {
  440. const axis = ctx.plotOptions.xaxis;
  441. expect(axis.timeformat).toBe('%m/%d');
  442. });
  443. });
  444. });
  445. describe('when graph is histogram, and enable stack', () => {
  446. beforeEach(() => {
  447. setupCtx(() => {
  448. ctrl.panel.xaxis.mode = 'histogram';
  449. ctrl.panel.stack = true;
  450. ctrl.hiddenSeries = {};
  451. ctx.data[0] = new TimeSeries({
  452. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  453. alias: 'series1',
  454. });
  455. ctx.data[1] = new TimeSeries({
  456. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  457. alias: 'series2',
  458. });
  459. });
  460. });
  461. it('should calculate correct histogram', () => {
  462. expect(ctx.plotData[0].data[0][0]).toBe(100);
  463. expect(ctx.plotData[0].data[0][1]).toBe(2);
  464. expect(ctx.plotData[1].data[0][0]).toBe(100);
  465. expect(ctx.plotData[1].data[0][1]).toBe(2);
  466. });
  467. });
  468. describe('when graph is histogram, and some series are hidden', () => {
  469. beforeEach(() => {
  470. setupCtx(() => {
  471. ctrl.panel.xaxis.mode = 'histogram';
  472. ctrl.panel.stack = false;
  473. ctrl.hiddenSeries = { series2: true };
  474. ctx.data[0] = new TimeSeries({
  475. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  476. alias: 'series1',
  477. });
  478. ctx.data[1] = new TimeSeries({
  479. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  480. alias: 'series2',
  481. });
  482. });
  483. });
  484. it('should calculate correct histogram', () => {
  485. expect(ctx.plotData[0].data[0][0]).toBe(100);
  486. expect(ctx.plotData[0].data[0][1]).toBe(2);
  487. });
  488. });
  489. describe('when graph is histogram, and xaxis min is set', () => {
  490. beforeEach(() => {
  491. setupCtx(() => {
  492. ctrl.panel.xaxis.mode = 'histogram';
  493. ctrl.panel.xaxis.min = 150;
  494. ctrl.panel.stack = false;
  495. ctrl.hiddenSeries = {};
  496. ctx.data[0] = new TimeSeries({
  497. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  498. alias: 'series1',
  499. });
  500. });
  501. });
  502. it('should not contain values lower than min', () => {
  503. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  504. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(200);
  505. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  506. });
  507. });
  508. describe('when graph is histogram, and xaxis min is zero', () => {
  509. beforeEach(() => {
  510. setupCtx(() => {
  511. ctrl.panel.xaxis.mode = 'histogram';
  512. ctrl.panel.xaxis.min = 0;
  513. ctrl.panel.stack = false;
  514. ctrl.hiddenSeries = {};
  515. ctx.data[0] = new TimeSeries({
  516. datapoints: [[-100, 1], [100, 2], [200, 3], [300, 4]],
  517. alias: 'series1',
  518. });
  519. });
  520. });
  521. it('should not contain values lower than zero', () => {
  522. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  523. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  524. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  525. });
  526. });
  527. describe('when graph is histogram, and xaxis min is null', () => {
  528. beforeEach(() => {
  529. setupCtx(() => {
  530. ctrl.panel.xaxis.mode = 'histogram';
  531. ctrl.panel.xaxis.min = null;
  532. ctrl.panel.stack = false;
  533. ctrl.hiddenSeries = {};
  534. ctx.data[0] = new TimeSeries({
  535. datapoints: [[-100, 1], [100, 2], [200, 3], [300, 4]],
  536. alias: 'series1',
  537. });
  538. });
  539. });
  540. it('xaxis min should not affect the histogram', () => {
  541. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  542. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(-100);
  543. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  544. });
  545. });
  546. describe('when graph is histogram, and xaxis min is undefined', () => {
  547. beforeEach(() => {
  548. setupCtx(() => {
  549. ctrl.panel.xaxis.mode = 'histogram';
  550. ctrl.panel.xaxis.min = undefined;
  551. ctrl.panel.stack = false;
  552. ctrl.hiddenSeries = {};
  553. ctx.data[0] = new TimeSeries({
  554. datapoints: [[-100, 1], [100, 2], [200, 3], [300, 4]],
  555. alias: 'series1',
  556. });
  557. });
  558. });
  559. it('xaxis min should not affect the histogram', () => {
  560. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  561. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(-100);
  562. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  563. });
  564. });
  565. describe('when graph is histogram, and xaxis max is set', () => {
  566. beforeEach(() => {
  567. setupCtx(() => {
  568. ctrl.panel.xaxis.mode = 'histogram';
  569. ctrl.panel.xaxis.max = 250;
  570. ctrl.panel.stack = false;
  571. ctrl.hiddenSeries = {};
  572. ctx.data[0] = new TimeSeries({
  573. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  574. alias: 'series1',
  575. });
  576. });
  577. });
  578. it('should not contain values greater than max', () => {
  579. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  580. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  581. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(200);
  582. });
  583. });
  584. describe('when graph is histogram, and xaxis max is zero', () => {
  585. beforeEach(() => {
  586. setupCtx(() => {
  587. ctrl.panel.xaxis.mode = 'histogram';
  588. ctrl.panel.xaxis.max = 0;
  589. ctrl.panel.stack = false;
  590. ctrl.hiddenSeries = {};
  591. ctx.data[0] = new TimeSeries({
  592. datapoints: [[-100, 1], [100, 1], [100, 2], [200, 3], [300, 4]],
  593. alias: 'series1',
  594. });
  595. });
  596. });
  597. it('should not contain values greater than zero', () => {
  598. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  599. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(-100);
  600. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(-100);
  601. });
  602. });
  603. describe('when graph is histogram, and xaxis max is null', () => {
  604. beforeEach(() => {
  605. setupCtx(() => {
  606. ctrl.panel.xaxis.mode = 'histogram';
  607. ctrl.panel.xaxis.max = null;
  608. ctrl.panel.stack = false;
  609. ctrl.hiddenSeries = {};
  610. ctx.data[0] = new TimeSeries({
  611. datapoints: [[-100, 1], [100, 1], [100, 2], [200, 3], [300, 4]],
  612. alias: 'series1',
  613. });
  614. });
  615. });
  616. it('xaxis max should not affect the histogram', () => {
  617. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  618. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(-100);
  619. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  620. });
  621. });
  622. describe('when graph is histogram, and xaxis max is undefined', () => {
  623. beforeEach(() => {
  624. setupCtx(() => {
  625. ctrl.panel.xaxis.mode = 'histogram';
  626. ctrl.panel.xaxis.max = undefined;
  627. ctrl.panel.stack = false;
  628. ctrl.hiddenSeries = {};
  629. ctx.data[0] = new TimeSeries({
  630. datapoints: [[-100, 1], [100, 1], [100, 2], [200, 3], [300, 4]],
  631. alias: 'series1',
  632. });
  633. });
  634. });
  635. it('xaxis max should not should node affect the histogram', () => {
  636. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  637. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(-100);
  638. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  639. });
  640. });
  641. describe('when graph is histogram, and xaxis min and max are set', () => {
  642. beforeEach(() => {
  643. setupCtx(() => {
  644. ctrl.panel.xaxis.mode = 'histogram';
  645. ctrl.panel.xaxis.min = 150;
  646. ctrl.panel.xaxis.max = 250;
  647. ctrl.panel.stack = false;
  648. ctrl.hiddenSeries = {};
  649. ctx.data[0] = new TimeSeries({
  650. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  651. alias: 'series1',
  652. });
  653. });
  654. });
  655. it('should not contain values lower than min and greater than max', () => {
  656. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  657. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(200);
  658. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(200);
  659. });
  660. });
  661. describe('when graph is histogram, and xaxis min and max are zero', () => {
  662. beforeEach(() => {
  663. setupCtx(() => {
  664. ctrl.panel.xaxis.mode = 'histogram';
  665. ctrl.panel.xaxis.min = 0;
  666. ctrl.panel.xaxis.max = 0;
  667. ctrl.panel.stack = false;
  668. ctrl.hiddenSeries = {};
  669. ctx.data[0] = new TimeSeries({
  670. datapoints: [[-100, 1], [100, 1], [100, 2], [200, 3], [300, 4]],
  671. alias: 'series1',
  672. });
  673. });
  674. });
  675. it('xaxis max should be ignored otherwise the bucketSize is zero', () => {
  676. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  677. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  678. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  679. });
  680. });
  681. describe('when graph is histogram, and xaxis min and max are null', () => {
  682. beforeEach(() => {
  683. setupCtx(() => {
  684. ctrl.panel.xaxis.mode = 'histogram';
  685. ctrl.panel.xaxis.min = null;
  686. ctrl.panel.xaxis.max = null;
  687. ctrl.panel.stack = false;
  688. ctrl.hiddenSeries = {};
  689. ctx.data[0] = new TimeSeries({
  690. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  691. alias: 'series1',
  692. });
  693. });
  694. });
  695. it('xaxis min and max should not affect the histogram', () => {
  696. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  697. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  698. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  699. });
  700. });
  701. describe('when graph is histogram, and xaxis min and max are undefined', () => {
  702. beforeEach(() => {
  703. setupCtx(() => {
  704. ctrl.panel.xaxis.mode = 'histogram';
  705. ctrl.panel.xaxis.min = undefined;
  706. ctrl.panel.xaxis.max = undefined;
  707. ctrl.panel.stack = false;
  708. ctrl.hiddenSeries = {};
  709. ctx.data[0] = new TimeSeries({
  710. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  711. alias: 'series1',
  712. });
  713. });
  714. });
  715. it('xaxis min and max should not affect the histogram', () => {
  716. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  717. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  718. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  719. });
  720. });
  721. describe('when graph is histogram, and xaxis min is greater than xaxis max', () => {
  722. beforeEach(() => {
  723. setupCtx(() => {
  724. ctrl.panel.xaxis.mode = 'histogram';
  725. ctrl.panel.xaxis.min = 150;
  726. ctrl.panel.xaxis.max = 100;
  727. ctrl.panel.stack = false;
  728. ctrl.hiddenSeries = {};
  729. ctx.data[0] = new TimeSeries({
  730. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  731. alias: 'series1',
  732. });
  733. });
  734. });
  735. it('xaxis max should be ignored otherwise the bucketSize is negative', () => {
  736. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  737. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(200);
  738. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  739. });
  740. });
  741. // aaa
  742. describe('when graph is histogram, and xaxis min is greater than the maximum value', () => {
  743. beforeEach(() => {
  744. setupCtx(() => {
  745. ctrl.panel.xaxis.mode = 'histogram';
  746. ctrl.panel.xaxis.min = 301;
  747. ctrl.panel.stack = false;
  748. ctrl.hiddenSeries = {};
  749. ctx.data[0] = new TimeSeries({
  750. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  751. alias: 'series1',
  752. });
  753. });
  754. });
  755. it('xaxis min should be ignored otherwise the bucketSize is negative', () => {
  756. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  757. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  758. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  759. });
  760. });
  761. describe('when graph is histogram, and xaxis min is equal to the maximum value', () => {
  762. beforeEach(() => {
  763. setupCtx(() => {
  764. ctrl.panel.xaxis.mode = 'histogram';
  765. ctrl.panel.xaxis.min = 300;
  766. ctrl.panel.stack = false;
  767. ctrl.hiddenSeries = {};
  768. ctx.data[0] = new TimeSeries({
  769. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  770. alias: 'series1',
  771. });
  772. });
  773. });
  774. it('xaxis min should be ignored otherwise the bucketSize is zero', () => {
  775. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  776. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  777. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  778. });
  779. });
  780. describe('when graph is histogram, and xaxis min is lower than the minimum value', () => {
  781. beforeEach(() => {
  782. setupCtx(() => {
  783. ctrl.panel.xaxis.mode = 'histogram';
  784. ctrl.panel.xaxis.min = 99;
  785. ctrl.panel.stack = false;
  786. ctrl.hiddenSeries = {};
  787. ctx.data[0] = new TimeSeries({
  788. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  789. alias: 'series1',
  790. });
  791. });
  792. });
  793. it('xaxis min should not affect the histogram', () => {
  794. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  795. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  796. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  797. });
  798. });
  799. describe('when graph is histogram, and xaxis max is equal to the minimum value', () => {
  800. beforeEach(() => {
  801. setupCtx(() => {
  802. ctrl.panel.xaxis.mode = 'histogram';
  803. ctrl.panel.xaxis.max = 100;
  804. ctrl.panel.stack = false;
  805. ctrl.hiddenSeries = {};
  806. ctx.data[0] = new TimeSeries({
  807. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  808. alias: 'series1',
  809. });
  810. });
  811. });
  812. it('should calculate correct histogram', () => {
  813. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  814. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  815. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  816. });
  817. });
  818. describe('when graph is histogram, and xaxis max is a lower than the minimum value', () => {
  819. beforeEach(() => {
  820. setupCtx(() => {
  821. ctrl.panel.xaxis.mode = 'histogram';
  822. ctrl.panel.xaxis.max = 99;
  823. ctrl.panel.stack = false;
  824. ctrl.hiddenSeries = {};
  825. ctx.data[0] = new TimeSeries({
  826. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  827. alias: 'series1',
  828. });
  829. });
  830. });
  831. it('should calculate empty histogram', () => {
  832. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  833. expect(nonZero.length).toBe(0);
  834. });
  835. });
  836. describe('when graph is histogram, and xaxis max is greater than the maximum value', () => {
  837. beforeEach(() => {
  838. setupCtx(() => {
  839. ctrl.panel.xaxis.mode = 'histogram';
  840. ctrl.panel.xaxis.max = 301;
  841. ctrl.panel.stack = false;
  842. ctrl.hiddenSeries = {};
  843. ctx.data[0] = new TimeSeries({
  844. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  845. alias: 'series1',
  846. });
  847. });
  848. });
  849. it('should calculate correct histogram', () => {
  850. const nonZero = ctx.plotData[0].data.filter(t => t[1] > 0);
  851. expect(Math.min.apply(Math, nonZero.map(t => t[0]))).toBe(100);
  852. expect(Math.max.apply(Math, nonZero.map(t => t[0]))).toBe(300);
  853. });
  854. });
  855. });