/frontend/express/public/javascripts/flot/jquery.flot.grow.js

https://github.com/leftshifters/countly-server · JavaScript · 140 lines · 115 code · 2 blank · 23 comment · 49 complexity · 805b92b0d24a6bdb2bcdddf72a61b08e MD5 · raw file

  1. /*
  2. * The MIT License
  3. Copyright (c) 2010 by Juergen Marsch
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. (function ($)
  21. { var options =
  22. { series: {
  23. grow: {
  24. active: true,
  25. valueIndex: 1,
  26. stepDelay: 1,
  27. steps:100,
  28. stepMode: "maximum",
  29. stepDirection: "up"
  30. }
  31. }
  32. };
  33. function init(plot) {
  34. var done = false;
  35. var growfunc;
  36. var plt = plot;
  37. var data = null;
  38. var opt = null;
  39. var valueIndex;
  40. plot.hooks.bindEvents.push(processbindEvents);
  41. plot.hooks.drawSeries.push(processSeries);
  42. function processSeries(plot, canvascontext, series)
  43. { opt = plot.getOptions();
  44. valueIndex = opt.series.grow.valueIndex;
  45. if(opt.series.grow.active == true)
  46. { if (done == false)
  47. { data = plot.getData();
  48. data.actualStep = 0;
  49. for (var j = 0; j < data.length; j++)
  50. { data[j].dataOrg = clone(data[j].data);
  51. for (var i = 0; i < data[j].data.length; i++)
  52. data[j].data[i][valueIndex] = 0;
  53. }
  54. plot.setData(data);
  55. done = true;
  56. }
  57. }
  58. }
  59. function processbindEvents(plot,eventHolder)
  60. { opt = plot.getOptions();
  61. if (opt.series.grow.active == true)
  62. { var d = plot.getData();
  63. for (var j = 0; j < data.length; j++) {
  64. opt.series.grow.steps = Math.max(opt.series.grow.steps, d[j].grow.steps);
  65. }
  66. if(opt.series.grow.stepDelay == 0) opt.series.grow.stepDelay++;
  67. growfunc = window.setInterval(growing, opt.series.grow.stepDelay);
  68. }
  69. }
  70. function growing()
  71. { if (data.actualStep < opt.series.grow.steps)
  72. { data.actualStep++;
  73. for(var j = 0; j < data.length; j++)
  74. { if (typeof data[j].grow.stepMode == "function")
  75. { data[j].grow.stepMode(data[j],data.actualStep,valueIndex); }
  76. else
  77. { if (data[j].grow.stepMode == "linear") growLinear();
  78. else if (data[j].grow.stepMode == "maximum") growMaximum();
  79. else if (data[j].grow.stepMode == "delay") growDelay();
  80. else growNone();
  81. }
  82. }
  83. plt.setData(data);
  84. plt.draw();
  85. }
  86. else
  87. { window.clearInterval(growfunc); }
  88. function growNone()
  89. { if (data.actualStep == 1)
  90. { for (var i = 0; i < data[j].data.length; i++)
  91. { data[j].data[i][valueIndex] = data[j].dataOrg[i][valueIndex]; }
  92. }
  93. }
  94. function growLinear()
  95. { if (data.actualStep <= data[j].grow.steps)
  96. { for (var i = 0; i < data[j].data.length; i++)
  97. { if (data[j].grow.stepDirection == "up")
  98. { data[j].data[i][valueIndex] = data[j].dataOrg[i][valueIndex] / data[j].grow.steps * data.actualStep;}
  99. else if(data[j].grow.stepDirection == "down")
  100. { data[j].data[i][valueIndex] = data[j].dataOrg[i][valueIndex] + (data[j].yaxis.max - data[j].dataOrg[i][valueIndex]) / data[j].grow.steps * (data[j].grow.steps - data.actualStep); }
  101. }
  102. }
  103. }
  104. function growMaximum()
  105. { if (data.actualStep <= data[j].grow.steps)
  106. { for (var i = 0; i < data[j].data.length; i++)
  107. { if (data[j].grow.stepDirection == "up")
  108. { data[j].data[i][valueIndex] = Math.min(data[j].dataOrg[i][valueIndex], data[j].yaxis.max / data[j].grow.steps * data.actualStep); }
  109. else if (data[j].grow.stepDirection == "down")
  110. { data[j].data[i][valueIndex] = Math.max(data[j].dataOrg[i][valueIndex], data[j].yaxis.max / data[j].grow.steps * (data[j].grow.steps - data.actualStep) ); }
  111. }
  112. }
  113. }
  114. function growDelay()
  115. { if (data.actualStep == data[j].grow.steps)
  116. { for (var i = 0; i < data[j].data.length; i++)
  117. { data[j].data[i][valueIndex] = data[j].dataOrg[i][valueIndex]; }
  118. }
  119. }
  120. }
  121. function clone(obj)
  122. { if(obj == null || typeof(obj) != 'object') return obj;
  123. var temp = new obj.constructor();
  124. for(var key in obj) temp[key] = clone(obj[key]);
  125. return temp;
  126. }
  127. }
  128. $.plot.plugins.push({
  129. init: init,
  130. options: options,
  131. name: 'grow',
  132. version: '0.2'
  133. });
  134. })(jQuery);