Guest User

Untitled

a guest
Jul 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. function stackData(plot, s, datapoints) {
  2. if (s.stack == null)
  3. return;
  4.  
  5. /********************************************************************************************
  6. * series:{ reverseStack:true } causes the first series to be stacked at the top,
  7. * the last series at the bottom. (Vertical ordering matches the legend.)
  8. *
  9. * Does not properly support excluding specific series from stacking; it's all or none.
  10. *
  11. * Assumes (requires) that all series have exactly the same x values (including same number).
  12. ********************************************************************************************/
  13. if (s.reverseStack){
  14. var allSeries = plot.getData();
  15. var myIndex = null;
  16. for (var i=allSeries.length-1;i>=0;--i){
  17. var series = allSeries[i];
  18. var nextun = allSeries[i+1];
  19. if (s == series) myIndex = i;
  20. if (!series.stackedPoints){
  21. var points = series.datapoints.points;
  22. var step = series.datapoints.pointsize;
  23. series.stackedPoints = points.concat();
  24. for (var xi=0,len=points.length-1; xi<len; xi+=step ){
  25. var y = points[xi+1];
  26. var prevtop = nextun ? nextun.stackedPoints[xi+1] : 0;
  27. series.stackedPoints[xi+1] = y + prevtop;
  28. if (step>2) series.stackedPoints[xi+2] = prevtop;
  29. }
  30. }
  31. }
  32. datapoints.points = allSeries[myIndex].stackedPoints;
  33. return;
  34. }
  35.  
  36. var other = findMatchingSeries(s, plot.getData());
  37. if (!other)
  38. return;
  39.  
  40. var ps = datapoints.pointsize,
  41. points = datapoints.points,
  42. otherps = other.datapoints.pointsize,
  43. otherpoints = other.datapoints.points,
  44. newpoints = [],
  45. px, py, intery, qx, qy, bottom,
  46. withlines = s.lines.show, withbars = s.bars.show,
  47. withsteps = withlines && s.lines.steps,
  48. i = 0, j = 0, l;
  49.  
  50. while (true) {
  51. if (i >= points.length)
  52. break;
  53.  
  54. l = newpoints.length;
  55.  
  56. if (j >= otherpoints.length
  57. || otherpoints[j] == null
  58. || points[i] == null) {
  59. // degenerate cases
  60. for (m = 0; m < ps; ++m)
  61. newpoints.push(points[i + m]);
  62. i += ps;
  63. }
  64. else {
  65. // cases where we actually got two points
  66. px = points[i];
  67. py = points[i + 1];
  68. qx = otherpoints[j];
  69. qy = otherpoints[j + 1];
  70. bottom = 0;
  71.  
  72. if (px == qx) {
  73. for (m = 0; m < ps; ++m)
  74. newpoints.push(points[i + m]);
  75.  
  76. newpoints[l + 1] += qy;
  77. bottom = qy;
  78.  
  79. i += ps;
  80. j += otherps;
  81. }
  82. else if (px > qx) {
  83. // we got past point below, might need to
  84. // insert interpolated extra point
  85. if (withlines && i > 0 && points[i - ps] != null) {
  86. intery = py + (points[i - ps + 1] - py) * (qx - px) / (points[i - ps] - px);
  87. newpoints.push(qx);
  88. newpoints.push(intery + qy)
  89. for (m = 2; m < ps; ++m)
  90. newpoints.push(points[i + m]);
  91. bottom = qy;
  92. }
  93.  
  94. j += otherps;
  95. }
  96. else {
  97. for (m = 0; m < ps; ++m)
  98. newpoints.push(points[i + m]);
  99.  
  100. // we might be able to interpolate a point below,
  101. // this can give us a better y
  102. if (withlines && j > 0 && otherpoints[j - ps] != null)
  103. bottom = qy + (otherpoints[j - ps + 1] - qy) * (px - qx) / (otherpoints[j - ps] - qx);
  104.  
  105. newpoints[l + 1] += bottom;
  106.  
  107. i += ps;
  108. }
  109.  
  110. if (l != newpoints.length && withbars)
  111. newpoints[l + 2] += bottom;
  112. }
  113.  
  114. // maintain the line steps invariant
  115. if (withsteps && l != newpoints.length && l > 0
  116. && newpoints[l] != null
  117. && newpoints[l] != newpoints[l - ps]
  118. && newpoints[l + 1] != newpoints[l - ps + 1]) {
  119. for (m = 0; m < ps; ++m)
  120. newpoints[l + ps + m] = newpoints[l + m];
  121. newpoints[l + 1] = newpoints[l - ps + 1];
  122. }
  123. }
  124. datapoints.points = newpoints;
  125. }
Add Comment
Please, Sign In to add comment