jquery.flot.stack.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. keyOffset = horizontal ? 1 : 0,
  60. accumulateOffset = horizontal ? 0 : 1,
  61. i = 0, j = 0, l, m;
  62. while (true) {
  63. if (i >= points.length && j >= otherpoints.length)
  64. break;
  65. l = newpoints.length;
  66. if (i < points.length && points[i] == null) {
  67. // copy gaps
  68. for (m = 0; m < ps; ++m)
  69. newpoints.push(points[i + m]);
  70. i += ps;
  71. }
  72. else if (i >= points.length) {
  73. // take the remaining points from the previous series
  74. for (m = 0; m < ps; ++m)
  75. newpoints.push(otherpoints[j + m]);
  76. if (withbottom)
  77. newpoints[l + 2] = otherpoints[j + accumulateOffset];
  78. j += otherps;
  79. }
  80. else if (j >= otherpoints.length) {
  81. // take the remaining points from the current series
  82. for (m = 0; m < ps; ++m)
  83. newpoints.push(points[i + m]);
  84. i += ps;
  85. }
  86. else if (j < otherpoints.length && otherpoints[j] == null) {
  87. // ignore point
  88. j += otherps;
  89. }
  90. else {
  91. // cases where we actually got two points
  92. px = points[i + keyOffset];
  93. py = points[i + accumulateOffset];
  94. qx = otherpoints[j + keyOffset];
  95. qy = otherpoints[j + accumulateOffset];
  96. bottom = 0;
  97. if (px == qx) {
  98. for (m = 0; m < ps; ++m)
  99. newpoints.push(points[i + m]);
  100. newpoints[l + accumulateOffset] += qy;
  101. bottom = qy;
  102. i += ps;
  103. j += otherps;
  104. }
  105. else if (px > qx) {
  106. // take the point from the previous series so that next series will correctly stack
  107. if (i == 0) {
  108. for (m = 0; m < ps; ++m)
  109. newpoints.push(otherpoints[j + m]);
  110. bottom = qy;
  111. }
  112. // we got past point below, might need to
  113. // insert interpolated extra point
  114. if (i > 0 && points[i - ps] != null) {
  115. intery = 0;
  116. newpoints.push(qx);
  117. newpoints.push(intery + qy);
  118. for (m = 2; m < ps; ++m)
  119. newpoints.push(points[i + m]);
  120. bottom = qy;
  121. }
  122. j += otherps;
  123. }
  124. else { // px < qx
  125. for (m = 0; m < ps; ++m)
  126. newpoints.push(points[i + m]);
  127. // we might be able to interpolate a point below,
  128. // this can give us a better y
  129. if (j > 0 && otherpoints[j - otherps] != null)
  130. bottom = 0;
  131. newpoints[l + accumulateOffset] += bottom;
  132. i += ps;
  133. }
  134. fromgap = false;
  135. if (l != newpoints.length && withbottom)
  136. newpoints[l + 2] = bottom;
  137. }
  138. // maintain the line steps invariant
  139. if (withsteps && l != newpoints.length && l > 0
  140. && newpoints[l] != null
  141. && newpoints[l] != newpoints[l - ps]
  142. && newpoints[l + 1] != newpoints[l - ps + 1]) {
  143. for (m = 0; m < ps; ++m)
  144. newpoints[l + ps + m] = newpoints[l + m];
  145. newpoints[l + 1] = newpoints[l - ps + 1];
  146. }
  147. }
  148. datapoints.points = newpoints;
  149. }
  150. plot.hooks.processDatapoints.push(stackData);
  151. }
  152. $.plot.plugins.push({
  153. init: init,
  154. options: options,
  155. name: 'stack',
  156. version: '1.2'
  157. });
  158. })(jQuery);