graph_specs.ts 13 KB

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