graph_specs.ts 6.7 KB

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