graph_specs.ts 6.7 KB

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