Javascript d3.js使用相同的选择选项控制两个图形
所以我需要把两个d3js图放在同一个网站上,我想用相同的选择元素来控制它们 首先,当我设法以我想要的方式运行第一个图形时,我只是复制了第二个图形的脚本,但是在不同的容器中。结果糟透了。图形相互冲突,因为变量的命名方式相同 后来,我通过在每个变量名称的末尾添加Javascript d3.js使用相同的选择选项控制两个图形,javascript,jquery,html,graph,d3.js,Javascript,Jquery,Html,Graph,D3.js,所以我需要把两个d3js图放在同一个网站上,我想用相同的选择元素来控制它们 首先,当我设法以我想要的方式运行第一个图形时,我只是复制了第二个图形的脚本,但是在不同的容器中。结果糟透了。图形相互冲突,因为变量的命名方式相同 后来,我通过在每个变量名称的末尾添加\u 1和在变量名称的末尾添加\u 2来区分第一个图中的所有变量 现在图形显示正确。但是我的问题是我的更改函数,它在更改选择选项时被调用。当每个图形都有一个选择框时,一切都很好,但当我试图设置它,使一个选择框控制两个图形时,我遇到了麻烦 代码
\u 1
和在变量名称的末尾添加\u 2
来区分第一个图中的所有变量
现在图形显示正确。但是我的问题是我的更改函数,它在更改选择选项时被调用。当每个图形都有一个选择框时,一切都很好,但当我试图设置它,使一个选择框控制两个图形时,我遇到了麻烦
代码非常长,因此在接下来的部分中,我将只在这里列出我所讨论的部分代码。完整的代码和示例如下:
(另外,第一个图形的脚本是html的,我知道这不是JSFIDLE的wright方法,但我想反映真实的状态,如原始文档中所示)
因此,对于第一个示例,我必须注册选择框的更改:
d3.selectAll(".graph_change_1").on("change", change_1);
然后调用change_1
函数:
function change_1() {
graph_n_1 = this.value;
//remove guides
d3.selectAll(".guide_1").transition().duration(100).styleTween("opacity",
function() { return d3.interpolate(.5, 0); })
.remove()
if(graph_n_1 == "BPR"){
x_1.domain([0, dataX_1[0].cn_bpr_to]);
y_1.domain([0, dataY_1[0].bpr_to]);
}
if(graph_n_1 == "PR"){
x_1.domain([0, dataX_1[0].cn_pr_to]);
y_1.domain([0, dataY_1[0].pr_to]);
}
if(graph_n_1 == "assets"){
x_1.domain([0, dataX_1[0].cn_as_to]);
y_1.domain([0, dataY_1[0].as_to]);
}
// First transition the line & label to the new city.
var t0_1 = svg_1.transition().duration(750);
t0_1.selectAll(".line_1").attr("d", line_1);
// Then transition the y-axis.
yAxisRF_1 = yAxis_1.tickFormat( function(n){
if(graph_n_1 == "BPR"){ return n; }
if(graph_n_1 == "PR"){ return n; }
if(graph_n_1 == "assets"){
var nLen_1 = n.toString().length;
if(nLen_1> 12){
nLenS_1 = n/Math.pow(10,12);
addS_1 = "Bil.€";
}else if(nLen_1 > 9){
nLenS_1 = n/Math.pow(10,9);
addS_1 = "Md.€";
}else if(nLen_1 > 6){
nLenS_1 = n/Math.pow(10,6);
addS_1 = "Mio.€";
}else if(nLen_1 > 3){
nLenS_1 = n/Math.pow(10,3);
addS_1 = "T€";
}else{
nLenS_1 = 0/Math.pow(10,0);
addS_1 = "€";
}
var podlj_1 = nLen_1/3;
var podljR_1 = Math.round(podlj_1)
if (podlj_1 == podljR_1){
x2_1=0
}else{ x2_1=1 }
var Fn_1 = nLenS_1.toFixed(x2);
var FnR_1 = Fn_1.toString().replace(".0","").replace(".",",");
var FaddT_1 = addS_1;
return FnR_1+" "+FaddT_1;
}
});
xAxisRF_1 = xAxis_1.tickValues(x_1.domain())
var t1_1 = t0_1.transition();
t1_1.selectAll(".desc_val_1").text( function(){
if(graph_n_1 == "BPR"){ return "Business Page Rank (BPR)"; }
if(graph_n_1 == "PR"){ return "Page Rank (PR)"; }
if(graph_n_1 == "assets"){ return "Euro (€)"; }
})
t1_1.selectAll(".line_1").attr("d", line_1);
t1_1.selectAll(".y.axis_1").call(yAxisRF_1);
t1_1.selectAll(".x.axis_1").call(xAxisRF_1);
line_1.interpolate("basis")
.x(function(d) {
if(graph_n_1 == "BPR"){ return x_1(d.cn_bpr); }
if(graph_n_1 == "PR"){ return x_1(d.cn_pr); }
if(graph_n_1 == "assets"){ return x_1(d.cn_as); }
})
.y(function(d) { return y_1(d[graph_n_1]); });
svg_1.append("g")
.attr("class", "guide_1")
.data(dataC_1)
.append("line")
.attr("x1", function(d) {
if(graph_n_1 == "BPR"){ return +x_1(d.x3); }
if(graph_n_1 == "PR"){ return +x_1(d.x2); }
if(graph_n_1 == "assets"){ return +x_1(d.x1); }
})
.attr("x2", function(d) {
if(graph_n_1 == "BPR"){ return +x_1(d.x3); }
if(graph_n_1 == "PR"){ return +x_1(d.x2); }
if(graph_n_1 == "assets"){ return +x_1(d.x1); }
})
.attr("y1", function(d) {
if(graph_n_1 == "BPR"){ return +y_1(d.y3)+4; }
if(graph_n_1 == "PR"){ return +y_1(d.y2)+4; }
if(graph_n_1 == "assets"){ return +y_1(d.y1)+4; }
})
.attr("y2", height_1)
.style("stroke", "#c45c28")
.transition().delay(500).duration(400).styleTween("opacity",
function() { return d3.interpolate(0, .5); })
svg_1.append("g")
.attr("class", "guide_1")
.data(dataC_1)
.append("line")
.attr("x1", function(d) {
if(graph_n_1 == "BPR"){ return +x_1(d.x3-5); }
if(graph_n_1 == "PR"){ return +x_1(d.x2-5); }
if(graph_n_1 == "assets"){ return +x_1(d.x1-5); }
})
.attr("x2", 0)
.attr("y1", function(d) {
if(graph_n_1 == "BPR"){ return +y_1(d.y3); }
if(graph_n_1 == "PR"){ return +y_1(d.y2); }
if(graph_n_1 == "assets"){ return +y_1(d.y1); }
})
.attr("y2", function(d) {
if(graph_n_1 == "BPR"){ return +y_1(d.y3); }
if(graph_n_1 == "PR"){ return +y_1(d.y2); }
if(graph_n_1 == "assets"){ return +y_1(d.y1); }
})
.style("stroke", "#c45c28")
.transition().delay(500).duration(400).styleTween("opacity",
function() { return d3.interpolate(0, .5); });
t1_1.selectAll(".circles_1").attr({
cx: function(d) {
if(graph_n_1 == "BPR"){ return +x_1(d.x3); }
if(graph_n_1 == "PR"){ return +x_1(d.x2); }
if(graph_n_1 == "assets"){ return +x_1(d.x1); }
},
cy: function(d) {
if(graph_n_1 == "BPR"){ return +y_1(d.y3); }
if(graph_n_1 == "PR"){ return +y_1(d.y2); }
if(graph_n_1 == "assets"){ return +y_1(d.y1); }
},
r: 4,
id: function(d) { return d.company; }
})
}
当我在第二个图形**h中以相同的名称添加相同的函数时,选择框的更改忽略了第一个图形
,只更改了**第二个图形
好吧,这是因为函数有相同的名称,我更改了函数名称,然后只选择了相同的选择框,但结果与之前的尝试相同
然后我添加了第二个选择框,并尝试用jQuery更改第一个选择框来控制第二个选择框。选择第一个图形时,第二个图形的值发生了变化,但第二个图形保持不变
很明显,插入值时没有更改事件
所以现在我不知道了。我需要改变什么才能用一个选择框控制两个图形
您可以检查我的情况并按上面的链接进行编辑:
欢迎提供任何帮助或建议。您需要一个从选择框调用的
更改
函数来更改两个图形。也就是说,从change_1
和change_2
中提取代码,并将其放入一个函数中。您需要一个change
函数,该函数从更改两个图形的选择框中调用。也就是说,从change_1
和change_2
中提取代码,并将其放入一个函数中。我就是这样做的,它工作起来很有魅力。。。即使我在你的建议之前就做了,请把答案放在下面:)