2
// -----------------------------------------------------------------------------
4
// -----------------------------------------------------------------------------
6
var indicators = []; // Array->associative array (id,name)
7
var periods = []; // Array->associative array (id,name)
8
var orgunits = []; // Array->associative array (id,name)
9
var data = []; // Associative array [(indicator-period-orgunit), (indicatorValue)]
11
var sizes = []; // Associative array (dimension, size)
13
var pivotIndicator = true; // Should correspond to init value in form
14
var pivotPeriod = false;
15
var pivotOrgunit = false;
17
var currentIndicator = 0;
18
var currentPeriod = 0;
19
var currentOrgunit = 0;
21
// -----------------------------------------------------------------------------
23
// -----------------------------------------------------------------------------
26
* This method is called from the UI and is responsible for retrieving data from
27
* the server and setting the global variables.
31
clearGlobalVariables();
33
var indicatorGroupList = document.getElementById( "indicatorGroup" );
34
var periodTypeList = document.getElementById( "periodType" );
35
var levelList = document.getElementById( "level" );
37
var indicatorGroupId = indicatorGroupList.options[ indicatorGroupList.selectedIndex ].value;
38
var indicatorGroupName = indicatorGroupList.options[ indicatorGroupList.selectedIndex ].text;
39
var startDate = document.getElementById( "startDate" ).value;
40
var endDate = document.getElementById( "endDate" ).value;
41
var periodTypeName = periodTypeList.options[ periodTypeList.selectedIndex ].value;
42
var level = levelList.options[ levelList.selectedIndex ].value;
45
indicatorGroupId = "1152";
46
periodTypeName = "Monthly";
47
startDate = "2006-01-01";
48
endDate = "2007-01-01";
52
document.getElementById( "dataLabel" ).innerHTML =
53
i18n_indicator_group + ": " + indicatorGroupName +
54
", " + i18n_start_date + ": " + startDate +
55
", " + i18n_end_date + ": " + endDate +
56
", " + i18n_period_type + ": " + periodTypeName +
57
", " + i18n_organisation_unit_level + ": " + level;
59
var url = "getPivotTable.action";
64
"indicatorGroupId": indicatorGroupId,
65
"periodTypeName": periodTypeName,
66
"startDate": startDate,
72
var pivot = json.pivotTable;
74
indicators = pivot.indicators;
75
periods = pivot.periods;
76
orgunits = pivot.organisationUnits;
78
sizes["indicator"] = pivot.sizeIndicators;
79
sizes["period"] = pivot.sizePeriods;
80
sizes["orgunit"] = pivot.sizeOrganisationUnits;
82
data = pivot.indicatorValues[0];
90
* This method is called from the UI and is responsible for pivoting the table.
94
pivotIndicator = document.getElementById( "indicatorBox" ).checked;
95
pivotPeriod = document.getElementById( "periodBox" ).checked;
96
pivotOrgunit = document.getElementById( "orgunitBox" ).checked;
102
* This method is called from the UI and shows a chart with the selected data.
104
function viewChart( chartIndicators, chartDimension )
106
var chartWidth = 750;
108
var url = "generateChart.action";
110
if ( chartIndicators == "single" && chartDimension == "period" )
112
url += "?indicatorId=" + currentIndicator + "&organisationUnitId=" + currentOrgunit + "&dimension=period®ression=true";
116
url += "&periodId=" + periods[p].id;
119
else if ( chartIndicators == "single" && chartDimension == "orgunit" )
121
url += "?indicatorId=" + currentIndicator + "&periodId=" + currentPeriod + "&dimension=organisationUnit®ression=false";
123
for ( o in orgunits )
125
url += "&organisationUnitId=" + orgunits[o].id;
128
else if ( chartIndicators == "all" && chartDimension == "period" )
130
url += "?organisationUnitId=" + currentOrgunit + "&dimension=period®ression=false&chartWidth=950";
132
for ( i in indicators )
134
url += "&indicatorId=" + indicators[i].id;
139
url += "&periodId=" + periods[p].id;
144
else if ( chartIndicators == "all" && chartDimension == "orgunit" )
146
url += "?periodId=" + currentPeriod + "&dimension=organisationUnit®ression=false&chartWidth=950";
148
for ( i in indicators )
150
url += "&indicatorId=" + indicators[i].id;
153
for ( o in orgunits )
155
url += "&organisationUnitId=" + orgunits[o].id;
163
window.open( url, "_blank", "directories=no, height=550, width=" + chartWidth + ", location=no, menubar=no, status=no, toolbar=no, scrollbars=no" );
167
* This method is called from the UI and will display the chart menu.
169
* @param indicatorId the indicator identifier
170
* @param periodId the period identifier
171
* @param orgunitId the organisation unit identifier
173
function viewChartMenu( indicatorId, periodId, orgunitId )
175
currentIndicator = indicatorId;
176
currentPeriod = periodId;
177
currentOrgunit = orgunitId;
179
showDropDown( "pivotMenu" );
183
* Loads the event listeners for the pivot table. Called after page is loaded.
185
function loadListeners()
187
var table = document.getElementById( "pivotTable" );
189
table.addEventListener( "click", setPosition, false );
192
// -----------------------------------------------------------------------------
193
// Supportive methods
194
// -----------------------------------------------------------------------------
197
* This method sets the position of the pivot menu, and is registered as a
198
* callback function for mouse click events.
200
function setPosition( e )
202
var left = e.pageX + "px";
203
var top = e.pageY + "px";
205
var pivotMenu = document.getElementById( "pivotMenu" );
207
pivotMenu.style.left = left;
208
pivotMenu.style.top = top;
212
* This method is responsible for generating the pivot table.
214
function generateTable()
218
var columnIndicators = pivotIndicator ? indicators : [null];
219
var columnPeriods = pivotPeriod ? periods : [null];
220
var columnOrgunits = pivotOrgunit ? orgunits : [null];
222
var rowIndicators = pivotIndicator ? [null] : indicators;
223
var rowPeriods = pivotPeriod ? [null] : periods;
224
var rowOrgunits = pivotOrgunit ? [null] : orgunits;
226
var table = document.getElementById( "pivotTable" );
230
var columns = getColumns( columnIndicators, columnPeriods, columnOrgunits );
231
var rows = getRows( rowIndicators, rowPeriods, rowOrgunits );
233
var columnDimensions = getColumnDimensions();
234
var rowDimensions = getRowDimensions();
236
var colSpans = getSpans( columnDimensions );
237
var rowSpans = getSpans( rowDimensions );
241
// ---------------------------------------------------------------------------
243
// ---------------------------------------------------------------------------
245
for ( d in columnDimensions )
247
for ( rowDimension in rowDimensions ) // Make space for row header
249
html += "<td class='row'></td>";
252
var dimension = columnDimensions[d];
253
var colSpan = colSpans[dimension];
257
var modulus = c % colSpan;
261
html += "<td class='column' colspan='" + colSpan + "'>" + columns[c][dimension] + "</td>";
268
// ---------------------------------------------------------------------------
270
// ---------------------------------------------------------------------------
276
for ( d in rowDimensions ) // Row headers
278
var dimension = rowDimensions[d];
279
var rowSpan = rowSpans[dimension];
280
var modulus = r % rowSpan;
284
html += "<td class='row' rowspan='" + rowSpan + "'>" + rows[r][dimension] + "</td>";
288
for ( c in columns ) // Values
290
var value = getValue( columns[c], rows[r] );
292
var ids = mergeArrays( columns[c], rows[r] );
294
html += "<td class='cell' onclick='viewChartMenu( \"" + ids.indicatorId + "\", \"" + ids.periodId + "\", \"" + ids.orgunitId + "\" )'>" + value + "</td>";
300
table.innerHTML = html;
306
* @param dimensions array -> dimensions
308
* @return associative array ( dimension, span )
310
function getSpans( dimensions )
314
var lastIndex = ( dimensions.length - 1 );
318
for ( var i=lastIndex; i>=0; i-- )
320
var dimension = dimensions[i];
322
spans[dimension] = span;
324
var dimensionSize = sizes[dimension];
326
span = ( span * dimensionSize );
333
* @param columnIndicators array -> associative array ( indicatorId, indicatorName )
334
* @param columnPeriods array -> associative array ( periodId, periodName )
335
* @param columnOrgunits array -> associative array ( orgunitId, orgunitName )
337
* @return array -> associative array ( indicatorId, indicator, periodId, period, orgunitId, orgunit )
339
function getColumns( columnIndicators, columnPeriods, columnOrgunits )
342
var columnsIndex = 0;
344
for ( var i=0; i<columnIndicators.length; i++ )
346
for ( var j=0; j<columnPeriods.length; j++ )
348
for ( var k=0; k<columnOrgunits.length; k++ )
352
if ( columnIndicators[i] != null )
354
column["indicatorId"] = columnIndicators[i].id;
355
column["indicator"] = columnIndicators[i].name;
358
if ( columnPeriods[j] != null )
360
column["periodId"] = columnPeriods[j].id;
361
column["period"] = columnPeriods[j].name;
364
if ( columnOrgunits[k] != null )
366
column["orgunitId"] = columnOrgunits[k].id;
367
column["orgunit"] = columnOrgunits[k].name;
370
columns[columnsIndex++] = column;
379
* @param rowIndicators array -> associative array ( indicatorId, indicatorName )
380
* @param rowPeriods array -> associative array ( periodId, periodName )
381
* @param rowOrgunits array -> associative array ( orgunitId, orgunitName )
383
* @return array -> associative array ( indicatorId, indicator, periodId, period, orgunitId, orgunit )
385
function getRows( rowIndicators, rowPeriods, rowOrgunits )
390
for ( var i=0; i<rowIndicators.length; i++ )
392
for ( var j=0; j<rowPeriods.length; j++ )
394
for ( var k=0; k<rowOrgunits.length; k++ )
398
if ( rowIndicators[i] != null )
400
row["indicatorId"] = rowIndicators[i].id;
401
row["indicator"] = rowIndicators[i].name;
404
if ( rowPeriods[j] != null )
406
row["periodId"] = rowPeriods[j].id;
407
row["period"] = rowPeriods[j].name;
410
if ( rowOrgunits[k] != null )
412
row["orgunitId"] = rowOrgunits[k].id;
413
row["orgunit"] = rowOrgunits[k].name;
416
rows[rowsIndex++] = row;
425
* @return array -> dimension
427
function getColumnDimensions()
431
if ( pivotIndicator )
433
dimensions[dimensions.length] = "indicator";
438
dimensions[dimensions.length] = "period";
443
dimensions[dimensions.length] = "orgunit";
450
* @return array -> dimension
452
function getRowDimensions()
456
if ( !pivotIndicator )
458
dimensions[dimensions.length] = "indicator";
463
dimensions[dimensions.length] = "period";
468
dimensions[dimensions.length] = "orgunit";
475
* @param array1 the first associative array.
476
* @param array2 the second associative array.
478
* @return an associative array with the merged contents of the input arrays.
480
function mergeArrays( array1, array2 )
484
array1[a2] = array2[a2];
491
* @param column associative array ( columnId, columnName )
492
* @param row associative array ( rowId, rowName )
494
* @return the value for the given combination of dimension identifiers.
496
function getValue( column, row )
498
var key = mergeArrays( column, row );
500
var keyString = key.indicatorId + "-" + key.periodId + "-" + key.orgunitId;
502
var value = data[keyString];
504
return value != null ? value : "";
510
function clearTable( table )
512
while ( table.rows.length > 0 )
514
table.deleteRow( 0 );
519
* Clears the global variables.
521
function clearGlobalVariables()
523
indicators.length = 0;