javascript - 为什么我的折线图过渡看起来滞后?

标签 javascript d3.js line

我的代码怎么了?为什么折线图动画不能流畅连续?

我使用的是最新的 d3.js v5。动画看起来很滞后真的很奇怪,我似乎无法弄清楚如何让它平滑过渡。

这是我的代码:

<html>
<head>
    <title>Test</title>
    <script src="https://d3js.org/d3.v5.js"></script>
    <style>
        path {
            stroke: steelblue;
            stroke-width: 1;
            fill: none;
        }
    </style>
</head>
<body>

<p>
    <b>Size:</b> 300x30 &nbsp;&nbsp;<b>Interpolation:</b> basis &nbsp;&nbsp;<b>Animation:</b> true &nbsp;&nbsp;<b>Transition:</b>
    1000ms &nbsp;&nbsp;<b>Update Frequency:</b> 1000ms
<div id="graph1" class="aGraph" style="width:300px; height:30px;"></div>
</p>
<script>

    function displayGraphExample(id, width, height, updateDelay, transitionDelay) {
        var graph = d3.select(id)
            .append("svg:svg")
            .attr("width", "100%")
            .attr("height", "100%");
        var data = [3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 9];
        var x = d3.scaleLinear().domain([0, d3.max(data) * 5]).range([-5, width]);
        var y = d3.scaleLinear().domain([0, 10]).range([0, height]);
        var line = d3.line()
            .x(function (d, i) {
                return x(i);
            })
            .y(function (d) {
                return y(d);
            })
            .curve(d3.curveBasis);
        graph.append("svg:path").attr("d", line(data));
        function redrawWithAnimation() {
            graph.selectAll("path")
                .data([data])
                .attr("transform", "translate(" + x(1) + ")")
                .attr("d", line) 
                .transition() 
                .ease(d3.easeLinear)
                .duration(transitionDelay)
                .attr("transform", "translate(" + x(0) + ")");
        }
        d3.interval(function() {
           var v = data.shift(); 
           data.push(v); 
        	   redrawWithAnimation();
        }, updateDelay);
    }

    displayGraphExample("#graph1", 300, 30, 1000, 1000);
</script>

</body>
</html>

最佳答案

这里的问题是您为 updateDelaytransitionDelay 设置了完全相同的值。因此,您在前一个过渡结束之前调用一个过渡,这实际上取消了当前过渡。

简单而幼稚的解决方案是稍微增加updateDelay。例如,多 50 毫秒:

<html>
<head>
    <title>Test</title>
    <script src="https://d3js.org/d3.v5.js"></script>
    <style>
        path {
            stroke: steelblue;
            stroke-width: 1;
            fill: none;
        }
    </style>
</head>
<body>

<p>
    <b>Size:</b> 300x30 &nbsp;&nbsp;<b>Interpolation:</b> basis &nbsp;&nbsp;<b>Animation:</b> true &nbsp;&nbsp;<b>Transition:</b>
    1000ms &nbsp;&nbsp;<b>Update Frequency:</b> 1000ms
<div id="graph1" class="aGraph" style="width:300px; height:30px;"></div>
</p>
<script>

    function displayGraphExample(id, width, height, updateDelay, transitionDelay) {
        var graph = d3.select(id)
            .append("svg:svg")
            .attr("width", "100%")
            .attr("height", "100%");
        var data = [3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 9];
        var x = d3.scaleLinear().domain([0, d3.max(data) * 5]).range([-5, width]);
        var y = d3.scaleLinear().domain([0, 10]).range([0, height]);
        var line = d3.line()
            .x(function (d, i) {
                return x(i);
            })
            .y(function (d) {
                return y(d);
            })
            .curve(d3.curveBasis);
        graph.append("svg:path").attr("d", line(data));
        function redrawWithAnimation() {
            graph.selectAll("path")
                .data([data])
                .attr("transform", "translate(" + x(1) + ")")
                .attr("d", line) 
                .transition() 
                .ease(d3.easeLinear)
                .duration(transitionDelay)
                .attr("transform", "translate(" + x(0) + ")");
        }
        d3.interval(function() {
           var v = data.shift(); 
           data.push(v); 
        	   redrawWithAnimation();
        }, updateDelay);
    }

    displayGraphExample("#graph1", 300, 30, 1050, 1000);
</script>

</body>
</html>

但是,这不是最好的方法:这里惯用的解决方案是在最后一个过渡结束时调用一个新的过渡。例如:

.on("end", function() {
    var v = data.shift();
    data.push(v);
    redrawWithAnimation();
});

这样我们就不需要猜测或摆弄魔术数字和值,这可能会出错:转换本身会在完成时调用您想要的任何函数。

这是演示:

<html>

<head>
  <title>Test</title>
  <script src="https://d3js.org/d3.v5.js"></script>
  <style>
    path {
      stroke: steelblue;
      stroke-width: 1;
      fill: none;
    }
  </style>
</head>

<body>

  <p>
    <b>Size:</b> 300x30 &nbsp;&nbsp;<b>Interpolation:</b> basis &nbsp;&nbsp;<b>Animation:</b> true &nbsp;&nbsp;<b>Transition:</b> 1000ms &nbsp;&nbsp;<b>Update Frequency:</b> 1000ms
    <div id="graph1" class="aGraph" style="width:300px; height:30px;"></div>
  </p>
  <script>
    function displayGraphExample(id, width, height, updateDelay, transitionDelay) {
      var graph = d3.select(id)
        .append("svg:svg")
        .attr("width", "100%")
        .attr("height", "100%");
      var data = [3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 9];
      var x = d3.scaleLinear().domain([0, d3.max(data) * 5]).range([-5, width]);
      var y = d3.scaleLinear().domain([0, 10]).range([0, height]);
      var line = d3.line()
        .x(function(d, i) {
          return x(i);
        })
        .y(function(d) {
          return y(d);
        })
        .curve(d3.curveBasis);
      graph.append("svg:path").attr("d", line(data));
      redrawWithAnimation();

      function redrawWithAnimation() {
        graph.selectAll("path")
          .data([data])
          .attr("transform", "translate(" + x(1) + ")")
          .attr("d", line)
          .transition()
          .ease(d3.easeLinear)
          .duration(transitionDelay)
          .attr("transform", "translate(" + x(0) + ")")
          .on("end", function() {
            var v = data.shift();
            data.push(v);
            redrawWithAnimation();
          })
      }
    }

    displayGraphExample("#graph1", 300, 30, 1050, 1000);
  </script>

</body>

</html>

关于javascript - 为什么我的折线图过渡看起来滞后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51593943/

相关文章:

python - python中不规则点之间的坐标列表

javascript - HighCharts datetime xAxis 没有缺失值(周末)

javascript - 获取 d3 以根据时间戳显示正确的日期

javascript - 如何从 d3 中的数组中获取索引号?

Java:CSV 文件易于读​​/写

Python,行迭代器

javascript - 在 Chrome 控制台中检查 JS 变量的值

javascript - 无法让 div 淡入然后平滑切换到另一个页面

javascript - 如何在 React native 应用程序和网站中实现位置的实时跟踪?

javascript - 如何更新D3js动态图表 slider 上的句柄位置?