dataSample.js 4.7 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. import { isFunction, isString } from 'zrender/lib/core/util.js';
  41. var samplers = {
  42. average: function (frame) {
  43. var sum = 0;
  44. var count = 0;
  45. for (var i = 0; i < frame.length; i++) {
  46. if (!isNaN(frame[i])) {
  47. sum += frame[i];
  48. count++;
  49. }
  50. }
  51. // Return NaN if count is 0
  52. return count === 0 ? NaN : sum / count;
  53. },
  54. sum: function (frame) {
  55. var sum = 0;
  56. for (var i = 0; i < frame.length; i++) {
  57. // Ignore NaN
  58. sum += frame[i] || 0;
  59. }
  60. return sum;
  61. },
  62. max: function (frame) {
  63. var max = -Infinity;
  64. for (var i = 0; i < frame.length; i++) {
  65. frame[i] > max && (max = frame[i]);
  66. }
  67. // NaN will cause illegal axis extent.
  68. return isFinite(max) ? max : NaN;
  69. },
  70. min: function (frame) {
  71. var min = Infinity;
  72. for (var i = 0; i < frame.length; i++) {
  73. frame[i] < min && (min = frame[i]);
  74. }
  75. // NaN will cause illegal axis extent.
  76. return isFinite(min) ? min : NaN;
  77. },
  78. minmax: function (frame) {
  79. var turningPointAbsoluteValue = -Infinity;
  80. var turningPointOriginalValue = -Infinity;
  81. for (var i = 0; i < frame.length; i++) {
  82. var originalValue = frame[i];
  83. var absoluteValue = Math.abs(originalValue);
  84. if (absoluteValue > turningPointAbsoluteValue) {
  85. turningPointAbsoluteValue = absoluteValue;
  86. turningPointOriginalValue = originalValue;
  87. }
  88. }
  89. return isFinite(turningPointOriginalValue) ? turningPointOriginalValue : NaN;
  90. },
  91. // TODO
  92. // Median
  93. nearest: function (frame) {
  94. return frame[0];
  95. }
  96. };
  97. var indexSampler = function (frame) {
  98. return Math.round(frame.length / 2);
  99. };
  100. export default function dataSample(seriesType) {
  101. return {
  102. seriesType: seriesType,
  103. // FIXME:TS never used, so comment it
  104. // modifyOutputEnd: true,
  105. reset: function (seriesModel, ecModel, api) {
  106. var data = seriesModel.getData();
  107. var sampling = seriesModel.get('sampling');
  108. var coordSys = seriesModel.coordinateSystem;
  109. var count = data.count();
  110. // Only cartesian2d support down sampling. Disable it when there is few data.
  111. if (count > 10 && coordSys.type === 'cartesian2d' && sampling) {
  112. var baseAxis = coordSys.getBaseAxis();
  113. var valueAxis = coordSys.getOtherAxis(baseAxis);
  114. var extent = baseAxis.getExtent();
  115. var dpr = api.getDevicePixelRatio();
  116. // Coordinste system has been resized
  117. var size = Math.abs(extent[1] - extent[0]) * (dpr || 1);
  118. var rate = Math.round(count / size);
  119. if (isFinite(rate) && rate > 1) {
  120. if (sampling === 'lttb') {
  121. seriesModel.setData(data.lttbDownSample(data.mapDimension(valueAxis.dim), 1 / rate));
  122. }
  123. var sampler = void 0;
  124. if (isString(sampling)) {
  125. sampler = samplers[sampling];
  126. } else if (isFunction(sampling)) {
  127. sampler = sampling;
  128. }
  129. if (sampler) {
  130. // Only support sample the first dim mapped from value axis.
  131. seriesModel.setData(data.downSample(data.mapDimension(valueAxis.dim), 1 / rate, sampler, indexSampler));
  132. }
  133. }
  134. }
  135. }
  136. };
  137. }