controller.doughnut.tests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // Test the bar controller
  2. describe('Doughnut controller tests', function() {
  3. beforeEach(function() {
  4. window.addDefaultMatchers(jasmine);
  5. });
  6. afterEach(function() {
  7. window.releaseAllCharts();
  8. });
  9. it('should be constructed', function() {
  10. var chart = window.acquireChart({
  11. type: 'doughnut',
  12. data: {
  13. datasets: [{
  14. data: []
  15. }],
  16. labels: []
  17. }
  18. });
  19. var meta = chart.getDatasetMeta(0);
  20. expect(meta.type).toBe('doughnut');
  21. expect(meta.controller).not.toBe(undefined);
  22. expect(meta.controller.index).toBe(0);
  23. expect(meta.data).toEqual([]);
  24. meta.controller.updateIndex(1);
  25. expect(meta.controller.index).toBe(1);
  26. });
  27. it('should create arc elements for each data item during initialization', function() {
  28. var chart = window.acquireChart({
  29. type: 'doughnut',
  30. data: {
  31. datasets: [{
  32. data: [10, 15, 0, 4]
  33. }],
  34. labels: []
  35. }
  36. });
  37. var meta = chart.getDatasetMeta(0);
  38. expect(meta.data.length).toBe(4); // 4 rectangles created
  39. expect(meta.data[0] instanceof Chart.elements.Arc).toBe(true);
  40. expect(meta.data[1] instanceof Chart.elements.Arc).toBe(true);
  41. expect(meta.data[2] instanceof Chart.elements.Arc).toBe(true);
  42. expect(meta.data[3] instanceof Chart.elements.Arc).toBe(true);
  43. });
  44. it ('should reset and update elements', function() {
  45. var chart = window.acquireChart({
  46. type: 'doughnut',
  47. data: {
  48. datasets: [{
  49. data: [1, 2, 3, 4],
  50. hidden: true
  51. }, {
  52. data: [5, 6, 0, 7]
  53. }, {
  54. data: [8, 9, 10, 11]
  55. }],
  56. labels: ['label0', 'label1', 'label2', 'label3']
  57. },
  58. options: {
  59. animation: {
  60. animateRotate: true,
  61. animateScale: false
  62. },
  63. cutoutPercentage: 50,
  64. rotation: Math.PI * -0.5,
  65. circumference: Math.PI * 2.0,
  66. elements: {
  67. arc: {
  68. backgroundColor: 'rgb(255, 0, 0)',
  69. borderColor: 'rgb(0, 0, 255)',
  70. borderWidth: 2,
  71. hoverBackgroundColor: 'rgb(255, 255, 255)'
  72. }
  73. }
  74. }
  75. });
  76. var meta = chart.getDatasetMeta(1);
  77. meta.controller.reset(); // reset first
  78. expect(meta.data.length).toBe(4);
  79. [ { c: 0 },
  80. { c: 0 },
  81. { c: 0, },
  82. { c: 0 }
  83. ].forEach(function(expected, i) {
  84. expect(meta.data[i]._model.x).toBeCloseToPixel(256);
  85. expect(meta.data[i]._model.y).toBeCloseToPixel(272);
  86. expect(meta.data[i]._model.outerRadius).toBeCloseToPixel(239);
  87. expect(meta.data[i]._model.innerRadius).toBeCloseToPixel(179);
  88. expect(meta.data[i]._model.circumference).toBeCloseTo(expected.c, 8);
  89. expect(meta.data[i]._model).toEqual(jasmine.objectContaining({
  90. startAngle: Math.PI * -0.5,
  91. endAngle: Math.PI * -0.5,
  92. label: chart.data.labels[i],
  93. hoverBackgroundColor: 'rgb(255, 255, 255)',
  94. backgroundColor: 'rgb(255, 0, 0)',
  95. borderColor: 'rgb(0, 0, 255)',
  96. borderWidth: 2
  97. }));
  98. })
  99. chart.update();
  100. [ { c: 1.7453292519, s: -1.5707963267, e: 0.1745329251 },
  101. { c: 2.0943951023, s: 0.1745329251, e: 2.2689280275 },
  102. { c: 0, s: 2.2689280275, e: 2.2689280275 },
  103. { c: 2.4434609527, s: 2.2689280275, e: 4.7123889803 }
  104. ].forEach(function(expected, i) {
  105. expect(meta.data[i]._model.x).toBeCloseToPixel(256);
  106. expect(meta.data[i]._model.y).toBeCloseToPixel(272);
  107. expect(meta.data[i]._model.outerRadius).toBeCloseToPixel(239);
  108. expect(meta.data[i]._model.innerRadius).toBeCloseToPixel(179);
  109. expect(meta.data[i]._model.circumference).toBeCloseTo(expected.c, 8);
  110. expect(meta.data[i]._model.startAngle).toBeCloseTo(expected.s, 8);
  111. expect(meta.data[i]._model.endAngle).toBeCloseTo(expected.e, 8);
  112. expect(meta.data[i]._model).toEqual(jasmine.objectContaining({
  113. label: chart.data.labels[i],
  114. hoverBackgroundColor: 'rgb(255, 255, 255)',
  115. backgroundColor: 'rgb(255, 0, 0)',
  116. borderColor: 'rgb(0, 0, 255)',
  117. borderWidth: 2
  118. }));
  119. })
  120. // Change the amount of data and ensure that arcs are updated accordingly
  121. chart.data.datasets[1].data = [1, 2]; // remove 2 elements from dataset 0
  122. chart.update();
  123. expect(meta.data.length).toBe(2);
  124. expect(meta.data[0] instanceof Chart.elements.Arc).toBe(true);
  125. expect(meta.data[1] instanceof Chart.elements.Arc).toBe(true);
  126. // Add data
  127. chart.data.datasets[1].data = [1, 2, 3, 4];
  128. chart.update();
  129. expect(meta.data.length).toBe(4);
  130. expect(meta.data[0] instanceof Chart.elements.Arc).toBe(true);
  131. expect(meta.data[1] instanceof Chart.elements.Arc).toBe(true);
  132. expect(meta.data[2] instanceof Chart.elements.Arc).toBe(true);
  133. expect(meta.data[3] instanceof Chart.elements.Arc).toBe(true);
  134. });
  135. it ('should rotate and limit circumference', function() {
  136. var chart = window.acquireChart({
  137. type: 'doughnut',
  138. data: {
  139. datasets: [{
  140. data: [2, 4],
  141. hidden: true
  142. }, {
  143. data: [1, 3]
  144. }, {
  145. data: [1, 0]
  146. }],
  147. labels: ['label0', 'label1']
  148. },
  149. options: {
  150. cutoutPercentage: 50,
  151. rotation: Math.PI,
  152. circumference: Math.PI * 0.5,
  153. elements: {
  154. arc: {
  155. backgroundColor: 'rgb(255, 0, 0)',
  156. borderColor: 'rgb(0, 0, 255)',
  157. borderWidth: 2,
  158. hoverBackgroundColor: 'rgb(255, 255, 255)'
  159. }
  160. }
  161. }
  162. });
  163. var meta = chart.getDatasetMeta(1);
  164. expect(meta.data.length).toBe(2);
  165. // Only startAngle, endAngle and circumference should be different.
  166. [ { c: Math.PI / 8, s: Math.PI, e: Math.PI + Math.PI / 8 },
  167. { c: 3 * Math.PI / 8, s: Math.PI + Math.PI / 8, e: Math.PI + Math.PI / 2 }
  168. ].forEach(function(expected, i) {
  169. expect(meta.data[i]._model.x).toBeCloseToPixel(495);
  170. expect(meta.data[i]._model.y).toBeCloseToPixel(511);
  171. expect(meta.data[i]._model.outerRadius).toBeCloseToPixel(478);
  172. expect(meta.data[i]._model.innerRadius).toBeCloseToPixel(359);
  173. expect(meta.data[i]._model.circumference).toBeCloseTo(expected.c,8);
  174. expect(meta.data[i]._model.startAngle).toBeCloseTo(expected.s, 8);
  175. expect(meta.data[i]._model.endAngle).toBeCloseTo(expected.e, 8);
  176. })
  177. });
  178. it ('should draw all arcs', function() {
  179. var chart = window.acquireChart({
  180. type: 'doughnut',
  181. data: {
  182. datasets: [{
  183. data: [10, 15, 0, 4]
  184. }],
  185. labels: ['label0', 'label1', 'label2', 'label3']
  186. }
  187. });
  188. var meta = chart.getDatasetMeta(0);
  189. spyOn(meta.data[0], 'draw');
  190. spyOn(meta.data[1], 'draw');
  191. spyOn(meta.data[2], 'draw');
  192. spyOn(meta.data[3], 'draw');
  193. chart.update();
  194. expect(meta.data[0].draw.calls.count()).toBe(1);
  195. expect(meta.data[1].draw.calls.count()).toBe(1);
  196. expect(meta.data[2].draw.calls.count()).toBe(1);
  197. expect(meta.data[3].draw.calls.count()).toBe(1);
  198. });
  199. it ('should set the hover style of an arc', function() {
  200. var chart = window.acquireChart({
  201. type: 'doughnut',
  202. data: {
  203. datasets: [{
  204. data: [10, 15, 0, 4]
  205. }],
  206. labels: ['label0', 'label1', 'label2', 'label3']
  207. },
  208. options: {
  209. elements: {
  210. arc: {
  211. backgroundColor: 'rgb(255, 0, 0)',
  212. borderColor: 'rgb(0, 0, 255)',
  213. borderWidth: 2,
  214. }
  215. }
  216. }
  217. });
  218. var meta = chart.getDatasetMeta(0);
  219. var arc = meta.data[0];
  220. meta.controller.setHoverStyle(arc);
  221. expect(arc._model.backgroundColor).toBe('rgb(230, 0, 0)');
  222. expect(arc._model.borderColor).toBe('rgb(0, 0, 230)');
  223. expect(arc._model.borderWidth).toBe(2);
  224. // Set a dataset style to take precedence
  225. chart.data.datasets[0].hoverBackgroundColor = 'rgb(9, 9, 9)';
  226. chart.data.datasets[0].hoverBorderColor = 'rgb(18, 18, 18)';
  227. chart.data.datasets[0].hoverBorderWidth = 1.56;
  228. meta.controller.setHoverStyle(arc);
  229. expect(arc._model.backgroundColor).toBe('rgb(9, 9, 9)');
  230. expect(arc._model.borderColor).toBe('rgb(18, 18, 18)');
  231. expect(arc._model.borderWidth).toBe(1.56);
  232. // Dataset styles can be an array
  233. chart.data.datasets[0].hoverBackgroundColor = ['rgb(255, 255, 255)', 'rgb(9, 9, 9)'];
  234. chart.data.datasets[0].hoverBorderColor = ['rgb(18, 18, 18)'];
  235. chart.data.datasets[0].hoverBorderWidth = [0.1, 1.56];
  236. meta.controller.setHoverStyle(arc);
  237. expect(arc._model.backgroundColor).toBe('rgb(255, 255, 255)');
  238. expect(arc._model.borderColor).toBe('rgb(18, 18, 18)');
  239. expect(arc._model.borderWidth).toBe(0.1);
  240. // Element custom styles also work
  241. arc.custom = {
  242. hoverBackgroundColor: 'rgb(7, 7, 7)',
  243. hoverBorderColor: 'rgb(17, 17, 17)',
  244. hoverBorderWidth: 3.14159,
  245. };
  246. meta.controller.setHoverStyle(arc);
  247. expect(arc._model.backgroundColor).toBe('rgb(7, 7, 7)');
  248. expect(arc._model.borderColor).toBe('rgb(17, 17, 17)');
  249. expect(arc._model.borderWidth).toBe(3.14159);
  250. });
  251. it ('should unset the hover style of an arc', function() {
  252. var chart = window.acquireChart({
  253. type: 'doughnut',
  254. data: {
  255. datasets: [{
  256. data: [10, 15, 0, 4]
  257. }],
  258. labels: ['label0', 'label1', 'label2', 'label3']
  259. },
  260. options: {
  261. elements: {
  262. arc: {
  263. backgroundColor: 'rgb(255, 0, 0)',
  264. borderColor: 'rgb(0, 0, 255)',
  265. borderWidth: 2,
  266. }
  267. }
  268. }
  269. });
  270. var meta = chart.getDatasetMeta(0);
  271. var arc = meta.data[0];
  272. meta.controller.removeHoverStyle(arc);
  273. expect(arc._model.backgroundColor).toBe('rgb(255, 0, 0)');
  274. expect(arc._model.borderColor).toBe('rgb(0, 0, 255)');
  275. expect(arc._model.borderWidth).toBe(2);
  276. // Set a dataset style to take precedence
  277. chart.data.datasets[0].backgroundColor = 'rgb(9, 9, 9)';
  278. chart.data.datasets[0].borderColor = 'rgb(18, 18, 18)';
  279. chart.data.datasets[0].borderWidth = 1.56;
  280. meta.controller.removeHoverStyle(arc);
  281. expect(arc._model.backgroundColor).toBe('rgb(9, 9, 9)');
  282. expect(arc._model.borderColor).toBe('rgb(18, 18, 18)');
  283. expect(arc._model.borderWidth).toBe(1.56);
  284. // Dataset styles can be an array
  285. chart.data.datasets[0].backgroundColor = ['rgb(255, 255, 255)', 'rgb(9, 9, 9)'];
  286. chart.data.datasets[0].borderColor = ['rgb(18, 18, 18)'];
  287. chart.data.datasets[0].borderWidth = [0.1, 1.56];
  288. meta.controller.removeHoverStyle(arc);
  289. expect(arc._model.backgroundColor).toBe('rgb(255, 255, 255)');
  290. expect(arc._model.borderColor).toBe('rgb(18, 18, 18)');
  291. expect(arc._model.borderWidth).toBe(0.1);
  292. // Element custom styles also work
  293. arc.custom = {
  294. backgroundColor: 'rgb(7, 7, 7)',
  295. borderColor: 'rgb(17, 17, 17)',
  296. borderWidth: 3.14159,
  297. };
  298. meta.controller.removeHoverStyle(arc);
  299. expect(arc._model.backgroundColor).toBe('rgb(7, 7, 7)');
  300. expect(arc._model.borderColor).toBe('rgb(17, 17, 17)');
  301. expect(arc._model.borderWidth).toBe(3.14159);
  302. });
  303. });