jquery.flot.stack.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Flot plugin for stacking data sets rather than overlyaing them.
  2. Copyright (c) 2007-2013 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. if (i >= points.length)
  65. break;
  66. l = newpoints.length;
  67. if (points[i] == null) {
  68. // copy gaps
  69. for (m = 0; m < ps; ++m)
  70. newpoints.push(points[i + m]);
  71. i += ps;
  72. }
  73. else if (j >= otherpoints.length) {
  74. // for lines, we can't use the rest of the points
  75. if (!withlines) {
  76. for (m = 0; m < ps; ++m)
  77. newpoints.push(points[i + m]);
  78. }
  79. i += ps;
  80. }
  81. else if (otherpoints[j] == null) {
  82. // oops, got a gap
  83. for (m = 0; m < ps; ++m)
  84. newpoints.push(null);
  85. fromgap = true;
  86. j += otherps;
  87. }
  88. else {
  89. // cases where we actually got two points
  90. px = points[i + keyOffset];
  91. py = points[i + accumulateOffset];
  92. qx = otherpoints[j + keyOffset];
  93. qy = otherpoints[j + accumulateOffset];
  94. bottom = 0;
  95. if (px == qx) {
  96. for (m = 0; m < ps; ++m)
  97. newpoints.push(points[i + m]);
  98. newpoints[l + accumulateOffset] += qy;
  99. bottom = qy;
  100. i += ps;
  101. j += otherps;
  102. }
  103. else if (px > qx) {
  104. // we got past point below, might need to
  105. // insert interpolated extra point
  106. if (withlines && i > 0 && points[i - ps] != null) {
  107. intery = py + (points[i - ps + accumulateOffset] - py) * (qx - px) / (points[i - ps + keyOffset] - px);
  108. newpoints.push(qx);
  109. newpoints.push(intery + qy);
  110. for (m = 2; m < ps; ++m)
  111. newpoints.push(points[i + m]);
  112. bottom = qy;
  113. }
  114. j += otherps;
  115. }
  116. else { // px < qx
  117. if (fromgap && withlines) {
  118. // if we come from a gap, we just skip this point
  119. i += ps;
  120. continue;
  121. }
  122. for (m = 0; m < ps; ++m)
  123. newpoints.push(points[i + m]);
  124. // we might be able to interpolate a point below,
  125. // this can give us a better y
  126. if (withlines && j > 0 && otherpoints[j - otherps] != null)
  127. bottom = qy + (otherpoints[j - otherps + accumulateOffset] - qy) * (px - qx) / (otherpoints[j - otherps + keyOffset] - qx);
  128. newpoints[l + accumulateOffset] += bottom;
  129. i += ps;
  130. }
  131. fromgap = false;
  132. if (l != newpoints.length && withbottom)
  133. newpoints[l + 2] += bottom;
  134. }
  135. // maintain the line steps invariant
  136. if (withsteps && l != newpoints.length && l > 0
  137. && newpoints[l] != null
  138. && newpoints[l] != newpoints[l - ps]
  139. && newpoints[l + 1] != newpoints[l - ps + 1]) {
  140. for (m = 0; m < ps; ++m)
  141. newpoints[l + ps + m] = newpoints[l + m];
  142. newpoints[l + 1] = newpoints[l - ps + 1];
  143. }
  144. }
  145. datapoints.points = newpoints;
  146. }
  147. plot.hooks.processDatapoints.push(stackData);
  148. }
  149. $.plot.plugins.push({
  150. init: init,
  151. options: options,
  152. name: 'stack',
  153. version: '1.2'
  154. });
  155. })(jQuery);