arc-spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. describe('c3 chart arc', function () {
  2. 'use strict';
  3. var chart, args;
  4. beforeEach(function (done) {
  5. chart = window.initChart(chart, args, done);
  6. });
  7. describe('show pie chart', function () {
  8. beforeAll(function () {
  9. args = {
  10. data: {
  11. columns: [
  12. ['data1', 30],
  13. ['data2', 150],
  14. ['data3', 120]
  15. ],
  16. type: 'pie'
  17. }
  18. };
  19. });
  20. it('should have correct classes', function () {
  21. var chartArc = d3.select('.c3-chart-arcs'),
  22. arcs = {
  23. data1: chartArc.select('.c3-chart-arc.c3-target.c3-target-data1')
  24. .select('g.c3-shapes.c3-shapes-data1.c3-arcs.c3-arcs-data1')
  25. .select('path.c3-shape.c3-shape.c3-arc.c3-arc-data1'),
  26. data2: chartArc.select('.c3-chart-arc.c3-target.c3-target-data2')
  27. .select('g.c3-shapes.c3-shapes-data2.c3-arcs.c3-arcs-data2')
  28. .select('path.c3-shape.c3-shape.c3-arc.c3-arc-data2'),
  29. data3: chartArc.select('.c3-chart-arc.c3-target.c3-target-data3')
  30. .select('g.c3-shapes.c3-shapes-data3.c3-arcs.c3-arcs-data3')
  31. .select('path.c3-shape.c3-shape.c3-arc.c3-arc-data3')
  32. };
  33. expect(arcs.data1.size()).toBe(1);
  34. expect(arcs.data2.size()).toBe(1);
  35. expect(arcs.data3.size()).toBe(1);
  36. });
  37. it('should have correct d', function () {
  38. expect(d3.select('.c3-arc-data1').attr('d')).toMatch(/M-124\..+,-171\..+A211\..+,211\..+,0,0,1,-3\..+,-211\..+L0,0Z/);
  39. expect(d3.select('.c3-arc-data2').attr('d')).toMatch(/M1\..+,-211\..+211\..+,211\..+,0,0,1,1\..+,211\..+L0,0Z/);
  40. expect(d3.select('.c3-arc-data3').attr('d')).toMatch(/M1\..+,211\..+211\..+,211\..+,0,0,1,-124\..+,-171\..+L0,0Z/);
  41. });
  42. describe('with data id that can be converted to a color', function () {
  43. beforeAll(function(){
  44. args.data.columns = [
  45. ['black', 30],
  46. ['data2', 150],
  47. ['data3', 120]
  48. ];
  49. });
  50. it('should have correct d even if data id can be converted to a color', function (done) {
  51. setTimeout(function () {
  52. expect(d3.select('.c3-arc-black').attr('d')).toMatch(/M-124\..+,-171\..+A211\..+,211\..+,0,0,1,-3\..+,-211\..+L0,0Z/);
  53. done();
  54. }, 500);
  55. });
  56. describe('with empty pie chart', function(){
  57. beforeAll(function () {
  58. args = {
  59. data: {
  60. columns: [
  61. ['data1', null],
  62. ['data2', null],
  63. ['data3', null]
  64. ],
  65. type: 'pie'
  66. }
  67. };
  68. });
  69. it('should have correct d attribute', function () {
  70. var chartArc = d3.select('.c3-chart-arcs'),
  71. arcs = {
  72. data1: chartArc.select('.c3-chart-arc.c3-target.c3-target-data1')
  73. .select('g.c3-shapes.c3-shapes-data1.c3-arcs.c3-arcs-data1')
  74. .select('path.c3-shape.c3-shape.c3-arc.c3-arc-data1'),
  75. data2: chartArc.select('.c3-chart-arc.c3-target.c3-target-data2')
  76. .select('g.c3-shapes.c3-shapes-data2.c3-arcs.c3-arcs-data2')
  77. .select('path.c3-shape.c3-shape.c3-arc.c3-arc-data2'),
  78. data3: chartArc.select('.c3-chart-arc.c3-target.c3-target-data3')
  79. .select('g.c3-shapes.c3-shapes-data3.c3-arcs.c3-arcs-data3')
  80. .select('path.c3-shape.c3-shape.c3-arc.c3-arc-data3')
  81. };
  82. expect(arcs.data1.attr('d').indexOf('NaN')).toBe(-1);
  83. expect(arcs.data2.attr('d').indexOf('NaN')).toBe(-1);
  84. expect(arcs.data3.attr('d').indexOf('NaN')).toBe(-1);
  85. });
  86. });
  87. });
  88. });
  89. describe('sort pie chart', function() {
  90. var createPie = function(order) {
  91. return {
  92. data: {
  93. order: order,
  94. columns: [
  95. ['data1', 30],
  96. ['data2', 150],
  97. ['data3', 120]
  98. ],
  99. type: 'pie'
  100. }
  101. };
  102. };
  103. var collectArcs = function() {
  104. return d3.selectAll('.c3-arc')
  105. .data()
  106. .sort(function(a, b) {
  107. return a.startAngle - b.startAngle;
  108. })
  109. .map(function(item) {
  110. return item.data.id;
  111. });
  112. };
  113. it('should update data_order to desc', function () {
  114. args = createPie('desc');
  115. expect(true).toBeTruthy();
  116. });
  117. it('it should have descending ordering', function () {
  118. expect(collectArcs()).toEqual([ 'data2', 'data3', 'data1' ]);
  119. });
  120. it('should update data_order to asc', function () {
  121. args = createPie('asc');
  122. expect(true).toBeTruthy();
  123. });
  124. it('it should have ascending ordering', function () {
  125. expect(collectArcs()).toEqual([ 'data1', 'data3', 'data2' ]);
  126. });
  127. it('should update data_order to NULL', function () {
  128. args = createPie(null);
  129. expect(true).toBeTruthy();
  130. });
  131. it('it should have no ordering', function () {
  132. expect(collectArcs()).toEqual([ 'data1', 'data2', 'data3' ]);
  133. });
  134. it('should update data_order to Array', function () {
  135. args = createPie([ 'data3', 'data2', 'data1' ]);
  136. expect(true).toBeTruthy();
  137. });
  138. it('it should have array specified ordering', function () {
  139. expect(collectArcs()).toEqual([ 'data3', 'data2', 'data1' ]);
  140. });
  141. it('should update data_order to Function', function () {
  142. var names = [ 'data2', 'data1', 'data3' ];
  143. args = createPie(function(a, b) {
  144. return names.indexOf(a.id) - names.indexOf(b.id);
  145. });
  146. expect(true).toBeTruthy();
  147. });
  148. it('it should have array specified ordering', function () {
  149. expect(collectArcs()).toEqual([ 'data2', 'data1', 'data3' ]);
  150. });
  151. });
  152. describe('show gauge', function () {
  153. describe('with a 180 degree gauge', function(){
  154. beforeAll(function () {
  155. args = {
  156. gauge: {
  157. width: 10,
  158. max: 10,
  159. expand: true
  160. },
  161. data: {
  162. columns: [
  163. ['data', 8]
  164. ],
  165. type: 'gauge'
  166. }
  167. };
  168. });
  169. it('should have correct d for Pi radian gauge', function () {
  170. var chartArc = d3.select('.c3-chart-arcs'),
  171. data = chartArc.select('.c3-chart-arc.c3-target.c3-target-data')
  172. .select('g.c3-shapes.c3-shapes-data.c3-arcs.c3-arcs-data')
  173. .select('path.c3-shape.c3-shape.c3-arc.c3-arc-data');
  174. expect(data.attr('d')).toMatch(/-258.4,-3\..+A258.4,258.4,0,0,1,209\..+,-151\..+L200\..+,-146\..+A248.39999999999998,248.39999999999998,0,0,0,-248.39999999999998,-3\..+Z/);
  175. });
  176. });
  177. describe('with a 2 Pi radian gauge that starts at Pi/2', function() {
  178. beforeAll(function(){
  179. args = {
  180. gauge: {
  181. width: 10,
  182. max: 10,
  183. expand: true,
  184. fullCircle: true
  185. },
  186. data: {
  187. columns: [
  188. ['data', 8]
  189. ],
  190. type: 'gauge',
  191. fullCircle: true,
  192. startingAngle: Math.PI/2
  193. }
  194. };
  195. });
  196. it('should have correct d for 2 Pi radian gauge starting at Pi/2', function() {
  197. var chartArc = d3.select('.c3-chart-arcs'),
  198. data = chartArc.select('.c3-chart-arc.c3-target.c3-target-data')
  199. .select('g.c3-shapes.c3-shapes-data.c3-arcs.c3-arcs-data')
  200. .select('path.c3-shape.c3-shape.c3-arc.c3-arc-data');
  201. // This test has bee updated to make tests pass. @TODO double-check this test is accurate.
  202. expect(data.attr('d')).toMatch(/M-180.*?,-2\..+A180.*?,180.*?,0,1,1,-55.*?,171.*?L-52.*?,161.*?A170.*?,170.*?,0,1,0,-170.*?,-2.*?Z/);
  203. });
  204. describe('with labels use custom text', function() {
  205. beforeAll(function(){
  206. args = {
  207. gauge: {
  208. width: 10,
  209. max: 100,
  210. expand: true,
  211. label: {
  212. extents: function (value, isMax) {
  213. if (isMax) {
  214. return 'Max: ' + value + '%';
  215. }
  216. return 'Min: ' + value + '%';
  217. }
  218. }
  219. },
  220. data: {
  221. columns: [
  222. ['data', 8]
  223. ],
  224. type: 'gauge',
  225. fullCircle: true,
  226. startingAngle: Math.PI/2
  227. }
  228. };
  229. });
  230. it('should show custom min/max guage labels', function () {
  231. var chartArc = d3.select('.c3-chart-arcs'),
  232. min = chartArc.select('.c3-chart-arcs-gauge-min'),
  233. max = chartArc.select('.c3-chart-arcs-gauge-max');
  234. expect(min.text()).toMatch('Min: 0%');
  235. expect(max.text()).toMatch('Max: 100%');
  236. });
  237. });
  238. });
  239. describe('with more than one data_column ', function () {
  240. beforeAll(function () {
  241. args = {
  242. data: {
  243. columns: [
  244. ['padded1', 100],
  245. ['padded2', 90],
  246. ['padded3', 50],
  247. ['padded4', 20]
  248. ],
  249. type: 'gauge'
  250. },
  251. color: {
  252. pattern: ['#FF0000', '#F97600', '#F6C600', '#60B044'],
  253. threshold: {
  254. values: [30, 80, 95]
  255. }
  256. }
  257. };
  258. });
  259. var arcColor = ['rgb(96, 176, 68)', 'rgb(246, 198, 0)', 'rgb(249, 118, 0)', 'rgb(255, 0, 0)'];
  260. describe('should contain arcs ', function () {
  261. it('each data_column should have one arc', function () {
  262. chart.internal.main.selectAll('.c3-chart-arc .c3-arc').each(function (d, i) {
  263. expect(d3.select(this).classed('c3-arc-' + args.data.columns[i][0])).toBeTruthy();
  264. });
  265. });
  266. it('each arc should have the color from color_pattern if color_treshold is given ', function () {
  267. chart.internal.main.selectAll('.c3-chart-arc .c3-arc').each(function (d, i) {
  268. expect(d3.select(this).style('fill')).toBe(arcColor[i]);
  269. });
  270. });
  271. });
  272. describe('should contain backgrounds ', function () {
  273. it('each data_column should have one background', function () {
  274. chart.internal.main.selectAll('.c3-chart-arcs path.c3-chart-arcs-background').each(function (d, i) {
  275. expect(d3.select(this).classed('c3-chart-arcs-background-'+ i)).toBeTruthy();
  276. });
  277. });
  278. it('each background should have tbe same color', function () {
  279. chart.internal.main.selectAll('.c3-chart-arcs path.c3-chart-arcs-background').each(function () {
  280. expect(d3.select(this).style('fill')).toBe('rgb(224, 224, 224)');
  281. });
  282. });
  283. });
  284. describe('should contain labels', function () {
  285. it('each data_column should have a label', function () {
  286. chart.internal.main.selectAll('.c3-chart-arc .c3-gauge-value').each(function (d, i) {
  287. expect(d3.select(this).text()).toBe(chart.internal.defaultArcValueFormat(null, args.data.columns[i][1] / 100));
  288. });
  289. });
  290. it('each label should have the same color', function () {
  291. chart.internal.main.selectAll('.c3-chart-arc .c3-gauge-value').each(function () {
  292. expect(d3.select(this).style('fill')).toBe('rgb(0, 0, 0)');
  293. });
  294. });
  295. it('if only one data_column is visible the label should have "" for transform', function (done) {
  296. var textBeforeHide = chart.internal.main.select('.c3-chart-arc.c3-target.c3-target-padded4 text');
  297. expect(textBeforeHide.attr('transform')).not.toBe('');
  298. chart.hide(['padded1', 'padded2', 'padded3']);
  299. setTimeout(function () {
  300. var textAfterHide = chart.internal.main.select('.c3-chart-arc.c3-target.c3-target-padded4 text');
  301. expect(textAfterHide.attr('transform')).toBe('');
  302. done();
  303. }, 1000);
  304. });
  305. });
  306. describe('should contain labellines', function () {
  307. it('each data_column should have a labelline', function () {
  308. chart.internal.main.selectAll('.c3-chart-arc .c3-arc-label-line').each(function (d, i) {
  309. expect(d3.select(this).classed('c3-target-' + args.data.columns[i][0])).toBeTruthy();
  310. });
  311. });
  312. it('each labelline should have the color from color_pattern if color_treshold is given', function () {
  313. chart.internal.main.selectAll('.c3-chart-arc .c3-arc-label-line').each(function (d, i) {
  314. expect(d3.select(this).style('fill')).toBe(arcColor[i]);
  315. });
  316. });
  317. });
  318. });
  319. });
  320. });