jquery.flot.stack.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Flot plugin for stacking data sets rather than overlyaing them.
  2. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3. Licensed under the MIT license.
  4. The plugin assumes the data is sorted on x (or y if stacking horizontally).
  5. For line charts, it is assumed that if a line has an undefined gap (from a
  6. null point), then the line above it should have the same gap - insert zeros
  7. instead of "null" if you want another behaviour. This also holds for the start
  8. and end of the chart. Note that stacking a mix of positive and negative values
  9. in most instances doesn't make sense (so it looks weird).
  10. Two or more series are stacked when their "stack" attribute is set to the same
  11. key (which can be any number or string or just "true"). To specify the default
  12. stack, you can set the stack option like this:
  13. series: {
  14. stack: null/false, true, or a key (number/string)
  15. }
  16. You can also specify it for a single series, like this:
  17. $.plot( $("#placeholder"), [{
  18. data: [ ... ],
  19. stack: true
  20. }])
  21. The stacking order is determined by the order of the data series in the array
  22. (later series end up on top of the previous).
  23. Internally, the plugin modifies the datapoints in each series, adding an
  24. offset to the y value. For line series, extra data points are inserted through
  25. interpolation. If there's a second y value, it's also adjusted (e.g for bar
  26. charts or filled areas).
  27. */
  28. (function ($) {
  29. var options = {
  30. series: { stack: null } // or number/string
  31. };
  32. function init(plot) {
  33. function findMatchingSeries(s, allseries) {
  34. var res = null;
  35. for (var i = 0; i < allseries.length; ++i) {
  36. if (s == allseries[i])
  37. break;
  38. if (allseries[i].stack == s.stack)
  39. res = allseries[i];
  40. }
  41. return res;
  42. }
  43. function stackData(plot, s, datapoints) {
  44. if (s.stack == null || s.stack === false)
  45. return;
  46. var other = findMatchingSeries(s, plot.getData());
  47. if (!other)
  48. return;
  49. var ps = datapoints.pointsize,
  50. points = datapoints.points,
  51. otherps = other.datapoints.pointsize,
  52. otherpoints = other.datapoints.points,
  53. newpoints = [],
  54. px, py, intery, qx, qy, bottom,
  55. withlines = s.lines.show,
  56. horizontal = s.bars.horizontal,
  57. withbottom = ps > 2 && (horizontal ? datapoints.format[2].x : datapoints.format[2].y),
  58. withsteps = withlines && s.lines.steps,
  59. fromgap = true,
  60. keyOffset = horizontal ? 1 : 0,
  61. accumulateOffset = horizontal ? 0 : 1,
  62. i = 0, j = 0, l, m;
  63. while (true) {
  64. // browse all points from the current series and from the previous series
  65. if (i >= points.length && j >= otherpoints.length)
  66. break;
  67. // newpoints will replace current series with
  68. // as many points as different timestamps we have in the 2 (current & previous) series
  69. l = newpoints.length;
  70. px = points[i + keyOffset];
  71. py = points[i + accumulateOffset];
  72. qx = otherpoints[j + keyOffset];
  73. qy = otherpoints[j + accumulateOffset];
  74. bottom = 0;
  75. if (i < points.length && px == null) {
  76. // let's ignore null points from current series, nothing to do with them
  77. i += ps;
  78. }
  79. else if (j < otherpoints.length && qx == null) {
  80. // let's ignore null points from previous series, nothing to do with them
  81. j += otherps;
  82. }
  83. else if (i >= points.length) {
  84. // no more points in the current series, simply take the remaining points
  85. // from the previous series so that next series will correctly stack
  86. for (m = 0; m < ps; ++m)
  87. newpoints.push(otherpoints[j + m]);
  88. bottom = qy;
  89. j += otherps;
  90. }
  91. else if (j >= otherpoints.length) {
  92. // no more points in the previous series, of course let's take
  93. // the remaining points from the current series
  94. for (m = 0; m < ps; ++m)
  95. newpoints.push(points[i + m]);
  96. i += ps;
  97. }
  98. else {
  99. // next available points from current and previous series have the same timestamp
  100. if (px == qx) {
  101. // so take the point from the current series and skip the previous' one
  102. for (m = 0; m < ps; ++m)
  103. newpoints.push(points[i + m]);
  104. newpoints[l + accumulateOffset] += qy;
  105. bottom = qy;
  106. i += ps;
  107. j += otherps;
  108. }
  109. // next available point with the smallest timestamp is from the previous series
  110. else if (px > qx) {
  111. // so take the point from the previous series so that next series will correctly stack
  112. for (m = 0; m < ps; ++m)
  113. newpoints.push(otherpoints[j + m]);
  114. // we might be able to interpolate
  115. if (i > 0 && points[i - ps] != null)
  116. newpoints[l + accumulateOffset] += py + (points[i - ps + accumulateOffset] - py) * (qx - px) / (points[i - ps + keyOffset] - px);
  117. bottom = qy;
  118. j += otherps;
  119. }
  120. // (px < qx) next available point with the smallest timestamp is from the current series
  121. else {
  122. // so of course let's take the point from the current series
  123. for (m = 0; m < ps; ++m)
  124. newpoints.push(points[i + m]);
  125. // we might be able to interpolate a point below,
  126. // this can give us a better y
  127. if (j > 0 && otherpoints[j - otherps] != null)
  128. bottom = qy + (otherpoints[j - otherps + accumulateOffset] - qy) * (px - qx) / (otherpoints[j - otherps + keyOffset] - qx);
  129. newpoints[l + accumulateOffset] += bottom;
  130. i += ps;
  131. }
  132. }
  133. if (l != newpoints.length && withbottom)
  134. newpoints[l + 2] = bottom;
  135. }
  136. datapoints.points = newpoints;
  137. }
  138. plot.hooks.processDatapoints.push(stackData);
  139. }
  140. $.plot.plugins.push({
  141. init: init,
  142. options: options,
  143. name: 'stack',
  144. version: '1.2'
  145. });
  146. })(jQuery);