dataSample.js 4.2 KB

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