Caption.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. topSuite('Ext.chart.Caption', ['Ext.chart.*', 'Ext.data.ArrayStore'], function() {
  2. var side = 400;
  3. var titleText = 'Title';
  4. var subtitleText = 'Subtitle';
  5. var creditsText = 'Credits';
  6. var titleConfig = {
  7. text: titleText,
  8. padding: 5
  9. };
  10. var subtitleConfig = {
  11. text: subtitleText,
  12. align: 'center'
  13. };
  14. var creditsConfig = {
  15. text: creditsText,
  16. docked: 'bottom'
  17. };
  18. var commonChartConfig = {
  19. renderTo: Ext.getBody(),
  20. width: side,
  21. height: side,
  22. legend: {
  23. type: 'dom',
  24. docked: 'bottom'
  25. },
  26. store: {
  27. data: [
  28. { x: 'A', y1: 2, y2: 6 },
  29. { x: 'B', y1: 3, y2: 5 },
  30. { x: 'C', y1: 1, y2: 7 }
  31. ]
  32. }
  33. };
  34. var cartesianChartConfig = Ext.merge(Ext.clone(commonChartConfig), {
  35. axes: [
  36. {
  37. type: 'category',
  38. position: 'bottom'
  39. },
  40. {
  41. type: 'numeric',
  42. position: 'left'
  43. }
  44. ],
  45. series: [
  46. {
  47. type: 'bar',
  48. stacked: false,
  49. xField: 'x',
  50. yField: ['y1', 'y2']
  51. }
  52. ]
  53. });
  54. var polarChartConfig = Ext.merge(Ext.clone(commonChartConfig), {
  55. axes: [
  56. {
  57. type: 'category',
  58. position: 'angular'
  59. },
  60. {
  61. type: 'numeric',
  62. position: 'radial'
  63. }
  64. ],
  65. series: [
  66. {
  67. type: 'radar',
  68. angleField: 'x',
  69. radiusField: 'y2',
  70. style: {
  71. fillOpacity: 0.6
  72. }
  73. },
  74. {
  75. type: 'radar',
  76. angleField: 'x',
  77. radiusField: 'y1',
  78. style: {
  79. fillOpacity: 0.6
  80. }
  81. }
  82. ]
  83. });
  84. var defaultCaptions = {
  85. title: titleConfig,
  86. subtitle: subtitleConfig,
  87. credits: creditsConfig
  88. };
  89. describe('cartesian layout', function () {
  90. var chart;
  91. afterEach(function () {
  92. chart = Ext.destroy(chart);
  93. });
  94. it("should have default captions properly positioned and chart surface properly sized", function () {
  95. var layoutDone;
  96. var config = Ext.merge(Ext.clone(cartesianChartConfig), {
  97. captions: Ext.clone(defaultCaptions),
  98. listeners: {
  99. layout: function () {
  100. layoutDone = true;
  101. }
  102. }
  103. });
  104. runs(function () {
  105. chart = new Ext.chart.CartesianChart(config);
  106. });
  107. waitsFor(function () {
  108. return layoutDone;
  109. });
  110. runs(function () {
  111. layoutDone = false;
  112. var captions = chart.getCaptions();
  113. var title = captions.title;
  114. var subtitle = captions.subtitle;
  115. var credits = captions.credits;
  116. var chartSurface = chart.getSurface('chart');
  117. var captionSurface = chart.getSurface(title.surfaceName);
  118. var chartSurfaceRect = chartSurface.getRect();
  119. var captionSurfaceRect = captionSurface.getRect();
  120. var titleBbox = title.getSprite().getBBox();
  121. var subtitleBbox = subtitle.getSprite().getBBox();
  122. var creditsBBox = credits.getSprite().getBBox();
  123. expect(titleBbox.y >= 0).toBe(true);
  124. expect(subtitleBbox.y >= (titleBbox.y + titleBbox.height)).toBe(true);
  125. expect(chartSurfaceRect[1] >= (subtitleBbox.y + subtitleBbox.height)).toBe(true);
  126. expect(chartSurfaceRect[3] <= (captionSurfaceRect[3] - (subtitleBbox.y + subtitleBbox.height) - credits.getRect()[3])).toBe(true);
  127. expect(creditsBBox.x + creditsBBox.height <= captionSurfaceRect[3]).toBe(true);
  128. });
  129. });
  130. it("should have weighted captions properly positioned and chart surface properly sized", function () {
  131. var layoutDone;
  132. var config = Ext.merge(Ext.clone(cartesianChartConfig), {
  133. captions: {
  134. title: Ext.merge(Ext.clone(titleConfig), {
  135. docked: 'top',
  136. weight: 1
  137. }),
  138. subtitle: Ext.merge(Ext.clone(subtitleConfig), {
  139. docked: 'top',
  140. weight: 0
  141. }),
  142. credits: Ext.merge(Ext.clone(creditsConfig), {
  143. docked: 'top',
  144. weight: 2
  145. })
  146. },
  147. listeners: {
  148. layout: function () {
  149. layoutDone = true;
  150. }
  151. }
  152. });
  153. runs(function () {
  154. chart = new Ext.chart.CartesianChart(config);
  155. });
  156. waitsFor(function () {
  157. return layoutDone;
  158. });
  159. runs(function () {
  160. layoutDone = false;
  161. var captions = chart.getCaptions();
  162. var title = captions.title;
  163. var subtitle = captions.subtitle;
  164. var credits = captions.credits;
  165. var chartSurface = chart.getSurface('chart');
  166. var captionSurface = chart.getSurface(title.surfaceName);
  167. var chartSurfaceRect = chartSurface.getRect();
  168. var captionSurfaceRect = captionSurface.getRect();
  169. var titleBbox = title.getSprite().getBBox();
  170. var subtitleBbox = subtitle.getSprite().getBBox();
  171. var creditsBBox = credits.getSprite().getBBox();
  172. expect(subtitleBbox.y >= 0).toBe(true);
  173. expect(titleBbox.y >= (subtitleBbox.y + subtitleBbox.height)).toBe(true);
  174. expect(creditsBBox.y >= (titleBbox.y + titleBbox.height)).toBe(true);
  175. expect(chartSurfaceRect[1] >= (creditsBBox.y + creditsBBox.height)).toBe(true);
  176. expect(chartSurfaceRect[3] <= (captionSurfaceRect[3] - (creditsBBox.y + creditsBBox.height))).toBe(true);
  177. });
  178. });
  179. it("should have style config values proxied to sprite attributes", function () {
  180. var layoutDone;
  181. var config = Ext.merge(Ext.clone(cartesianChartConfig), {
  182. captions: {
  183. title: Ext.merge(Ext.clone(titleConfig), {
  184. style: {
  185. fontSize: 8,
  186. fontWeight: 'lighter',
  187. fontFamily: 'Verdana'
  188. }
  189. }),
  190. subtitle: Ext.merge(Ext.clone(subtitleConfig), {
  191. style: {
  192. fontSize: 14,
  193. fontWeight: 'bold'
  194. }
  195. }),
  196. credits: Ext.merge(Ext.clone(creditsConfig), {
  197. style: {
  198. fontSize: 20,
  199. fontWeight: '300'
  200. }
  201. })
  202. },
  203. listeners: {
  204. layout: function () {
  205. layoutDone = true;
  206. }
  207. }
  208. });
  209. runs(function () {
  210. chart = new Ext.chart.CartesianChart(config);
  211. });
  212. waitsFor(function () {
  213. return layoutDone;
  214. });
  215. runs(function () {
  216. layoutDone = false;
  217. var captions = chart.getCaptions();
  218. var title = captions.title;
  219. var subtitle = captions.subtitle;
  220. var credits = captions.credits;
  221. var titleSprite = title.getSprite();
  222. var subtitleSprite = subtitle.getSprite();
  223. var creditsSprite = credits.getSprite();
  224. expect(titleSprite.attr.text).toBe(titleText);
  225. expect(subtitleSprite.attr.text).toBe(subtitleText);
  226. expect(creditsSprite.attr.text).toBe(creditsText);
  227. expect(titleSprite.attr.fontSize).toBe('8px');
  228. expect(titleSprite.attr.fontWeight).toBe('lighter');
  229. expect(titleSprite.attr.fontFamily).toBe('Verdana');
  230. expect(subtitleSprite.attr.fontSize).toBe('14px');
  231. expect(subtitleSprite.attr.fontWeight).toBe('bold');
  232. expect(creditsSprite.attr.fontSize).toBe('20px');
  233. expect(creditsSprite.attr.fontWeight).toBe('300');
  234. });
  235. });
  236. it("should align properly", function () {
  237. var layoutDone;
  238. var config = Ext.merge(Ext.clone(cartesianChartConfig), {
  239. captions: {
  240. title: Ext.merge(Ext.clone(titleConfig), {
  241. align: 'center'
  242. }),
  243. subtitle: Ext.merge(Ext.clone(subtitleConfig), {
  244. align: 'left'
  245. }),
  246. credits: Ext.merge(Ext.clone(creditsConfig), {
  247. align: 'right'
  248. })
  249. },
  250. listeners: {
  251. layout: function () {
  252. layoutDone = true;
  253. }
  254. }
  255. });
  256. runs(function () {
  257. chart = new Ext.chart.CartesianChart(config);
  258. });
  259. waitsFor(function () {
  260. return layoutDone;
  261. });
  262. runs(function () {
  263. layoutDone = false;
  264. var captions = chart.getCaptions();
  265. var title = captions.title;
  266. var subtitle = captions.subtitle;
  267. var credits = captions.credits;
  268. var seriesSurface = chart.getSurface('series');
  269. var seriesSurfaceRect = seriesSurface.getRect();
  270. var titleBbox = title.getSprite().getBBox();
  271. var subtitleBbox = subtitle.getSprite().getBBox();
  272. var creditsBBox = credits.getSprite().getBBox();
  273. var tolerance = 2; // in pixels
  274. expect( Ext.Number.isEqual(titleBbox.x, seriesSurfaceRect[0] + seriesSurfaceRect[2] / 2 - titleBbox.width / 2, tolerance) ).toBe(true);
  275. expect( Ext.Number.isEqual(subtitleBbox.x, seriesSurfaceRect[0], tolerance) ).toBe(true);
  276. expect( Ext.Number.isEqual(creditsBBox.x + creditsBBox.width, seriesSurfaceRect[0] + seriesSurfaceRect[2], tolerance) ).toBe(true);
  277. });
  278. });
  279. it("should align to chart properly", function () {
  280. var layoutDone;
  281. var config = Ext.merge(Ext.clone(cartesianChartConfig), {
  282. captions: {
  283. title: Ext.merge(Ext.clone(titleConfig), {
  284. align: 'center',
  285. alignTo: 'chart'
  286. }),
  287. subtitle: Ext.merge(Ext.clone(subtitleConfig), {
  288. align: 'left',
  289. alignTo: 'chart'
  290. }),
  291. credits: Ext.merge(Ext.clone(creditsConfig), {
  292. align: 'right',
  293. alignTo: 'chart'
  294. })
  295. },
  296. listeners: {
  297. layout: function () {
  298. layoutDone = true;
  299. }
  300. }
  301. });
  302. runs(function () {
  303. chart = new Ext.chart.CartesianChart(config);
  304. });
  305. waitsFor(function () {
  306. return layoutDone;
  307. });
  308. runs(function () {
  309. layoutDone = false;
  310. var captions = chart.getCaptions();
  311. var title = captions.title;
  312. var subtitle = captions.subtitle;
  313. var credits = captions.credits;
  314. var seriesSurface = chart.getSurface('series');
  315. var chartSurface = chart.getSurface('chart');
  316. var chartSurfaceRect = chartSurface.getRect();
  317. var titleBbox = title.getSprite().getBBox();
  318. var subtitleBbox = subtitle.getSprite().getBBox();
  319. var creditsBBox = credits.getSprite().getBBox();
  320. var tolerance = 2; // in pixels
  321. expect( Ext.Number.isEqual(titleBbox.x, chartSurfaceRect[0] + chartSurfaceRect[2] / 2 - titleBbox.width / 2, tolerance) ).toBe(true);
  322. expect( Ext.Number.isEqual(subtitleBbox.x, chartSurfaceRect[0], tolerance) ).toBe(true);
  323. expect( Ext.Number.isEqual(creditsBBox.x + creditsBBox.width, chartSurfaceRect[0] + chartSurfaceRect[2], tolerance) ).toBe(true);
  324. });
  325. });
  326. });
  327. });