admin管理员组

文章数量:1355614

I'm using the D3 javascript library to dynamically change line thicknesses. What I want to achieve is a line that increase in thickness, and decreases in thickness, repeatedly constantly. To draw a line, I used the following code:

<!DOCTYPE html>
<html>
<head>  
      <script type="text/javascript" src=".js"></script>
</head>
<body>  
    <div id="D3line"></div>
            <script type="text/javascript">

            var lineSVG = d3.select("#D3line")  
                .append("svg:svg")
                .attr("width", 500)  
                .attr("height", 200);               

            var myLine = lineSVG.append("svg:line")
                .attr("x1", 60)
                .attr("y1", 60)
                .attr("x2", 450)
                .attr("y2", 150)
                .style("stroke", "rgb(6,120,155)")
                .style("stroke-opacity", 2);                


            </script>
</body>
</html>

Then, to change the line stroke thickness, I used the following code:

var lines = lineSVG.selectAll("line")   // select all lines 

function makeLinesThick()
{
    lines.transition().duration(500)
    .style("stroke-width", "5")
    .each("end", makeLinesThin);
}

function makeLinesThin(){
    lines.transition().duration(500)
    .style("stroke-width", "2")
    .each("end", makeLinesThick);
}

// call function to change lines
makeLinesThick()

However, I end up with this not running properly and getting an 'Unresponsive script' message in my browser. I'm not sure if I am structuring the callbacks properly in this case.

Edit: I changed my incorrect callback handling by removing the () in the .each() line.

I'm using the D3 javascript library to dynamically change line thicknesses. What I want to achieve is a line that increase in thickness, and decreases in thickness, repeatedly constantly. To draw a line, I used the following code:

<!DOCTYPE html>
<html>
<head>  
      <script type="text/javascript" src="http://mbostock.github./d3/d3.js"></script>
</head>
<body>  
    <div id="D3line"></div>
            <script type="text/javascript">

            var lineSVG = d3.select("#D3line")  
                .append("svg:svg")
                .attr("width", 500)  
                .attr("height", 200);               

            var myLine = lineSVG.append("svg:line")
                .attr("x1", 60)
                .attr("y1", 60)
                .attr("x2", 450)
                .attr("y2", 150)
                .style("stroke", "rgb(6,120,155)")
                .style("stroke-opacity", 2);                


            </script>
</body>
</html>

Then, to change the line stroke thickness, I used the following code:

var lines = lineSVG.selectAll("line")   // select all lines 

function makeLinesThick()
{
    lines.transition().duration(500)
    .style("stroke-width", "5")
    .each("end", makeLinesThin);
}

function makeLinesThin(){
    lines.transition().duration(500)
    .style("stroke-width", "2")
    .each("end", makeLinesThick);
}

// call function to change lines
makeLinesThick()

However, I end up with this not running properly and getting an 'Unresponsive script' message in my browser. I'm not sure if I am structuring the callbacks properly in this case.

Edit: I changed my incorrect callback handling by removing the () in the .each() line.

Share Improve this question edited Aug 5, 2012 at 16:02 djq asked Aug 5, 2012 at 15:08 djqdjq 15.3k46 gold badges126 silver badges158 bronze badges 7
  • I'm not familiar with d3, but in the .each() blocks, are you calling the opposite function within the .each()? Maybe that works differently in d3 than jQuery, but in jQuery, that would presumably create a recurring call/inverse call loop. If that's meant to pass a callback handler, take the () off so you pass a reference instead of calling the function, e.g.: .each("end", makeLinesThick) – Jared Farrish Commented Aug 5, 2012 at 15:12
  • Yes, I'm calling the opposite function within each .each(). I was trying to pass a callback handler. I took the () away (good to learn how to do it properly!), but I still get an unresponsive script error. – djq Commented Aug 5, 2012 at 15:18
  • @JaredFarrish that would be infinite recursion regardless of what .each does because .each is never even called – Esailija Commented Aug 5, 2012 at 15:18
  • @Esailija - I guess I don't quite follow you. – Jared Farrish Commented Aug 5, 2012 at 15:23
  • @JaredFarrish see: jsfiddle/zWHYf it doesn't matter what .asd does or if it even exists. – Esailija Commented Aug 5, 2012 at 15:29
 |  Show 2 more ments

3 Answers 3

Reset to default 6

The problem is that .each("end", ...) is called for every element that you're selecting. That is, makeLinesThin is called once for each line in makeLinesThick. This is what causes your browser to hang.

There are several ways you could make it work. You could change your code to do the transitions for each line individually (see the documentation for transition.each()) or you could schedule the transitions on all lines separately using settimeout(). Note in particular the documentation for transition.transition() -- you can schedule another transition before the current one is plete.

You might also want to have a look at d3.timer(), example here.

use .each("start") and .each("end") to set your transitions

var mutateLine =
    function(line, t, width, altWidth) {
        d3.select(line).transition().duration(t).style("stroke-width", width)
        .each("end", function() {
            if ( mutateLine ) mutateLine(line, t, altWidth, width);
        });
    }

// assumes lines is obtained from d3.selectAll()
var mutateLines =
    function(lines, t, width, altWidth) {
        if ( !mutateLine)
            return;
        lines[0].forEach(function(line) {
            mutateLine(line, t, width, altWidth);
        }
    };

// start mutating
mutateLines(lines, 500, 5, 2);

// stop mutating
mutateLine = null;

本文标签: javascriptCallbacks with D3Stack Overflow