defaultAxisExtentFromData.js 9.1 KB

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