您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

104 行
3.2 KiB

  1. /*
  2. Flot plugin for thresholding data. Controlled through the option
  3. "threshold" in either the global series options
  4. series: {
  5. threshold: {
  6. below: number
  7. color: colorspec
  8. }
  9. }
  10. or in a specific series
  11. $.plot($("#placeholder"), [{ data: [ ... ], threshold: { ... }}])
  12. The data points below "below" are drawn with the specified color. This
  13. makes it easy to mark points below 0, e.g. for budget data.
  14. Internally, the plugin works by splitting the data into two series,
  15. above and below the threshold. The extra series below the threshold
  16. will have its label cleared and the special "originSeries" attribute
  17. set to the original series. You may need to check for this in hover
  18. events.
  19. */
  20. (function ($) {
  21. var options = {
  22. series: { threshold: null } // or { below: number, color: color spec}
  23. };
  24. function init(plot) {
  25. function thresholdData(plot, s, datapoints) {
  26. if (!s.threshold)
  27. return;
  28. var ps = datapoints.pointsize, i, x, y, p, prevp,
  29. thresholded = $.extend({}, s); // note: shallow copy
  30. thresholded.datapoints = { points: [], pointsize: ps };
  31. thresholded.label = null;
  32. thresholded.color = s.threshold.color;
  33. thresholded.threshold = null;
  34. thresholded.originSeries = s;
  35. thresholded.data = [];
  36. var below = s.threshold.below,
  37. origpoints = datapoints.points,
  38. addCrossingPoints = s.lines.show;
  39. threspoints = [];
  40. newpoints = [];
  41. for (i = 0; i < origpoints.length; i += ps) {
  42. x = origpoints[i]
  43. y = origpoints[i + 1];
  44. prevp = p;
  45. if (y < below)
  46. p = threspoints;
  47. else
  48. p = newpoints;
  49. if (addCrossingPoints && prevp != p && x != null
  50. && i > 0 && origpoints[i - ps] != null) {
  51. var interx = (x - origpoints[i - ps]) / (y - origpoints[i - ps + 1]) * (below - y) + x;
  52. prevp.push(interx);
  53. prevp.push(below);
  54. for (m = 2; m < ps; ++m)
  55. prevp.push(origpoints[i + m]);
  56. p.push(null); // start new segment
  57. p.push(null);
  58. for (m = 2; m < ps; ++m)
  59. p.push(origpoints[i + m]);
  60. p.push(interx);
  61. p.push(below);
  62. for (m = 2; m < ps; ++m)
  63. p.push(origpoints[i + m]);
  64. }
  65. p.push(x);
  66. p.push(y);
  67. }
  68. datapoints.points = newpoints;
  69. thresholded.datapoints.points = threspoints;
  70. if (thresholded.datapoints.points.length > 0)
  71. plot.getData().push(thresholded);
  72. // FIXME: there are probably some edge cases left in bars
  73. }
  74. plot.hooks.processDatapoints.push(thresholdData);
  75. }
  76. $.plot.plugins.push({
  77. init: init,
  78. options: options,
  79. name: 'threshold',
  80. version: '1.0'
  81. });
  82. })(jQuery);