jquery.flot-drawSeries.Test.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /* eslint-disable */
  2. /* global $, describe, it, xit, xdescribe, after, afterEach, expect*/
  3. describe('drawSeries', function() {
  4. describe('drawSeriesLines', function() {
  5. var minx = 0, maxx = 200, miny = 0, maxy = 100,
  6. series, ctx, plotWidth, plotHeight, plotOffset,
  7. drawSeriesLines = jQuery.plot.drawSeries.drawSeriesLines,
  8. getColorOrGradient;
  9. beforeEach(function() {
  10. series = {
  11. lines: {
  12. lineWidth: 1
  13. },
  14. datapoints: {
  15. format: null,
  16. points: null,
  17. pointsize: 2
  18. },
  19. xaxis: {
  20. min: minx,
  21. max: maxx,
  22. p2c: function(p) { return p; }
  23. },
  24. yaxis: {
  25. min: miny,
  26. max: maxy,
  27. p2c: function(p) { return p; }
  28. }
  29. };
  30. 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>')
  31. .find('#theCanvas')[0]
  32. .getContext('2d');
  33. plotWidth = 200;
  34. plotHeight = 100;
  35. plotOffset = { top: 0, left: 0 };
  36. getColorOrGradient = jasmine.createSpy().and.returnValue('rgb(10,200,10)');
  37. });
  38. it('should draw nothing when the values are null', function () {
  39. series.datapoints.points = [null, null, null, null];
  40. spyOn(ctx, 'moveTo').and.callThrough();
  41. spyOn(ctx, 'lineTo').and.callThrough();
  42. drawSeriesLines(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  43. expect(ctx.moveTo).not.toHaveBeenCalled();
  44. expect(ctx.lineTo).not.toHaveBeenCalled();
  45. });
  46. it('should draw lines for values', function () {
  47. series.datapoints.points = [0, 0, 150, 25, 50, 75, 200, 100];
  48. spyOn(ctx, 'lineTo').and.callThrough();
  49. drawSeriesLines(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  50. expect(ctx.lineTo).toHaveBeenCalled();
  51. });
  52. it('should decimate when a decimate function is provided', function () {
  53. series.datapoints.points = [-1, -1, 0, 0, 1, 1, 2, 2, 3, 3];
  54. series.decimate = function() {
  55. return [0, 0, 1, 1];
  56. };
  57. spyOn(ctx, 'moveTo').and.callThrough();
  58. spyOn(ctx, 'lineTo').and.callThrough();
  59. drawSeriesLines(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  60. expect(ctx.moveTo).toHaveBeenCalledWith(0, 0);
  61. expect(ctx.lineTo).toHaveBeenCalledWith(1, 1);
  62. });
  63. it('should clip the lines when the points are outside the range of the axes', function () {
  64. series.datapoints.points = [
  65. minx - 8, 50,
  66. maxx + 8, 50,
  67. 100, miny - 8,
  68. 100, maxy + 8,
  69. minx - 8, 50];
  70. spyOn(ctx, 'moveTo').and.callThrough();
  71. spyOn(ctx, 'lineTo').and.callThrough();
  72. drawSeriesLines(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  73. validatePointsAreInsideTheAxisRanges(
  74. ctx.moveTo.calls.allArgs().concat(
  75. ctx.lineTo.calls.allArgs()));
  76. });
  77. it('should clip the lines and the fill area when the points are outside the range of the axes', function () {
  78. series.datapoints.points = [
  79. minx - 8, 50,
  80. maxx + 8, 50,
  81. 100, miny - 8,
  82. 100, maxy + 8,
  83. minx - 8, 50];
  84. series.lines.fill = true;
  85. series.lines.fillColor = 'rgb(200, 200, 200)';
  86. spyOn(ctx, 'moveTo').and.callThrough();
  87. spyOn(ctx, 'lineTo').and.callThrough();
  88. drawSeriesLines(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  89. validatePointsAreInsideTheAxisRanges(
  90. ctx.moveTo.calls.allArgs().concat(
  91. ctx.lineTo.calls.allArgs()));
  92. });
  93. it('should draw the lines when trailing step interpolation is enabled', function () {
  94. series.datapoints.points = [0, 0, 150, 25, 50, 75, 200, 100];
  95. series.lines.steps = true;
  96. spyOn(ctx, 'lineTo').and.callThrough();
  97. drawSeriesLines(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  98. expect(ctx.lineTo).toHaveBeenCalled();
  99. });
  100. it('should fill the area when trailing step interpolation is enabled', function () {
  101. series.datapoints.points = [0, 0, 150, 25, 50, 75, 200, 100];
  102. series.lines.steps = true;
  103. series.lines.fill = true;
  104. series.lines.lineWidth = 0;
  105. spyOn(ctx, 'moveTo').and.callThrough();
  106. spyOn(ctx, 'lineTo').and.callThrough();
  107. spyOn(ctx, 'fill').and.callThrough();
  108. drawSeriesLines(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  109. expect(ctx.moveTo).toHaveBeenCalled();
  110. expect(ctx.lineTo).toHaveBeenCalled();
  111. expect(ctx.fill).toHaveBeenCalled();
  112. });
  113. function validatePointsAreInsideTheAxisRanges(points) {
  114. points.forEach(function(point) {
  115. var x = point[0], y = point[1];
  116. expect(minx <= x && x <= maxx).toBe(true);
  117. expect(miny <= y && y <= maxy).toBe(true);
  118. });
  119. }
  120. });
  121. describe('drawSeriesPoints', function() {
  122. var minx = 0, maxx = 200, miny = 0, maxy = 100,
  123. series, ctx, plotWidth, plotHeight, plotOffset,
  124. drawSeriesPoints = jQuery.plot.drawSeries.drawSeriesPoints,
  125. dollar, getColorOrGradient;
  126. beforeEach(function() {
  127. series = {
  128. points: {
  129. show: true,
  130. symbol: 'circle',
  131. radius: 5
  132. },
  133. datapoints: {
  134. format: null,
  135. points: null,
  136. pointsize: 2
  137. },
  138. xaxis: {
  139. min: minx,
  140. max: maxx,
  141. p2c: function(p) { return p; }
  142. },
  143. yaxis: {
  144. min: miny,
  145. max: maxy,
  146. p2c: function(p) { return p; }
  147. }
  148. };
  149. 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>')
  150. .find('#theCanvas')[0]
  151. .getContext('2d');
  152. plotWidth = 200;
  153. plotHeight = 100;
  154. plotOffset = { top: 0, left: 0 };
  155. dollar = jasmine.createSpy().and.callFake(function (ctx, x, y, radius, shadow) {
  156. ctx.strokeText('$', x, y);
  157. });
  158. getColorOrGradient = jasmine.createSpy().and.returnValue('rgb(10,200,10)');
  159. });
  160. it('should draw nothing when the values are null', function () {
  161. series.datapoints.points = [null, null, null, null];
  162. spyOn(ctx, 'moveTo').and.callThrough();
  163. spyOn(ctx, 'lineTo').and.callThrough();
  164. drawSeriesPoints(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  165. expect(ctx.moveTo).not.toHaveBeenCalled();
  166. expect(ctx.lineTo).not.toHaveBeenCalled();
  167. });
  168. it('should draw circles by default for values', function () {
  169. series.datapoints.points = [0, 0, 150, 25, 50, 75, 200, 100];
  170. spyOn(ctx, 'arc').and.callThrough();
  171. drawSeriesPoints(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  172. expect(ctx.arc).toHaveBeenCalled();
  173. });
  174. it('should draw custom symbols given by name for values', function () {
  175. series.points.symbol = 'dollar';
  176. series.datapoints.points = [0, 0, 150, 25, 50, 75, 200, 100];
  177. drawSeriesPoints(series, ctx, plotOffset, plotWidth, plotHeight, {'dollar': dollar}, getColorOrGradient);
  178. expect(dollar).toHaveBeenCalled();
  179. });
  180. it('should draw custom symbols given by function for values', function () {
  181. series.points.symbol = 'dollar';
  182. series.datapoints.points = [0, 0, 150, 25, 50, 75, 200, 100];
  183. drawSeriesPoints(series, ctx, plotOffset, plotWidth, plotHeight, dollar, getColorOrGradient);
  184. expect(dollar).toHaveBeenCalled();
  185. });
  186. it('should not fill when the symbol function doesn`t need fill', function () {
  187. dollar.fill = undefined;
  188. series.points.symbol = 'dollar';
  189. series.datapoints.points = [0, 0, 150, 25, 50, 75, 200, 100];
  190. spyOn(ctx, 'fill').and.callThrough();
  191. drawSeriesPoints(series, ctx, plotOffset, plotWidth, plotHeight, dollar, getColorOrGradient);
  192. expect(ctx.fill).not.toHaveBeenCalled();
  193. });
  194. it('should fill only once when the symbol function needs fill', function () {
  195. dollar.fill = true;
  196. series.points.symbol = 'dollar';
  197. series.datapoints.points = [0, 0, 150, 25, 50, 75, 200, 100];
  198. spyOn(ctx, 'fill').and.callThrough();
  199. drawSeriesPoints(series, ctx, plotOffset, plotWidth, plotHeight, dollar, getColorOrGradient);
  200. expect(ctx.fill).toHaveBeenCalled();
  201. expect(ctx.fill.calls.count()).toBe(1);
  202. });
  203. });
  204. describe('drawSeriesBars', function() {
  205. var minx = -200, maxx = 200, miny = -100, maxy = 100,
  206. series, ctx, plotWidth, plotHeight, plotOffset,
  207. drawSeriesBars = jQuery.plot.drawSeries.drawSeriesBars,
  208. getColorOrGradient;
  209. beforeEach(function() {
  210. series = {
  211. bars: {
  212. lineWidth: 1,
  213. show: true,
  214. fillColor: 'blue',
  215. barWidth: 0.8
  216. },
  217. datapoints: {
  218. format: null,
  219. points: null,
  220. pointsize: 2
  221. },
  222. xaxis: {
  223. min: minx,
  224. max: maxx,
  225. autoScale: 'exact',
  226. p2c: function(p) { return p; }
  227. },
  228. yaxis: {
  229. min: miny,
  230. max: maxy,
  231. autoScale: 'exact',
  232. p2c: function(p) { return p; }
  233. }
  234. };
  235. 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>')
  236. .find('#theCanvas')[0]
  237. .getContext('2d');
  238. plotWidth = 200;
  239. plotHeight = 100;
  240. plotOffset = { top: 0, left: 0 };
  241. });
  242. function getPixelColor(ctx, x, y) {
  243. return ctx.getImageData(x, y, 1, 1).data;
  244. }
  245. function rgba(r, g, b, a) {
  246. return [r, g, b, a * 255];
  247. }
  248. it('should draw nothing when the values are null', function () {
  249. series.datapoints.points = [null, null, null, null];
  250. spyOn(ctx, 'moveTo').and.callThrough();
  251. spyOn(ctx, 'lineTo').and.callThrough();
  252. drawSeriesBars(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  253. expect(ctx.moveTo).not.toHaveBeenCalled();
  254. expect(ctx.lineTo).not.toHaveBeenCalled();
  255. });
  256. it('should work with NaN, Infinity and -Infinity values', function () {
  257. spyOn(ctx, 'lineTo').and.callThrough();
  258. series.datapoints.points = [Infinity, 0, NaN, NaN, 0, Infinity, 10, -Infinity, -Infinity, 10, 3, 5, 8, 2];
  259. drawSeriesBars(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  260. expect(ctx.lineTo).toHaveBeenCalled();
  261. });
  262. it('should draw bars for values', function () {
  263. series.datapoints.points = [0, 0, 150, 25, 50, 75, 200, 100];
  264. spyOn(ctx, 'lineTo').and.callThrough();
  265. drawSeriesBars(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  266. expect(ctx.lineTo).toHaveBeenCalled();
  267. });
  268. it('should draw bars using bottom points if provided', function () {
  269. getColorOrGradient = jasmine.createSpy().and.returnValue('rgb(10,200,10)');
  270. series.bars.fill = true;
  271. series.datapoints.points = [0, 10, 0, 1, 10, 1, 2, 10, 2];
  272. series.datapoints.pointsize = 3;
  273. spyOn(ctx, 'fillRect').and.callThrough();
  274. drawSeriesBars(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  275. var barHeights = ctx.fillRect.calls.allArgs().map(function (args) {
  276. return args[3];
  277. });
  278. // bottom - top
  279. expect(barHeights).toEqual([-10, -9, -8]);
  280. });
  281. it('should draw bars for fillTowards infinity', function () {
  282. series.datapoints.points = [150, 25];
  283. series.bars.fillTowards = Infinity;
  284. var barWidth = series.bars.barWidth[0] || series.bars.barWidth;
  285. var xaxis = series.xaxis,
  286. yaxis = series.yaxis,
  287. leftValue = xaxis.p2c(series.datapoints.points[0] - barWidth / 2),
  288. rightValue = xaxis.p2c(series.datapoints.points[0] + barWidth / 2),
  289. topValue = yaxis.p2c(maxy),
  290. yValue = xaxis.p2c(series.datapoints.points[1]);
  291. spyOn(ctx, 'lineTo').and.callThrough();
  292. drawSeriesBars(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  293. expect(ctx.lineTo.calls.argsFor(0)).toEqual([leftValue, topValue]);
  294. expect(ctx.lineTo.calls.argsFor(1)).toEqual([rightValue, yValue]);
  295. expect(ctx.lineTo.calls.argsFor(2)).toEqual([leftValue, yValue]);
  296. });
  297. it('should draw bars for fillTowards -infinity', function () {
  298. series.datapoints.points = [150, 25];
  299. series.bars.fillTowards = -Infinity;
  300. var barWidth = series.bars.barWidth[0] || series.bars.barWidth;
  301. var xaxis = series.xaxis,
  302. yaxis = series.yaxis,
  303. leftValue = xaxis.p2c(series.datapoints.points[0] - barWidth / 2),
  304. rightValue = xaxis.p2c(series.datapoints.points[0] + barWidth / 2),
  305. bottomValue = yaxis.p2c(miny),
  306. yValue = xaxis.p2c(series.datapoints.points[1]);
  307. spyOn(ctx, 'lineTo').and.callThrough();
  308. drawSeriesBars(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  309. expect(ctx.lineTo.calls.argsFor(0)).toEqual([leftValue, yValue]);
  310. expect(ctx.lineTo.calls.argsFor(1)).toEqual([rightValue, yValue]);
  311. expect(ctx.lineTo.calls.argsFor(2)).toEqual([rightValue, bottomValue]);
  312. });
  313. it('should draw bars for fillTowards zero', function () {
  314. series.datapoints.points = [150, 25];
  315. series.bars.fillTowards = 0;
  316. var barWidth = series.bars.barWidth[0] || series.bars.barWidth;
  317. var xaxis = series.xaxis,
  318. yaxis = series.yaxis,
  319. leftValue = xaxis.p2c(series.datapoints.points[0] - barWidth / 2),
  320. rightValue = xaxis.p2c(series.datapoints.points[0] + barWidth / 2),
  321. zeroValue = yaxis.p2c(0),
  322. yValue = xaxis.p2c(series.datapoints.points[1]);
  323. spyOn(ctx, 'lineTo').and.callThrough();
  324. drawSeriesBars(series, ctx, plotOffset, plotWidth, plotHeight, null, getColorOrGradient);
  325. expect(ctx.lineTo.calls.argsFor(0)).toEqual([leftValue, yValue]);
  326. expect(ctx.lineTo.calls.argsFor(1)).toEqual([rightValue, yValue]);
  327. expect(ctx.lineTo.calls.argsFor(2)).toEqual([rightValue, zeroValue]);
  328. });
  329. it('should draw bars with specified color', function () {
  330. var fixture = setFixtures('<div id="demo-container" style="width: 800px;height: 600px">').find('#demo-container').get(0),
  331. placeholder = $('<div id="placeholder" style="width: 100%;height: 100%">');
  332. placeholder.appendTo(fixture);
  333. $.plot(placeholder, [[[45, 25]]], {
  334. series: {
  335. bars: {
  336. lineWidth: 1,
  337. show: true,
  338. fillColor: 'blue'
  339. }
  340. },
  341. xaxis: { autoScale:'exact' }
  342. });
  343. var ctx = $(placeholder).find('.flot-base').get(0).getContext('2d')
  344. insideColor1 = getPixelColor(ctx, ctx.canvas.width / 2, ctx.canvas.height / 2),
  345. insideColor2 = getPixelColor(ctx, ctx.canvas.width / 2 + 35, ctx.canvas.height / 2 - 20),
  346. insideColor3 =getPixelColor(ctx, ctx.canvas.width / 2 - 10, ctx.canvas.height / 2 + 30);
  347. expect(Array.prototype.slice.call(insideColor1)).toEqual(rgba(0,0,255,1));
  348. expect(Array.prototype.slice.call(insideColor2)).toEqual(rgba(0,0,255,1));
  349. expect(Array.prototype.slice.call(insideColor3)).toEqual(rgba(0,0,255,1));
  350. });
  351. it('should use a barWidth based on points distance', function () {
  352. var fixture = setFixtures('<div id="demo-container" style="width: 800px;height: 600px">').find('#demo-container').get(0),
  353. placeholder = $('<div id="placeholder" style="width: 100%;height: 100%">');
  354. placeholder.appendTo(fixture);
  355. var testVector = [[[[[0.1, 1], [0.2, 10]]], 0.08],
  356. [[[[1, 1], [2, 10]]], 0.8],
  357. [[[[10, 1], [20, 10]]], 8],
  358. [[[[1000, 1], [2000, 10], [2100, 10]]], 80],
  359. [[[]], 0.8],
  360. [[[[-5, 1], [30, 15], [20, 7], [5, 2]]], 8]],
  361. plot;
  362. for (var i = 0; i< testVector.length; i++) {
  363. plot = $.plot(placeholder, testVector[i][0], {
  364. series: {
  365. bars: {
  366. lineWidth: 1,
  367. show: true,
  368. }
  369. },
  370. });
  371. var barWidth = plot.getData()[0].bars.barWidth[0] || plot.getData()[0].bars.barWidth;
  372. expect(barWidth).toBeCloseTo(testVector[i][1], 4);
  373. }
  374. });
  375. it('should be able to compute barWidth for NaN, Infinity and -Infinity values', function () {
  376. var fixture = setFixtures('<div id="test-container" style="width: 600px;height: 400px"><canvas id="theCanvas" style="width: 100%; height: 100%" /></div>'),
  377. placeholder = fixture.find('#test-container');
  378. placeholder.appendTo(fixture);
  379. var plot = $.plot(placeholder, [[[Infinity, 0], [0, Infinity], [10, -Infinity], [-Infinity, 10], [3, 5], [8, 2], [NaN, NaN]]], {series: series});
  380. var axes = plot.getAxes();
  381. var barWidth = plot.getData()[0].bars.barWidth[0] || plot.getData()[0].bars.barWidth;
  382. expect(barWidth).toEqual(4);
  383. });
  384. });
  385. });