graph-specs.js 6.5 KB

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