dataSample.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. var samplers = {
  41. average: function (frame) {
  42. var sum = 0;
  43. var count = 0;
  44. for (var i = 0; i < frame.length; i++) {
  45. if (!isNaN(frame[i])) {
  46. sum += frame[i];
  47. count++;
  48. }
  49. } // Return NaN if count is 0
  50. return count === 0 ? NaN : sum / count;
  51. },
  52. sum: function (frame) {
  53. var sum = 0;
  54. for (var i = 0; i < frame.length; i++) {
  55. // Ignore NaN
  56. sum += frame[i] || 0;
  57. }
  58. return sum;
  59. },
  60. max: function (frame) {
  61. var max = -Infinity;
  62. for (var i = 0; i < frame.length; i++) {
  63. frame[i] > max && (max = frame[i]);
  64. } // NaN will cause illegal axis extent.
  65. return isFinite(max) ? max : NaN;
  66. },
  67. min: function (frame) {
  68. var min = Infinity;
  69. for (var i = 0; i < frame.length; i++) {
  70. frame[i] < min && (min = frame[i]);
  71. } // NaN will cause illegal axis extent.
  72. return isFinite(min) ? min : NaN;
  73. },
  74. // TODO
  75. // Median
  76. nearest: function (frame) {
  77. return frame[0];
  78. }
  79. };
  80. var indexSampler = function (frame) {
  81. return Math.round(frame.length / 2);
  82. };
  83. export default function dataSample(seriesType) {
  84. return {
  85. seriesType: seriesType,
  86. // FIXME:TS never used, so comment it
  87. // modifyOutputEnd: true,
  88. reset: function (seriesModel, ecModel, api) {
  89. var data = seriesModel.getData();
  90. var sampling = seriesModel.get('sampling');
  91. var coordSys = seriesModel.coordinateSystem;
  92. var count = data.count(); // Only cartesian2d support down sampling. Disable it when there is few data.
  93. if (count > 10 && coordSys.type === 'cartesian2d' && sampling) {
  94. var baseAxis = coordSys.getBaseAxis();
  95. var valueAxis = coordSys.getOtherAxis(baseAxis);
  96. var extent = baseAxis.getExtent();
  97. var dpr = api.getDevicePixelRatio(); // Coordinste system has been resized
  98. var size = Math.abs(extent[1] - extent[0]) * (dpr || 1);
  99. var rate = Math.round(count / size);
  100. if (rate > 1) {
  101. if (sampling === 'lttb') {
  102. seriesModel.setData(data.lttbDownSample(data.mapDimension(valueAxis.dim), 1 / rate));
  103. }
  104. var sampler = void 0;
  105. if (typeof sampling === 'string') {
  106. sampler = samplers[sampling];
  107. } else if (typeof sampling === 'function') {
  108. sampler = sampling;
  109. }
  110. if (sampler) {
  111. // Only support sample the first dim mapped from value axis.
  112. seriesModel.setData(data.downSample(data.mapDimension(valueAxis.dim), 1 / rate, sampler, indexSampler));
  113. }
  114. }
  115. }
  116. }
  117. };
  118. }