chordCircularLayout.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 zrUtil = require("zrender/lib/core/util");
  20. var _number = require("../../util/number");
  21. var parsePercent = _number.parsePercent;
  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. * @param {module:echarts/data/Graph} graph
  42. */
  43. function layout(graphs, opts) {
  44. if (!zrUtil.isArray(graphs)) {
  45. graphs = [graphs];
  46. }
  47. var graph0 = graphs[0];
  48. var groups = []; // Init groups
  49. graph0.eachNode(function (node) {
  50. var group = {
  51. size: 0,
  52. subGroups: [],
  53. node: node
  54. };
  55. groups.push(group);
  56. });
  57. zrUtil.each(graphs, function (graph) {
  58. graph.eachEdge(function (edge) {
  59. var g1 = groups[edge.node1.dataIndex];
  60. g1.size += edge.getValue('value') || 0;
  61. g1.subGroups.push({
  62. size: edge.getValue('value'),
  63. edge: edge
  64. });
  65. });
  66. });
  67. var sumSize = zrUtil.reduce(groups, function (sumSize, group) {
  68. return sumSize + group.size;
  69. }, 0);
  70. if (opts.sort && opts.sort !== 'none') {
  71. groups.sort(compareGroups);
  72. if (opts.sort === 'descending') {
  73. groups.reverse();
  74. }
  75. }
  76. var unitAngle = (Math.PI * 2 - opts.padding * graph0.data.count()) / sumSize;
  77. var angle = opts.startAngle * Math.PI / 180;
  78. var sign = opts.clockwise ? -1 : 1;
  79. zrUtil.each(groups, function (group) {
  80. if (opts.sortSub && opts.sortSub !== 'none') {
  81. group.subGroups.sort(compareGroups);
  82. if (opts.sortSub === 'descending') {
  83. group.subGroups.reverse();
  84. }
  85. }
  86. var endAngle = angle + sign * group.size * unitAngle;
  87. group.node.setLayout({
  88. startAngle: -angle,
  89. endAngle: -endAngle,
  90. cx: opts.cx,
  91. cy: opts.cy,
  92. r0: opts.r0,
  93. r: opts.r,
  94. clockwise: opts.clockwise
  95. });
  96. zrUtil.each(group.subGroups, function (subGroup) {
  97. var startAngle = angle;
  98. var endAngle = angle + sign * subGroup.size * unitAngle;
  99. var layout = subGroup.edge.getLayout() || {
  100. cx: opts.cx,
  101. cy: opts.cy,
  102. r: opts.r0,
  103. clockwise: opts.clockwise
  104. };
  105. layout.startAngle = -startAngle;
  106. layout.endAngle = -endAngle;
  107. subGroup.edge.setLayout(layout);
  108. angle = endAngle;
  109. });
  110. angle = endAngle + sign * opts.padding;
  111. });
  112. }
  113. var compareGroups = function (a, b) {
  114. return a.size - b.size;
  115. };
  116. function _default(ecModel, api, payload) {
  117. ecModel.eachSeriesByType('chord', function (chordSeries) {
  118. var graph = chordSeries.getGraph();
  119. var center = chordSeries.get('center');
  120. var radius = chordSeries.get('radius');
  121. var viewWidth = api.getWidth();
  122. var viewHeight = api.getHeight();
  123. var viewSize = Math.min(viewWidth, viewHeight) / 2;
  124. layout(graph, {
  125. sort: chordSeries.get('sort'),
  126. sortSub: chordSeries.get('sortSub'),
  127. padding: chordSeries.get('padding'),
  128. startAngle: chordSeries.get('startAngle'),
  129. clockwise: chordSeries.get('clockwise'),
  130. cx: parsePercent(center[0], viewWidth),
  131. cy: parsePercent(center[1], viewHeight),
  132. r0: parsePercent(radius[0], viewSize),
  133. r: parsePercent(radius[1], viewSize)
  134. });
  135. });
  136. }
  137. module.exports = _default;