GoogleChart.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. define( ["https://www.gstatic.com/charts/loader.js"], function() {
  2. "use strict";
  3. function Control()
  4. {
  5. };
  6. Control.prototype.initialize = function( oControlHost, fnDoneInitializing )
  7. {
  8. this.m_oControlHost = oControlHost;
  9. this.m_sChartType = this.getConfigurationValue( "Chart type", "bar" );
  10. fnDoneInitializing();
  11. };
  12. Control.prototype.destroy = function( oControlHost )
  13. {
  14. this.m_oControlHost = null;
  15. };
  16. Control.prototype.getConfigurationValue = function( sName, sDefaultValue )
  17. {
  18. var o = this.m_oControlHost.configuration;
  19. return ( o && ( o[sName] !== undefined ) ) ? o[sName] : sDefaultValue;
  20. };
  21. Control.prototype.draw = function( oControlHost )
  22. {
  23. var oPackages = ( this.m_sChartType == "orgchart" ) ? ["orgchart"] : ["corechart", "bar"];
  24. google.charts.load( "current", { packages : oPackages } );
  25. google.charts.setOnLoadCallback( this.drawChart.bind( this ) );
  26. };
  27. Control.prototype.drawChart = function()
  28. {
  29. switch ( this.m_sChartType )
  30. {
  31. case "bar":
  32. this.drawBarChart();
  33. break;
  34. case "orgchart":
  35. this.drawOrgChart();
  36. break;
  37. }
  38. };
  39. Control.prototype.drawBarChart = function()
  40. {
  41. this.m_oGoogleDataTable = this.createPivotedGoogleDataTableFromDataStore( this.m_oDataStore );
  42. var oChart = new google.charts.Bar( this.m_oControlHost.container );
  43. var oOptions =
  44. {
  45. width : this.getConfigurationValue( "Width", 500 ),
  46. height : this.getConfigurationValue( "Height", 350 ),
  47. bars : this.getConfigurationValue( "Horizontal", true ) ? "horizontal" : "vertical"
  48. };
  49. oChart.draw( this.m_oGoogleDataTable, oOptions );
  50. };
  51. Control.prototype.drawOrgChart = function()
  52. {
  53. this.m_oGoogleDataTable = this.createGoogleDataTableFromDataStore( this.m_oDataStore );
  54. if ( this.m_oDataStore.columnCount == 3 )
  55. {
  56. this.insertOrgChartColumnIntoDataTable( this.m_oDataStore, 2, this.m_oGoogleDataTable );
  57. }
  58. var oChart = new google.visualization.OrgChart( this.m_oControlHost.container );
  59. var oOptions =
  60. {
  61. allowHtml : true,
  62. color : "#E8E9EA",
  63. size : "large"
  64. };
  65. oChart.draw( this.m_oGoogleDataTable, oOptions );
  66. };
  67. Control.prototype.setData = function( oControlHost, oDataStore )
  68. {
  69. this.m_oDataStore = oDataStore;
  70. };
  71. Control.prototype.insertOrgChartColumnIntoDataTable = function( oDataStore, iColumnToInsert, oDataTable )
  72. {
  73. var iColCount = oDataTable.getNumberOfColumns();
  74. var iRowCount = oDataTable.getNumberOfRows();
  75. for ( var iRow = 0; iRow < iRowCount; iRow++ )
  76. {
  77. for ( var iCol = 0; iCol < iColCount; iCol++ )
  78. {
  79. var sValue = oDataTable.getValue( iRow, iCol );
  80. var sHtml = '<div style="color:#4178BE; font-size:10pt;">' + this.HTMLEncode( oDataStore.getCellValue( iRow, iColumnToInsert ) ) + '</div>';
  81. oDataTable.setFormattedValue( iRow, iCol, sValue + sHtml );
  82. }
  83. }
  84. };
  85. Control.prototype.createGoogleDataTableFromDataStore = function( oDataStore )
  86. {
  87. var iColCount = oDataStore.columnCount;
  88. var iRowCount = oDataStore.rowCount;
  89. var oDataTable = new google.visualization.DataTable();
  90. for ( var iCol = 0; iCol < iColCount; iCol++ )
  91. {
  92. oDataTable.addColumn( oDataStore.dataTypes[iCol], oDataStore.columnNames[iCol] );
  93. }
  94. oDataTable.addRows( iRowCount );
  95. for ( var iRow = 0; iRow < iRowCount; iRow++ )
  96. {
  97. for ( var iCol = 0; iCol < iColCount; iCol++ )
  98. {
  99. oDataTable.setCell( iRow, iCol, oDataStore.getCellValue( iRow, iCol ) );
  100. }
  101. }
  102. return oDataTable;
  103. };
  104. Control.prototype.createPivotedGoogleDataTableFromDataStore = function( oDataStore, iDsCategoriesColumn, iDsSeriesColumn, iDsValuesColumn )
  105. {
  106. if ( iDsCategoriesColumn === undefined )
  107. {
  108. iDsCategoriesColumn = 0;
  109. }
  110. if ( iDsSeriesColumn === undefined )
  111. {
  112. iDsSeriesColumn = ( oDataStore.columnCount > 2 ) ? oDataStore.columnCount - 2 : NaN;
  113. }
  114. if ( iDsValuesColumn === undefined )
  115. {
  116. iDsValuesColumn = oDataStore.columnCount - 1;
  117. }
  118. var aCategoriesValues = oDataStore.columnValues[iDsCategoriesColumn];
  119. var aSeriesValues = isNaN( iDsSeriesColumn ) ? null : oDataStore.columnValues[iDsSeriesColumn];
  120. // Sort categories
  121. var iCategoriesValuesLength = aCategoriesValues.length;
  122. var aSortedCategoryNumbers = new Array( iCategoriesValuesLength );
  123. for ( var i = 0; i < iCategoriesValuesLength; i++ )
  124. {
  125. aSortedCategoryNumbers[i] = i;
  126. }
  127. aSortedCategoryNumbers.sort( this.getFnSortCategoryByString( true, aCategoriesValues ) );
  128. var v_aSortedCategoryLookup = [];
  129. for ( var i = 0; i < iCategoriesValuesLength; i++ )
  130. {
  131. v_aSortedCategoryLookup[aSortedCategoryNumbers[i]] = i;
  132. }
  133. // Sort series
  134. if ( aSeriesValues )
  135. {
  136. var iSeriesValuesLength = aSeriesValues.length;
  137. var aSortedSeriesNumbers = new Array( iSeriesValuesLength );
  138. for ( var i = 0; i < iSeriesValuesLength; i++ )
  139. {
  140. aSortedSeriesNumbers[i] = i;
  141. }
  142. aSortedSeriesNumbers.sort( this.getFnSortCategoryByString( true, aSeriesValues ) );
  143. var v_aSortedSeriesLookup = [];
  144. for ( var i = 0; i < iSeriesValuesLength; i++ )
  145. {
  146. v_aSortedSeriesLookup[aSortedSeriesNumbers[i]] = i;
  147. }
  148. }
  149. var oDataTable = new google.visualization.DataTable();
  150. oDataTable.addRows( aCategoriesValues.length );
  151. // Add category column
  152. oDataTable.addColumn( oDataStore.dataTypes[iDsCategoriesColumn], oDataStore.columnNames[iDsCategoriesColumn] );
  153. for ( var i = 0; i < aCategoriesValues.length; i++ )
  154. {
  155. oDataTable.setCell( v_aSortedCategoryLookup[i], 0, aCategoriesValues[i] );
  156. }
  157. // Add series columns
  158. if ( aSeriesValues )
  159. {
  160. for ( var i = 0; i < aSeriesValues.length; i++ )
  161. {
  162. var sValue = aSeriesValues[aSortedSeriesNumbers[i]];
  163. oDataTable.addColumn( oDataStore.dataTypes[iDsValuesColumn], sValue );
  164. }
  165. }
  166. else
  167. {
  168. oDataTable.addColumn( oDataStore.dataTypes[iDsValuesColumn], oDataStore.columnNames[iDsValuesColumn] );
  169. }
  170. var iRowCount = oDataStore.rowCount;
  171. for ( var iRow = 0; iRow < iRowCount; iRow++ )
  172. {
  173. var oCategoryCell = oDataStore.getCell( iRow, iDsCategoriesColumn );
  174. var iCategoryValueIndex = v_aSortedCategoryLookup[oCategoryCell.valueIndex];
  175. var iSeriesValueIndex = aSeriesValues ? v_aSortedSeriesLookup[oDataStore.getCell( iRow, iDsSeriesColumn ).valueIndex] : 0;
  176. oDataTable.setCell( iCategoryValueIndex, iSeriesValueIndex + 1, oDataStore.getCellValue( iRow, iDsValuesColumn ) );
  177. }
  178. return oDataTable;
  179. };
  180. Control.prototype.getFnSortCategoryByString = function( v_bAscending, v_aCategories )
  181. {
  182. if ( v_bAscending )
  183. {
  184. return function(i1, i2)
  185. {
  186. var s1 = v_aCategories[i1];
  187. var s2 = v_aCategories[i2];
  188. var i;
  189. if (s1 && s2)
  190. {
  191. i = s1.localeCompare(s2);
  192. }
  193. else if (s1)
  194. {
  195. i = 1;
  196. }
  197. else if (s2)
  198. {
  199. i = -1;
  200. }
  201. else
  202. {
  203. i = 0;
  204. }
  205. return ( i == 0 ) ? ( i1 - i2 ) : i;
  206. };
  207. }
  208. return function(i1, i2)
  209. {
  210. var s1 = v_aCategories[i1];
  211. var s2 = v_aCategories[i2];
  212. var i;
  213. if (s1 && s2)
  214. {
  215. i = s2.localeCompare(s1);
  216. }
  217. else if (s1)
  218. {
  219. i = -1;
  220. }
  221. else if (s2)
  222. {
  223. i = 1;
  224. }
  225. else
  226. {
  227. i = 0;
  228. }
  229. return ( i == 0 ) ? ( i2 - i1 ) : i;
  230. };
  231. };
  232. Control.prototype.HTMLEncode = function( s )
  233. {
  234. return String( s ).replace( /&/g, "&amp;" ).replace( /</g, "&lt;" ).replace( />/g, "&gt;" );
  235. };
  236. return Control;
  237. });