graph.test.ts 30 KB

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