123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- define( ["https://www.gstatic.com/charts/loader.js"], function() {
- "use strict";
- function Control()
- {
- };
- Control.prototype.initialize = function( oControlHost, fnDoneInitializing )
- {
- this.m_oControlHost = oControlHost;
- this.m_sChartType = this.getConfigurationValue( "Chart type", "bar" );
- fnDoneInitializing();
- };
- Control.prototype.destroy = function( oControlHost )
- {
- this.m_oControlHost = null;
- };
- Control.prototype.getConfigurationValue = function( sName, sDefaultValue )
- {
- var o = this.m_oControlHost.configuration;
- return ( o && ( o[sName] !== undefined ) ) ? o[sName] : sDefaultValue;
- };
- Control.prototype.draw = function( oControlHost )
- {
- var oPackages = ( this.m_sChartType == "orgchart" ) ? ["orgchart"] : ["corechart", "bar"];
- google.charts.load( "current", { packages : oPackages } );
- google.charts.setOnLoadCallback( this.drawChart.bind( this ) );
- };
- Control.prototype.drawChart = function()
- {
- switch ( this.m_sChartType )
- {
- case "bar":
- this.drawBarChart();
- break;
- case "orgchart":
- this.drawOrgChart();
- break;
- }
- };
- Control.prototype.drawBarChart = function()
- {
- this.m_oGoogleDataTable = this.createPivotedGoogleDataTableFromDataStore( this.m_oDataStore );
- var oChart = new google.charts.Bar( this.m_oControlHost.container );
- var oOptions =
- {
- width : this.getConfigurationValue( "Width", 500 ),
- height : this.getConfigurationValue( "Height", 350 ),
- bars : this.getConfigurationValue( "Horizontal", true ) ? "horizontal" : "vertical"
- };
- oChart.draw( this.m_oGoogleDataTable, oOptions );
- };
- Control.prototype.drawOrgChart = function()
- {
- this.m_oGoogleDataTable = this.createGoogleDataTableFromDataStore( this.m_oDataStore );
- if ( this.m_oDataStore.columnCount == 3 )
- {
- this.insertOrgChartColumnIntoDataTable( this.m_oDataStore, 2, this.m_oGoogleDataTable );
- }
- var oChart = new google.visualization.OrgChart( this.m_oControlHost.container );
- var oOptions =
- {
- allowHtml : true,
- color : "#E8E9EA",
- size : "large"
- };
- oChart.draw( this.m_oGoogleDataTable, oOptions );
- };
- Control.prototype.setData = function( oControlHost, oDataStore )
- {
- this.m_oDataStore = oDataStore;
- };
- Control.prototype.insertOrgChartColumnIntoDataTable = function( oDataStore, iColumnToInsert, oDataTable )
- {
- var iColCount = oDataTable.getNumberOfColumns();
- var iRowCount = oDataTable.getNumberOfRows();
- for ( var iRow = 0; iRow < iRowCount; iRow++ )
- {
- for ( var iCol = 0; iCol < iColCount; iCol++ )
- {
- var sValue = oDataTable.getValue( iRow, iCol );
- var sHtml = '<div style="color:#4178BE; font-size:10pt;">' + this.HTMLEncode( oDataStore.getCellValue( iRow, iColumnToInsert ) ) + '</div>';
- oDataTable.setFormattedValue( iRow, iCol, sValue + sHtml );
- }
- }
- };
- Control.prototype.createGoogleDataTableFromDataStore = function( oDataStore )
- {
- var iColCount = oDataStore.columnCount;
- var iRowCount = oDataStore.rowCount;
- var oDataTable = new google.visualization.DataTable();
- for ( var iCol = 0; iCol < iColCount; iCol++ )
- {
- oDataTable.addColumn( oDataStore.dataTypes[iCol], oDataStore.columnNames[iCol] );
- }
- oDataTable.addRows( iRowCount );
- for ( var iRow = 0; iRow < iRowCount; iRow++ )
- {
- for ( var iCol = 0; iCol < iColCount; iCol++ )
- {
- oDataTable.setCell( iRow, iCol, oDataStore.getCellValue( iRow, iCol ) );
- }
- }
- return oDataTable;
- };
- Control.prototype.createPivotedGoogleDataTableFromDataStore = function( oDataStore, iDsCategoriesColumn, iDsSeriesColumn, iDsValuesColumn )
- {
- if ( iDsCategoriesColumn === undefined )
- {
- iDsCategoriesColumn = 0;
- }
- if ( iDsSeriesColumn === undefined )
- {
- iDsSeriesColumn = ( oDataStore.columnCount > 2 ) ? oDataStore.columnCount - 2 : NaN;
- }
- if ( iDsValuesColumn === undefined )
- {
- iDsValuesColumn = oDataStore.columnCount - 1;
- }
- var aCategoriesValues = oDataStore.columnValues[iDsCategoriesColumn];
- var aSeriesValues = isNaN( iDsSeriesColumn ) ? null : oDataStore.columnValues[iDsSeriesColumn];
- // Sort categories
- var iCategoriesValuesLength = aCategoriesValues.length;
- var aSortedCategoryNumbers = new Array( iCategoriesValuesLength );
- for ( var i = 0; i < iCategoriesValuesLength; i++ )
- {
- aSortedCategoryNumbers[i] = i;
- }
- aSortedCategoryNumbers.sort( this.getFnSortCategoryByString( true, aCategoriesValues ) );
- var v_aSortedCategoryLookup = [];
- for ( var i = 0; i < iCategoriesValuesLength; i++ )
- {
- v_aSortedCategoryLookup[aSortedCategoryNumbers[i]] = i;
- }
- // Sort series
- if ( aSeriesValues )
- {
- var iSeriesValuesLength = aSeriesValues.length;
- var aSortedSeriesNumbers = new Array( iSeriesValuesLength );
- for ( var i = 0; i < iSeriesValuesLength; i++ )
- {
- aSortedSeriesNumbers[i] = i;
- }
- aSortedSeriesNumbers.sort( this.getFnSortCategoryByString( true, aSeriesValues ) );
- var v_aSortedSeriesLookup = [];
- for ( var i = 0; i < iSeriesValuesLength; i++ )
- {
- v_aSortedSeriesLookup[aSortedSeriesNumbers[i]] = i;
- }
- }
- var oDataTable = new google.visualization.DataTable();
- oDataTable.addRows( aCategoriesValues.length );
- // Add category column
- oDataTable.addColumn( oDataStore.dataTypes[iDsCategoriesColumn], oDataStore.columnNames[iDsCategoriesColumn] );
- for ( var i = 0; i < aCategoriesValues.length; i++ )
- {
- oDataTable.setCell( v_aSortedCategoryLookup[i], 0, aCategoriesValues[i] );
- }
- // Add series columns
- if ( aSeriesValues )
- {
- for ( var i = 0; i < aSeriesValues.length; i++ )
- {
- var sValue = aSeriesValues[aSortedSeriesNumbers[i]];
- oDataTable.addColumn( oDataStore.dataTypes[iDsValuesColumn], sValue );
- }
- }
- else
- {
- oDataTable.addColumn( oDataStore.dataTypes[iDsValuesColumn], oDataStore.columnNames[iDsValuesColumn] );
- }
- var iRowCount = oDataStore.rowCount;
- for ( var iRow = 0; iRow < iRowCount; iRow++ )
- {
- var oCategoryCell = oDataStore.getCell( iRow, iDsCategoriesColumn );
- var iCategoryValueIndex = v_aSortedCategoryLookup[oCategoryCell.valueIndex];
- var iSeriesValueIndex = aSeriesValues ? v_aSortedSeriesLookup[oDataStore.getCell( iRow, iDsSeriesColumn ).valueIndex] : 0;
- oDataTable.setCell( iCategoryValueIndex, iSeriesValueIndex + 1, oDataStore.getCellValue( iRow, iDsValuesColumn ) );
- }
- return oDataTable;
- };
- Control.prototype.getFnSortCategoryByString = function( v_bAscending, v_aCategories )
- {
- if ( v_bAscending )
- {
- return function(i1, i2)
- {
- var s1 = v_aCategories[i1];
- var s2 = v_aCategories[i2];
- var i;
- if (s1 && s2)
- {
- i = s1.localeCompare(s2);
- }
- else if (s1)
- {
- i = 1;
- }
- else if (s2)
- {
- i = -1;
- }
- else
- {
- i = 0;
- }
- return ( i == 0 ) ? ( i1 - i2 ) : i;
- };
- }
- return function(i1, i2)
- {
- var s1 = v_aCategories[i1];
- var s2 = v_aCategories[i2];
- var i;
- if (s1 && s2)
- {
- i = s2.localeCompare(s1);
- }
- else if (s1)
- {
- i = -1;
- }
- else if (s2)
- {
- i = 1;
- }
- else
- {
- i = 0;
- }
- return ( i == 0 ) ? ( i2 - i1 ) : i;
- };
- };
- Control.prototype.HTMLEncode = function( s )
- {
- return String( s ).replace( /&/g, "&" ).replace( /</g, "<" ).replace( />/g, ">" );
- };
- return Control;
- });
|