PHP/JavaScript webapp to analyse spending habits
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

jquery.flot.threshold.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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);