shape.bar-spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. describe('c3 chart shape bar', function () {
  2. 'use strict';
  3. var chart, args;
  4. beforeEach(function (done) {
  5. chart = window.initChart(chart, args, done);
  6. });
  7. describe('with groups', function () {
  8. describe('with indexed data', function () {
  9. beforeAll(function () {
  10. args = {
  11. data: {
  12. columns: [
  13. ['data1', 30, 200, -100, 400, -150, 250],
  14. ['data2', 50, 20, 10, 40, 15, 25],
  15. ],
  16. groups: [
  17. ['data1', 'data2'],
  18. ],
  19. type: 'bar'
  20. },
  21. };
  22. });
  23. it('should be stacked', function () {
  24. var expectedBottom = [275, 293, 365, 281, 395, 290];
  25. chart.internal.main.selectAll('.c3-bars-data1 .c3-bar').each(function (d, i) {
  26. var rect = d3.select(this).node().getBoundingClientRect();
  27. expect(rect.bottom).toBeCloseTo(expectedBottom[i], -1);
  28. });
  29. });
  30. });
  31. describe('with timeseries data', function () {
  32. beforeAll(function () {
  33. args = {
  34. data: {
  35. x: 'date',
  36. columns: [
  37. ['date', '2012-12-24', '2012-12-25', '2012-12-26', '2012-12-27', '2012-12-28', '2012-12-29'],
  38. ['data1', 30, 200, -100, 400, -150, 250],
  39. ['data2', 50, 20, 10, 40, 15, 25],
  40. ],
  41. groups: [
  42. ['data1', 'data2'],
  43. ],
  44. type: 'bar'
  45. },
  46. axis: {
  47. x: {
  48. type: 'timeseries',
  49. }
  50. }
  51. };
  52. });
  53. it('should be stacked', function () {
  54. var expectedBottom = [275, 293, 365, 281, 395, 290];
  55. chart.internal.main.selectAll('.c3-bars-data1 .c3-bar').each(function (d, i) {
  56. var rect = d3.select(this).node().getBoundingClientRect();
  57. expect(rect.bottom).toBeCloseTo(expectedBottom[i], -1);
  58. });
  59. });
  60. });
  61. describe('with category data', function () {
  62. beforeAll(function () {
  63. args = {
  64. data: {
  65. x: 'date',
  66. columns: [
  67. ['date', '2012-12-24', '2012-12-25', '2012-12-26', '2012-12-27', '2012-12-28', '2012-12-29'],
  68. ['data1', 30, 200, -100, 400, -150, 250],
  69. ['data2', 50, 20, 10, 40, 15, 25],
  70. ],
  71. groups: [
  72. ['data1', 'data2'],
  73. ],
  74. type: 'bar'
  75. },
  76. axis: {
  77. x: {
  78. type: 'category',
  79. }
  80. }
  81. };
  82. });
  83. it('should be stacked', function () {
  84. var expectedBottom = [275, 293, 365, 281, 395, 290];
  85. chart.internal.main.selectAll('.c3-bars-data1 .c3-bar').each(function (d, i) {
  86. var rect = d3.select(this).node().getBoundingClientRect();
  87. expect(rect.bottom).toBeCloseTo(expectedBottom[i], -1);
  88. });
  89. });
  90. });
  91. });
  92. describe('internal.isWithinBar', function () {
  93. describe('with normal axis', function () {
  94. beforeAll(function () {
  95. args = {
  96. data: {
  97. columns: [
  98. ['data1', 30, 200, 100, 400, -150, 250],
  99. ['data2', 50, 20, 10, 40, 15, 25],
  100. ['data3', -150, 120, 110, 140, 115, 125]
  101. ],
  102. type: 'bar'
  103. },
  104. axis: {
  105. rotated: false
  106. }
  107. };
  108. });
  109. it('should not be within bar', function () {
  110. var bar = d3.select('.c3-target-data1 .c3-bar-0').node();
  111. expect(chart.internal.isWithinBar([0, 0], bar)).toBeFalsy();
  112. });
  113. it('should be within bar', function () {
  114. var bar = d3.select('.c3-target-data1 .c3-bar-0').node();
  115. expect(chart.internal.isWithinBar([31, 280], bar)).toBeTruthy();
  116. });
  117. it('should not be within bar of negative value', function () {
  118. var bar = d3.select('.c3-target-data3 .c3-bar-0').node();
  119. expect(chart.internal.isWithinBar([68, 280], bar)).toBeFalsy();
  120. });
  121. it('should be within bar of negative value', function () {
  122. var bar = d3.select('.c3-target-data3 .c3-bar-0').node();
  123. expect(chart.internal.isWithinBar([68, 350], bar)).toBeTruthy();
  124. });
  125. });
  126. describe('with rotated axis', function () {
  127. beforeAll(function () {
  128. args.axis.rotated = true;
  129. });
  130. it('should not be within bar', function () {
  131. var bar = d3.select('.c3-target-data1 .c3-bar-0').node();
  132. expect(chart.internal.isWithinBar([0, 0], bar)).toBeFalsy();
  133. });
  134. it('should be within bar', function () {
  135. var bar = d3.select('.c3-target-data1 .c3-bar-0').node();
  136. expect(chart.internal.isWithinBar([190, 20], bar)).toBeTruthy();
  137. });
  138. it('should be within bar of negative value', function () {
  139. var bar = d3.select('.c3-target-data3 .c3-bar-0').node();
  140. expect(chart.internal.isWithinBar([68, 50], bar)).toBeTruthy();
  141. });
  142. });
  143. });
  144. describe('bar spacing', function() {
  145. var createArgs = function(spacing) {
  146. return {
  147. size: {
  148. width: 500
  149. },
  150. data: {
  151. columns: [
  152. ['data1', 30, 200, 100],
  153. ['data2', 50, 20, 10],
  154. ['data3', 150, 120, 110],
  155. ['data4', 12, 24, 20 ]
  156. ],
  157. type: 'bar',
  158. groups: [
  159. [ 'data1', 'data4' ]
  160. ]
  161. },
  162. bar: {
  163. space: spacing
  164. }
  165. };
  166. };
  167. var getBBox = function(selector) {
  168. return d3.select(selector).node().getBBox();
  169. };
  170. var getBarContainerWidth = function() {
  171. return parseInt(getBBox('.c3-chart-bars').width);
  172. };
  173. var getBarContainerOffset = function() {
  174. return parseInt(getBBox('.c3-chart-bars').x);
  175. };
  176. var getBarBBox = function(name, idx) {
  177. return getBBox('.c3-target-' + name + ' .c3-bar-' + (idx || 0));
  178. };
  179. var getBarWidth = function(name, idx) {
  180. return parseInt(getBarBBox(name, idx).width);
  181. };
  182. var getBarOffset = function(name1, name2, idx) {
  183. var bbox1 = getBarBBox(name1, idx);
  184. var bbox2 = getBarBBox(name2, idx);
  185. return parseInt(bbox2.x - (bbox1.x + bbox1.width));
  186. };
  187. it('should set bar spacing to 0', function () {
  188. args = createArgs(0);
  189. expect(true).toBeTruthy();
  190. });
  191. it('should display the bars without any spacing', function () {
  192. // all bars should have the same width
  193. expect(getBarWidth('data1', 0)).toEqual(30);
  194. expect(getBarWidth('data2', 0)).toEqual(30);
  195. expect(getBarWidth('data3', 0)).toEqual(30);
  196. expect(getBarWidth('data1', 1)).toEqual(30);
  197. expect(getBarWidth('data2', 1)).toEqual(30);
  198. expect(getBarWidth('data3', 1)).toEqual(30);
  199. expect(getBarWidth('data1', 2)).toEqual(30);
  200. expect(getBarWidth('data2', 2)).toEqual(30);
  201. expect(getBarWidth('data3', 2)).toEqual(30);
  202. // all offsets should be the same
  203. expect(getBarOffset('data1', 'data2', 0)).toEqual(0);
  204. expect(getBarOffset('data2', 'data3', 0)).toEqual(0);
  205. expect(getBarOffset('data1', 'data2', 1)).toEqual(0);
  206. expect(getBarOffset('data2', 'data3', 1)).toEqual(0);
  207. expect(getBarOffset('data1', 'data2', 2)).toEqual(0);
  208. expect(getBarOffset('data2', 'data3', 2)).toEqual(0);
  209. // default width/offset of the container for this chart
  210. expect(getBarContainerWidth()).toEqual(396);
  211. expect(getBarContainerOffset()).toEqual(31);
  212. });
  213. it('should set bar spacing to 0.25', function () {
  214. args = createArgs(0.25);
  215. expect(true).toBeTruthy();
  216. });
  217. it('should display the bars with a spacing ratio of 0.25', function () {
  218. // with bar_space of 0.25, the space between bars is
  219. // expected to be 25% of the original bar's width
  220. // which is ~7
  221. // expect all bars to be the same width
  222. expect(getBarWidth('data1', 0)).toEqual(22);
  223. expect(getBarWidth('data2', 0)).toEqual(22);
  224. expect(getBarWidth('data3', 0)).toEqual(22);
  225. expect(getBarWidth('data1', 1)).toEqual(22);
  226. expect(getBarWidth('data2', 1)).toEqual(22);
  227. expect(getBarWidth('data3', 1)).toEqual(22);
  228. expect(getBarWidth('data1', 2)).toEqual(22);
  229. expect(getBarWidth('data2', 2)).toEqual(22);
  230. expect(getBarWidth('data3', 2)).toEqual(22);
  231. // all offsets should be the same
  232. expect(getBarOffset('data1', 'data2', 0)).toEqual(7);
  233. expect(getBarOffset('data2', 'data3', 0)).toEqual(7);
  234. expect(getBarOffset('data1', 'data2', 1)).toEqual(7);
  235. expect(getBarOffset('data2', 'data3', 1)).toEqual(7);
  236. expect(getBarOffset('data1', 'data2', 2)).toEqual(7);
  237. expect(getBarOffset('data2', 'data3', 2)).toEqual(7);
  238. // expect the container to shrink a little because of
  239. // the offsets from the first/last chart
  240. // we add/subtract 1 because of approximation due to rounded values
  241. expect(getBarContainerWidth()).toEqual(396 - 7 - 1);
  242. expect(getBarContainerOffset()).toEqual(31 + (parseInt(7 / 2) + 1));
  243. });
  244. it('should set bar spacing to 0.5', function () {
  245. args = createArgs(0.5);
  246. expect(true).toBeTruthy();
  247. });
  248. it('should display the bars with a spacing ratio of 0.5', function () {
  249. // with bar_space of 0.5, the space between bars is
  250. // expected to be 50% of the original bar's width
  251. // which is ~15
  252. // expect all bars to be the same width
  253. expect(getBarWidth('data1', 0)).toEqual(15);
  254. expect(getBarWidth('data2', 0)).toEqual(15);
  255. expect(getBarWidth('data3', 0)).toEqual(15);
  256. expect(getBarWidth('data1', 1)).toEqual(15);
  257. expect(getBarWidth('data2', 1)).toEqual(15);
  258. expect(getBarWidth('data3', 1)).toEqual(15);
  259. expect(getBarWidth('data1', 2)).toEqual(15);
  260. expect(getBarWidth('data2', 2)).toEqual(15);
  261. expect(getBarWidth('data3', 2)).toEqual(15);
  262. // all offsets should be the same
  263. expect(getBarOffset('data1', 'data2', 0)).toEqual(15);
  264. expect(getBarOffset('data2', 'data3', 0)).toEqual(15);
  265. expect(getBarOffset('data1', 'data2', 1)).toEqual(15);
  266. expect(getBarOffset('data2', 'data3', 1)).toEqual(15);
  267. expect(getBarOffset('data1', 'data2', 2)).toEqual(15);
  268. expect(getBarOffset('data2', 'data3', 2)).toEqual(15);
  269. // expect the container to shrink a little because of
  270. // the offsets from the first/last chart
  271. expect(getBarContainerWidth()).toEqual(396 - 15);
  272. expect(getBarContainerOffset()).toEqual(31 + parseInt(15 / 2));
  273. });
  274. });
  275. });