D3.js如何从d3.select()更新全局变量 [英] D3.js How to update a global variable from d3.select()

查看:495
本文介绍了D3.js如何从d3.select()更新全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了全局/局部变量问题.
在这里,我的代码基于 http://www.d3noob .org/2014/04/using-html-inputs-with-d3js.html

I'm stuck with a global/local variable issue.
Here, my code based on http://www.d3noob.org/2014/04/using-html-inputs-with-d3js.html

index.html

index.html

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <title>Input test (circle)</title>
    <head>
        <script src="http://d3js.org/d3.v3.min.js"></script>
    </head>
    <body>
        <p>
          <label for="nRadius" 
             style="display: inline-block; width: 240px; text-align: right">
             radius = <span id="nRadius-value">…</span>
          </label>
          <input type="range" min="1" max="150" id="nRadius">
        </p>
        <script src="./slider.js"></script>
    </body>
</html>

slider.js

slider.js

// update the circle radius
function update(nRadius) {
    // adjust the text on the range slider
    d3.select("#nRadius-value").text(nRadius);
    d3.select("#nRadius").property("value", nRadius);

    // update the circle radius
    circle.selectAll("circle").attr("r", nRadius);
    return nRadius;
    }

var width = 600;
var height = 300;     
var circle = d3.select("body")
            .append("svg")
            .attr("width", width)    
            .attr("height", height);

// Circle with no radius
circle.append("circle")
      .attr("cx", 300)
      .attr("cy", 150) 
      .style("fill", "none")   
      .style("stroke", "blue");

// Initial radius 
update(50);

// when the input range changes update the circle 
d3.select("#nRadius").on("input", function() {
    update(+this.value);
    });

此代码可以正常工作,但是我想做的是能够在d3.select外部导出"this.value",以便在其他功能中使用它.

This code works just fine, but what I'd like to do is to be able to export 'this.value' outside the d3.select, in order to use it in some other function.

下面显示的d3.select已经包含一些通过搜索我的问题发现的建议:

The d3.select showed below already includes some suggestions found by googling my question:

var currentRadius = 0;
d3.select("#nRadius").on("input", function() {
    currentRadius = +this.value
    console.log("Inside: " + currentRadius);
    currentRadius = update(currentRadius);
    });

console.log("Outside: " + currentRadius);

但这是行不通的.

推荐答案

问题在这里

//this is your global variable
var currentRadius = 0;

d3.select("#nRadius").on("input", function() {
    //you are changing the global value here on change event.
    currentRadius = +this.value
    console.log("Inside: " + currentRadius);
    currentRadius = update(currentRadius);
    //call your function 
    outside();
    });
//some function somewhere
function outside(){
   console.log(currentRadius)
}
//value of global variable when this executed is 0 
//because no change event has been called
console.log("Outside: " + currentRadius);

因此,简而言之,全局变量更改将在change函数中发生. 并且console.log("Outside: " + currentRadius);是在该事件被触发之前完成的.

So in short the global variable change will happen in the change function. And the console.log("Outside: " + currentRadius); is done much before that event got fired.

我希望这对您有帮助

这篇关于D3.js如何从d3.select()更新全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆