PiecewiseView.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. var zrUtil = require("zrender/lib/core/util");
  2. var VisualMapView = require("./VisualMapView");
  3. var graphic = require("../../util/graphic");
  4. var _symbol = require("../../util/symbol");
  5. var createSymbol = _symbol.createSymbol;
  6. var layout = require("../../util/layout");
  7. var helper = require("./helper");
  8. var PiecewiseVisualMapView = VisualMapView.extend({
  9. type: 'visualMap.piecewise',
  10. /**
  11. * @protected
  12. * @override
  13. */
  14. doRender: function () {
  15. var thisGroup = this.group;
  16. thisGroup.removeAll();
  17. var visualMapModel = this.visualMapModel;
  18. var textGap = visualMapModel.get('textGap');
  19. var textStyleModel = visualMapModel.textStyleModel;
  20. var textFont = textStyleModel.getFont();
  21. var textFill = textStyleModel.getTextColor();
  22. var itemAlign = this._getItemAlign();
  23. var itemSize = visualMapModel.itemSize;
  24. var viewData = this._getViewData();
  25. var endsText = viewData.endsText;
  26. var showLabel = zrUtil.retrieve(visualMapModel.get('showLabel', true), !endsText);
  27. endsText && this._renderEndsText(thisGroup, endsText[0], itemSize, showLabel, itemAlign);
  28. zrUtil.each(viewData.viewPieceList, renderItem, this);
  29. endsText && this._renderEndsText(thisGroup, endsText[1], itemSize, showLabel, itemAlign);
  30. layout.box(visualMapModel.get('orient'), thisGroup, visualMapModel.get('itemGap'));
  31. this.renderBackground(thisGroup);
  32. this.positionGroup(thisGroup);
  33. function renderItem(item) {
  34. var piece = item.piece;
  35. var itemGroup = new graphic.Group();
  36. itemGroup.onclick = zrUtil.bind(this._onItemClick, this, piece);
  37. this._enableHoverLink(itemGroup, item.indexInModelPieceList);
  38. var representValue = visualMapModel.getRepresentValue(piece);
  39. this._createItemSymbol(itemGroup, representValue, [0, 0, itemSize[0], itemSize[1]]);
  40. if (showLabel) {
  41. var visualState = this.visualMapModel.getValueState(representValue);
  42. itemGroup.add(new graphic.Text({
  43. style: {
  44. x: itemAlign === 'right' ? -textGap : itemSize[0] + textGap,
  45. y: itemSize[1] / 2,
  46. text: piece.text,
  47. textVerticalAlign: 'middle',
  48. textAlign: itemAlign,
  49. textFont: textFont,
  50. textFill: textFill,
  51. opacity: visualState === 'outOfRange' ? 0.5 : 1
  52. }
  53. }));
  54. }
  55. thisGroup.add(itemGroup);
  56. }
  57. },
  58. /**
  59. * @private
  60. */
  61. _enableHoverLink: function (itemGroup, pieceIndex) {
  62. itemGroup.on('mouseover', zrUtil.bind(onHoverLink, this, 'highlight')).on('mouseout', zrUtil.bind(onHoverLink, this, 'downplay'));
  63. function onHoverLink(method) {
  64. var visualMapModel = this.visualMapModel;
  65. visualMapModel.option.hoverLink && this.api.dispatchAction({
  66. type: method,
  67. batch: helper.convertDataIndex(visualMapModel.findTargetDataIndices(pieceIndex))
  68. });
  69. }
  70. },
  71. /**
  72. * @private
  73. */
  74. _getItemAlign: function () {
  75. var visualMapModel = this.visualMapModel;
  76. var modelOption = visualMapModel.option;
  77. if (modelOption.orient === 'vertical') {
  78. return helper.getItemAlign(visualMapModel, this.api, visualMapModel.itemSize);
  79. } else {
  80. // horizontal, most case left unless specifying right.
  81. var align = modelOption.align;
  82. if (!align || align === 'auto') {
  83. align = 'left';
  84. }
  85. return align;
  86. }
  87. },
  88. /**
  89. * @private
  90. */
  91. _renderEndsText: function (group, text, itemSize, showLabel, itemAlign) {
  92. if (!text) {
  93. return;
  94. }
  95. var itemGroup = new graphic.Group();
  96. var textStyleModel = this.visualMapModel.textStyleModel;
  97. itemGroup.add(new graphic.Text({
  98. style: {
  99. x: showLabel ? itemAlign === 'right' ? itemSize[0] : 0 : itemSize[0] / 2,
  100. y: itemSize[1] / 2,
  101. textVerticalAlign: 'middle',
  102. textAlign: showLabel ? itemAlign : 'center',
  103. text: text,
  104. textFont: textStyleModel.getFont(),
  105. textFill: textStyleModel.getTextColor()
  106. }
  107. }));
  108. group.add(itemGroup);
  109. },
  110. /**
  111. * @private
  112. * @return {Object} {peiceList, endsText} The order is the same as screen pixel order.
  113. */
  114. _getViewData: function () {
  115. var visualMapModel = this.visualMapModel;
  116. var viewPieceList = zrUtil.map(visualMapModel.getPieceList(), function (piece, index) {
  117. return {
  118. piece: piece,
  119. indexInModelPieceList: index
  120. };
  121. });
  122. var endsText = visualMapModel.get('text'); // Consider orient and inverse.
  123. var orient = visualMapModel.get('orient');
  124. var inverse = visualMapModel.get('inverse'); // Order of model pieceList is always [low, ..., high]
  125. if (orient === 'horizontal' ? inverse : !inverse) {
  126. viewPieceList.reverse();
  127. } // Origin order of endsText is [high, low]
  128. else if (endsText) {
  129. endsText = endsText.slice().reverse();
  130. }
  131. return {
  132. viewPieceList: viewPieceList,
  133. endsText: endsText
  134. };
  135. },
  136. /**
  137. * @private
  138. */
  139. _createItemSymbol: function (group, representValue, shapeParam) {
  140. group.add(createSymbol(this.getControllerVisual(representValue, 'symbol'), shapeParam[0], shapeParam[1], shapeParam[2], shapeParam[3], this.getControllerVisual(representValue, 'color')));
  141. },
  142. /**
  143. * @private
  144. */
  145. _onItemClick: function (piece) {
  146. var visualMapModel = this.visualMapModel;
  147. var option = visualMapModel.option;
  148. var selected = zrUtil.clone(option.selected);
  149. var newKey = visualMapModel.getSelectedMapKey(piece);
  150. if (option.selectedMode === 'single') {
  151. selected[newKey] = true;
  152. zrUtil.each(selected, function (o, key) {
  153. selected[key] = key === newKey;
  154. });
  155. } else {
  156. selected[newKey] = !selected[newKey];
  157. }
  158. this.api.dispatchAction({
  159. type: 'selectDataRange',
  160. from: this.uid,
  161. visualMapId: this.visualMapModel.id,
  162. selected: selected
  163. });
  164. }
  165. });
  166. var _default = PiecewiseVisualMapView;
  167. module.exports = _default;