graph_specs.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 '../../../../core/time_series2';
  8. import moment from 'moment';
  9. describe('grafanaGraph', function() {
  10. beforeEach(angularMocks.module('grafana.directives'));
  11. function graphScenario(desc, func, elementWidth = 500) {
  12. describe(desc, function() {
  13. var ctx: any = {};
  14. ctx.setup = function(setupFunc) {
  15. beforeEach(angularMocks.module(function($provide) {
  16. $provide.value("timeSrv", new helpers.TimeSrvStub());
  17. }));
  18. beforeEach(angularMocks.inject(function($rootScope, $compile) {
  19. var ctrl: any = {};
  20. var scope = $rootScope.$new();
  21. scope.ctrl = ctrl;
  22. var element = angular.element("<div style='width:" + elementWidth + "px' grafana-graph><div>");
  23. ctrl.height = '200px';
  24. ctrl.panel = {
  25. legend: {},
  26. grid: { },
  27. y_formats: [],
  28. seriesOverrides: [],
  29. tooltip: {
  30. shared: true
  31. }
  32. };
  33. $rootScope.onAppEvent = sinon.spy();
  34. ctrl.otherPanelInFullscreenMode = sinon.spy();
  35. ctrl.renderingCompleted = sinon.spy();
  36. ctrl.hiddenSeries = {};
  37. ctrl.dashboard = { timezone: 'browser' };
  38. ctrl.range = {
  39. from: moment([2015, 1, 1, 10]),
  40. to: moment([2015, 1, 1, 22]),
  41. };
  42. ctx.data = [];
  43. ctx.data.push(new TimeSeries({
  44. datapoints: [[1,1],[2,2]],
  45. alias: 'series1'
  46. }));
  47. ctx.data.push(new TimeSeries({
  48. datapoints: [[1,1],[2,2]],
  49. alias: 'series2'
  50. }));
  51. setupFunc(ctrl, ctx.data);
  52. $compile(element)(scope);
  53. scope.$digest();
  54. $.plot = ctx.plotSpy = sinon.spy();
  55. scope.$emit('render', ctx.data);
  56. ctx.plotData = ctx.plotSpy.getCall(0).args[1];
  57. ctx.plotOptions = ctx.plotSpy.getCall(0).args[2];
  58. }));
  59. };
  60. func(ctx);
  61. });
  62. }
  63. graphScenario('simple lines options', function(ctx) {
  64. ctx.setup(function(ctrl) {
  65. ctrl.panel.lines = true;
  66. ctrl.panel.fill = 5;
  67. ctrl.panel.linewidth = 3;
  68. ctrl.panel.steppedLine = true;
  69. });
  70. it('should configure plot with correct options', function() {
  71. expect(ctx.plotOptions.series.lines.show).to.be(true);
  72. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  73. expect(ctx.plotOptions.series.lines.lineWidth).to.be(3);
  74. expect(ctx.plotOptions.series.lines.steps).to.be(true);
  75. });
  76. });
  77. graphScenario('grid thresholds 100, 200', function(ctx) {
  78. ctx.setup(function(ctrl) {
  79. ctrl.panel.grid = {
  80. threshold1: 100,
  81. threshold1Color: "#111",
  82. threshold2: 200,
  83. threshold2Color: "#222",
  84. };
  85. });
  86. it('should add grid markings', function() {
  87. var markings = ctx.plotOptions.grid.markings;
  88. expect(markings[0].yaxis.from).to.be(100);
  89. expect(markings[0].yaxis.to).to.be(200);
  90. expect(markings[0].color).to.be('#111');
  91. expect(markings[1].yaxis.from).to.be(200);
  92. expect(markings[1].yaxis.to).to.be(Infinity);
  93. });
  94. });
  95. graphScenario('inverted grid thresholds 200, 100', function(ctx) {
  96. ctx.setup(function(ctrl) {
  97. ctrl.panel.grid = {
  98. threshold1: 200,
  99. threshold1Color: "#111",
  100. threshold2: 100,
  101. threshold2Color: "#222",
  102. };
  103. });
  104. it('should add grid markings', function() {
  105. var markings = ctx.plotOptions.grid.markings;
  106. expect(markings[0].yaxis.from).to.be(200);
  107. expect(markings[0].yaxis.to).to.be(100);
  108. expect(markings[0].color).to.be('#111');
  109. expect(markings[1].yaxis.from).to.be(100);
  110. expect(markings[1].yaxis.to).to.be(-Infinity);
  111. });
  112. });
  113. graphScenario('grid thresholds from zero', function(ctx) {
  114. ctx.setup(function(ctrl) {
  115. ctrl.panel.grid = {
  116. threshold1: 0,
  117. threshold1Color: "#111",
  118. };
  119. });
  120. it('should add grid markings', function() {
  121. var markings = ctx.plotOptions.grid.markings;
  122. expect(markings[0].yaxis.from).to.be(0);
  123. });
  124. });
  125. graphScenario('when logBase is log 10', function(ctx) {
  126. ctx.setup(function(ctrl) {
  127. ctrl.panel.grid = {
  128. leftMax: null,
  129. rightMax: null,
  130. leftMin: null,
  131. rightMin: null,
  132. leftLogBase: 10,
  133. };
  134. });
  135. it('should apply axis transform and ticks', function() {
  136. var axis = ctx.plotOptions.yaxes[0];
  137. expect(axis.transform(100)).to.be(Math.log(100+0.1));
  138. expect(axis.ticks[0]).to.be(0);
  139. expect(axis.ticks[1]).to.be(1);
  140. });
  141. });
  142. graphScenario('should use timeStep for barWidth', function(ctx) {
  143. ctx.setup(function(ctrl, data) {
  144. ctrl.panel.bars = true;
  145. data[0] = new TimeSeries({
  146. datapoints: [[1,10],[2,20]],
  147. alias: 'series1',
  148. });
  149. });
  150. it('should set barWidth', function() {
  151. expect(ctx.plotOptions.series.bars.barWidth).to.be(10/1.5);
  152. });
  153. });
  154. graphScenario('series option overrides, fill & points', function(ctx) {
  155. ctx.setup(function(ctrl, data) {
  156. ctrl.panel.lines = true;
  157. ctrl.panel.fill = 5;
  158. ctrl.panel.seriesOverrides = [
  159. { alias: 'test', fill: 0, points: true }
  160. ];
  161. data[1].alias = 'test';
  162. });
  163. it('should match second series and fill zero, and enable points', function() {
  164. expect(ctx.plotOptions.series.lines.fill).to.be(0.5);
  165. expect(ctx.plotData[1].lines.fill).to.be(0.001);
  166. expect(ctx.plotData[1].points.show).to.be(true);
  167. });
  168. });
  169. graphScenario('should order series order according to zindex', function(ctx) {
  170. ctx.setup(function(ctrl) {
  171. ctrl.panel.seriesOverrides = [{ alias: 'series1', zindex: 2 }];
  172. });
  173. it('should move zindex 2 last', function() {
  174. expect(ctx.plotData[0].alias).to.be('series2');
  175. expect(ctx.plotData[1].alias).to.be('series1');
  176. });
  177. });
  178. graphScenario('when series is hidden', function(ctx) {
  179. ctx.setup(function(ctrl) {
  180. ctrl.hiddenSeries = {'series2': true};
  181. });
  182. it('should remove datapoints and disable stack', function() {
  183. expect(ctx.plotData[0].alias).to.be('series1');
  184. expect(ctx.plotData[1].data.length).to.be(0);
  185. expect(ctx.plotData[1].stack).to.be(false);
  186. });
  187. });
  188. graphScenario('when stack and percent', function(ctx) {
  189. ctx.setup(function(ctrl) {
  190. ctrl.panel.percentage = true;
  191. ctrl.panel.stack = true;
  192. });
  193. it('should show percentage', function() {
  194. var axis = ctx.plotOptions.yaxes[0];
  195. expect(axis.tickFormatter(100, axis)).to.be("100%");
  196. });
  197. });
  198. graphScenario('when panel too narrow to show x-axis dates in same granularity as wide panels', function(ctx) {
  199. describe('and the range is less than 24 hours', function() {
  200. ctx.setup(function(ctrl) {
  201. ctrl.range.from = moment([2015, 1, 1, 10]);
  202. ctrl.range.to = moment([2015, 1, 1, 22]);
  203. });
  204. it('should format dates as hours minutes', function() {
  205. var axis = ctx.plotOptions.xaxis;
  206. expect(axis.timeformat).to.be('%H:%M');
  207. });
  208. });
  209. describe('and the range is less than one year', function() {
  210. ctx.setup(function(scope) {
  211. scope.range.from = moment([2015, 1, 1]);
  212. scope.range.to = moment([2015, 11, 20]);
  213. });
  214. it('should format dates as month days', function() {
  215. var axis = ctx.plotOptions.xaxis;
  216. expect(axis.timeformat).to.be('%m/%d');
  217. });
  218. });
  219. }, 10);
  220. });