barchart.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: cogadmin
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2010
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. //
  9. //
  10. // Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  11. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  12. function bar_chart(chartdef) {
  13. this.chartdef = chartdef;
  14. this.chart = null;
  15. if (chartdef.columns != '') {
  16. this.columnCount = chartdef.columns;
  17. }
  18. else {
  19. // default to 3 columns which is what's shown in liz.
  20. this.columnCount = 3;
  21. }
  22. this.maxValue = 0;
  23. this.total = 0;
  24. for (var i=0; i < this.chartdef.rows.length; i++) {
  25. if (parseInt(this.chartdef.rows[i].value) > this.maxValue) {
  26. this.maxValue = parseInt(this.chartdef.rows[i].value);
  27. }
  28. this.total += new Number(this.chartdef.rows[i].value);
  29. }
  30. // Always want ticks to be a multiple of 5
  31. this.axisMultiplier = 5;
  32. // All of this to make sure the maximum y value is divisible by axisMultiplier (5) and columnCount.
  33. // Make sure we get astheticly pleasing numbers for the ticks.
  34. var axisRangeMultiplier = this.columnCount * this.axisMultiplier;
  35. var maxAxisValue = Math.ceil(this.maxValue / axisRangeMultiplier) * axisRangeMultiplier;
  36. // Each tick will be a multiple of this value
  37. this.axisTickInterval = Math.ceil(maxAxisValue / this.columnCount);
  38. }
  39. bar_chart.prototype = {
  40. buildChart: function() {
  41. var table = document.createElement('table');
  42. table.cellSpacing="0";
  43. table.cellPadding="0";
  44. table.border="0";
  45. if (this.chartdef.width) {
  46. table.style.width = this.chartdef.width;
  47. }
  48. tbody = document.createElement('tbody');
  49. table.appendChild(tbody);
  50. // title
  51. if (this.chartdef.title != '') {
  52. var tr = document.createElement('tr');
  53. // empty td for the labels
  54. tr.appendChild(document.createElement('td'));
  55. var td = document.createElement('td');
  56. td.className = 'tableText';
  57. td.setAttribute("colSpan",this.columnCount);
  58. td.style.textAlign = 'center';
  59. td.style.paddingBottom = '15px';
  60. td.style.fontWeight = 'bold';
  61. td.style.width = '100%';
  62. td.appendChild(document.createTextNode(this.chartdef.title + ' (' + this.total + ')'));
  63. tr.appendChild(td);
  64. tbody.appendChild(tr);
  65. }
  66. var columnWidth = Math.ceil(80 / this.columnCount);
  67. // build up the rows
  68. for (var row=0; row < this.chartdef.rows.length; row++) {
  69. var paddingTop = '2px';
  70. var paddingBottom = '2px';
  71. // label column
  72. var tr = document.createElement('tr');
  73. var td = tr.appendChild( document.createElement('td') );
  74. td.width = "20%";
  75. td.className = "tableText";
  76. td.style.textAlign = 'right';
  77. td.style.paddingRight = '5px';
  78. td.style.borderRight = '1px solid #000000';
  79. td.style.paddingTop = paddingTop;
  80. td.style.paddingBottom = paddingBottom;
  81. td.appendChild(document.createTextNode(this.chartdef.rows[row].label));
  82. for (var column=0; column < this.columnCount; column++) {
  83. var td = tr.appendChild( document.createElement('td') );
  84. td.setAttribute("id",this.chartdef.unique_id + row + column);
  85. td.setAttribute("nowrap","nowrap");
  86. td.style.width = columnWidth + "%";
  87. td.style.emptyCells = 'show';
  88. td.style.height = '21px';
  89. td.style.borderRight = '1px';
  90. td.style.borderColor = '#999999';
  91. td.style.borderRightStyle = 'dotted';
  92. td.style.paddingTop = paddingTop;
  93. td.style.paddingBottom = paddingBottom;
  94. td.style.verticalAlign = 'top';
  95. // if we're on the last row, need some extra styles
  96. if ((row + 1) == this.chartdef.rows.length) {
  97. td.style.borderBottom = '1px solid #000000';
  98. }
  99. if (column == 0) {
  100. var div = td.appendChild(document.createElement('div'))
  101. div.setAttribute("nowrap","nowrap");
  102. div.id = this.chartdef.unique_id + '_div_' + row;
  103. div.style.position = 'absolute';
  104. div.style.width = '500px';
  105. div.style.height = '17px';
  106. var table_bar = div.appendChild(document.createElement('table'));
  107. table_bar.cellSpacing="0";
  108. table_bar.cellPadding="0";
  109. table_bar.style.position = 'relative';
  110. table_bar.border="0";
  111. var tbody_bar = table_bar.appendChild(document.createElement('tbody'));
  112. var tr_bar = tbody_bar.appendChild(document.createElement('tr'));
  113. var td_bar = tr_bar.appendChild(document.createElement('td'));
  114. td_bar.id = this.chartdef.unique_id + '_td_' + row;
  115. td_bar.style.height = '17px';
  116. td_bar.style.backgroundImage = 'url(' + this.chartdef.bar_image + ')';
  117. var td_bar_value = tr_bar.appendChild(document.createElement('td'));
  118. if (this.chartdef.rows[row].value != '') {
  119. td_bar_value.appendChild(document.createTextNode(this.chartdef.rows[row].value));
  120. }
  121. else {
  122. td_bar_value.appendChild(document.createTextNode('0'));
  123. }
  124. td_bar_value.style.fontFamily = 'tahoma';
  125. td_bar_value.style.fontWeight = 'bold';
  126. td_bar_value.style.color = '#0000CC';
  127. td_bar_value.style.textAlign = 'left';
  128. td_bar_value.style.paddingLeft = '5px';
  129. }
  130. td.appendChild(document.createTextNode(' '));
  131. tr.appendChild(td);
  132. }
  133. tbody.appendChild(tr);
  134. }
  135. // row to show the x axis values
  136. var tr = document.createElement('tr');
  137. for (var column=-1; column < this.columnCount; column++) {
  138. var td = tr.appendChild( document.createElement('td') );
  139. td.setAttribute("nowrap","nowrap");
  140. td.className = 'dialogHeaderText';
  141. td.style.textAlign = 'right';
  142. var span = td.appendChild( document.createElement('span') );
  143. span.className = 'inactiveText';
  144. // when all the bars are zero, just put 1,2,3,... for the y axis values
  145. if (this.axisTickInterval == 0 ) {
  146. span.appendChild(document.createTextNode((column+1) * this.axisMultiplier));
  147. }
  148. else {
  149. span.appendChild(document.createTextNode(this.axisTickInterval * (column + 1)));
  150. }
  151. tr.appendChild(td);
  152. }
  153. tbody.appendChild(tr);
  154. $(this.chartdef.div_id).appendChild(table);
  155. this.chart = $(this.chartdef.div_id);
  156. },
  157. resizeBars: function() {
  158. // when a fragment that already had a graph gets refreshed, the onrezie even fires before we've
  159. // rebuilt the graph. Catch it here before we do any farther.
  160. if (!$(this.chartdef.unique_id + '_div_0')) {
  161. return;
  162. }
  163. var totalWidth = 0;
  164. var xAxisLimit = this.axisTickInterval * this.columnCount;
  165. // if xAxisLimit is 0 then return. This can happen if all the bars have a value of 0
  166. if (xAxisLimit == 0) {
  167. return;
  168. }
  169. // calculate the total width of the columns
  170. for (var column=0; column < this.columnCount; column++) {
  171. totalWidth += xWidth($(this.chartdef.unique_id + '0' + column));
  172. }
  173. // only resize the bars if we have a width. If the width is zero then it
  174. // means our div is hidden so there's no reason to resize the bars. Also
  175. // resizing the bars when we're hidden was causing a firefox only bug
  176. if (totalWidth > 0) {
  177. // resize the bars
  178. for (var row=0; row < this.chartdef.rows.length; row++) {
  179. // make sure the absolute div is wide enough for the table inside it (IE fix)
  180. $(this.chartdef.unique_id + '_div_' + row).style.width = totalWidth;
  181. var rowValue = this.chartdef.rows[row].value;
  182. var barDiv = $(this.chartdef.unique_id + '_td_' + row);
  183. var barLength = new Number( (rowValue * totalWidth ) / xAxisLimit).toFixed();
  184. barDiv.style.width = barLength + 'px';
  185. }
  186. }
  187. },
  188. show: function() {
  189. if (!this.chart) {
  190. this.buildChart();
  191. }
  192. $(this.chartdef.div_id).style.display = 'block';
  193. this.resizeBars();
  194. }
  195. }