Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

361 lines
9.5KB

  1. // Test the category scale
  2. describe('Category scale tests', function() {
  3. it('Should register the constructor with the scale service', function() {
  4. var Constructor = Chart.scaleService.getScaleConstructor('category');
  5. expect(Constructor).not.toBe(undefined);
  6. expect(typeof Constructor).toBe('function');
  7. });
  8. it('Should have the correct default config', function() {
  9. var defaultConfig = Chart.scaleService.getScaleDefaults('category');
  10. expect(defaultConfig).toEqual({
  11. display: true,
  12. gridLines: {
  13. color: "rgba(0, 0, 0, 0.1)",
  14. drawBorder: true,
  15. drawOnChartArea: true,
  16. drawTicks: true, // draw ticks extending towards the label
  17. tickMarkLength: 10,
  18. lineWidth: 1,
  19. offsetGridLines: false,
  20. display: true,
  21. zeroLineColor: "rgba(0,0,0,0.25)",
  22. zeroLineWidth: 1
  23. },
  24. position: "bottom",
  25. scaleLabel: {
  26. labelString: '',
  27. display: false
  28. },
  29. ticks: {
  30. beginAtZero: false,
  31. minRotation: 0,
  32. maxRotation: 50,
  33. mirror: false,
  34. padding: 10,
  35. reverse: false,
  36. display: true,
  37. callback: defaultConfig.ticks.callback, // make this nicer, then check explicitly below
  38. autoSkip: true,
  39. autoSkipPadding: 0,
  40. labelOffset: 0
  41. }
  42. });
  43. // Is this actually a function
  44. expect(defaultConfig.ticks.callback).toEqual(jasmine.any(Function));
  45. });
  46. it('Should generate ticks from the data labales', function() {
  47. var scaleID = 'myScale';
  48. var mockData = {
  49. datasets: [{
  50. yAxisID: scaleID,
  51. data: [10, 5, 0, 25, 78]
  52. }],
  53. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
  54. };
  55. var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category'));
  56. var Constructor = Chart.scaleService.getScaleConstructor('category');
  57. var scale = new Constructor({
  58. ctx: {},
  59. options: config,
  60. chart: {
  61. data: mockData
  62. },
  63. id: scaleID
  64. });
  65. scale.determineDataLimits();
  66. scale.buildTicks();
  67. expect(scale.ticks).toEqual(mockData.labels);
  68. });
  69. it ('should get the correct label for the index', function() {
  70. var scaleID = 'myScale';
  71. var mockData = {
  72. datasets: [{
  73. yAxisID: scaleID,
  74. data: [10, 5, 0, 25, 78]
  75. }],
  76. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
  77. };
  78. var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category'));
  79. var Constructor = Chart.scaleService.getScaleConstructor('category');
  80. var scale = new Constructor({
  81. ctx: {},
  82. options: config,
  83. chart: {
  84. data: mockData
  85. },
  86. id: scaleID
  87. });
  88. scale.determineDataLimits();
  89. scale.buildTicks();
  90. expect(scale.getLabelForIndex(1)).toBe('tick2');
  91. });
  92. it ('Should get the correct pixel for a value when horizontal', function() {
  93. var scaleID = 'myScale';
  94. var mockData = {
  95. datasets: [{
  96. yAxisID: scaleID,
  97. data: [10, 5, 0, 25, 78]
  98. }],
  99. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick_last']
  100. };
  101. var mockContext = window.createMockContext();
  102. var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category'));
  103. config.gridLines.offsetGridLines = true;
  104. var Constructor = Chart.scaleService.getScaleConstructor('category');
  105. var scale = new Constructor({
  106. ctx: mockContext,
  107. options: config,
  108. chart: {
  109. data: mockData
  110. },
  111. id: scaleID
  112. });
  113. var minSize = scale.update(600, 100);
  114. expect(scale.width).toBe(600);
  115. expect(scale.height).toBe(28);
  116. expect(scale.paddingTop).toBe(0);
  117. expect(scale.paddingBottom).toBe(0);
  118. expect(scale.paddingLeft).toBe(28);
  119. expect(scale.paddingRight).toBe(48);
  120. expect(scale.labelRotation).toBe(0);
  121. expect(minSize).toEqual({
  122. width: 600,
  123. height: 28,
  124. });
  125. scale.left = 5;
  126. scale.top = 5;
  127. scale.right = 605;
  128. scale.bottom = 33;
  129. expect(scale.getPixelForValue(0, 0, 0, false)).toBe(33);
  130. expect(scale.getPixelForValue(0, 0, 0, true)).toBe(85);
  131. expect(scale.getValueForPixel(33)).toBe(0);
  132. expect(scale.getValueForPixel(85)).toBe(0);
  133. expect(scale.getPixelForValue(0, 4, 0, false)).toBe(452);
  134. expect(scale.getPixelForValue(0, 4, 0, true)).toBe(505);
  135. expect(scale.getValueForPixel(452)).toBe(4);
  136. expect(scale.getValueForPixel(505)).toBe(4);
  137. config.gridLines.offsetGridLines = false;
  138. expect(scale.getPixelForValue(0, 0, 0, false)).toBe(33);
  139. expect(scale.getPixelForValue(0, 0, 0, true)).toBe(33);
  140. expect(scale.getValueForPixel(33)).toBe(0);
  141. expect(scale.getPixelForValue(0, 4, 0, false)).toBe(557);
  142. expect(scale.getPixelForValue(0, 4, 0, true)).toBe(557);
  143. expect(scale.getValueForPixel(557)).toBe(4);
  144. });
  145. it ('Should get the correct pixel for a value when horizontal and zoomed', function() {
  146. var scaleID = 'myScale';
  147. var mockData = {
  148. datasets: [{
  149. yAxisID: scaleID,
  150. data: [10, 5, 0, 25, 78]
  151. }],
  152. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick_last']
  153. };
  154. var mockContext = window.createMockContext();
  155. var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category'));
  156. config.gridLines.offsetGridLines = true;
  157. config.ticks.min = "tick2";
  158. config.ticks.max = "tick4";
  159. var Constructor = Chart.scaleService.getScaleConstructor('category');
  160. var scale = new Constructor({
  161. ctx: mockContext,
  162. options: config,
  163. chart: {
  164. data: mockData
  165. },
  166. id: scaleID
  167. });
  168. var minSize = scale.update(600, 100);
  169. expect(scale.width).toBe(600);
  170. expect(scale.height).toBe(28);
  171. expect(scale.paddingTop).toBe(0);
  172. expect(scale.paddingBottom).toBe(0);
  173. expect(scale.paddingLeft).toBe(28);
  174. expect(scale.paddingRight).toBe(28);
  175. expect(scale.labelRotation).toBe(0);
  176. expect(minSize).toEqual({
  177. width: 600,
  178. height: 28,
  179. });
  180. scale.left = 5;
  181. scale.top = 5;
  182. scale.right = 605;
  183. scale.bottom = 33;
  184. expect(scale.getPixelForValue(0, 1, 0, false)).toBe(33);
  185. expect(scale.getPixelForValue(0, 1, 0, true)).toBe(124);
  186. expect(scale.getPixelForValue(0, 3, 0, false)).toBe(396);
  187. expect(scale.getPixelForValue(0, 3, 0, true)).toBe(486);
  188. config.gridLines.offsetGridLines = false;
  189. expect(scale.getPixelForValue(0, 1, 0, false)).toBe(33);
  190. expect(scale.getPixelForValue(0, 1, 0, true)).toBe(33);
  191. expect(scale.getPixelForValue(0, 3, 0, false)).toBe(577);
  192. expect(scale.getPixelForValue(0, 3, 0, true)).toBe(577);
  193. });
  194. it ('should get the correct pixel for a value when vertical', function() {
  195. var scaleID = 'myScale';
  196. var mockData = {
  197. datasets: [{
  198. yAxisID: scaleID,
  199. data: [10, 5, 0, 25, 78]
  200. }],
  201. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick_last']
  202. };
  203. var mockContext = window.createMockContext();
  204. var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category'));
  205. config.gridLines.offsetGridLines = true;
  206. config.position = "left";
  207. var Constructor = Chart.scaleService.getScaleConstructor('category');
  208. var scale = new Constructor({
  209. ctx: mockContext,
  210. options: config,
  211. chart: {
  212. data: mockData
  213. },
  214. id: scaleID
  215. });
  216. var minSize = scale.update(100, 200);
  217. expect(scale.width).toBe(100);
  218. expect(scale.height).toBe(200);
  219. expect(scale.paddingTop).toBe(6);
  220. expect(scale.paddingBottom).toBe(6);
  221. expect(scale.paddingLeft).toBe(0);
  222. expect(scale.paddingRight).toBe(0);
  223. expect(scale.labelRotation).toBe(0);
  224. expect(minSize).toEqual({
  225. width: 100,
  226. height: 200,
  227. });
  228. scale.left = 5;
  229. scale.top = 5;
  230. scale.right = 105;
  231. scale.bottom = 205;
  232. expect(scale.getPixelForValue(0, 0, 0, false)).toBe(11);
  233. expect(scale.getPixelForValue(0, 0, 0, true)).toBe(30);
  234. expect(scale.getValueForPixel(11)).toBe(0);
  235. expect(scale.getValueForPixel(30)).toBe(0);
  236. expect(scale.getPixelForValue(0, 4, 0, false)).toBe(161);
  237. expect(scale.getPixelForValue(0, 4, 0, true)).toBe(180);
  238. expect(scale.getValueForPixel(161)).toBe(4);
  239. config.gridLines.offsetGridLines = false;
  240. expect(scale.getPixelForValue(0, 0, 0, false)).toBe(11);
  241. expect(scale.getPixelForValue(0, 0, 0, true)).toBe(11);
  242. expect(scale.getValueForPixel(11)).toBe(0);
  243. expect(scale.getPixelForValue(0, 4, 0, false)).toBe(199);
  244. expect(scale.getPixelForValue(0, 4, 0, true)).toBe(199);
  245. expect(scale.getValueForPixel(199)).toBe(4);
  246. });
  247. it ('should get the correct pixel for a value when vertical and zoomed', function() {
  248. var scaleID = 'myScale';
  249. var mockData = {
  250. datasets: [{
  251. yAxisID: scaleID,
  252. data: [10, 5, 0, 25, 78]
  253. }],
  254. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick_last']
  255. };
  256. var mockContext = window.createMockContext();
  257. var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category'));
  258. config.gridLines.offsetGridLines = true;
  259. config.ticks.min = "tick2";
  260. config.ticks.max = "tick4";
  261. config.position = "left";
  262. var Constructor = Chart.scaleService.getScaleConstructor('category');
  263. var scale = new Constructor({
  264. ctx: mockContext,
  265. options: config,
  266. chart: {
  267. data: mockData
  268. },
  269. id: scaleID
  270. });
  271. var minSize = scale.update(100, 200);
  272. expect(scale.width).toBe(70);
  273. expect(scale.height).toBe(200);
  274. expect(scale.paddingTop).toBe(6);
  275. expect(scale.paddingBottom).toBe(6);
  276. expect(scale.paddingLeft).toBe(0);
  277. expect(scale.paddingRight).toBe(0);
  278. expect(scale.labelRotation).toBe(0);
  279. expect(minSize).toEqual({
  280. width: 70,
  281. height: 200,
  282. });
  283. scale.left = 5;
  284. scale.top = 5;
  285. scale.right = 75;
  286. scale.bottom = 205;
  287. expect(scale.getPixelForValue(0, 1, 0, false)).toBe(11);
  288. expect(scale.getPixelForValue(0, 1, 0, true)).toBe(42);
  289. expect(scale.getPixelForValue(0, 3, 0, false)).toBe(136);
  290. expect(scale.getPixelForValue(0, 3, 0, true)).toBe(168);
  291. config.gridLines.offsetGridLines = false;
  292. expect(scale.getPixelForValue(0, 1, 0, false)).toBe(11);
  293. expect(scale.getPixelForValue(0, 1, 0, true)).toBe(11);
  294. expect(scale.getPixelForValue(0, 3, 0, false)).toBe(199);
  295. expect(scale.getPixelForValue(0, 3, 0, true)).toBe(199);
  296. });
  297. });