visualSolution.js 6.9 KB

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