PiecewiseView.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 =
  49. /** @class */
  50. function (_super) {
  51. __extends(PiecewiseVisualMapView, _super);
  52. function PiecewiseVisualMapView() {
  53. var _this = _super !== null && _super.apply(this, arguments) || this;
  54. _this.type = PiecewiseVisualMapView.type;
  55. return _this;
  56. }
  57. PiecewiseVisualMapView.prototype.doRender = function () {
  58. var thisGroup = this.group;
  59. thisGroup.removeAll();
  60. var visualMapModel = this.visualMapModel;
  61. var textGap = visualMapModel.get('textGap');
  62. var textStyleModel = visualMapModel.textStyleModel;
  63. var textFont = textStyleModel.getFont();
  64. var textFill = textStyleModel.getTextColor();
  65. var itemAlign = this._getItemAlign();
  66. var itemSize = visualMapModel.itemSize;
  67. var viewData = this._getViewData();
  68. var endsText = viewData.endsText;
  69. var showLabel = zrUtil.retrieve(visualMapModel.get('showLabel', true), !endsText);
  70. endsText && this._renderEndsText(thisGroup, endsText[0], itemSize, showLabel, itemAlign);
  71. zrUtil.each(viewData.viewPieceList, function (item) {
  72. var piece = item.piece;
  73. var itemGroup = new graphic.Group();
  74. itemGroup.onclick = zrUtil.bind(this._onItemClick, this, piece);
  75. this._enableHoverLink(itemGroup, item.indexInModelPieceList); // TODO Category
  76. var representValue = visualMapModel.getRepresentValue(piece);
  77. this._createItemSymbol(itemGroup, representValue, [0, 0, itemSize[0], itemSize[1]]);
  78. if (showLabel) {
  79. var visualState = this.visualMapModel.getValueState(representValue);
  80. itemGroup.add(new graphic.Text({
  81. style: {
  82. x: itemAlign === 'right' ? -textGap : itemSize[0] + textGap,
  83. y: itemSize[1] / 2,
  84. text: piece.text,
  85. verticalAlign: 'middle',
  86. align: itemAlign,
  87. font: textFont,
  88. fill: textFill,
  89. opacity: visualState === 'outOfRange' ? 0.5 : 1
  90. }
  91. }));
  92. }
  93. thisGroup.add(itemGroup);
  94. }, this);
  95. endsText && this._renderEndsText(thisGroup, endsText[1], itemSize, showLabel, itemAlign);
  96. layout.box(visualMapModel.get('orient'), thisGroup, visualMapModel.get('itemGap'));
  97. this.renderBackground(thisGroup);
  98. this.positionGroup(thisGroup);
  99. };
  100. PiecewiseVisualMapView.prototype._enableHoverLink = function (itemGroup, pieceIndex) {
  101. var _this = this;
  102. itemGroup.on('mouseover', function () {
  103. return onHoverLink('highlight');
  104. }).on('mouseout', function () {
  105. return onHoverLink('downplay');
  106. });
  107. var onHoverLink = function (method) {
  108. var visualMapModel = _this.visualMapModel; // 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'); // Consider orient and inverse.
  159. var orient = visualMapModel.get('orient');
  160. var inverse = visualMapModel.get('inverse'); // Order of model pieceList is always [low, ..., high]
  161. if (orient === 'horizontal' ? inverse : !inverse) {
  162. viewPieceList.reverse();
  163. } // Origin order of endsText is [high, low]
  164. else if (endsText) {
  165. endsText = endsText.slice().reverse();
  166. }
  167. return {
  168. viewPieceList: viewPieceList,
  169. endsText: endsText
  170. };
  171. };
  172. PiecewiseVisualMapView.prototype._createItemSymbol = function (group, representValue, shapeParam) {
  173. group.add(createSymbol( // symbol will be string
  174. this.getControllerVisual(representValue, 'symbol'), shapeParam[0], shapeParam[1], shapeParam[2], shapeParam[3], // color will be string
  175. this.getControllerVisual(representValue, 'color')));
  176. };
  177. PiecewiseVisualMapView.prototype._onItemClick = function (piece) {
  178. var visualMapModel = this.visualMapModel;
  179. var option = visualMapModel.option;
  180. var selectedMode = option.selectedMode;
  181. if (!selectedMode) {
  182. return;
  183. }
  184. var selected = zrUtil.clone(option.selected);
  185. var newKey = visualMapModel.getSelectedMapKey(piece);
  186. if (selectedMode === 'single' || selectedMode === true) {
  187. selected[newKey] = true;
  188. zrUtil.each(selected, function (o, key) {
  189. selected[key] = key === newKey;
  190. });
  191. } else {
  192. selected[newKey] = !selected[newKey];
  193. }
  194. this.api.dispatchAction({
  195. type: 'selectDataRange',
  196. from: this.uid,
  197. visualMapId: this.visualMapModel.id,
  198. selected: selected
  199. });
  200. };
  201. PiecewiseVisualMapView.type = 'visualMap.piecewise';
  202. return PiecewiseVisualMapView;
  203. }(VisualMapView);
  204. export default PiecewiseVisualMapView;