VisualMapView.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 echarts = require("../../echarts");
  20. var zrUtil = require("zrender/lib/core/util");
  21. var graphic = require("../../util/graphic");
  22. var formatUtil = require("../../util/format");
  23. var layout = require("../../util/layout");
  24. var VisualMapping = require("../../visual/VisualMapping");
  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. var _default = echarts.extendComponentView({
  44. type: 'visualMap',
  45. /**
  46. * @readOnly
  47. * @type {Object}
  48. */
  49. autoPositionValues: {
  50. left: 1,
  51. right: 1,
  52. top: 1,
  53. bottom: 1
  54. },
  55. init: function (ecModel, api) {
  56. /**
  57. * @readOnly
  58. * @type {module:echarts/model/Global}
  59. */
  60. this.ecModel = ecModel;
  61. /**
  62. * @readOnly
  63. * @type {module:echarts/ExtensionAPI}
  64. */
  65. this.api = api;
  66. /**
  67. * @readOnly
  68. * @type {module:echarts/component/visualMap/visualMapModel}
  69. */
  70. this.visualMapModel;
  71. },
  72. /**
  73. * @protected
  74. */
  75. render: function (visualMapModel, ecModel, api, payload) {
  76. this.visualMapModel = visualMapModel;
  77. if (visualMapModel.get('show') === false) {
  78. this.group.removeAll();
  79. return;
  80. }
  81. this.doRender.apply(this, arguments);
  82. },
  83. /**
  84. * @protected
  85. */
  86. renderBackground: function (group) {
  87. var visualMapModel = this.visualMapModel;
  88. var padding = formatUtil.normalizeCssArray(visualMapModel.get('padding') || 0);
  89. var rect = group.getBoundingRect();
  90. group.add(new graphic.Rect({
  91. z2: -1,
  92. // Lay background rect on the lowest layer.
  93. silent: true,
  94. shape: {
  95. x: rect.x - padding[3],
  96. y: rect.y - padding[0],
  97. width: rect.width + padding[3] + padding[1],
  98. height: rect.height + padding[0] + padding[2]
  99. },
  100. style: {
  101. fill: visualMapModel.get('backgroundColor'),
  102. stroke: visualMapModel.get('borderColor'),
  103. lineWidth: visualMapModel.get('borderWidth')
  104. }
  105. }));
  106. },
  107. /**
  108. * @protected
  109. * @param {number} targetValue can be Infinity or -Infinity
  110. * @param {string=} visualCluster Only can be 'color' 'opacity' 'symbol' 'symbolSize'
  111. * @param {Object} [opts]
  112. * @param {string=} [opts.forceState] Specify state, instead of using getValueState method.
  113. * @param {string=} [opts.convertOpacityToAlpha=false] For color gradient in controller widget.
  114. * @return {*} Visual value.
  115. */
  116. getControllerVisual: function (targetValue, visualCluster, opts) {
  117. opts = opts || {};
  118. var forceState = opts.forceState;
  119. var visualMapModel = this.visualMapModel;
  120. var visualObj = {}; // Default values.
  121. if (visualCluster === 'symbol') {
  122. visualObj.symbol = visualMapModel.get('itemSymbol');
  123. }
  124. if (visualCluster === 'color') {
  125. var defaultColor = visualMapModel.get('contentColor');
  126. visualObj.color = defaultColor;
  127. }
  128. function getter(key) {
  129. return visualObj[key];
  130. }
  131. function setter(key, value) {
  132. visualObj[key] = value;
  133. }
  134. var mappings = visualMapModel.controllerVisuals[forceState || visualMapModel.getValueState(targetValue)];
  135. var visualTypes = VisualMapping.prepareVisualTypes(mappings);
  136. zrUtil.each(visualTypes, function (type) {
  137. var visualMapping = mappings[type];
  138. if (opts.convertOpacityToAlpha && type === 'opacity') {
  139. type = 'colorAlpha';
  140. visualMapping = mappings.__alphaForOpacity;
  141. }
  142. if (VisualMapping.dependsOn(type, visualCluster)) {
  143. visualMapping && visualMapping.applyVisual(targetValue, getter, setter);
  144. }
  145. });
  146. return visualObj[visualCluster];
  147. },
  148. /**
  149. * @protected
  150. */
  151. positionGroup: function (group) {
  152. var model = this.visualMapModel;
  153. var api = this.api;
  154. layout.positionElement(group, model.getBoxLayoutParams(), {
  155. width: api.getWidth(),
  156. height: api.getHeight()
  157. });
  158. },
  159. /**
  160. * @protected
  161. * @abstract
  162. */
  163. doRender: zrUtil.noop
  164. });
  165. module.exports = _default;