jquery.flot.threshold.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Flot plugin for thresholding data.
  2. Copyright (c) 2007-2013 IOLA and Ole Laursen.
  3. Licensed under the MIT license.
  4. The plugin supports these options:
  5. series: {
  6. threshold: {
  7. below: number
  8. color: colorspec
  9. }
  10. }
  11. It can also be applied to a single series, like this:
  12. $.plot( $("#placeholder"), [{
  13. data: [ ... ],
  14. threshold: { ... }
  15. }])
  16. An array can be passed for multiple thresholding, like this:
  17. threshold: [{
  18. below: number1
  19. color: color1
  20. },{
  21. below: number2
  22. color: color2
  23. }]
  24. These multiple threshold objects can be passed in any order since they are
  25. sorted by the processing function.
  26. The data points below "below" are drawn with the specified color. This makes
  27. it easy to mark points below 0, e.g. for budget data.
  28. Internally, the plugin works by splitting the data into two series, above and
  29. below the threshold. The extra series below the threshold will have its label
  30. cleared and the special "originSeries" attribute set to the original series.
  31. You may need to check for this in hover events.
  32. */
  33. (function ($) {
  34. var options = {
  35. series: { threshold: null } // or { below: number, color: color spec}
  36. };
  37. function init(plot) {
  38. function thresholdData(plot, s, datapoints, below, color) {
  39. var ps = datapoints.pointsize, i, x, y, p, prevp,
  40. thresholded = $.extend({}, s); // note: shallow copy
  41. thresholded.datapoints = { points: [], pointsize: ps, format: datapoints.format };
  42. thresholded.label = null;
  43. thresholded.color = color;
  44. thresholded.threshold = null;
  45. thresholded.originSeries = s;
  46. thresholded.data = [];
  47. var origpoints = datapoints.points,
  48. addCrossingPoints = s.lines.show;
  49. var threspoints = [];
  50. var newpoints = [];
  51. var m;
  52. for (i = 0; i < origpoints.length; i += ps) {
  53. x = origpoints[i];
  54. y = origpoints[i + 1];
  55. prevp = p;
  56. if (y < below)
  57. p = threspoints;
  58. else
  59. p = newpoints;
  60. if (addCrossingPoints && prevp != p && x != null
  61. && i > 0 && origpoints[i - ps] != null) {
  62. var interx = x + (below - y) * (x - origpoints[i - ps]) / (y - origpoints[i - ps + 1]);
  63. prevp.push(interx);
  64. prevp.push(below);
  65. for (m = 2; m < ps; ++m)
  66. prevp.push(origpoints[i + m]);
  67. p.push(null); // start new segment
  68. p.push(null);
  69. for (m = 2; m < ps; ++m)
  70. p.push(origpoints[i + m]);
  71. p.push(interx);
  72. p.push(below);
  73. for (m = 2; m < ps; ++m)
  74. p.push(origpoints[i + m]);
  75. }
  76. p.push(x);
  77. p.push(y);
  78. for (m = 2; m < ps; ++m)
  79. p.push(origpoints[i + m]);
  80. }
  81. datapoints.points = newpoints;
  82. thresholded.datapoints.points = threspoints;
  83. if (thresholded.datapoints.points.length > 0) {
  84. var origIndex = $.inArray(s, plot.getData());
  85. // Insert newly-generated series right after original one (to prevent it from becoming top-most)
  86. plot.getData().splice(origIndex + 1, 0, thresholded);
  87. }
  88. // FIXME: there are probably some edge cases left in bars
  89. }
  90. function processThresholds(plot, s, datapoints) {
  91. if (!s.threshold)
  92. return;
  93. if (s.threshold instanceof Array) {
  94. s.threshold.sort(function(a, b) {
  95. return a.below - b.below;
  96. });
  97. $(s.threshold).each(function(i, th) {
  98. thresholdData(plot, s, datapoints, th.below, th.color);
  99. });
  100. }
  101. else {
  102. thresholdData(plot, s, datapoints, s.threshold.below, s.threshold.color);
  103. }
  104. }
  105. plot.hooks.processDatapoints.push(processThresholds);
  106. }
  107. $.plot.plugins.push({
  108. init: init,
  109. options: options,
  110. name: 'threshold',
  111. version: '1.2'
  112. });
  113. })(jQuery);