graph_specs.ts 13 KB

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