graph_specs.ts 7.6 KB

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