graph.test.ts 30 KB

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