1/* 2Flot plugin for specifying range of thresolds on data. 3Controlled through the option "constraints" in a specific series 4 5usage - 6 $.plot($("#placeholder"), [{ data: [ ... ], constraints: [constraint1, constraint2]},{data:[...],constraints:[...]}]) 7 8 where constraint1 = { 9 threshold: 2, 10 color: "rgb(0,0,0)", 11 evaluate : function(y,threshold){ return y < threshold; } 12 } 13 threshold -> y-limit on the plot. 14 evaluate -> the function which defines the limit.This function defines wheteher y < threshold or y > threshold. 15 color -> the color with which to plot portion of the graph which satisfies the limit condition. 16 17 18 19Internally, the plugin works by splitting the data into different series, one for each constraint. 20 21*/ 22(function($){ 23 24 function testing(){ 25 return "working"; 26 } 27 function init(plot){ 28 29 function plotWithMultipleThresholds(plot,s,datapoints){ 30 if(s.data && s.data.length > 0 && s.constraints && s.constraints.length>0){ 31 var series = new Graph(s.data,s.constraints).getPlotData(); 32 for(var i=0;i<series.length;i++){ 33 var ss = $.extend({},s); 34 ss.constraints = []; 35 ss.data = series[i].data; 36 ss.color = series[i].color; 37 plot.getData().push(ss); 38 } 39 } 40 } 41 42 function Graph(dataset, constraints) { 43 this._constraints = _getSortedConstraints(dataset,constraints); 44 this._dataset = dataset; 45 this._plotData = []; 46 47 this.getPlotData = function() { 48 49 if(this._constraints.length == 0)return []; 50 51 for ( var i = this._constraints.length - 1; i >= 0 ; i--) { 52 var constraint = this._constraints[i]; 53 if(null != constraint.threshold){ 54 var set = new Resolve(this._dataset).using(constraint.threshold, constraint.evaluate); 55 this._plotData.push( { 56 data : set, 57 color : constraint.color 58 }); 59 } 60 } 61 return this._plotData; 62 }; 63 64 function Resolve(originalPonits) { 65 this._originalPoints = originalPonits; 66 this._data = []; 67 this._getPointOnThreshold = _getPointOnThreshold; 68 this.using = using ; 69 70 function using(threshold, evaluate) { 71 var count = 0; 72 for ( var i = 0; i < this._originalPoints.length; i++) { 73 var currentPoint = this._originalPoints[i]; 74 if (evaluate(currentPoint[1],threshold)) { 75 if (i > 0 76 && (this._data.length == 0 || this._data[count - 1] == null)) { 77 this._data[count++] = this._getPointOnThreshold(threshold,this._originalPoints[i - 1], currentPoint); 78 } 79 this._data[count++] = currentPoint; 80 } else { 81 if (this._data.length > 0 && this._data[count - 1] != null) { 82 this._data[count++] = this._getPointOnThreshold(threshold,this._originalPoints[i - 1], currentPoint); 83 this._data[count++] = null; 84 this._data[count++] = null; 85 } 86 } 87 } 88 return this._data; 89 } 90 91 function _getPointOnThreshold(threshold, prevP, currP) { 92 var currentX = currP[0]; 93 var currentY = currP[1]; 94 95 var prevX = prevP[0]; 96 var prevY = prevP[1]; 97 98 var slope = (threshold - currentY) 99 / (prevY - currentY); 100 var xOnConstraintLine = slope * (prevX - currentX) + currentX; 101 102 return [ xOnConstraintLine, threshold ]; 103 } 104 } 105 106 function _getSortedConstraints(originalpoints,constraints){ 107 108 var dataRange = _findMaxAndMin(originalpoints); 109 110 if(undefined == dataRange)return []; 111 112 var max = dataRange.max; 113 var min = dataRange.min; 114 var thresholdRanges = []; 115 var sortedConstraints = []; 116 for(var i = 0; i < constraints.length ; i++){ 117 var constraint = constraints[i]; 118 var range = 0; 119 if(constraint.evaluate(min,constraint.threshold)){ 120 range = Math.abs(constraint.threshold - min); 121 }else{ 122 range = Math.abs(max - constraint.threshold); 123 } 124 thresholdRanges.push({constraint:constraint,range:range}); 125 } 126 QuickSort(thresholdRanges,function(obj1,obj2){ return obj1.range < obj2.range;}); 127 for(var i=thresholdRanges.length-1;i>=0;i--){ 128 sortedConstraints[i] = thresholdRanges[i].constraint; 129 } 130 return sortedConstraints; 131 } 132 133 function _findMaxAndMin(dataset){ 134 if(undefined == dataset)return undefined; 135 var arr = []; 136 for( var i=0;i<dataset.length;i++){ 137 arr[i] = dataset[i][1]; 138 } 139 QuickSort(arr,function(p1,p2){return p1 < p2;}); 140 return { min:arr[0],max:arr[arr.length-1]}; 141 } 142 143} 144 145 function QuickSort(dataset,comparator){ 146 sort(dataset, 0, dataset.length-1, comparator); 147 148 function sort(array, left, right,comparator){ 149 if(right > left){ 150 var pivotIndex = Math.floor(( left + right )/2); 151 var pivotNewIndex = partition(array,left,right,pivotIndex,comparator); 152 sort(array, left, pivotNewIndex - 1,comparator); 153 sort(array, pivotNewIndex + 1, right,comparator); 154 155 } 156 } 157 158 function partition(array,left,right,pivotIndex,comparator){ 159 var pivot = array[pivotIndex]; 160 swap(array,pivotIndex,right); 161 var storeIndex = left; 162 for( var i= left ; i < right ; i++){ 163 if(comparator(array[i] , pivot)){ 164 swap(array,i,storeIndex); 165 storeIndex = storeIndex + 1; 166 } 167 } 168 swap(array,storeIndex,right); 169 170 return storeIndex; 171 } 172 173 function swap(array,index1,index2){ 174 var temp = array[index1]; 175 array[index1] = array[index2]; 176 array[index2] = temp; 177 } 178 } 179 180 181 plot.hooks.processRawData.push(plotWithMultipleThresholds); 182 } 183 184$.plot.plugins.push({ 185 init: init, 186 name: 'multiple.threshold', 187 version: '1.0' 188 }); 189})(jQuery); 190 191
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.