github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/js/charts/flot/jquery.flot.grow.js (about) 1 /* 2 * The MIT License 3 4 Copyright (c) 2010,2011,2012, 2013 by Juergen Marsch 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 */ 24 /* 25 * Added changerequest from Thodoris Greasidis 26 * It makes growing more 27 */ 28 (function ($){ 29 "use strict"; 30 var pluginName = "grow", pluginVersion = "0.4"; 31 var options ={ 32 series: { 33 grow: { 34 active: false, 35 stepDelay: 20, 36 steps:100, 37 growings:[ 38 { 39 valueIndex: 1, 40 stepMode: "linear", 41 stepDirection: "up" 42 } 43 ] 44 } 45 } 46 }; 47 function init(plot) { 48 var done = false; 49 var growfunc; 50 var plt = plot; 51 var data = null; 52 var opt = null; 53 var serie = null; 54 var valueIndex; 55 plot.hooks.bindEvents.push(processbindEvents); 56 plot.hooks.drawSeries.push(processSeries); 57 plot.hooks.shutdown.push(shutdown); 58 function processSeries(plot, canvascontext, series){ 59 opt = plot.getOptions(); 60 valueIndex = opt.series.grow.valueIndex; 61 if(opt.series.grow.active === true){ 62 if (done === false){ 63 data = plot.getData(); 64 data.actualStep = 0; 65 data.growingIndex = 0; 66 for (var j = 0; j < data.length; j++){ 67 data[j].dataOrg = clone(data[j].data); 68 for (var i = 0; i < data[j].data.length; i++){ data[j].data[i][valueIndex] = 0;} 69 } 70 plot.setData(data); 71 done = true; 72 } 73 } 74 } 75 function processbindEvents(plot,eventHolder){ 76 opt = plot.getOptions(); 77 if (opt.series.grow.active === true){ 78 var d = plot.getData(); 79 for (var j = 0; j < data.length; j++) { 80 opt.series.grow.steps = Math.max(opt.series.grow.steps, d[j].grow.steps); 81 } 82 if(opt.series.grow.stepDelay === 0){ opt.series.grow.stepDelay++;} 83 growingLoop(); 84 if (isPluginRegistered("resize")) { 85 plot.getPlaceholder().bind("resize", onResize); 86 } 87 } 88 } 89 function growingLoop(){ 90 var growing, startTime = new Date(), timeDiff; 91 if (data.actualStep < opt.series.grow.steps){ 92 data.actualStep++; 93 for(var j = 0; j < data.length; j++){ 94 for(var g = 0; g < data[j].grow.growings.length; g++){ 95 growing = data[j].grow.growings[g]; 96 if (typeof growing.stepMode === "function"){ 97 growing.stepMode(data[j],data.actualStep,growing); 98 } 99 else{ 100 if (growing.stepMode === "linear"){ growLinear();} 101 else if (growing.stepMode === "maximum"){ growMaximum();} 102 else if (growing.stepMode === "delay"){ growDelay();} 103 else{growNone();} 104 } 105 } 106 } 107 plt.setData(data); 108 plt.draw(); 109 timeDiff = new Date() - startTime; 110 growfunc = window.setTimeout(growingLoop, Math.max(0,opt.series.grow.stepDelay - timeDiff)); 111 } 112 else{ window.clearTimeout(growfunc); growfunc = null; } 113 function growNone(){ 114 if (data.actualStep === 1){ 115 for (var i = 0; i < data[j].data.length; i++){ 116 data[j].data[i][valueIndex] = data[j].dataOrg[i][growing.valueIndex]; 117 } 118 } 119 } 120 function growLinear(){ 121 if (data.actualStep <= data[j].grow.steps){ 122 for (var i = 0; i < data[j].data.length; i++){ 123 if (growing.stepDirection === "up"){ 124 data[j].data[i][growing.valueIndex] = data[j].dataOrg[i][growing.valueIndex] / data[j].grow.steps * data.actualStep; 125 } 126 else if(growing.stepDirection === "down"){ 127 data[j].data[i][growing.valueIndex] = data[j].dataOrg[i][growing.valueIndex] + (data[j].yaxis.max - data[j].dataOrg[i][growing.valueIndex]) / data[j].grow.steps * (data[j].grow.steps - data.actualStep); 128 } 129 } 130 } 131 } 132 function growMaximum(){ 133 if (data.actualStep <= data[j].grow.steps){ 134 for (var i = 0; i < data[j].data.length; i++){ 135 if (growing.stepDirection === "up"){ 136 data[j].data[i][growing.valueIndex] = Math.min(data[j].dataOrg[i][growing.valueIndex], data[j].yaxis.max / data[j].grow.steps * data.actualStep); 137 } 138 else if (growing.stepDirection === "down"){ 139 data[j].data[i][growing.valueIndex] = Math.max(data[j].dataOrg[i][growing.valueIndex], data[j].yaxis.max / data[j].grow.steps * (data[j].grow.steps - data.actualStep) ); 140 } 141 } 142 } 143 } 144 function growDelay(){ 145 if (data.actualStep == data[j].grow.steps){ 146 for (var i = 0; i < data[j].data.length; i++){ 147 data[j].data[i][growing.valueIndex] = data[j].dataOrg[i][growing.valueIndex]; 148 } 149 } 150 } 151 } 152 function onResize() { 153 if (growfunc) { window.clearTimeout(growfunc); growfunc = null; } 154 } 155 function shutdown(plot, eventHolder) { 156 plot.getPlaceholder().unbind("resize", onResize); 157 } 158 function isPluginRegistered(pluginName) { 159 var plugins = $.plot.plugins; 160 for (var i = 0, len = plugins.length; i < len; i++) { 161 var plug = plugins[i]; 162 if (plug.name === pluginName) { 163 return true; 164 } 165 } 166 return false; 167 } 168 function clone(obj){ 169 if(obj === null || typeof(obj) !== 'object'){ return obj;} 170 var temp = new obj.constructor(); 171 for(var key in obj){temp[key] = clone(obj[key]); } 172 return temp; 173 } 174 } 175 $.plot.plugins.push({ 176 init: init, 177 options: options, 178 name: pluginName, 179 version: pluginVersion 180 }); 181 })(jQuery);