mapDataStatistic.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 * as zrUtil from 'zrender/lib/core/util.js'; // FIXME 公用?
  41. function dataStatistics(datas, statisticType) {
  42. var dataNameMap = {};
  43. zrUtil.each(datas, function (data) {
  44. data.each(data.mapDimension('value'), function (value, idx) {
  45. // Add prefix to avoid conflict with Object.prototype.
  46. var mapKey = 'ec-' + data.getName(idx);
  47. dataNameMap[mapKey] = dataNameMap[mapKey] || [];
  48. if (!isNaN(value)) {
  49. dataNameMap[mapKey].push(value);
  50. }
  51. });
  52. });
  53. return datas[0].map(datas[0].mapDimension('value'), function (value, idx) {
  54. var mapKey = 'ec-' + datas[0].getName(idx);
  55. var sum = 0;
  56. var min = Infinity;
  57. var max = -Infinity;
  58. var len = dataNameMap[mapKey].length;
  59. for (var i = 0; i < len; i++) {
  60. min = Math.min(min, dataNameMap[mapKey][i]);
  61. max = Math.max(max, dataNameMap[mapKey][i]);
  62. sum += dataNameMap[mapKey][i];
  63. }
  64. var result;
  65. if (statisticType === 'min') {
  66. result = min;
  67. } else if (statisticType === 'max') {
  68. result = max;
  69. } else if (statisticType === 'average') {
  70. result = sum / len;
  71. } else {
  72. result = sum;
  73. }
  74. return len === 0 ? NaN : result;
  75. });
  76. }
  77. export default function mapDataStatistic(ecModel) {
  78. var seriesGroups = {};
  79. ecModel.eachSeriesByType('map', function (seriesModel) {
  80. var hostGeoModel = seriesModel.getHostGeoModel();
  81. var key = hostGeoModel ? 'o' + hostGeoModel.id : 'i' + seriesModel.getMapType();
  82. (seriesGroups[key] = seriesGroups[key] || []).push(seriesModel);
  83. });
  84. zrUtil.each(seriesGroups, function (seriesList, key) {
  85. var data = dataStatistics(zrUtil.map(seriesList, function (seriesModel) {
  86. return seriesModel.getData();
  87. }), seriesList[0].get('mapValueCalculation'));
  88. for (var i = 0; i < seriesList.length; i++) {
  89. seriesList[i].originalData = seriesList[i].getData();
  90. } // FIXME Put where?
  91. for (var i = 0; i < seriesList.length; i++) {
  92. seriesList[i].seriesGroup = seriesList;
  93. seriesList[i].needsDrawMap = i === 0 && !seriesList[i].getHostGeoModel();
  94. seriesList[i].setData(data.cloneShallow());
  95. seriesList[i].mainSeries = seriesList[0];
  96. }
  97. });
  98. }