ThemeRiverView.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 echarts = require("../../echarts");
  20. var _poly = require("../line/poly");
  21. var Polygon = _poly.Polygon;
  22. var graphic = require("../../util/graphic");
  23. var _util = require("zrender/lib/core/util");
  24. var bind = _util.bind;
  25. var extend = _util.extend;
  26. var DataDiffer = require("../../data/DataDiffer");
  27. /*
  28. * Licensed to the Apache Software Foundation (ASF) under one
  29. * or more contributor license agreements. See the NOTICE file
  30. * distributed with this work for additional information
  31. * regarding copyright ownership. The ASF licenses this file
  32. * to you under the Apache License, Version 2.0 (the
  33. * "License"); you may not use this file except in compliance
  34. * with the License. You may obtain a copy of the License at
  35. *
  36. * http://www.apache.org/licenses/LICENSE-2.0
  37. *
  38. * Unless required by applicable law or agreed to in writing,
  39. * software distributed under the License is distributed on an
  40. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  41. * KIND, either express or implied. See the License for the
  42. * specific language governing permissions and limitations
  43. * under the License.
  44. */
  45. var _default = echarts.extendChartView({
  46. type: 'themeRiver',
  47. init: function () {
  48. this._layers = [];
  49. },
  50. render: function (seriesModel, ecModel, api) {
  51. var data = seriesModel.getData();
  52. var group = this.group;
  53. var layerSeries = seriesModel.getLayerSeries();
  54. var layoutInfo = data.getLayout('layoutInfo');
  55. var rect = layoutInfo.rect;
  56. var boundaryGap = layoutInfo.boundaryGap;
  57. group.attr('position', [0, rect.y + boundaryGap[0]]);
  58. function keyGetter(item) {
  59. return item.name;
  60. }
  61. var dataDiffer = new DataDiffer(this._layersSeries || [], layerSeries, keyGetter, keyGetter);
  62. var newLayersGroups = {};
  63. dataDiffer.add(bind(process, this, 'add')).update(bind(process, this, 'update')).remove(bind(process, this, 'remove')).execute();
  64. function process(status, idx, oldIdx) {
  65. var oldLayersGroups = this._layers;
  66. if (status === 'remove') {
  67. group.remove(oldLayersGroups[idx]);
  68. return;
  69. }
  70. var points0 = [];
  71. var points1 = [];
  72. var color;
  73. var indices = layerSeries[idx].indices;
  74. for (var j = 0; j < indices.length; j++) {
  75. var layout = data.getItemLayout(indices[j]);
  76. var x = layout.x;
  77. var y0 = layout.y0;
  78. var y = layout.y;
  79. points0.push([x, y0]);
  80. points1.push([x, y0 + y]);
  81. color = data.getItemVisual(indices[j], 'color');
  82. }
  83. var polygon;
  84. var text;
  85. var textLayout = data.getItemLayout(indices[0]);
  86. var itemModel = data.getItemModel(indices[j - 1]);
  87. var labelModel = itemModel.getModel('label');
  88. var margin = labelModel.get('margin');
  89. if (status === 'add') {
  90. var layerGroup = newLayersGroups[idx] = new graphic.Group();
  91. polygon = new Polygon({
  92. shape: {
  93. points: points0,
  94. stackedOnPoints: points1,
  95. smooth: 0.4,
  96. stackedOnSmooth: 0.4,
  97. smoothConstraint: false
  98. },
  99. z2: 0
  100. });
  101. text = new graphic.Text({
  102. style: {
  103. x: textLayout.x - margin,
  104. y: textLayout.y0 + textLayout.y / 2
  105. }
  106. });
  107. layerGroup.add(polygon);
  108. layerGroup.add(text);
  109. group.add(layerGroup);
  110. polygon.setClipPath(createGridClipShape(polygon.getBoundingRect(), seriesModel, function () {
  111. polygon.removeClipPath();
  112. }));
  113. } else {
  114. var layerGroup = oldLayersGroups[oldIdx];
  115. polygon = layerGroup.childAt(0);
  116. text = layerGroup.childAt(1);
  117. group.add(layerGroup);
  118. newLayersGroups[idx] = layerGroup;
  119. graphic.updateProps(polygon, {
  120. shape: {
  121. points: points0,
  122. stackedOnPoints: points1
  123. }
  124. }, seriesModel);
  125. graphic.updateProps(text, {
  126. style: {
  127. x: textLayout.x - margin,
  128. y: textLayout.y0 + textLayout.y / 2
  129. }
  130. }, seriesModel);
  131. }
  132. var hoverItemStyleModel = itemModel.getModel('emphasis.itemStyle');
  133. var itemStyleModel = itemModel.getModel('itemStyle');
  134. graphic.setTextStyle(text.style, labelModel, {
  135. text: labelModel.get('show') ? seriesModel.getFormattedLabel(indices[j - 1], 'normal') || data.getName(indices[j - 1]) : null,
  136. textVerticalAlign: 'middle'
  137. });
  138. polygon.setStyle(extend({
  139. fill: color
  140. }, itemStyleModel.getItemStyle(['color'])));
  141. graphic.setHoverStyle(polygon, hoverItemStyleModel.getItemStyle());
  142. }
  143. this._layersSeries = layerSeries;
  144. this._layers = newLayersGroups;
  145. },
  146. dispose: function () {}
  147. }); // add animation to the view
  148. function createGridClipShape(rect, seriesModel, cb) {
  149. var rectEl = new graphic.Rect({
  150. shape: {
  151. x: rect.x - 10,
  152. y: rect.y - 10,
  153. width: 0,
  154. height: rect.height + 20
  155. }
  156. });
  157. graphic.initProps(rectEl, {
  158. shape: {
  159. width: rect.width + 20,
  160. height: rect.height + 20
  161. }
  162. }, seriesModel, cb);
  163. return rectEl;
  164. }
  165. module.exports = _default;