defaultAxisExtentFromData.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 echarts from '../../core/echarts.js';
  41. import { createHashMap, each, hasOwn, keys, map } from 'zrender/lib/core/util.js';
  42. import { isCartesian2DSeries, findAxisModels } from './cartesianAxisHelper.js';
  43. import { getDataDimensionsOnAxis, unionAxisExtentFromData } from '../axisHelper.js';
  44. import { ensureScaleRawExtentInfo } from '../scaleRawExtentInfo.js';
  45. // A tricky: the priority is just after dataZoom processor.
  46. // If dataZoom has fixed the min/max, this processor do not need to work.
  47. // TODO: SELF REGISTERED.
  48. echarts.registerProcessor(echarts.PRIORITY.PROCESSOR.FILTER + 10, {
  49. getTargetSeries: function (ecModel) {
  50. var seriesModelMap = createHashMap();
  51. ecModel.eachSeries(function (seriesModel) {
  52. isCartesian2DSeries(seriesModel) && seriesModelMap.set(seriesModel.uid, seriesModel);
  53. });
  54. return seriesModelMap;
  55. },
  56. overallReset: function (ecModel, api) {
  57. var seriesRecords = [];
  58. var axisRecordMap = createHashMap();
  59. prepareDataExtentOnAxis(ecModel, axisRecordMap, seriesRecords);
  60. calculateFilteredExtent(axisRecordMap, seriesRecords);
  61. shrinkAxisExtent(axisRecordMap);
  62. }
  63. });
  64. function prepareDataExtentOnAxis(ecModel, axisRecordMap, seriesRecords) {
  65. ecModel.eachSeries(function (seriesModel) {
  66. if (!isCartesian2DSeries(seriesModel)) {
  67. return;
  68. }
  69. var axesModelMap = findAxisModels(seriesModel);
  70. var xAxisModel = axesModelMap.xAxisModel;
  71. var yAxisModel = axesModelMap.yAxisModel;
  72. var xAxis = xAxisModel.axis;
  73. var yAxis = yAxisModel.axis;
  74. var xRawExtentInfo = xAxis.scale.rawExtentInfo;
  75. var yRawExtentInfo = yAxis.scale.rawExtentInfo;
  76. var data = seriesModel.getData();
  77. // If either axis controlled by other filter like "dataZoom",
  78. // use the rule of dataZoom rather than adopting the rules here.
  79. if (xRawExtentInfo && xRawExtentInfo.frozen || yRawExtentInfo && yRawExtentInfo.frozen) {
  80. return;
  81. }
  82. seriesRecords.push({
  83. seriesModel: seriesModel,
  84. xAxisModel: xAxisModel,
  85. yAxisModel: yAxisModel
  86. });
  87. // FIXME: this logic needs to be consistent with
  88. // `coord/cartesian/Grid.ts#_updateScale`.
  89. // It's not good to implement one logic in multiple places.
  90. unionAxisExtentFromData(prepareAxisRecord(axisRecordMap, xAxisModel).condExtent, data, xAxis.dim);
  91. unionAxisExtentFromData(prepareAxisRecord(axisRecordMap, yAxisModel).condExtent, data, yAxis.dim);
  92. });
  93. }
  94. function calculateFilteredExtent(axisRecordMap, seriesRecords) {
  95. each(seriesRecords, function (seriesRecord) {
  96. var xAxisModel = seriesRecord.xAxisModel;
  97. var yAxisModel = seriesRecord.yAxisModel;
  98. var xAxis = xAxisModel.axis;
  99. var yAxis = yAxisModel.axis;
  100. var xAxisRecord = prepareAxisRecord(axisRecordMap, xAxisModel);
  101. var yAxisRecord = prepareAxisRecord(axisRecordMap, yAxisModel);
  102. xAxisRecord.rawExtentInfo = ensureScaleRawExtentInfo(xAxis.scale, xAxisModel, xAxisRecord.condExtent);
  103. yAxisRecord.rawExtentInfo = ensureScaleRawExtentInfo(yAxis.scale, yAxisModel, yAxisRecord.condExtent);
  104. xAxisRecord.rawExtentResult = xAxisRecord.rawExtentInfo.calculate();
  105. yAxisRecord.rawExtentResult = yAxisRecord.rawExtentInfo.calculate();
  106. // If the "xAxis" is set `min`/`max`, some data items might be out of the cartesian.
  107. // then the "yAxis" may needs to calculate extent only based on the data items inside
  108. // the cartesian (similar to what "dataZoom" did).
  109. // A typical case is bar-racing, where bars ara sort dynamically and may only need to
  110. // displayed part of the whole bars.
  111. var data = seriesRecord.seriesModel.getData();
  112. // For duplication removal.
  113. var condDimMap = {};
  114. var tarDimMap = {};
  115. var condAxis;
  116. var tarAxisRecord;
  117. function addCondition(axis, axisRecord) {
  118. // But for simplicity and safety and performance, we only adopt this
  119. // feature on category axis at present.
  120. var condExtent = axisRecord.condExtent;
  121. var rawExtentResult = axisRecord.rawExtentResult;
  122. if (axis.type === 'category' && (condExtent[0] < rawExtentResult.min || rawExtentResult.max < condExtent[1])) {
  123. each(getDataDimensionsOnAxis(data, axis.dim), function (dataDim) {
  124. if (!hasOwn(condDimMap, dataDim)) {
  125. condDimMap[dataDim] = true;
  126. condAxis = axis;
  127. }
  128. });
  129. }
  130. }
  131. function addTarget(axis, axisRecord) {
  132. var rawExtentResult = axisRecord.rawExtentResult;
  133. if (axis.type !== 'category' && (!rawExtentResult.minFixed || !rawExtentResult.maxFixed)) {
  134. each(getDataDimensionsOnAxis(data, axis.dim), function (dataDim) {
  135. if (!hasOwn(condDimMap, dataDim) && !hasOwn(tarDimMap, dataDim)) {
  136. tarDimMap[dataDim] = true;
  137. tarAxisRecord = axisRecord;
  138. }
  139. });
  140. }
  141. }
  142. addCondition(xAxis, xAxisRecord);
  143. addCondition(yAxis, yAxisRecord);
  144. addTarget(xAxis, xAxisRecord);
  145. addTarget(yAxis, yAxisRecord);
  146. var condDims = keys(condDimMap);
  147. var tarDims = keys(tarDimMap);
  148. var tarDimExtents = map(tarDims, function () {
  149. return initExtent();
  150. });
  151. var condDimsLen = condDims.length;
  152. var tarDimsLen = tarDims.length;
  153. if (!condDimsLen || !tarDimsLen) {
  154. return;
  155. }
  156. var singleCondDim = condDimsLen === 1 ? condDims[0] : null;
  157. var singleTarDim = tarDimsLen === 1 ? tarDims[0] : null;
  158. var dataLen = data.count();
  159. // Time consuming, because this is a "block task".
  160. // Simple optimization for the vast majority of cases.
  161. if (singleCondDim && singleTarDim) {
  162. for (var dataIdx = 0; dataIdx < dataLen; dataIdx++) {
  163. var condVal = data.get(singleCondDim, dataIdx);
  164. if (condAxis.scale.isInExtentRange(condVal)) {
  165. unionExtent(tarDimExtents[0], data.get(singleTarDim, dataIdx));
  166. }
  167. }
  168. } else {
  169. for (var dataIdx = 0; dataIdx < dataLen; dataIdx++) {
  170. for (var j = 0; j < condDimsLen; j++) {
  171. var condVal = data.get(condDims[j], dataIdx);
  172. if (condAxis.scale.isInExtentRange(condVal)) {
  173. for (var k = 0; k < tarDimsLen; k++) {
  174. unionExtent(tarDimExtents[k], data.get(tarDims[k], dataIdx));
  175. }
  176. // Any one dim is in range means satisfied.
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. each(tarDimExtents, function (tarDimExtent, i) {
  183. var dim = tarDims[i];
  184. // FIXME: if there has been approximateExtent set?
  185. data.setApproximateExtent(tarDimExtent, dim);
  186. var tarAxisExtent = tarAxisRecord.tarExtent = tarAxisRecord.tarExtent || initExtent();
  187. unionExtent(tarAxisExtent, tarDimExtent[0]);
  188. unionExtent(tarAxisExtent, tarDimExtent[1]);
  189. });
  190. });
  191. }
  192. function shrinkAxisExtent(axisRecordMap) {
  193. axisRecordMap.each(function (axisRecord) {
  194. var tarAxisExtent = axisRecord.tarExtent;
  195. if (tarAxisExtent) {
  196. var rawExtentResult = axisRecord.rawExtentResult;
  197. var rawExtentInfo = axisRecord.rawExtentInfo;
  198. // Shink the original extent.
  199. if (!rawExtentResult.minFixed && tarAxisExtent[0] > rawExtentResult.min) {
  200. rawExtentInfo.modifyDataMinMax('min', tarAxisExtent[0]);
  201. }
  202. if (!rawExtentResult.maxFixed && tarAxisExtent[1] < rawExtentResult.max) {
  203. rawExtentInfo.modifyDataMinMax('max', tarAxisExtent[1]);
  204. }
  205. }
  206. });
  207. }
  208. function prepareAxisRecord(axisRecordMap, axisModel) {
  209. return axisRecordMap.get(axisModel.uid) || axisRecordMap.set(axisModel.uid, {
  210. condExtent: initExtent()
  211. });
  212. }
  213. function initExtent() {
  214. return [Infinity, -Infinity];
  215. }
  216. function unionExtent(extent, val) {
  217. val < extent[0] && (extent[0] = val);
  218. val > extent[1] && (extent[1] = val);
  219. }