PiecewiseView.js 8.0 KB

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