d3.js使用不同的数据创建相同的svg对象

时间:2017-12-06 04:51:39

标签: javascript jquery d3.js svg

我有一个脚本,工作正常并创建我需要的和弦图。 但是现在我想在另一个.svg上创建相同的div文件但是使用不同的矩阵。 我是否应该为新的svg复制相同的脚本,或者我可以更有效地执行此操作?

您可以找到我的代码here

现在我的.Js脚本使用drawChordWithMatrix(matrix_T1_T2)id chart上显示div的和弦 如何在drawChordWithMatrix(matrix_T2_T3)

上运行id chart1

1 个答案:

答案 0 :(得分:3)

我能想到的一种方式是

1)将SVG创建移动到drawChordWithMatrix

所以你需要传递你需要附加SVG的id。 像这样的function drawChordWithMatrix(matrix, id),你可以像这样在函数中创建你的SVG。

var svg = d3.select(id).append("svg")//selecting on basis of ID.
            .attr("width", (width + margin.left + margin.right))
            .attr("height", (height + margin.top + margin.bottom));

2)接下来将fade,fadeOnChord等所有函数移动到drawChordWithMatrix中,使它们都具有相同的范围。

工作代码here