PiecewiseView.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. import { __extends } from "tslib";
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import VisualMapView from './VisualMapView.js';
  43. import * as graphic from '../../util/graphic.js';
  44. import { createSymbol } from '../../util/symbol.js';
  45. import * as layout from '../../util/layout.js';
  46. import * as helper from './helper.js';
  47. import { createTextStyle } from '../../label/labelStyle.js';
  48. var PiecewiseVisualMapView = /** @class */function (_super) {
  49. __extends(PiecewiseVisualMapView, _super);
  50. function PiecewiseVisualMapView() {
  51. var _this = _super !== null && _super.apply(this, arguments) || this;
  52. _this.type = PiecewiseVisualMapView.type;
  53. return _this;
  54. }
  55. PiecewiseVisualMapView.prototype.doRender = function () {
  56. var thisGroup = this.group;
  57. thisGroup.removeAll();
  58. var visualMapModel = this.visualMapModel;
  59. var textGap = visualMapModel.get('textGap');
  60. var textStyleModel = visualMapModel.textStyleModel;
  61. var itemAlign = this._getItemAlign();
  62. var itemSize = visualMapModel.itemSize;
  63. var viewData = this._getViewData();
  64. var endsText = viewData.endsText;
  65. var showLabel = zrUtil.retrieve(visualMapModel.get('showLabel', true), !endsText);
  66. var silent = !visualMapModel.get('selectedMode');
  67. endsText && this._renderEndsText(thisGroup, endsText[0], itemSize, showLabel, itemAlign);
  68. zrUtil.each(viewData.viewPieceList, function (item) {
  69. var piece = item.piece;
  70. var itemGroup = new graphic.Group();
  71. itemGroup.onclick = zrUtil.bind(this._onItemClick, this, piece);
  72. this._enableHoverLink(itemGroup, item.indexInModelPieceList);
  73. // TODO Category
  74. var representValue = visualMapModel.getRepresentValue(piece);
  75. this._createItemSymbol(itemGroup, representValue, [0, 0, itemSize[0], itemSize[1]], silent);
  76. if (showLabel) {
  77. var visualState = this.visualMapModel.getValueState(representValue);
  78. var align = textStyleModel.get('align') || itemAlign;
  79. itemGroup.add(new graphic.Text({
  80. style: createTextStyle(textStyleModel, {
  81. x: align === 'right' ? -textGap : itemSize[0] + textGap,
  82. y: itemSize[1] / 2,
  83. text: piece.text,
  84. verticalAlign: textStyleModel.get('verticalAlign') || 'middle',
  85. align: align,
  86. opacity: zrUtil.retrieve2(textStyleModel.get('opacity'), visualState === 'outOfRange' ? 0.5 : 1)
  87. }),
  88. silent: silent
  89. }));
  90. }
  91. thisGroup.add(itemGroup);
  92. }, this);
  93. endsText && this._renderEndsText(thisGroup, endsText[1], itemSize, showLabel, itemAlign);
  94. layout.box(visualMapModel.get('orient'), thisGroup, visualMapModel.get('itemGap'));
  95. this.renderBackground(thisGroup);
  96. this.positionGroup(thisGroup);
  97. };
  98. PiecewiseVisualMapView.prototype._enableHoverLink = function (itemGroup, pieceIndex) {
  99. var _this = this;
  100. itemGroup.on('mouseover', function () {
  101. return onHoverLink('highlight');
  102. }).on('mouseout', function () {
  103. return onHoverLink('downplay');
  104. });
  105. var onHoverLink = function (method) {
  106. var visualMapModel = _this.visualMapModel;
  107. // TODO: TYPE More detailed action types
  108. visualMapModel.option.hoverLink && _this.api.dispatchAction({
  109. type: method,
  110. batch: helper.makeHighDownBatch(visualMapModel.findTargetDataIndices(pieceIndex), visualMapModel)
  111. });
  112. };
  113. };
  114. PiecewiseVisualMapView.prototype._getItemAlign = function () {
  115. var visualMapModel = this.visualMapModel;
  116. var modelOption = visualMapModel.option;
  117. if (modelOption.orient === 'vertical') {
  118. return helper.getItemAlign(visualMapModel, this.api, visualMapModel.itemSize);
  119. } else {
  120. // horizontal, most case left unless specifying right.
  121. var align = modelOption.align;
  122. if (!align || align === 'auto') {
  123. align = 'left';
  124. }
  125. return align;
  126. }
  127. };
  128. PiecewiseVisualMapView.prototype._renderEndsText = function (group, text, itemSize, showLabel, itemAlign) {
  129. if (!text) {
  130. return;
  131. }
  132. var itemGroup = new graphic.Group();
  133. var textStyleModel = this.visualMapModel.textStyleModel;
  134. itemGroup.add(new graphic.Text({
  135. style: createTextStyle(textStyleModel, {
  136. x: showLabel ? itemAlign === 'right' ? itemSize[0] : 0 : itemSize[0] / 2,
  137. y: itemSize[1] / 2,
  138. verticalAlign: 'middle',
  139. align: showLabel ? itemAlign : 'center',
  140. text: text
  141. })
  142. }));
  143. group.add(itemGroup);
  144. };
  145. /**
  146. * @private
  147. * @return {Object} {peiceList, endsText} The order is the same as screen pixel order.
  148. */
  149. PiecewiseVisualMapView.prototype._getViewData = function () {
  150. var visualMapModel = this.visualMapModel;
  151. var viewPieceList = zrUtil.map(visualMapModel.getPieceList(), function (piece, index) {
  152. return {
  153. piece: piece,
  154. indexInModelPieceList: index
  155. };
  156. });
  157. var endsText = visualMapModel.get('text');
  158. // Consider orient and inverse.
  159. var orient = visualMapModel.get('orient');
  160. var inverse = visualMapModel.get('inverse');
  161. // Order of model pieceList is always [low, ..., high]
  162. if (orient === 'horizontal' ? inverse : !inverse) {
  163. viewPieceList.reverse();
  164. }
  165. // Origin order of endsText is [high, low]
  166. else if (endsText) {
  167. endsText = endsText.slice().reverse();
  168. }
  169. return {
  170. viewPieceList: viewPieceList,
  171. endsText: endsText
  172. };
  173. };
  174. PiecewiseVisualMapView.prototype._createItemSymbol = function (group, representValue, shapeParam, silent) {
  175. var itemSymbol = createSymbol(
  176. // symbol will be string
  177. this.getControllerVisual(representValue, 'symbol'), shapeParam[0], shapeParam[1], shapeParam[2], shapeParam[3],
  178. // color will be string
  179. this.getControllerVisual(representValue, 'color'));
  180. itemSymbol.silent = silent;
  181. group.add(itemSymbol);
  182. };
  183. PiecewiseVisualMapView.prototype._onItemClick = function (piece) {
  184. var visualMapModel = this.visualMapModel;
  185. var option = visualMapModel.option;
  186. var selectedMode = option.selectedMode;
  187. if (!selectedMode) {
  188. return;
  189. }
  190. var selected = zrUtil.clone(option.selected);
  191. var newKey = visualMapModel.getSelectedMapKey(piece);
  192. if (selectedMode === 'single' || selectedMode === true) {
  193. selected[newKey] = true;
  194. zrUtil.each(selected, function (o, key) {
  195. selected[key] = key === newKey;
  196. });
  197. } else {
  198. selected[newKey] = !selected[newKey];
  199. }
  200. this.api.dispatchAction({
  201. type: 'selectDataRange',
  202. from: this.uid,
  203. visualMapId: this.visualMapModel.id,
  204. selected: selected
  205. });
  206. };
  207. PiecewiseVisualMapView.type = 'visualMap.piecewise';
  208. return PiecewiseVisualMapView;
  209. }(VisualMapView);
  210. export default PiecewiseVisualMapView;