Aggregate categorized views into JSON for consumption in e.g d3js


function getCategoryData(viewName, dataColumn, getChildren, key) {
	var v = database.getView(viewName);
	var nav;
	if (key) {
		nav = v.createViewNavFromCategory(key);
	} else {
	   nav= v.createViewNav();
	}
	var ve = nav.getFirst();
	var isFirst = true;
	var result = "[";
	while (ve) {
		if (!ve.isTotal()) {
			var curData = ve.getColumnValues();
			if (!isFirst) {
				result += ",";
			}
			result += "{label : \"";
			if (key) {
				result += curData[1];
			} else {
				result += curData[0];	
			}			
			result += "\", value : ";
			result += curData[dataColumn];
			/* for 2 level categories we fetch additional data */
			if (getChildren) {
				var childve = nav.getChild();
				var firstChild = true;
				result += ", children : [";
				while (childve) {
					var childData = childve.getColumnValues();
					if (!firstChild) {
						result += ",";
					}
					result += "{label : \"";
					result += childData[1];
					result += "\", value : ";
					result += childData[dataColumn];
					result += "}";			
					firstChild = false;
					childve = nav.getNextSibling(childve);
				}
				result += "]"
			}
			result += "}";			
			isFirst = false;
		}		
		ve = nav.getNextSibling(ve);
	}
	result += "]";
	return result;
}

/**
 * Loads data from a categorized view, view can have one or two
 * categories, for top level only one category is needed (getChildren = false)
 * The total values are compounded, so to get e.g. Year-to-date totals
 * The key is used to get the required subset of data
 */
function getCumulativeCategoryData(viewName, key, dataColumn, fetchChildren) {
	var v = database.getView(viewName);
	
	var nav = v.createViewNavFromCategory(key);
	var ve = nav.getFirst();
	var nextVe;
	var isFirst = true;
	var result = "[";
	var runningTotal = 0;
	while (ve) {
		// Prefetch the next entry
		nextVe = nav.getNextSibling(ve);
		if (!ve.isTotal()) {
			var curData = ve.getColumnValues();
			if (!isFirst) {
				result += ",";
			}
			result += "{label : \"";
			result += curData[1];
			result += "\", value : ";
			runningTotal += Number(curData[dataColumn]);
			result += runningTotal;
			/* for 2 level categories we fetch additional data */
			if (fetchChildren) {
				var runningSubTotal = 0;
				var childve = nav.getChild();
				var nextChildVe;
				var firstChild = true;
				result += ", children : [";
				while (childve) {
					nextChildVe = nav.getNextSibling(childve);
					var childData = childve.getColumnValues();
					if (!firstChild) {
						result += ",";
					}
					result += "{label : \"";
					result += childData[2];
					result += "\", value : ";
					runningSubTotal += Number(childData[dataColumn]);
					result += runningSubTotal;
					result += "}";			
					firstChild = false;
					try {
						childve.recycle();
					} catch (e) {
						// No action
					}
					childve = nextChildVe;				
				result += "]"
				}	
			}
			result += "}";			
			isFirst = false;
		}
		
		// Cleanup view entry
		try {
			ve.recycle();
		} catch (e) {
			// We don't care
		}
		ve = nextVe;
	}
	result += "]";
	// Cleanup all objects
	try {
		nav.recyle();
		v.recycle();
	} catch (e) {
		// We don't care
	}
	return result;
}
/**
 * Adds empty value pairs in front of an array
 * based on the first value in the array
 * This is to make graphics start in the right month
 */
function paddMonthData(rawData) {
	var rawJson = fromJson(rawData);
	var pad = Number(rawJson[0].label)-1;
	var result = [];
	for (var i=0; i < pad; i++) {
		result.push({label : (i+1).toFixed(0), value : 0});
	}
	for (var j=0; j<rawJson.length;j++) {
		result.push(rawJson[j]);
	}
	return toJson(result);
}
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
1 comment(s)Login first to comment...
fatih duranoğlu
(at 08:23 on 19.08.2014)
Hi Stephan

i have error
Script interpreter error, line=51, col=27: [TypeError] Exception occurred calling method NotesViewNavigator.getChild() null
at getCategoryData((BarChartPaymentsPMonthKey),Istanbul.Test Projesi,5,true)

My view have two categories (BarChartPaymentsPMonthKey)

>Istanbul.Test Projesi
>April.2014
Invoice etc...................
>May.2014


Thanks for everythink