dataSample.js 3.8 KB

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