graph_specs.ts 13 KB

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