javascript - d3.js 动画线条绘制发生在与预期相反的方向

标签 javascript svg d3.js

我在 d3.js 中有一个简单的线图,我希望为线的绘制设置动画。 fiddle :here .动画方面有效,但我希望从左到右绘制线条,而不是像 fiddle 中那样从右到左绘制。为什么这条线是从头到尾画的而不是副

/*canvas setup*/
var margin = {top: 20, right: 20, bottom: 70, left: 100},
            width = 960 - margin.left - margin.right,
            height = 500 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg").attr("id","chart")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom)
            .append("g")
            .attr("class","container")
            .attr("transform",translate(margin.left,margin.bottom));
function translate(a,b){
    return "translate(" + a + "," + b + ")";
}
/*data*/

    var data = [{
        "time": "2013-03-12 15:09:04",
        "value": "5"
    }, {
        "time": "2013-03-12 14:59:06",
        "value": "65"
    }, {
        "time": "2013-03-12 14:49:04",
        "value": "15"
    }, {
        "time": "2013-03-12 14:39:06",
        "value": "25"
    },{
        "time": "2013-03-12 14:29:03",
        "value": "5"
    }];
    /*end data*/

    var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
        data.forEach(function(d) {
            d.time = parseDate(d.time);
            d.value = +d.value;
        });
        //create scales
        var xScale = d3.time.scale()
            .range([0,width])
            .domain(d3.extent(data,function(d){ return d.time})),
            yScale = d3.scale.linear()
                .range([height,0])
                .domain([0,d3.max(data,function(d){ return +d.value})]);

        //create axes
        var xAxis = d3.svg.axis()
            .scale(xScale)
            .orient("bottom")
            .tickFormat(d3.time.format("%H:%m"));

        //create line
        var line = d3.svg.line()
                .x(function(d){return xScale(d.time)})
                .y(function(d){ return yScale(d.value)});

        var _line; //path that will be appended to the chart

        var yAxis = d3.svg.axis()
            .scale(yScale)
            .orient("left");

        var _x = svg.append("g")
                        .attr("class","xAxis")
                        .attr("transform",translate(0,height))
                        .call(xAxis);
       var _y = svg.append("g")
                        .attr("class","yAxis")
                        .call(yAxis);
    //draw line
    function drawLine(){
        _line = svg.append("path")
                        .attr("d", line(data))
                        .attr("stroke", "steelblue")
                        .attr("stroke-width", "2")
                        .attr("fill", "none");

        var totalLength = _line.node().getTotalLength();
        _line.attr("stroke-dasharray", totalLength + " " + totalLength)
                            .attr("stroke-dashoffset", totalLength)
                            .transition()
                            .duration(750)
                            .ease("swing")
                            .attr("stroke-dashoffset", 0);

    }
    setTimeout(function(){
        drawLine();
    },500)

最佳答案

线条是从右边开始动画的,因为你从 totalLength 到 0 的偏移量设置动画。因此你的动画起点是:线条从末端开始,你的动画结束是线条开始于真正的开始。 您只需通过设置 .attr("stroke-dashoffset", -totalLength)

来扭转这个逻辑,让它以负偏移量开始

我更新了你的 fiddle有了这个小改动。

关于javascript - d3.js 动画线条绘制发生在与预期相反的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30790671/

相关文章:

javascript - 在 D3 强制布局中的节点上显示鼠标悬停时的 div 元素

php - 让用户呈现自己的 SVG 文件的安全隐患

javascript - D3 : how to select the div from multiple divs with the same class by the inner html in D3?

javascript - d3 中的 d3.time.interval.range() 不返回等距间隔

javascript - Mongoose .js : How to Implement Tree Structure via Population

ios - iOS Safari 的 SVG 文件大小限制

javascript - 如何将此 SVG 三 Angular 形转换为 "wandering"路径表示?

javascript - 需要在 JSON 格式的 API 输出中搜索值并打印整个数组内容

javascript - 剥离 JavaScript 的 URL

Javascript:如何从 ASP.NET 中的代码隐藏检查 bool 值