FunnelView.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 graphic = require("../../util/graphic");
  20. var zrUtil = require("zrender/lib/core/util");
  21. var ChartView = require("../../view/Chart");
  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. /**
  41. * Piece of pie including Sector, Label, LabelLine
  42. * @constructor
  43. * @extends {module:zrender/graphic/Group}
  44. */
  45. function FunnelPiece(data, idx) {
  46. graphic.Group.call(this);
  47. var polygon = new graphic.Polygon();
  48. var labelLine = new graphic.Polyline();
  49. var text = new graphic.Text();
  50. this.add(polygon);
  51. this.add(labelLine);
  52. this.add(text);
  53. this.highDownOnUpdate = function (fromState, toState) {
  54. if (toState === 'emphasis') {
  55. labelLine.ignore = labelLine.hoverIgnore;
  56. text.ignore = text.hoverIgnore;
  57. } else {
  58. labelLine.ignore = labelLine.normalIgnore;
  59. text.ignore = text.normalIgnore;
  60. }
  61. };
  62. this.updateData(data, idx, true);
  63. }
  64. var funnelPieceProto = FunnelPiece.prototype;
  65. var opacityAccessPath = ['itemStyle', 'opacity'];
  66. funnelPieceProto.updateData = function (data, idx, firstCreate) {
  67. var polygon = this.childAt(0);
  68. var seriesModel = data.hostModel;
  69. var itemModel = data.getItemModel(idx);
  70. var layout = data.getItemLayout(idx);
  71. var opacity = data.getItemModel(idx).get(opacityAccessPath);
  72. opacity = opacity == null ? 1 : opacity; // Reset style
  73. polygon.useStyle({});
  74. if (firstCreate) {
  75. polygon.setShape({
  76. points: layout.points
  77. });
  78. polygon.setStyle({
  79. opacity: 0
  80. });
  81. graphic.initProps(polygon, {
  82. style: {
  83. opacity: opacity
  84. }
  85. }, seriesModel, idx);
  86. } else {
  87. graphic.updateProps(polygon, {
  88. style: {
  89. opacity: opacity
  90. },
  91. shape: {
  92. points: layout.points
  93. }
  94. }, seriesModel, idx);
  95. } // Update common style
  96. var itemStyleModel = itemModel.getModel('itemStyle');
  97. var visualColor = data.getItemVisual(idx, 'color');
  98. polygon.setStyle(zrUtil.defaults({
  99. lineJoin: 'round',
  100. fill: visualColor
  101. }, itemStyleModel.getItemStyle(['opacity'])));
  102. polygon.hoverStyle = itemStyleModel.getModel('emphasis').getItemStyle();
  103. this._updateLabel(data, idx);
  104. graphic.setHoverStyle(this);
  105. };
  106. funnelPieceProto._updateLabel = function (data, idx) {
  107. var labelLine = this.childAt(1);
  108. var labelText = this.childAt(2);
  109. var seriesModel = data.hostModel;
  110. var itemModel = data.getItemModel(idx);
  111. var layout = data.getItemLayout(idx);
  112. var labelLayout = layout.label;
  113. var visualColor = data.getItemVisual(idx, 'color');
  114. graphic.updateProps(labelLine, {
  115. shape: {
  116. points: labelLayout.linePoints || labelLayout.linePoints
  117. }
  118. }, seriesModel, idx);
  119. graphic.updateProps(labelText, {
  120. style: {
  121. x: labelLayout.x,
  122. y: labelLayout.y
  123. }
  124. }, seriesModel, idx);
  125. labelText.attr({
  126. rotation: labelLayout.rotation,
  127. origin: [labelLayout.x, labelLayout.y],
  128. z2: 10
  129. });
  130. var labelModel = itemModel.getModel('label');
  131. var labelHoverModel = itemModel.getModel('emphasis.label');
  132. var labelLineModel = itemModel.getModel('labelLine');
  133. var labelLineHoverModel = itemModel.getModel('emphasis.labelLine');
  134. var visualColor = data.getItemVisual(idx, 'color');
  135. graphic.setLabelStyle(labelText.style, labelText.hoverStyle = {}, labelModel, labelHoverModel, {
  136. labelFetcher: data.hostModel,
  137. labelDataIndex: idx,
  138. defaultText: data.getName(idx),
  139. autoColor: visualColor,
  140. useInsideStyle: !!labelLayout.inside
  141. }, {
  142. textAlign: labelLayout.textAlign,
  143. textVerticalAlign: labelLayout.verticalAlign
  144. });
  145. labelText.ignore = labelText.normalIgnore = !labelModel.get('show');
  146. labelText.hoverIgnore = !labelHoverModel.get('show');
  147. labelLine.ignore = labelLine.normalIgnore = !labelLineModel.get('show');
  148. labelLine.hoverIgnore = !labelLineHoverModel.get('show'); // Default use item visual color
  149. labelLine.setStyle({
  150. stroke: visualColor
  151. });
  152. labelLine.setStyle(labelLineModel.getModel('lineStyle').getLineStyle());
  153. labelLine.hoverStyle = labelLineHoverModel.getModel('lineStyle').getLineStyle();
  154. };
  155. zrUtil.inherits(FunnelPiece, graphic.Group);
  156. var FunnelView = ChartView.extend({
  157. type: 'funnel',
  158. render: function (seriesModel, ecModel, api) {
  159. var data = seriesModel.getData();
  160. var oldData = this._data;
  161. var group = this.group;
  162. data.diff(oldData).add(function (idx) {
  163. var funnelPiece = new FunnelPiece(data, idx);
  164. data.setItemGraphicEl(idx, funnelPiece);
  165. group.add(funnelPiece);
  166. }).update(function (newIdx, oldIdx) {
  167. var piePiece = oldData.getItemGraphicEl(oldIdx);
  168. piePiece.updateData(data, newIdx);
  169. group.add(piePiece);
  170. data.setItemGraphicEl(newIdx, piePiece);
  171. }).remove(function (idx) {
  172. var piePiece = oldData.getItemGraphicEl(idx);
  173. group.remove(piePiece);
  174. }).execute();
  175. this._data = data;
  176. },
  177. remove: function () {
  178. this.group.removeAll();
  179. this._data = null;
  180. },
  181. dispose: function () {}
  182. });
  183. var _default = FunnelView;
  184. module.exports = _default;