visualSolution.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 zrUtil = require("zrender/lib/core/util");
  20. var VisualMapping = require("./VisualMapping");
  21. /*
  22. * Licensed to the Apache Software Foundation (ASF) under one
  23. * or more contributor license agreements. See the NOTICE file
  24. * distributed with this work for additional information
  25. * regarding copyright ownership. The ASF licenses this file
  26. * to you under the Apache License, Version 2.0 (the
  27. * "License"); you may not use this file except in compliance
  28. * with the License. You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing,
  33. * software distributed under the License is distributed on an
  34. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  35. * KIND, either express or implied. See the License for the
  36. * specific language governing permissions and limitations
  37. * under the License.
  38. */
  39. /**
  40. * @file Visual solution, for consistent option specification.
  41. */
  42. var each = zrUtil.each;
  43. function hasKeys(obj) {
  44. if (obj) {
  45. for (var name in obj) {
  46. if (obj.hasOwnProperty(name)) {
  47. return true;
  48. }
  49. }
  50. }
  51. }
  52. /**
  53. * @param {Object} option
  54. * @param {Array.<string>} stateList
  55. * @param {Function} [supplementVisualOption]
  56. * @return {Object} visualMappings <state, <visualType, module:echarts/visual/VisualMapping>>
  57. */
  58. function createVisualMappings(option, stateList, supplementVisualOption) {
  59. var visualMappings = {};
  60. each(stateList, function (state) {
  61. var mappings = visualMappings[state] = createMappings();
  62. each(option[state], function (visualData, visualType) {
  63. if (!VisualMapping.isValidType(visualType)) {
  64. return;
  65. }
  66. var mappingOption = {
  67. type: visualType,
  68. visual: visualData
  69. };
  70. supplementVisualOption && supplementVisualOption(mappingOption, state);
  71. mappings[visualType] = new VisualMapping(mappingOption); // Prepare a alpha for opacity, for some case that opacity
  72. // is not supported, such as rendering using gradient color.
  73. if (visualType === 'opacity') {
  74. mappingOption = zrUtil.clone(mappingOption);
  75. mappingOption.type = 'colorAlpha';
  76. mappings.__hidden.__alphaForOpacity = new VisualMapping(mappingOption);
  77. }
  78. });
  79. });
  80. return visualMappings;
  81. function createMappings() {
  82. var Creater = function () {}; // 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. /**
  90. * @param {Object} thisOption
  91. * @param {Object} newOption
  92. * @param {Array.<string>} keys
  93. */
  94. function replaceVisualOption(thisOption, newOption, keys) {
  95. // Visual attributes merge is not supported, otherwise it
  96. // brings overcomplicated merge logic. See #2853. So if
  97. // newOption has anyone of these keys, all of these keys
  98. // will be reset. Otherwise, all keys remain.
  99. var has;
  100. zrUtil.each(keys, function (key) {
  101. if (newOption.hasOwnProperty(key) && hasKeys(newOption[key])) {
  102. has = true;
  103. }
  104. });
  105. has && zrUtil.each(keys, function (key) {
  106. if (newOption.hasOwnProperty(key) && hasKeys(newOption[key])) {
  107. thisOption[key] = zrUtil.clone(newOption[key]);
  108. } else {
  109. delete thisOption[key];
  110. }
  111. });
  112. }
  113. /**
  114. * @param {Array.<string>} stateList
  115. * @param {Object} visualMappings <state, Object.<visualType, module:echarts/visual/VisualMapping>>
  116. * @param {module:echarts/data/List} list
  117. * @param {Function} getValueState param: valueOrIndex, return: state.
  118. * @param {object} [scope] Scope for getValueState
  119. * @param {string} [dimension] Concrete dimension, if used.
  120. */
  121. // ???! handle brush?
  122. function applyVisual(stateList, visualMappings, data, getValueState, scope, dimension) {
  123. var visualTypesMap = {};
  124. zrUtil.each(stateList, function (state) {
  125. var visualTypes = VisualMapping.prepareVisualTypes(visualMappings[state]);
  126. visualTypesMap[state] = visualTypes;
  127. });
  128. var dataIndex;
  129. function getVisual(key) {
  130. return data.getItemVisual(dataIndex, key);
  131. }
  132. function setVisual(key, value) {
  133. data.setItemVisual(dataIndex, key, value);
  134. }
  135. if (dimension == null) {
  136. data.each(eachItem);
  137. } else {
  138. data.each([dimension], eachItem);
  139. }
  140. function eachItem(valueOrIndex, index) {
  141. dataIndex = dimension == null ? valueOrIndex : index;
  142. var rawDataItem = data.getRawDataItem(dataIndex); // Consider performance
  143. if (rawDataItem && rawDataItem.visualMap === false) {
  144. return;
  145. }
  146. var valueState = getValueState.call(scope, valueOrIndex);
  147. var mappings = visualMappings[valueState];
  148. var visualTypes = visualTypesMap[valueState];
  149. for (var i = 0, len = visualTypes.length; i < len; i++) {
  150. var type = visualTypes[i];
  151. mappings[type] && mappings[type].applyVisual(valueOrIndex, getVisual, setVisual);
  152. }
  153. }
  154. }
  155. /**
  156. * @param {module:echarts/data/List} data
  157. * @param {Array.<string>} stateList
  158. * @param {Object} visualMappings <state, Object.<visualType, module:echarts/visual/VisualMapping>>
  159. * @param {Function} getValueState param: valueOrIndex, return: state.
  160. * @param {number} [dim] dimension or dimension index.
  161. */
  162. function incrementalApplyVisual(stateList, visualMappings, getValueState, dim) {
  163. var visualTypesMap = {};
  164. zrUtil.each(stateList, function (state) {
  165. var visualTypes = VisualMapping.prepareVisualTypes(visualMappings[state]);
  166. visualTypesMap[state] = visualTypes;
  167. });
  168. function progress(params, data) {
  169. if (dim != null) {
  170. dim = data.getDimension(dim);
  171. }
  172. function getVisual(key) {
  173. return data.getItemVisual(dataIndex, key);
  174. }
  175. function setVisual(key, value) {
  176. data.setItemVisual(dataIndex, key, value);
  177. }
  178. var dataIndex;
  179. while ((dataIndex = params.next()) != null) {
  180. var rawDataItem = data.getRawDataItem(dataIndex); // Consider performance
  181. if (rawDataItem && rawDataItem.visualMap === false) {
  182. continue;
  183. }
  184. var value = dim != null ? data.get(dim, dataIndex, true) : dataIndex;
  185. var valueState = getValueState(value);
  186. var mappings = visualMappings[valueState];
  187. var visualTypes = visualTypesMap[valueState];
  188. for (var i = 0, len = visualTypes.length; i < len; i++) {
  189. var type = visualTypes[i];
  190. mappings[type] && mappings[type].applyVisual(value, getVisual, setVisual);
  191. }
  192. }
  193. }
  194. return {
  195. progress: progress
  196. };
  197. }
  198. exports.createVisualMappings = createVisualMappings;
  199. exports.replaceVisualOption = replaceVisualOption;
  200. exports.applyVisual = applyVisual;
  201. exports.incrementalApplyVisual = incrementalApplyVisual;