You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

126 line
4.3 KiB

  1. /*
  2. * The MIT License
  3. Copyright (c) 2013 by Sveinn Steinarsson
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. (function ($) {
  21. "use strict";
  22. var floor = Math.floor,
  23. abs = Math.abs;
  24. function largestTriangleThreeBuckets(data, threshold) {
  25. var data_length = data.length;
  26. if (threshold >= data_length || threshold === 0) {
  27. return data; // Nothing to do
  28. }
  29. var sampled = [],
  30. sampled_index = 0;
  31. // Bucket size. Leave room for start and end data points
  32. var every = (data_length - 2) / (threshold - 2);
  33. var a = 0, // Initially a is the first point in the triangle
  34. max_area_point,
  35. max_area,
  36. area,
  37. next_a;
  38. sampled[ sampled_index++ ] = data[ a ]; // Always add the first point
  39. for (var i = 0; i < threshold - 2; i++) {
  40. // Calculate point average for next bucket (containing c)
  41. var avg_x = 0,
  42. avg_y = 0,
  43. avg_range_start = floor( ( i + 1 ) * every ) + 1,
  44. avg_range_end = floor( ( i + 2 ) * every ) + 1;
  45. avg_range_end = avg_range_end < data_length ? avg_range_end : data_length;
  46. var avg_range_length = avg_range_end - avg_range_start;
  47. for ( ; avg_range_start<avg_range_end; avg_range_start++ ) {
  48. avg_x += data[ avg_range_start ][ 0 ] * 1; // * 1 enforces Number (value may be Date)
  49. avg_y += data[ avg_range_start ][ 1 ] * 1;
  50. }
  51. avg_x /= avg_range_length;
  52. avg_y /= avg_range_length;
  53. // Get the range for this bucket
  54. var range_offs = floor( (i + 0) * every ) + 1,
  55. range_to = floor( (i + 1) * every ) + 1;
  56. // Point a
  57. var point_a_x = data[ a ][ 0 ] * 1, // enforce Number (value may be Date)
  58. point_a_y = data[ a ][ 1 ] * 1;
  59. max_area = area = -1;
  60. for ( ; range_offs < range_to; range_offs++ ) {
  61. // Calculate triangle area over three buckets
  62. area = abs( ( point_a_x - avg_x ) * ( data[ range_offs ][ 1 ] - point_a_y ) -
  63. ( point_a_x - data[ range_offs ][ 0 ] ) * ( avg_y - point_a_y )
  64. ) * 0.5;
  65. if ( area > max_area ) {
  66. max_area = area;
  67. max_area_point = data[ range_offs ];
  68. next_a = range_offs; // Next a is this b
  69. }
  70. }
  71. sampled[ sampled_index++ ] = max_area_point; // Pick this point from the bucket
  72. a = next_a; // This a is the next a (chosen b)
  73. }
  74. sampled[ sampled_index++ ] = data[ data_length - 1 ]; // Always add last
  75. return sampled;
  76. }
  77. function processRawData ( plot, series ) {
  78. series.data = largestTriangleThreeBuckets( series.data, series.downsample.threshold );
  79. }
  80. var options = {
  81. series: {
  82. downsample: {
  83. threshold: 1000 // 0 disables downsampling for this series.
  84. }
  85. }
  86. };
  87. function init(plot) {
  88. plot.hooks.processRawData.push(processRawData);
  89. }
  90. $.plot.plugins.push({
  91. init: init,
  92. options: options,
  93. name: "downsample",
  94. version: "0.1"
  95. });
  96. })(jQuery);