defaultAxisExtentFromData.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 { isCartesian2DDeclaredSeries, findAxisModels, isCartesian2DInjectedAsDataCoordSys } 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. isCartesian2DDeclaredSeries(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 pie (or other similar series) use cartesian2d, the logic below is
  67. // probably wrong, therefore skip it temporarily.
  68. // TODO: support union extent in this case.
  69. // e.g. make a fake seriesData by series.coord/series.center, and it can be
  70. // performed by data processing (such as, filter), and applied here.
  71. if (!isCartesian2DInjectedAsDataCoordSys(seriesModel)) {
  72. return;
  73. }
  74. var axesModelMap = findAxisModels(seriesModel);
  75. var xAxisModel = axesModelMap.xAxisModel;
  76. var yAxisModel = axesModelMap.yAxisModel;
  77. var xAxis = xAxisModel.axis;
  78. var yAxis = yAxisModel.axis;
  79. var xRawExtentInfo = xAxis.scale.rawExtentInfo;
  80. var yRawExtentInfo = yAxis.scale.rawExtentInfo;
  81. var data = seriesModel.getData();
  82. // If either axis controlled by other filter like "dataZoom",
  83. // use the rule of dataZoom rather than adopting the rules here.
  84. if (xRawExtentInfo && xRawExtentInfo.frozen || yRawExtentInfo && yRawExtentInfo.frozen) {
  85. return;
  86. }
  87. seriesRecords.push({
  88. seriesModel: seriesModel,
  89. xAxisModel: xAxisModel,
  90. yAxisModel: yAxisModel
  91. });
  92. // FIXME: this logic needs to be consistent with
  93. // `coord/cartesian/Grid.ts#_updateScale`.
  94. // It's not good to implement one logic in multiple places.
  95. unionAxisExtentFromData(prepareAxisRecord(axisRecordMap, xAxisModel).condExtent, data, xAxis.dim);
  96. unionAxisExtentFromData(prepareAxisRecord(axisRecordMap, yAxisModel).condExtent, data, yAxis.dim);
  97. });
  98. }
  99. function calculateFilteredExtent(axisRecordMap, seriesRecords) {
  100. each(seriesRecords, function (seriesRecord) {
  101. var xAxisModel = seriesRecord.xAxisModel;
  102. var yAxisModel = seriesRecord.yAxisModel;
  103. var xAxis = xAxisModel.axis;
  104. var yAxis = yAxisModel.axis;
  105. var xAxisRecord = prepareAxisRecord(axisRecordMap, xAxisModel);
  106. var yAxisRecord = prepareAxisRecord(axisRecordMap, yAxisModel);
  107. xAxisRecord.rawExtentInfo = ensureScaleRawExtentInfo(xAxis.scale, xAxisModel, xAxisRecord.condExtent);
  108. yAxisRecord.rawExtentInfo = ensureScaleRawExtentInfo(yAxis.scale, yAxisModel, yAxisRecord.condExtent);
  109. xAxisRecord.rawExtentResult = xAxisRecord.rawExtentInfo.calculate();
  110. yAxisRecord.rawExtentResult = yAxisRecord.rawExtentInfo.calculate();
  111. // If the "xAxis" is set `min`/`max`, some data items might be out of the cartesian.
  112. // then the "yAxis" may needs to calculate extent only based on the data items inside
  113. // the cartesian (similar to what "dataZoom" did).
  114. // A typical case is bar-racing, where bars ara sort dynamically and may only need to
  115. // displayed part of the whole bars.
  116. var data = seriesRecord.seriesModel.getData();
  117. // For duplication removal.
  118. var condDimMap = {};
  119. var tarDimMap = {};
  120. var condAxis;
  121. var tarAxisRecord;
  122. function addCondition(axis, axisRecord) {
  123. // But for simplicity and safety and performance, we only adopt this
  124. // feature on category axis at present.
  125. var condExtent = axisRecord.condExtent;
  126. var rawExtentResult = axisRecord.rawExtentResult;
  127. if (axis.type === 'category' && (condExtent[0] < rawExtentResult.min || rawExtentResult.max < condExtent[1])) {
  128. each(getDataDimensionsOnAxis(data, axis.dim), function (dataDim) {
  129. if (!hasOwn(condDimMap, dataDim)) {
  130. condDimMap[dataDim] = true;
  131. condAxis = axis;
  132. }
  133. });
  134. }
  135. }
  136. function addTarget(axis, axisRecord) {
  137. var rawExtentResult = axisRecord.rawExtentResult;
  138. if (axis.type !== 'category' && (!rawExtentResult.minFixed || !rawExtentResult.maxFixed)) {
  139. each(getDataDimensionsOnAxis(data, axis.dim), function (dataDim) {
  140. if (!hasOwn(condDimMap, dataDim) && !hasOwn(tarDimMap, dataDim)) {
  141. tarDimMap[dataDim] = true;
  142. tarAxisRecord = axisRecord;
  143. }
  144. });
  145. }
  146. }
  147. addCondition(xAxis, xAxisRecord);
  148. addCondition(yAxis, yAxisRecord);
  149. addTarget(xAxis, xAxisRecord);
  150. addTarget(yAxis, yAxisRecord);
  151. var condDims = keys(condDimMap);
  152. var tarDims = keys(tarDimMap);
  153. var tarDimExtents = map(tarDims, function () {
  154. return initExtent();
  155. });
  156. var condDimsLen = condDims.length;
  157. var tarDimsLen = tarDims.length;
  158. if (!condDimsLen || !tarDimsLen) {
  159. return;
  160. }
  161. var singleCondDim = condDimsLen === 1 ? condDims[0] : null;
  162. var singleTarDim = tarDimsLen === 1 ? tarDims[0] : null;
  163. var dataLen = data.count();
  164. // Time consuming, because this is a "block task".
  165. // Simple optimization for the vast majority of cases.
  166. if (singleCondDim && singleTarDim) {
  167. for (var dataIdx = 0; dataIdx < dataLen; dataIdx++) {
  168. var condVal = data.get(singleCondDim, dataIdx);
  169. if (condAxis.scale.isInExtentRange(condVal)) {
  170. unionExtent(tarDimExtents[0], data.get(singleTarDim, dataIdx));
  171. }
  172. }
  173. } else {
  174. for (var dataIdx = 0; dataIdx < dataLen; dataIdx++) {
  175. for (var j = 0; j < condDimsLen; j++) {
  176. var condVal = data.get(condDims[j], dataIdx);
  177. if (condAxis.scale.isInExtentRange(condVal)) {
  178. for (var k = 0; k < tarDimsLen; k++) {
  179. unionExtent(tarDimExtents[k], data.get(tarDims[k], dataIdx));
  180. }
  181. // Any one dim is in range means satisfied.
  182. break;
  183. }
  184. }
  185. }
  186. }
  187. each(tarDimExtents, function (tarDimExtent, i) {
  188. var dim = tarDims[i];
  189. // FIXME: if there has been approximateExtent set?
  190. data.setApproximateExtent(tarDimExtent, dim);
  191. var tarAxisExtent = tarAxisRecord.tarExtent = tarAxisRecord.tarExtent || initExtent();
  192. unionExtent(tarAxisExtent, tarDimExtent[0]);
  193. unionExtent(tarAxisExtent, tarDimExtent[1]);
  194. });
  195. });
  196. }
  197. function shrinkAxisExtent(axisRecordMap) {
  198. axisRecordMap.each(function (axisRecord) {
  199. var tarAxisExtent = axisRecord.tarExtent;
  200. if (tarAxisExtent) {
  201. var rawExtentResult = axisRecord.rawExtentResult;
  202. var rawExtentInfo = axisRecord.rawExtentInfo;
  203. // Shink the original extent.
  204. if (!rawExtentResult.minFixed && tarAxisExtent[0] > rawExtentResult.min) {
  205. rawExtentInfo.modifyDataMinMax('min', tarAxisExtent[0]);
  206. }
  207. if (!rawExtentResult.maxFixed && tarAxisExtent[1] < rawExtentResult.max) {
  208. rawExtentInfo.modifyDataMinMax('max', tarAxisExtent[1]);
  209. }
  210. }
  211. });
  212. }
  213. function prepareAxisRecord(axisRecordMap, axisModel) {
  214. return axisRecordMap.get(axisModel.uid) || axisRecordMap.set(axisModel.uid, {
  215. condExtent: initExtent()
  216. });
  217. }
  218. function initExtent() {
  219. return [Infinity, -Infinity];
  220. }
  221. function unionExtent(extent, val) {
  222. val < extent[0] && (extent[0] = val);
  223. val > extent[1] && (extent[1] = val);
  224. }