visualSolution.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. /**
  41. * @file Visual solution, for consistent option specification.
  42. */
  43. import * as zrUtil from 'zrender/lib/core/util.js';
  44. import VisualMapping from './VisualMapping.js';
  45. import { getItemVisualFromData, setItemVisualFromData } from './helper.js';
  46. var each = zrUtil.each;
  47. function hasKeys(obj) {
  48. if (obj) {
  49. for (var name_1 in obj) {
  50. if (obj.hasOwnProperty(name_1)) {
  51. return true;
  52. }
  53. }
  54. }
  55. }
  56. export function createVisualMappings(option, stateList, supplementVisualOption) {
  57. var visualMappings = {};
  58. each(stateList, function (state) {
  59. var mappings = visualMappings[state] = createMappings();
  60. each(option[state], function (visualData, visualType) {
  61. if (!VisualMapping.isValidType(visualType)) {
  62. return;
  63. }
  64. var mappingOption = {
  65. type: visualType,
  66. visual: visualData
  67. };
  68. supplementVisualOption && supplementVisualOption(mappingOption, state);
  69. mappings[visualType] = new VisualMapping(mappingOption);
  70. // Prepare a alpha for opacity, for some case that opacity
  71. // is not supported, such as rendering using gradient color.
  72. if (visualType === 'opacity') {
  73. mappingOption = zrUtil.clone(mappingOption);
  74. mappingOption.type = 'colorAlpha';
  75. mappings.__hidden.__alphaForOpacity = new VisualMapping(mappingOption);
  76. }
  77. });
  78. });
  79. return visualMappings;
  80. function createMappings() {
  81. var Creater = function () {};
  82. // Make sure hidden fields will not be visited by
  83. // object iteration (with hasOwnProperty checking).
  84. Creater.prototype.__hidden = Creater.prototype;
  85. var obj = new Creater();
  86. return obj;
  87. }
  88. }
  89. export function replaceVisualOption(thisOption, newOption, keys) {
  90. // Visual attributes merge is not supported, otherwise it
  91. // brings overcomplicated merge logic. See #2853. So if
  92. // newOption has anyone of these keys, all of these keys
  93. // will be reset. Otherwise, all keys remain.
  94. var has;
  95. zrUtil.each(keys, function (key) {
  96. if (newOption.hasOwnProperty(key) && hasKeys(newOption[key])) {
  97. has = true;
  98. }
  99. });
  100. has && zrUtil.each(keys, function (key) {
  101. if (newOption.hasOwnProperty(key) && hasKeys(newOption[key])) {
  102. thisOption[key] = zrUtil.clone(newOption[key]);
  103. } else {
  104. delete thisOption[key];
  105. }
  106. });
  107. }
  108. /**
  109. * @param stateList
  110. * @param visualMappings
  111. * @param list
  112. * @param getValueState param: valueOrIndex, return: state.
  113. * @param scope Scope for getValueState
  114. * @param dimension Concrete dimension, if used.
  115. */
  116. // ???! handle brush?
  117. export function applyVisual(stateList, visualMappings, data, getValueState, scope, dimension) {
  118. var visualTypesMap = {};
  119. zrUtil.each(stateList, function (state) {
  120. var visualTypes = VisualMapping.prepareVisualTypes(visualMappings[state]);
  121. visualTypesMap[state] = visualTypes;
  122. });
  123. var dataIndex;
  124. function getVisual(key) {
  125. return getItemVisualFromData(data, dataIndex, key);
  126. }
  127. function setVisual(key, value) {
  128. setItemVisualFromData(data, dataIndex, key, value);
  129. }
  130. if (dimension == null) {
  131. data.each(eachItem);
  132. } else {
  133. data.each([dimension], eachItem);
  134. }
  135. function eachItem(valueOrIndex, index) {
  136. dataIndex = dimension == null ? valueOrIndex // First argument is index
  137. : index;
  138. var rawDataItem = data.getRawDataItem(dataIndex);
  139. // Consider performance
  140. // @ts-ignore
  141. if (rawDataItem && rawDataItem.visualMap === false) {
  142. return;
  143. }
  144. var valueState = getValueState.call(scope, valueOrIndex);
  145. var mappings = visualMappings[valueState];
  146. var visualTypes = visualTypesMap[valueState];
  147. for (var i = 0, len = visualTypes.length; i < len; i++) {
  148. var type = visualTypes[i];
  149. mappings[type] && mappings[type].applyVisual(valueOrIndex, getVisual, setVisual);
  150. }
  151. }
  152. }
  153. /**
  154. * @param data
  155. * @param stateList
  156. * @param visualMappings <state, Object.<visualType, module:echarts/visual/VisualMapping>>
  157. * @param getValueState param: valueOrIndex, return: state.
  158. * @param dim dimension or dimension index.
  159. */
  160. export function incrementalApplyVisual(stateList, visualMappings, getValueState, dim) {
  161. var visualTypesMap = {};
  162. zrUtil.each(stateList, function (state) {
  163. var visualTypes = VisualMapping.prepareVisualTypes(visualMappings[state]);
  164. visualTypesMap[state] = visualTypes;
  165. });
  166. return {
  167. progress: function progress(params, data) {
  168. var dimIndex;
  169. if (dim != null) {
  170. dimIndex = data.getDimensionIndex(dim);
  171. }
  172. function getVisual(key) {
  173. return getItemVisualFromData(data, dataIndex, key);
  174. }
  175. function setVisual(key, value) {
  176. setItemVisualFromData(data, dataIndex, key, value);
  177. }
  178. var dataIndex;
  179. var store = data.getStore();
  180. while ((dataIndex = params.next()) != null) {
  181. var rawDataItem = data.getRawDataItem(dataIndex);
  182. // Consider performance
  183. // @ts-ignore
  184. if (rawDataItem && rawDataItem.visualMap === false) {
  185. continue;
  186. }
  187. var value = dim != null ? store.get(dimIndex, dataIndex) : dataIndex;
  188. var valueState = getValueState(value);
  189. var mappings = visualMappings[valueState];
  190. var visualTypes = visualTypesMap[valueState];
  191. for (var i = 0, len = visualTypes.length; i < len; i++) {
  192. var type = visualTypes[i];
  193. mappings[type] && mappings[type].applyVisual(value, getVisual, setVisual);
  194. }
  195. }
  196. }
  197. };
  198. }