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