scaleMapper.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 { assert, bind, each, extend, keys, noop } from 'zrender/lib/core/util.js';
  41. import { initExtentForUnion, isValidBoundsForExtent } from '../util/model.js';
  42. import { getScaleBreakHelper } from './break.js';
  43. import { error } from '../util/log.js';
  44. export var SCALE_EXTENT_KIND_EFFECTIVE = 0;
  45. export var SCALE_EXTENT_KIND_MAPPING = 1;
  46. var SCALE_MAPPER_METHOD_NAMES_MAP = {
  47. needTransform: 1,
  48. normalize: 1,
  49. scale: 1,
  50. transformIn: 1,
  51. transformOut: 1,
  52. contain: 1,
  53. getExtent: 1,
  54. getExtentUnsafe: 1,
  55. setExtent: 1,
  56. setExtent2: 1,
  57. getFilter: 1,
  58. sanitize: 1,
  59. getDefaultStartValue: 1,
  60. freeze: 1
  61. };
  62. var SCALE_MAPPER_METHOD_NAMES = keys(SCALE_MAPPER_METHOD_NAMES_MAP);
  63. export var SCALE_MAPPER_DEPTH_OUT_OF_BREAK = 2;
  64. export var SCALE_MAPPER_DEPTH_INNERMOST = 3;
  65. export function initBreakOrLinearMapper(
  66. // If input `null/undefined`, a mapper will be created.
  67. mapper, breakParsed, initialExtent) {
  68. var brk;
  69. mapper = mapper || {};
  70. var scaleBreakHelper = getScaleBreakHelper();
  71. if (scaleBreakHelper) {
  72. var brkMapper_1 = scaleBreakHelper.createBreakScaleMapper(breakParsed, initialExtent);
  73. if (brkMapper_1.hasBreaks()) {
  74. // Some `ScaleMapper` methods (such as `normalize`) needs to be fast for large data
  75. // when no breaks, so mount break methods only when breaks really exist.
  76. each(SCALE_MAPPER_METHOD_NAMES, function (methodName) {
  77. if (brkMapper_1[methodName]) {
  78. mapper[methodName] = bind(brkMapper_1[methodName], brkMapper_1);
  79. }
  80. });
  81. brk = brkMapper_1;
  82. }
  83. }
  84. if (brk == null) {
  85. initLinearScaleMapper(mapper, initialExtent);
  86. }
  87. return {
  88. brk: brk,
  89. mapper: mapper
  90. };
  91. }
  92. export function decorateScaleMapper(host, decoratedMapperMethods) {
  93. each(SCALE_MAPPER_METHOD_NAMES, function (methodName) {
  94. host[methodName] = decoratedMapperMethods[methodName];
  95. });
  96. }
  97. export function enableScaleMapperFreeze(host, subMapper) {
  98. host.freeze = noop;
  99. if (process.env.NODE_ENV !== 'production') {
  100. host.freeze = function () {
  101. subMapper.freeze();
  102. };
  103. }
  104. ;
  105. }
  106. export function getScaleExtentForTickUnsafe(mapper) {
  107. return mapper.getExtentUnsafe(SCALE_EXTENT_KIND_EFFECTIVE, SCALE_MAPPER_DEPTH_OUT_OF_BREAK);
  108. }
  109. export function getScaleExtentForMappingUnsafe(mapper,
  110. // NullUndefined means the outermost space.
  111. depth) {
  112. return mapper.getExtentUnsafe(SCALE_EXTENT_KIND_MAPPING, depth) || mapper.getExtentUnsafe(SCALE_EXTENT_KIND_EFFECTIVE, depth);
  113. }
  114. export function getScaleLinearSpanForMapping(mapper) {
  115. var extent = getScaleExtentForMappingUnsafe(mapper, SCALE_MAPPER_DEPTH_INNERMOST);
  116. return extent[1] - extent[0];
  117. }
  118. export function getScaleLinearSpanEffective(mapper) {
  119. var extent = mapper.getExtentUnsafe(SCALE_EXTENT_KIND_EFFECTIVE, SCALE_MAPPER_DEPTH_INNERMOST);
  120. return extent[1] - extent[0];
  121. }
  122. export function initLinearScaleMapper(
  123. // If input `null/undefined`, a mapper will be created.
  124. mapper, initialExtent) {
  125. var linearMapper = mapper || {};
  126. var extendList = [];
  127. // @ts-ignore
  128. linearMapper._extents = extendList;
  129. extendList[SCALE_EXTENT_KIND_EFFECTIVE] = initialExtent ? initialExtent.slice() : initExtentForUnion();
  130. extend(linearMapper, linearScaleMapperMethods);
  131. return linearMapper;
  132. }
  133. var linearScaleMapperMethods = {
  134. needTransform: function () {
  135. return false;
  136. },
  137. normalize: function (val) {
  138. var extent = this._extents[SCALE_EXTENT_KIND_MAPPING] || this._extents[SCALE_EXTENT_KIND_EFFECTIVE];
  139. if (extent[1] === extent[0]) {
  140. return 0.5;
  141. }
  142. return (val - extent[0]) / (extent[1] - extent[0]);
  143. },
  144. scale: function (val) {
  145. var extent = this._extents[SCALE_EXTENT_KIND_MAPPING] || this._extents[SCALE_EXTENT_KIND_EFFECTIVE];
  146. return val * (extent[1] - extent[0]) + extent[0];
  147. },
  148. transformIn: function (val) {
  149. return val;
  150. },
  151. transformOut: function (val) {
  152. return val;
  153. },
  154. contain: function (val) {
  155. // This method is typically used in axis trigger and markers.
  156. // Users may be confused if the extent is restricted to `SCALE_EXTENT_KIND_EFFECTIVE`.
  157. var extent = getScaleExtentForMappingUnsafe(this, null);
  158. return val >= extent[0] && val <= extent[1];
  159. },
  160. getExtent: function () {
  161. return this._extents[SCALE_EXTENT_KIND_EFFECTIVE].slice();
  162. },
  163. getExtentUnsafe: function (kind) {
  164. return this._extents[kind];
  165. },
  166. setExtent: function (start, end) {
  167. if (process.env.NODE_ENV !== 'production') {
  168. assert(!this._frozen);
  169. }
  170. writeExtent(this._extents, SCALE_EXTENT_KIND_EFFECTIVE, start, end);
  171. },
  172. setExtent2: function (kind, start, end) {
  173. if (process.env.NODE_ENV !== 'production') {
  174. assert(!this._frozen);
  175. }
  176. var extentList = this._extents;
  177. if (!extentList[kind]) {
  178. extentList[kind] = extentList[SCALE_EXTENT_KIND_EFFECTIVE].slice();
  179. }
  180. writeExtent(extentList, kind, start, end);
  181. },
  182. freeze: function () {
  183. if (process.env.NODE_ENV !== 'production') {
  184. // @ts-ignore
  185. this._frozen = true;
  186. }
  187. }
  188. };
  189. function writeExtent(extentList, kind, start, end) {
  190. // NOTE: `NaN` should be excluded. e.g., `scaleRawExtentInfo.resultMinMax` may be `[NaN, NaN]`.
  191. if (isValidBoundsForExtent(start, end)) {
  192. extentList[kind][0] = start;
  193. extentList[kind][1] = end;
  194. } else {
  195. if (process.env.NODE_ENV !== 'production') {
  196. // PENDING: should use `assert` after fixing all invalid calls.
  197. if (start != null && end != null && start <= end) {
  198. error("Invalid setExtent call - start: " + start + ", end: " + end);
  199. }
  200. }
  201. }
  202. }
  203. // ------ END: Linear Scale Mapper ------