/* $This file is distributed under the terms of the license in /doc/license.txt$ */ /** * init sets some initial options for the default graph. i.e for when the page * is initially loaded or when its refreshed or when all the checkboxes on the * page are unchecked. * * @param graphContainer * is the div that contains the main graph. */ function init(graphContainer) { var optionSelected = $("select.comparisonValues option:selected").val(); // TODO: make use of the id on the select field instead of a generic one. $("#comparisonParameter").text("Total Number of " + $("select.comparisonValues option:selected").val()); $('#yaxislabel').html("Number of " + optionSelected).mbFlipText(false); $('#comparisonHeader').html(optionSelected).css('font-weight', 'bold'); var defaultFlotOptions = { xaxis : { min : 1996, max : 2008, tickDecimals : 0, tickSize : 2 }, yaxis: { tickDecimals : 0, min : 0, max: 5 }, grid: { borderColor : "#D9D9D9" } }; /* * [[]] is an empty 2D array object. $.plot is passed this for the default * behavior. Ex.When the page initially loads, or when no graphs are present * on the webpage. */ var initialDataObject = [ [] ]; $.plot(graphContainer, initialDataObject, defaultFlotOptions); } /** * unStuffZerosFromLineGraphs removes the previously stuffed zero values. r is * the current data object. s is the current min and max {year} values. All the * datapoints < curr_min{year} && > > curr_max{year} are removed, so that they * don't show up on the graph * * @param {Object} * jsonObject * @param {Object} * arrayOfMinAndMaxYears * @returns jsonObject with modified data points. */ //TODO: side-effect year. function unStuffZerosFromLineGraphs(jsonObject, year) { calcZeroLessMinAndMax(jsonObject, year); var currentMinYear = year.globalMin, currentMaxYear = year.globalMax; $ .each( jsonObject, function(key, val) { var i = 0; for (i = 0; i < val.data.length; i++) { if (((val.data[i][0] < currentMinYear) || (val.data[i][0] > currentMaxYear)) && val.data[i][1] == 0) { val.data.splice(i, 1); i--; } else { continue; } } }); } /** * while unStuffZerosFromLineGraphs is for a group of data objects, * unStuffZerosFromLineGraph is for a single data object. It removes zeroes from * the single object passed as parameter. * * @param {Object} * jsonObject */ function unStuffZerosFromLineGraph(jsonObject) { var i = 0; for (i = 0; i < jsonObject.data.length; i++) { if (jsonObject.data[i][1] == 0) { jsonObject.data.splice(i, 1); i--; } } } /** * stuffZerosIntoLineGraphs is used to fill discontinuities in data points. For * example, if a linegraph has the following data points [1990, * 2],[1992,3],[1994, 5],[1996,5],[2000,4],[2001,1]. stuffZerosIntoLineGraphs * inserts [1991,0],[1993,0],1995,0]..and so on. It also inserts zeroes at the * beginning and the end if the max and min{year} of the current linegraph fall * in between the global max and min{year} * * @param {Object} * jsonObject * @param {Object} * arrayOfMinAndMaxYears * @returns jsonObject with stuffed data points. */ function stuffZerosIntoLineGraphs(jsonObject, year) { calcZeroLessMinAndMax(jsonObject, year); var arrayOfMinAndMaxYears = [ year.globalMin, year.globalMax ]; $ .each( jsonObject, function(key, val) { var position = arrayOfMinAndMaxYears[0], i = 0; for (i = 0; i < (arrayOfMinAndMaxYears[1] - arrayOfMinAndMaxYears[0]) + 1; i++) { if (val.data[i]) { if (val.data[i][0] != position && position <= arrayOfMinAndMaxYears[1]) { val.data.splice(i, 0, [ position, 0 ]); } } else { val.data.push( [ position, 0 ]); } position++; } }); } /** * During runtime, when the user checks/unchecks a checkbox, the zeroes have to * be inserted and removed dynamically. This function calculates the max{year} * and min{year} among all the linegraphs present on the graph at a particular * instance in time . * * @param {Object} * jsonObject * @returns an array of current min and max years. */ function calcZeroLessMinAndMax(jsonObject, year) { var globalMinYear = 5000, globalMaxYear = 0, minYear, maxYear, i = 0; $.each(jsonObject, function(key, val) { for (i = 0; i < val.data.length; i++) { if (val.data[i][1] != 0) { minYear = val.data[i][0]; break; } } for (i = val.data.length - 1; i >= 0; i--) { if (val.data[i][1] != 0 && val.data[i][0] != -1) { maxYear = val.data[i][0]; break; } } if (globalMinYear > minYear) globalMinYear = minYear; if (globalMaxYear < maxYear) globalMaxYear = maxYear; }); year.globalMin = globalMinYear; year.globalMax = globalMaxYear; } /** * z is an an object with two properties label and data. data is of the form * [year,value] This function returns the min and max values of all such years. * * @param {Object} * jsonObject * @returns [minYear, maxYear] */ function calcMinandMaxYears(jsonObject, year) { var minYear = 5000, maxYear = 0; $.each(jsonObject, function(key, val) { if (minYear > val.data[0][0]) { minYear = val.data[0][0]; } if (maxYear < val.data[val.data.length - 1][0] && val.data[val.data.length - 1][0] != -1){ maxYear = val.data[val.data.length - 1][0]; }else { if(val.data.length != 1){ maxYear = val.data[val.data.length - 2][0]; } } }); year.min = minYear; year.max = maxYear; } /** * y is an an object with two properties label and data. data is of the form * [year,value] This function returns the max of all values. * * @param {Object} * jsonObject * @returns maxCount */ function calcMaxOfComparisonParameter(jsonObject) { var sum = 0, i = 0, maxCount = 0; $.each(jsonObject, function(key, val) { for (i = 0; i < val.data.length; i++) sum += val.data[i][1]; if (maxCount < sum) maxCount = sum; sum = 0; }); // console.log('returning max value' + maxCount); return maxCount; } function calcMaxWithinComparisonParameter(jsonObject){ var value = 0, i = 0, maxCount = 0; $.each(jsonObject, function(key, val) { for (i = 0; i < val.data.length; i++){ value = val.data[i][1]; // console.log(val.data[i][1]); if (maxCount < value){ maxCount = value; } } }); //console.log('max value: ' + maxCount); return maxCount; } /** * x is an object and it has two properties label and data. data is a two * dimensional array of the form [year, value] This function returns the sum of * all the values. * * @param {Object} * jsonObject * @returns sum{values}. */ function calcSumOfComparisonParameter(jsonObject) { var sum = 0, i = 0; for (i = 0; i < jsonObject.data.length; i++) { sum += jsonObject.data[i][1]; } // sum += jsonObject.publicationCount; return sum; } /** * A simple function to see if the passed * * @param {array} * objectArray * @param {Object} * object * @returns a flag - 0/1 - indicating whether a contains b. */ function contains(objectArray, object) { var i = 0, flag = 0; for (i = 0; i < objectArray.length; i++) { if (objectArray[i] == object) flag = i; } return flag; } /** * Dynamically change the linewidth and ticksize based on input year range. * * @param {Object} * yearRange */ function setLineWidthAndTickSize(yearRange, flotOptions) { if (yearRange > 0 && yearRange < 15) { flotOptions.series.lines.lineWidth = 3; flotOptions.xaxis.tickSize = 1; } else if (yearRange > 15 && yearRange < 70) { flotOptions.series.lines.lineWidth = 2; flotOptions.xaxis.tickSize = 5; } else { flotOptions.series.lines.lineWidth = 1; flotOptions.xaxis.tickSize = 10; } } /** * Dynamically change the ticksize of y-axis. */ function setTickSizeOfYAxis(maxValue, flotOptions){ var tickSize = 0; if (maxValue > 0 && maxValue <= 5) { flotOptions.yaxis.tickSize = 1; } else if (maxValue > 5 && maxValue <= 10) { flotOptions.yaxis.tickSize = 2; } else if (maxValue > 10 && maxValue <= 15) { flotOptions.yaxis.tickSize = 5; } else if (maxValue > 15 && maxValue <= 70) { flotOptions.yaxis.tickSize = 5; } else { flotOptions.yaxis.tickSize = 10; } } /** * Create a div that represents the rectangular bar A hidden input class that is * used to pass the value and a label beside the checkbox. * * @param {Object} * entityLabel */ function createGraphic(entity, bottomDiv) { var parentP = $('

'); parentP.attr('id', slugify(entity.label)); var labelDiv = $('

') labelDiv.attr('id', 'label'); labelDiv.html(''); // labelDiv.children('a').after(''); labelDiv.children('a').after(''); var checkbox = $(''); checkbox.attr('type','checkbox'); checkbox.attr('checked', true); checkbox.attr('id','checkbox'); checkbox.attr('class', 'easyDeselectCheckbox'); checkbox.attr('value', entity.label); var hiddenLabel = $('