mapDataStatistic.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. var zrUtil = require("zrender/lib/core/util");
  20. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. // FIXME 公用?
  39. /**
  40. * @param {Array.<module:echarts/data/List>} datas
  41. * @param {string} statisticType 'average' 'sum'
  42. * @inner
  43. */
  44. function dataStatistics(datas, statisticType) {
  45. var dataNameMap = {};
  46. zrUtil.each(datas, function (data) {
  47. data.each(data.mapDimension('value'), function (value, idx) {
  48. // Add prefix to avoid conflict with Object.prototype.
  49. var mapKey = 'ec-' + data.getName(idx);
  50. dataNameMap[mapKey] = dataNameMap[mapKey] || [];
  51. if (!isNaN(value)) {
  52. dataNameMap[mapKey].push(value);
  53. }
  54. });
  55. });
  56. return datas[0].map(datas[0].mapDimension('value'), function (value, idx) {
  57. var mapKey = 'ec-' + datas[0].getName(idx);
  58. var sum = 0;
  59. var min = Infinity;
  60. var max = -Infinity;
  61. var len = dataNameMap[mapKey].length;
  62. for (var i = 0; i < len; i++) {
  63. min = Math.min(min, dataNameMap[mapKey][i]);
  64. max = Math.max(max, dataNameMap[mapKey][i]);
  65. sum += dataNameMap[mapKey][i];
  66. }
  67. var result;
  68. if (statisticType === 'min') {
  69. result = min;
  70. } else if (statisticType === 'max') {
  71. result = max;
  72. } else if (statisticType === 'average') {
  73. result = sum / len;
  74. } else {
  75. result = sum;
  76. }
  77. return len === 0 ? NaN : result;
  78. });
  79. }
  80. function _default(ecModel) {
  81. var seriesGroups = {};
  82. ecModel.eachSeriesByType('map', function (seriesModel) {
  83. var hostGeoModel = seriesModel.getHostGeoModel();
  84. var key = hostGeoModel ? 'o' + hostGeoModel.id : 'i' + seriesModel.getMapType();
  85. (seriesGroups[key] = seriesGroups[key] || []).push(seriesModel);
  86. });
  87. zrUtil.each(seriesGroups, function (seriesList, key) {
  88. var data = dataStatistics(zrUtil.map(seriesList, function (seriesModel) {
  89. return seriesModel.getData();
  90. }), seriesList[0].get('mapValueCalculation'));
  91. for (var i = 0; i < seriesList.length; i++) {
  92. seriesList[i].originalData = seriesList[i].getData();
  93. } // FIXME Put where?
  94. for (var i = 0; i < seriesList.length; i++) {
  95. seriesList[i].seriesGroup = seriesList;
  96. seriesList[i].needsDrawMap = i === 0 && !seriesList[i].getHostGeoModel();
  97. seriesList[i].setData(data.cloneShallow());
  98. seriesList[i].mainSeries = seriesList[0];
  99. }
  100. });
  101. }
  102. module.exports = _default;