From d1e84a6466925814088cac6b12fcb2c08f73c860 Mon Sep 17 00:00:00 2001 From: Yi Shen Date: Thu, 31 Oct 2019 01:19:13 +0800 Subject: [PATCH] feat(force): Add initial friction config in force layout #11024 (#11276) * feat(force): Add initial friction config in force layout #11024 * test(force): Add test case for the friction parameter in force layout --- src/chart/graph/GraphSeries.js | 2 + src/chart/graph/forceHelper.js | 5 +- src/chart/graph/forceLayout.js | 3 +- test/force-friction.html | 100 +++++++++++++++++++++++++++++++++ test/force.html | 14 +---- test/force2.html | 12 +--- test/force3.html | 16 +----- 7 files changed, 111 insertions(+), 41 deletions(-) create mode 100644 test/force-friction.html diff --git a/src/chart/graph/GraphSeries.js b/src/chart/graph/GraphSeries.js index a69f7aded0..2ea09b2c4e 100644 --- a/src/chart/graph/GraphSeries.js +++ b/src/chart/graph/GraphSeries.js @@ -215,6 +215,8 @@ var GraphSeries = echarts.extendSeriesModel({ // Node repulsion. Can be an array to represent range. repulsion: [0, 50], gravity: 0.1, + // Initial friction + friction: 0.6, // Edge length. Can be an array to represent range. edgeLength: 30, diff --git a/src/chart/graph/forceHelper.js b/src/chart/graph/forceHelper.js index afce6c7803..a50fa58140 100644 --- a/src/chart/graph/forceHelper.js +++ b/src/chart/graph/forceHelper.js @@ -69,11 +69,12 @@ export function forceLayout(nodes, edges, opts) { // var k = scale * Math.sqrt(width * height / nodes.length); // var k2 = k * k; - var friction = 0.6; + var initialFriction = opts.friction == null ? 0.6 : opts.friction; + var friction = initialFriction; return { warmUp: function () { - friction = 0.5; + friction = initialFriction * 0.8; }, setFixed: function (idx) { diff --git a/src/chart/graph/forceLayout.js b/src/chart/graph/forceLayout.js index eff23fb58a..e61555ac1d 100644 --- a/src/chart/graph/forceLayout.js +++ b/src/chart/graph/forceLayout.js @@ -97,7 +97,8 @@ export default function (ecModel) { var rect = coordSys.getBoundingRect(); var forceInstance = forceLayout(nodes, edges, { rect: rect, - gravity: forceModel.get('gravity') + gravity: forceModel.get('gravity'), + friction: forceModel.get('friction') }); var oldStep = forceInstance.step; forceInstance.step = function (cb) { diff --git a/test/force-friction.html b/test/force-friction.html new file mode 100644 index 0000000000..324bd160a5 --- /dev/null +++ b/test/force-friction.html @@ -0,0 +1,100 @@ + + + + + + + + + + + + + +
+
+ + + \ No newline at end of file diff --git a/test/force.html b/test/force.html index e330971dc4..37942263de 100644 --- a/test/force.html +++ b/test/force.html @@ -39,21 +39,9 @@ require([ 'echarts' - - // 'extension/dataTool/gexf', - - // 'echarts/chart/graph', - - // 'echarts/component/title', - // 'echarts/component/legend', - // 'echarts/component/geo', - // 'echarts/component/tooltip', - // 'echarts/component/visualMap' ], function (echarts) { - var chart = echarts.init(document.getElementById('main'), null, { - - }); + var chart = echarts.init(document.getElementById('main')); function createNodes(count) { var nodes = []; diff --git a/test/force2.html b/test/force2.html index ae7dee4d31..0bd5ebf112 100644 --- a/test/force2.html +++ b/test/force2.html @@ -41,21 +41,11 @@ 'echarts', 'extension/dataTool' - - // 'echarts/chart/graph', - - // 'echarts/component/title', - // 'echarts/component/legend', - // 'echarts/component/geo', - // 'echarts/component/tooltip', - // 'echarts/component/visualMap' ], function (echarts, dataTool) { var gexf = dataTool.gexf; - var chart = echarts.init(document.getElementById('main'), null, { - - }); + var chart = echarts.init(document.getElementById('main'); $.get('./data/les-miserables.gexf', function (xml) { var graph = gexf.parse(xml); diff --git a/test/force3.html b/test/force3.html index e7ad1ae60c..1d529dfe9b 100644 --- a/test/force3.html +++ b/test/force3.html @@ -38,22 +38,10 @@