graph_specs.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. import { describe, beforeEach, it, sinon, expect, angularMocks } from '../../../../../test/lib/common';
  2. import '../module';
  3. import angular from 'angular';
  4. import $ from 'jquery';
  5. import helpers from 'test/specs/helpers';
  6. import TimeSeries from 'app/core/time_series2';
  7. import moment from 'moment';
  8. import { Emitter } from 'app/core/core';
  9. describe('grafanaGraph', function() {
  10. beforeEach(angularMocks.module('grafana.core'));
  11. function graphScenario(desc, func, elementWidth = 500) {
  12. describe(desc, () => {
  13. var ctx: any = {};
  14. ctx.setup = setupFunc => {
  15. beforeEach(
  16. angularMocks.module($provide => {
  17. $provide.value('timeSrv', new helpers.TimeSrvStub());
  18. })
  19. );
  20. beforeEach(
  21. angularMocks.inject(($rootScope, $compile) => {
  22. var ctrl: any = {
  23. height: 200,
  24. panel: {
  25. events: new Emitter(),
  26. legend: {},
  27. grid: {},
  28. yaxes: [
  29. {
  30. min: null,
  31. max: null,
  32. format: 'short',
  33. logBase: 1,
  34. },
  35. {
  36. min: null,
  37. max: null,
  38. format: 'short',
  39. logBase: 1,
  40. },
  41. ],
  42. thresholds: [],
  43. xaxis: {},
  44. seriesOverrides: [],
  45. tooltip: {
  46. shared: true,
  47. },
  48. },
  49. renderingCompleted: sinon.spy(),
  50. hiddenSeries: {},
  51. dashboard: {
  52. getTimezone: sinon.stub().returns('browser'),
  53. },
  54. range: {
  55. from: moment([2015, 1, 1, 10]),
  56. to: moment([2015, 1, 1, 22]),
  57. },
  58. };
  59. var scope = $rootScope.$new();
  60. scope.ctrl = ctrl;
  61. scope.ctrl.events = ctrl.panel.events;
  62. $rootScope.onAppEvent = sinon.spy();
  63. ctx.data = [];
  64. ctx.data.push(
  65. new TimeSeries({
  66. datapoints: [[1, 1], [2, 2]],
  67. alias: 'series1',
  68. })
  69. );
  70. ctx.data.push(
  71. new TimeSeries({
  72. datapoints: [[10, 1], [20, 2]],
  73. alias: 'series2',
  74. })
  75. );
  76. setupFunc(ctrl, ctx.data);
  77. var element = angular.element("<div style='width:" + elementWidth + "px' grafana-graph><div>");
  78. $compile(element)(scope);
  79. scope.$digest();
  80. $.plot = ctx.plotSpy = sinon.spy();
  81. ctrl.events.emit('render', ctx.data);
  82. ctrl.events.emit('render-legend');
  83. ctrl.events.emit('legend-rendering-complete');
  84. ctx.plotData = ctx.plotSpy.getCall(0).args[1];
  85. ctx.plotOptions = ctx.plotSpy.getCall(0).args[2];
  86. })
  87. );
  88. };
  89. func(ctx);
  90. });
  91. }
  92. graphScenario('simple lines options', ctx => {
  93. ctx.setup(ctrl => {
  94. ctrl.panel.lines = true;
  95. ctrl.panel.fill = 5;
  96. ctrl.panel.linewidth = 3;
  97. ctrl.panel.steppedLine = true;
  98. });
  99. it('should configure plot with correct options', () => {
  100. expect(ctx.plotOptions.series.lines.show).to.be(true);
  101. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  102. expect(ctx.plotOptions.series.lines.lineWidth).to.be(3);
  103. expect(ctx.plotOptions.series.lines.steps).to.be(true);
  104. });
  105. });
  106. graphScenario('sorting stacked series as legend. disabled', ctx => {
  107. ctx.setup(ctrl => {
  108. ctrl.panel.legend.sort = undefined;
  109. ctrl.panel.stack = false;
  110. });
  111. it('should not modify order of time series', () => {
  112. expect(ctx.plotData[0].alias).to.be('series1');
  113. expect(ctx.plotData[1].alias).to.be('series2');
  114. });
  115. });
  116. graphScenario('sorting stacked series as legend. min descending order', ctx => {
  117. ctx.setup(ctrl => {
  118. ctrl.panel.legend.sort = 'min';
  119. ctrl.panel.legend.sortDesc = true;
  120. ctrl.panel.stack = true;
  121. });
  122. it('highest value should be first', () => {
  123. expect(ctx.plotData[0].alias).to.be('series2');
  124. expect(ctx.plotData[1].alias).to.be('series1');
  125. });
  126. });
  127. graphScenario('sorting stacked series as legend. min ascending order', ctx => {
  128. ctx.setup((ctrl, data) => {
  129. ctrl.panel.legend.sort = 'min';
  130. ctrl.panel.legend.sortDesc = false;
  131. ctrl.panel.stack = true;
  132. });
  133. it('lowest value should be first', () => {
  134. expect(ctx.plotData[0].alias).to.be('series1');
  135. expect(ctx.plotData[1].alias).to.be('series2');
  136. });
  137. });
  138. graphScenario('sorting stacked series as legend. stacking disabled', ctx => {
  139. ctx.setup(ctrl => {
  140. ctrl.panel.legend.sort = 'min';
  141. ctrl.panel.legend.sortDesc = true;
  142. ctrl.panel.stack = false;
  143. });
  144. it('highest value should be first', () => {
  145. expect(ctx.plotData[0].alias).to.be('series1');
  146. expect(ctx.plotData[1].alias).to.be('series2');
  147. });
  148. });
  149. graphScenario('sorting stacked series as legend. current descending order', ctx => {
  150. ctx.setup(ctrl => {
  151. ctrl.panel.legend.sort = 'current';
  152. ctrl.panel.legend.sortDesc = true;
  153. ctrl.panel.stack = true;
  154. });
  155. it('highest last value should be first', () => {
  156. expect(ctx.plotData[0].alias).to.be('series2');
  157. expect(ctx.plotData[1].alias).to.be('series1');
  158. });
  159. });
  160. graphScenario('when logBase is log 10', function(ctx) {
  161. ctx.setup(function(ctrl, data) {
  162. ctrl.panel.yaxes[0].logBase = 10;
  163. data[0] = new TimeSeries({
  164. datapoints: [[2000, 1], [0.002, 2], [0, 3], [-1, 4]],
  165. alias: 'seriesAutoscale',
  166. });
  167. data[0].yaxis = 1;
  168. ctrl.panel.yaxes[1].logBase = 10;
  169. ctrl.panel.yaxes[1].min = '0.05';
  170. ctrl.panel.yaxes[1].max = '1500';
  171. data[1] = new TimeSeries({
  172. datapoints: [[2000, 1], [0.002, 2], [0, 3], [-1, 4]],
  173. alias: 'seriesFixedscale',
  174. });
  175. data[1].yaxis = 2;
  176. });
  177. it('should apply axis transform, autoscaling (if necessary) and ticks', function() {
  178. var axisAutoscale = ctx.plotOptions.yaxes[0];
  179. expect(axisAutoscale.transform(100)).to.be(2);
  180. expect(axisAutoscale.inverseTransform(-3)).to.within(0.00099999999, 0.00100000001);
  181. expect(axisAutoscale.min).to.within(0.00099999999, 0.00100000001);
  182. expect(axisAutoscale.max).to.be(10000);
  183. expect(axisAutoscale.ticks.length).to.within(7, 8);
  184. expect(axisAutoscale.ticks[0]).to.within(0.00099999999, 0.00100000001);
  185. if (axisAutoscale.ticks.length === 7) {
  186. expect(axisAutoscale.ticks[axisAutoscale.ticks.length - 1]).to.within(999.9999, 1000.0001);
  187. } else {
  188. expect(axisAutoscale.ticks[axisAutoscale.ticks.length - 1]).to.be(10000);
  189. }
  190. var axisFixedscale = ctx.plotOptions.yaxes[1];
  191. expect(axisFixedscale.min).to.be(0.05);
  192. expect(axisFixedscale.max).to.be(1500);
  193. expect(axisFixedscale.ticks.length).to.be(5);
  194. expect(axisFixedscale.ticks[0]).to.be(0.1);
  195. expect(axisFixedscale.ticks[4]).to.be(1000);
  196. });
  197. });
  198. graphScenario('when logBase is log 10 and data points contain only zeroes', function(ctx) {
  199. ctx.setup(function(ctrl, data) {
  200. ctrl.panel.yaxes[0].logBase = 10;
  201. data[0] = new TimeSeries({
  202. datapoints: [[0, 1], [0, 2], [0, 3], [0, 4]],
  203. alias: 'seriesAutoscale',
  204. });
  205. data[0].yaxis = 1;
  206. });
  207. it('should not set min and max and should create some fake ticks', function() {
  208. var axisAutoscale = ctx.plotOptions.yaxes[0];
  209. expect(axisAutoscale.transform(100)).to.be(2);
  210. expect(axisAutoscale.inverseTransform(-3)).to.within(0.00099999999, 0.00100000001);
  211. expect(axisAutoscale.min).to.be(undefined);
  212. expect(axisAutoscale.max).to.be(undefined);
  213. expect(axisAutoscale.ticks.length).to.be(2);
  214. expect(axisAutoscale.ticks[0]).to.be(1);
  215. expect(axisAutoscale.ticks[1]).to.be(2);
  216. });
  217. });
  218. // y-min set 0 is a special case for log scale,
  219. // this approximates it by setting min to 0.1
  220. graphScenario('when logBase is log 10 and y-min is set to 0 and auto min is > 0.1', function(ctx) {
  221. ctx.setup(function(ctrl, data) {
  222. ctrl.panel.yaxes[0].logBase = 10;
  223. ctrl.panel.yaxes[0].min = '0';
  224. data[0] = new TimeSeries({
  225. datapoints: [[2000, 1], [4, 2], [500, 3], [3000, 4]],
  226. alias: 'seriesAutoscale',
  227. });
  228. data[0].yaxis = 1;
  229. });
  230. it('should set min to 0.1 and add a tick for 0.1', function() {
  231. var axisAutoscale = ctx.plotOptions.yaxes[0];
  232. expect(axisAutoscale.transform(100)).to.be(2);
  233. expect(axisAutoscale.inverseTransform(-3)).to.within(0.00099999999, 0.00100000001);
  234. expect(axisAutoscale.min).to.be(0.1);
  235. expect(axisAutoscale.max).to.be(10000);
  236. expect(axisAutoscale.ticks.length).to.be(6);
  237. expect(axisAutoscale.ticks[0]).to.be(0.1);
  238. expect(axisAutoscale.ticks[5]).to.be(10000);
  239. });
  240. });
  241. graphScenario('when logBase is log 2 and y-min is set to 0 and num of ticks exceeds max', function(ctx) {
  242. ctx.setup(function(ctrl, data) {
  243. const heightForApprox5Ticks = 125;
  244. ctrl.height = heightForApprox5Ticks;
  245. ctrl.panel.yaxes[0].logBase = 2;
  246. ctrl.panel.yaxes[0].min = '0';
  247. data[0] = new TimeSeries({
  248. datapoints: [[2000, 1], [4, 2], [500, 3], [3000, 4], [10000, 5], [100000, 6]],
  249. alias: 'seriesAutoscale',
  250. });
  251. data[0].yaxis = 1;
  252. });
  253. it('should regenerate ticks so that if fits on the y-axis', function() {
  254. var axisAutoscale = ctx.plotOptions.yaxes[0];
  255. expect(axisAutoscale.min).to.be(0.1);
  256. expect(axisAutoscale.ticks.length).to.be(8);
  257. expect(axisAutoscale.ticks[0]).to.be(0.1);
  258. expect(axisAutoscale.ticks[7]).to.be(262144);
  259. expect(axisAutoscale.max).to.be(262144);
  260. });
  261. it('should set axis max to be max tick value', function() {
  262. expect(ctx.plotOptions.yaxes[0].max).to.be(262144);
  263. });
  264. });
  265. graphScenario('dashed lines options', function(ctx) {
  266. ctx.setup(function(ctrl) {
  267. ctrl.panel.lines = true;
  268. ctrl.panel.linewidth = 2;
  269. ctrl.panel.dashes = true;
  270. });
  271. it('should configure dashed plot with correct options', function() {
  272. expect(ctx.plotOptions.series.lines.show).to.be(true);
  273. expect(ctx.plotOptions.series.dashes.lineWidth).to.be(2);
  274. expect(ctx.plotOptions.series.dashes.show).to.be(true);
  275. });
  276. });
  277. graphScenario('should use timeStep for barWidth', function(ctx) {
  278. ctx.setup(function(ctrl, data) {
  279. ctrl.panel.bars = true;
  280. data[0] = new TimeSeries({
  281. datapoints: [[1, 10], [2, 20]],
  282. alias: 'series1',
  283. });
  284. });
  285. it('should set barWidth', function() {
  286. expect(ctx.plotOptions.series.bars.barWidth).to.be(1 / 1.5);
  287. });
  288. });
  289. graphScenario('series option overrides, fill & points', function(ctx) {
  290. ctx.setup(function(ctrl, data) {
  291. ctrl.panel.lines = true;
  292. ctrl.panel.fill = 5;
  293. data[0].zindex = 10;
  294. data[1].alias = 'test';
  295. data[1].lines = { fill: 0.001 };
  296. data[1].points = { show: true };
  297. });
  298. it('should match second series and fill zero, and enable points', function() {
  299. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  300. expect(ctx.plotData[1].lines.fill).to.be(0.001);
  301. expect(ctx.plotData[1].points.show).to.be(true);
  302. });
  303. });
  304. graphScenario('should order series order according to zindex', function(ctx) {
  305. ctx.setup(function(ctrl, data) {
  306. data[1].zindex = 1;
  307. data[0].zindex = 10;
  308. });
  309. it('should move zindex 2 last', function() {
  310. expect(ctx.plotData[0].alias).to.be('series2');
  311. expect(ctx.plotData[1].alias).to.be('series1');
  312. });
  313. });
  314. graphScenario('when series is hidden', function(ctx) {
  315. ctx.setup(function(ctrl) {
  316. ctrl.hiddenSeries = { series2: true };
  317. });
  318. it('should remove datapoints and disable stack', function() {
  319. expect(ctx.plotData[0].alias).to.be('series1');
  320. expect(ctx.plotData[1].data.length).to.be(0);
  321. expect(ctx.plotData[1].stack).to.be(false);
  322. });
  323. });
  324. graphScenario('when stack and percent', function(ctx) {
  325. ctx.setup(function(ctrl) {
  326. ctrl.panel.percentage = true;
  327. ctrl.panel.stack = true;
  328. });
  329. it('should show percentage', function() {
  330. var axis = ctx.plotOptions.yaxes[0];
  331. expect(axis.tickFormatter(100, axis)).to.be('100%');
  332. });
  333. });
  334. graphScenario(
  335. 'when panel too narrow to show x-axis dates in same granularity as wide panels',
  336. function(ctx) {
  337. describe('and the range is less than 24 hours', function() {
  338. ctx.setup(function(ctrl) {
  339. ctrl.range.from = moment([2015, 1, 1, 10]);
  340. ctrl.range.to = moment([2015, 1, 1, 22]);
  341. });
  342. it('should format dates as hours minutes', function() {
  343. var axis = ctx.plotOptions.xaxis;
  344. expect(axis.timeformat).to.be('%H:%M');
  345. });
  346. });
  347. describe('and the range is less than one year', function() {
  348. ctx.setup(function(scope) {
  349. scope.range.from = moment([2015, 1, 1]);
  350. scope.range.to = moment([2015, 11, 20]);
  351. });
  352. it('should format dates as month days', function() {
  353. var axis = ctx.plotOptions.xaxis;
  354. expect(axis.timeformat).to.be('%m/%d');
  355. });
  356. });
  357. },
  358. 10
  359. );
  360. graphScenario('when graph is histogram, and enable stack', function(ctx) {
  361. ctx.setup(function(ctrl, data) {
  362. ctrl.panel.xaxis.mode = 'histogram';
  363. ctrl.panel.stack = true;
  364. ctrl.hiddenSeries = {};
  365. data[0] = new TimeSeries({
  366. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  367. alias: 'series1',
  368. });
  369. data[1] = new TimeSeries({
  370. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  371. alias: 'series2',
  372. });
  373. });
  374. it('should calculate correct histogram', function() {
  375. expect(ctx.plotData[0].data[0][0]).to.be(100);
  376. expect(ctx.plotData[0].data[0][1]).to.be(2);
  377. expect(ctx.plotData[1].data[0][0]).to.be(100);
  378. expect(ctx.plotData[1].data[0][1]).to.be(2);
  379. });
  380. });
  381. graphScenario('when graph is histogram, and some series are hidden', function(ctx) {
  382. ctx.setup(function(ctrl, data) {
  383. ctrl.panel.xaxis.mode = 'histogram';
  384. ctrl.panel.stack = false;
  385. ctrl.hiddenSeries = { series2: true };
  386. data[0] = new TimeSeries({
  387. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  388. alias: 'series1',
  389. });
  390. data[1] = new TimeSeries({
  391. datapoints: [[100, 1], [100, 2], [200, 3], [300, 4]],
  392. alias: 'series2',
  393. });
  394. });
  395. it('should calculate correct histogram', function() {
  396. expect(ctx.plotData[0].data[0][0]).to.be(100);
  397. expect(ctx.plotData[0].data[0][1]).to.be(2);
  398. });
  399. });
  400. });