jquery.flot.fillbetween.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Flot plugin for computing bottoms for filled line and bar charts.
  2. Copyright (c) 2007-2013 IOLA and Ole Laursen.
  3. Licensed under the MIT license.
  4. The case: you've got two series that you want to fill the area between. In Flot
  5. terms, you need to use one as the fill bottom of the other. You can specify the
  6. bottom of each data point as the third coordinate manually, or you can use this
  7. plugin to compute it for you.
  8. In order to name the other series, you need to give it an id, like this:
  9. var dataset = [
  10. { data: [ ... ], id: "foo" } , // use default bottom
  11. { data: [ ... ], fillBetween: "foo" }, // use first dataset as bottom
  12. ];
  13. $.plot($("#placeholder"), dataset, { lines: { show: true, fill: true }});
  14. As a convenience, if the id given is a number that doesn't appear as an id in
  15. the series, it is interpreted as the index in the array instead (so fillBetween:
  16. 0 can also mean the first series).
  17. Internally, the plugin modifies the datapoints in each series. For line series,
  18. extra data points might be inserted through interpolation. Note that at points
  19. where the bottom line is not defined (due to a null point or start/end of line),
  20. the current line will show a gap too. The algorithm comes from the
  21. jquery.flot.stack.js plugin, possibly some code could be shared.
  22. */
  23. (function ( $ ) {
  24. var options = {
  25. series: {
  26. fillBetween: null // or number
  27. }
  28. };
  29. function init( plot ) {
  30. function findBottomSeries( s, allseries ) {
  31. var i;
  32. for ( i = 0; i < allseries.length; ++i ) {
  33. if ( allseries[ i ].id === s.fillBetween ) {
  34. return allseries[ i ];
  35. }
  36. }
  37. if ( typeof s.fillBetween === "number" ) {
  38. if ( s.fillBetween < 0 || s.fillBetween >= allseries.length ) {
  39. return null;
  40. }
  41. return allseries[ s.fillBetween ];
  42. }
  43. return null;
  44. }
  45. function computeFillBottoms( plot, s, datapoints ) {
  46. if ( s.fillBetween == null ) {
  47. return;
  48. }
  49. var other = findBottomSeries( s, plot.getData() );
  50. if ( !other ) {
  51. return;
  52. }
  53. var ps = datapoints.pointsize,
  54. points = datapoints.points,
  55. otherps = other.datapoints.pointsize,
  56. otherpoints = other.datapoints.points,
  57. newpoints = [],
  58. px, py, intery, qx, qy, bottom,
  59. withlines = s.lines.show,
  60. withbottom = ps > 2 && datapoints.format[2].y,
  61. withsteps = withlines && s.lines.steps,
  62. fromgap = true,
  63. i = 0,
  64. j = 0,
  65. l, m;
  66. while ( true ) {
  67. if ( i >= points.length ) {
  68. break;
  69. }
  70. l = newpoints.length;
  71. if ( points[ i ] == null ) {
  72. // copy gaps
  73. for ( m = 0; m < ps; ++m ) {
  74. newpoints.push( points[ i + m ] );
  75. }
  76. i += ps;
  77. } else if ( j >= otherpoints.length ) {
  78. // for lines, we can't use the rest of the points
  79. if ( !withlines ) {
  80. for ( m = 0; m < ps; ++m ) {
  81. newpoints.push( points[ i + m ] );
  82. }
  83. }
  84. i += ps;
  85. } else if ( otherpoints[ j ] == null ) {
  86. // oops, got a gap
  87. for ( m = 0; m < ps; ++m ) {
  88. newpoints.push( null );
  89. }
  90. fromgap = true;
  91. j += otherps;
  92. } else {
  93. // cases where we actually got two points
  94. px = points[ i ];
  95. py = points[ i + 1 ];
  96. qx = otherpoints[ j ];
  97. qy = otherpoints[ j + 1 ];
  98. bottom = 0;
  99. if ( px === qx ) {
  100. for ( m = 0; m < ps; ++m ) {
  101. newpoints.push( points[ i + m ] );
  102. }
  103. //newpoints[ l + 1 ] += qy;
  104. bottom = qy;
  105. i += ps;
  106. j += otherps;
  107. } else if ( px > qx ) {
  108. // we got past point below, might need to
  109. // insert interpolated extra point
  110. if ( withlines && i > 0 && points[ i - ps ] != null ) {
  111. intery = py + ( points[ i - ps + 1 ] - py ) * ( qx - px ) / ( points[ i - ps ] - px );
  112. newpoints.push( qx );
  113. newpoints.push( intery );
  114. for ( m = 2; m < ps; ++m ) {
  115. newpoints.push( points[ i + m ] );
  116. }
  117. bottom = qy;
  118. }
  119. j += otherps;
  120. } else { // px < qx
  121. // if we come from a gap, we just skip this point
  122. if ( fromgap && withlines ) {
  123. i += ps;
  124. continue;
  125. }
  126. for ( m = 0; m < ps; ++m ) {
  127. newpoints.push( points[ i + m ] );
  128. }
  129. // we might be able to interpolate a point below,
  130. // this can give us a better y
  131. if ( withlines && j > 0 && otherpoints[ j - otherps ] != null ) {
  132. bottom = qy + ( otherpoints[ j - otherps + 1 ] - qy ) * ( px - qx ) / ( otherpoints[ j - otherps ] - qx );
  133. }
  134. //newpoints[l + 1] += bottom;
  135. i += ps;
  136. }
  137. fromgap = false;
  138. if ( l !== newpoints.length && withbottom ) {
  139. newpoints[ l + 2 ] = bottom;
  140. }
  141. }
  142. // maintain the line steps invariant
  143. if ( withsteps && l !== newpoints.length && l > 0 &&
  144. newpoints[ l ] !== null &&
  145. newpoints[ l ] !== newpoints[ l - ps ] &&
  146. newpoints[ l + 1 ] !== newpoints[ l - ps + 1 ] ) {
  147. for (m = 0; m < ps; ++m) {
  148. newpoints[ l + ps + m ] = newpoints[ l + m ];
  149. }
  150. newpoints[ l + 1 ] = newpoints[ l - ps + 1 ];
  151. }
  152. }
  153. datapoints.points = newpoints;
  154. }
  155. plot.hooks.processDatapoints.push( computeFillBottoms );
  156. }
  157. $.plot.plugins.push({
  158. init: init,
  159. options: options,
  160. name: "fillbetween",
  161. version: "1.0"
  162. });
  163. })(jQuery);