jquery.flot.errorbars.Test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* eslint-disable */
  2. /* global $, describe, it, xit, xdescribe, after, afterEach, expect*/
  3. describe("flot errorbars plugin", function() {
  4. var minx = 0, maxx = 200, miny = 0, maxy = 100;
  5. var series, ctx, plotWidth, plotHeight, plotOffset,
  6. getColorOrGradient;
  7. var drawFuncs = {
  8. drawArrow: function(ctx, x, y, radius){
  9. ctx.beginPath();
  10. ctx.moveTo(x + radius, y + radius);
  11. ctx.lineTo(x, y);
  12. ctx.lineTo(x - radius, y + radius);
  13. ctx.stroke();
  14. },
  15. drawSemiCircle: function(ctx, x, y, radius){
  16. ctx.beginPath();
  17. ctx.arc(x, y, radius, 0, Math.PI, false);
  18. ctx.moveTo(x - radius, y);
  19. ctx.lineTo(x + radius, y);
  20. ctx.stroke();
  21. }
  22. };
  23. function validatePointsAreInsideTheAxisRanges(points) {
  24. points.forEach(function(point) {
  25. var x = point[0], y = point[1];
  26. expect(minx <= x && x <= maxx).toBe(true);
  27. expect(miny <= y && y <= maxy).toBe(true);
  28. });
  29. }
  30. function executeHooks(plot, hooks, args) {
  31. args = [plot].concat(args);
  32. for (var i = 0; i < hooks.length; ++i) {
  33. hooks[i].apply(plot, args);
  34. }
  35. }
  36. beforeEach(function() {
  37. ctx = setFixtures('<div id="test-container" style="width: 200px;height: 100px;border-style: solid;border-width: 1px"><canvas id="theCanvas" style="width: 100%; height: 100%" /></div>')
  38. .find('#theCanvas')[0]
  39. .getContext('2d');
  40. plotWidth = 200;
  41. plotHeight = 100;
  42. plotOffset = { top: 0, left: 0 };
  43. getColorOrGradient = jasmine.createSpy().and.returnValue('rgb(10,200,10)');
  44. });
  45. it('should draw nothing when the values are null', function () {
  46. spyOn(ctx, 'moveTo').and.callThrough();
  47. spyOn(ctx, 'lineTo').and.callThrough();
  48. var plot = $.plot($("#test-container"), [[]], {
  49. series: {
  50. lines: {
  51. show: false
  52. }
  53. },
  54. xaxis: {
  55. autoScale: 'none',
  56. min: 0.6,
  57. max: 3.1
  58. },
  59. yaxis: {
  60. autoScale: 'none',
  61. min: 0,
  62. max: 3.5
  63. }
  64. });
  65. expect(ctx.moveTo).not.toHaveBeenCalled();
  66. expect(ctx.lineTo).not.toHaveBeenCalled();
  67. var points = ctx.moveTo.calls.allArgs().concat(
  68. ctx.lineTo.calls.allArgs());
  69. validatePointsAreInsideTheAxisRanges(points);
  70. });
  71. it('should draw lines for error bars on points', function () {
  72. spyOn(ctx, 'lineTo').and.callThrough();
  73. spyOn(ctx, 'moveTo').and.callThrough();
  74. var data1 = [
  75. [1,1,.5,.1,.3],
  76. [2,2,.3,.5,.2],
  77. [3,3,.9,.5,.2],
  78. [1.5,-.05,.5,.1,.3],
  79. [3.15,1.,.5,.1,.3],
  80. [2.5,-1.,.5,.1,.3]
  81. ];
  82. var data1_points = {
  83. show: true,
  84. radius: 5,
  85. fillColor: "blue",
  86. errorbars: "xy",
  87. xerr: {show: true, asymmetric: true, upperCap: "-", lowerCap: "-"},
  88. yerr: {show: true, color: "red", upperCap: "-"}
  89. };
  90. var data = [
  91. {color: "blue", points: data1_points, data: data1, label: "data1"}
  92. ];
  93. var plot = $.plot($("#test-container"), data, {
  94. series: {
  95. lines: {
  96. show: false
  97. }
  98. },
  99. xaxis: {
  100. autoScale: 'none',
  101. min: 0.6,
  102. max: 3.1
  103. },
  104. yaxis: {
  105. autoScale: 'none',
  106. min: 0,
  107. max: 3.5
  108. }
  109. });
  110. executeHooks(plot, plot.hooks.draw, [ctx]);
  111. expect(ctx.lineTo).toHaveBeenCalled();
  112. expect(ctx.moveTo).toHaveBeenCalled();
  113. var points = ctx.moveTo.calls.allArgs().concat(
  114. ctx.lineTo.calls.allArgs());
  115. validatePointsAreInsideTheAxisRanges(points);
  116. });
  117. it('should draw lines for error bars on lines', function () {
  118. spyOn(ctx, 'lineTo').and.callThrough();
  119. spyOn(ctx, 'moveTo').and.callThrough();
  120. var data3 = [
  121. [1,2,.4],
  122. [2,0.5,.3],
  123. [2.7,2,.5]
  124. ];
  125. var data3_points = {
  126. //do not show points
  127. radius: 0,
  128. errorbars: "y",
  129. yerr: {show:true, upperCap: "-", lowerCap: "-", radius: 5}
  130. };
  131. var data = [
  132. {color: "green", lines: {show: true}, points: data3_points, data: data3, label: "data3"},
  133. ];
  134. var plot = $.plot($("#test-container"), data, {
  135. series: {
  136. lines: {
  137. show: false
  138. }
  139. },
  140. xaxis: {
  141. autoScale: 'none',
  142. min: 0.6,
  143. max: 3.1
  144. },
  145. yaxis: {
  146. autoScale: 'none',
  147. min: 0,
  148. max: 3.5
  149. }
  150. });
  151. executeHooks(plot, plot.hooks.draw, [ctx]);
  152. expect(ctx.lineTo).toHaveBeenCalled();
  153. expect(ctx.moveTo).toHaveBeenCalled();
  154. var points = ctx.moveTo.calls.allArgs().concat(
  155. ctx.lineTo.calls.allArgs());
  156. validatePointsAreInsideTheAxisRanges(points);
  157. });
  158. it('should draw lines for error bars on bars', function () {
  159. spyOn(ctx, 'lineTo').and.callThrough();
  160. spyOn(ctx, 'moveTo').and.callThrough();
  161. var data4 = [
  162. [1.3, 1],
  163. [1.75, 2.5],
  164. [2.5, 0.5]
  165. ];
  166. var data4_points = {
  167. //do not show points
  168. radius: 0,
  169. errorbars: "y",
  170. yerr: {show:true, upperCap: "-", lowerCap: "-", radius: 5}
  171. };
  172. var data4_errors = [0.1, 0.4, 0.2];
  173. for (var i = 0; i < data4.length; i++) {
  174. data4_errors[i] = data4[i].concat(data4_errors[i])
  175. }
  176. var data = [
  177. {color: "orange", bars: {show: true, align: "center", barWidth: 0.25}, data: data4, label: "data4"},
  178. {color: "orange", points: data4_points, data: data4_errors}
  179. ];
  180. var plot = $.plot($("#test-container"), data, {
  181. series: {
  182. lines: {
  183. show: false
  184. }
  185. },
  186. xaxis: {
  187. autoScale: 'none',
  188. min: 0.6,
  189. max: 3.1
  190. },
  191. yaxis: {
  192. autoScale: 'none',
  193. min: 0,
  194. max: 3.5
  195. }
  196. });
  197. executeHooks(plot, plot.hooks.draw, [ctx]);
  198. expect(ctx.lineTo).toHaveBeenCalled();
  199. expect(ctx.moveTo).toHaveBeenCalled();
  200. var points = ctx.moveTo.calls.allArgs().concat(
  201. ctx.lineTo.calls.allArgs());
  202. validatePointsAreInsideTheAxisRanges(points);
  203. expect(ctx.lineTo).toHaveBeenCalled();
  204. });
  205. it('should use custom draw functions', function () {
  206. spyOn(drawFuncs, 'drawArrow').and.callThrough();
  207. spyOn(drawFuncs, 'drawSemiCircle').and.callThrough();
  208. var data2 = [
  209. [.7,3,.2,.4],
  210. [1.5,2.2,.3,.4],
  211. [2.3,1,.5,.2]
  212. ];
  213. var data2_points = {
  214. show: true,
  215. radius: 5,
  216. errorbars: "y",
  217. yerr: {show:true, asymmetric:true, upperCap: drawFuncs.drawArrow, lowerCap: drawFuncs.drawSemiCircle}
  218. };
  219. var data = [
  220. {color: "red", points: data2_points, data: data2, label: "data2"},
  221. ];
  222. var plot = $.plot($("#test-container"), data, {
  223. series: {
  224. lines: {
  225. show: false
  226. }
  227. },
  228. xaxis: {
  229. autoScale: 'none',
  230. min: 0.6,
  231. max: 3.1
  232. },
  233. yaxis: {
  234. autoScale: 'none',
  235. min: 0,
  236. max: 3.5
  237. }
  238. });
  239. executeHooks(plot, plot.hooks.draw, [ctx]);
  240. expect(drawFuncs.drawArrow).toHaveBeenCalled();
  241. expect(drawFuncs.drawSemiCircle).toHaveBeenCalled();
  242. });
  243. });