referHelper.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 _config = require("../config");
  20. var __DEV__ = _config.__DEV__;
  21. var _util = require("zrender/lib/core/util");
  22. var createHashMap = _util.createHashMap;
  23. var retrieve = _util.retrieve;
  24. var each = _util.each;
  25. /*
  26. * Licensed to the Apache Software Foundation (ASF) under one
  27. * or more contributor license agreements. See the NOTICE file
  28. * distributed with this work for additional information
  29. * regarding copyright ownership. The ASF licenses this file
  30. * to you under the Apache License, Version 2.0 (the
  31. * "License"); you may not use this file except in compliance
  32. * with the License. You may obtain a copy of the License at
  33. *
  34. * http://www.apache.org/licenses/LICENSE-2.0
  35. *
  36. * Unless required by applicable law or agreed to in writing,
  37. * software distributed under the License is distributed on an
  38. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  39. * KIND, either express or implied. See the License for the
  40. * specific language governing permissions and limitations
  41. * under the License.
  42. */
  43. /**
  44. * Helper for model references.
  45. * There are many manners to refer axis/coordSys.
  46. */
  47. // TODO
  48. // merge relevant logic to this file?
  49. // check: "modelHelper" of tooltip and "BrushTargetManager".
  50. /**
  51. * @class
  52. * For example:
  53. * {
  54. * coordSysName: 'cartesian2d',
  55. * coordSysDims: ['x', 'y', ...],
  56. * axisMap: HashMap({
  57. * x: xAxisModel,
  58. * y: yAxisModel
  59. * }),
  60. * categoryAxisMap: HashMap({
  61. * x: xAxisModel,
  62. * y: undefined
  63. * }),
  64. * // The index of the first category axis in `coordSysDims`.
  65. * // `null/undefined` means no category axis exists.
  66. * firstCategoryDimIndex: 1,
  67. * // To replace user specified encode.
  68. * }
  69. */
  70. function CoordSysInfo(coordSysName) {
  71. /**
  72. * @type {string}
  73. */
  74. this.coordSysName = coordSysName;
  75. /**
  76. * @type {Array.<string>}
  77. */
  78. this.coordSysDims = [];
  79. /**
  80. * @type {module:zrender/core/util#HashMap}
  81. */
  82. this.axisMap = createHashMap();
  83. /**
  84. * @type {module:zrender/core/util#HashMap}
  85. */
  86. this.categoryAxisMap = createHashMap();
  87. /**
  88. * @type {number}
  89. */
  90. this.firstCategoryDimIndex = null;
  91. }
  92. /**
  93. * @return {module:model/referHelper#CoordSysInfo}
  94. */
  95. function getCoordSysInfoBySeries(seriesModel) {
  96. var coordSysName = seriesModel.get('coordinateSystem');
  97. var result = new CoordSysInfo(coordSysName);
  98. var fetch = fetchers[coordSysName];
  99. if (fetch) {
  100. fetch(seriesModel, result, result.axisMap, result.categoryAxisMap);
  101. return result;
  102. }
  103. }
  104. var fetchers = {
  105. cartesian2d: function (seriesModel, result, axisMap, categoryAxisMap) {
  106. var xAxisModel = seriesModel.getReferringComponents('xAxis')[0];
  107. var yAxisModel = seriesModel.getReferringComponents('yAxis')[0];
  108. result.coordSysDims = ['x', 'y'];
  109. axisMap.set('x', xAxisModel);
  110. axisMap.set('y', yAxisModel);
  111. if (isCategory(xAxisModel)) {
  112. categoryAxisMap.set('x', xAxisModel);
  113. result.firstCategoryDimIndex = 0;
  114. }
  115. if (isCategory(yAxisModel)) {
  116. categoryAxisMap.set('y', yAxisModel);
  117. result.firstCategoryDimIndex == null & (result.firstCategoryDimIndex = 1);
  118. }
  119. },
  120. singleAxis: function (seriesModel, result, axisMap, categoryAxisMap) {
  121. var singleAxisModel = seriesModel.getReferringComponents('singleAxis')[0];
  122. result.coordSysDims = ['single'];
  123. axisMap.set('single', singleAxisModel);
  124. if (isCategory(singleAxisModel)) {
  125. categoryAxisMap.set('single', singleAxisModel);
  126. result.firstCategoryDimIndex = 0;
  127. }
  128. },
  129. polar: function (seriesModel, result, axisMap, categoryAxisMap) {
  130. var polarModel = seriesModel.getReferringComponents('polar')[0];
  131. var radiusAxisModel = polarModel.findAxisModel('radiusAxis');
  132. var angleAxisModel = polarModel.findAxisModel('angleAxis');
  133. result.coordSysDims = ['radius', 'angle'];
  134. axisMap.set('radius', radiusAxisModel);
  135. axisMap.set('angle', angleAxisModel);
  136. if (isCategory(radiusAxisModel)) {
  137. categoryAxisMap.set('radius', radiusAxisModel);
  138. result.firstCategoryDimIndex = 0;
  139. }
  140. if (isCategory(angleAxisModel)) {
  141. categoryAxisMap.set('angle', angleAxisModel);
  142. result.firstCategoryDimIndex == null && (result.firstCategoryDimIndex = 1);
  143. }
  144. },
  145. geo: function (seriesModel, result, axisMap, categoryAxisMap) {
  146. result.coordSysDims = ['lng', 'lat'];
  147. },
  148. parallel: function (seriesModel, result, axisMap, categoryAxisMap) {
  149. var ecModel = seriesModel.ecModel;
  150. var parallelModel = ecModel.getComponent('parallel', seriesModel.get('parallelIndex'));
  151. var coordSysDims = result.coordSysDims = parallelModel.dimensions.slice();
  152. each(parallelModel.parallelAxisIndex, function (axisIndex, index) {
  153. var axisModel = ecModel.getComponent('parallelAxis', axisIndex);
  154. var axisDim = coordSysDims[index];
  155. axisMap.set(axisDim, axisModel);
  156. if (isCategory(axisModel) && result.firstCategoryDimIndex == null) {
  157. categoryAxisMap.set(axisDim, axisModel);
  158. result.firstCategoryDimIndex = index;
  159. }
  160. });
  161. }
  162. };
  163. function isCategory(axisModel) {
  164. return axisModel.get('type') === 'category';
  165. }
  166. exports.getCoordSysInfoBySeries = getCoordSysInfoBySeries;