d3js Text Transitioin-示例代码不起作用 [英] d3js Text Transitioin - example code not working

查看:83
本文介绍了d3js Text Transitioin-示例代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是d3.js的新手,正在尝试执行d3示例中给出的一些示例代码.我尝试复制Text Transition-1(链接)示例代码.但是在控制台中遇到了此错误

I am new to d3.js and trying to execute some of the example codes given in the d3 examples. I tried copying the Text Transition-1 (link) example code. But came across this error in the console

text-transition.html:26未捕获的TypeError:
d3.select(...).transition(...).duration(...).on不是函数

text-transition.html:26 Uncaught TypeError:
d3.select(...).transition(...).duration(...).on is not a function

这是我执行的代码:

<!DOCTYPE html>
<html>
    <head>
        <title>Text Transition animation 1</title>
        <meta charset="utf-8">
        <style>
            h1 {
              font: 400 120px/500px "Helvetica Neue";
              text-align: center;
              width: 960px;
              height: 500px;
              margin: 0;
            }
        </style>
        <script type="text/javascript" src="d3/d3.min.js"></script>
    </head>
    <body>
        <h1>0</h1>
        <script type="text/javascript">
            console.log("the script is executing")
            var format = d3.format(",d");

            d3.select("h1")
              .transition()
                .duration(2500)
                .on("start", function repeat() {

                  d3.active(this)
                      .tween("text", function() {
                        var that = d3.select(this),
                            i = d3.interpolateNumber(that.text().replace(/,/g, ""), Math.random() * 1e6);
                        return function(t) { that.text(format(i(t))); };
                      })
                    .transition()
                      .delay(1500)
                      .on("start", repeat);
                });

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

我正在使用的d3js版本是3.5.17.在Google Chrome浏览器上执行代码段.

The d3js version I am using is 3.5.17. Executing the snippet on Google Chrome browser.

推荐答案

问题似乎出在d3版本上.

The problem is seems to be with the d3 version.

尝试使用<script src="//d3js.org/d3.v4.0.0-alpha.23.min.js"></script>.

来源: http://d3js.org/d3.v4. 0.0-alpha.23.min.js

console.log("the script is executing")
var format = d3.format(",d");

d3.select("h1")
  .transition()
  .duration(2500)
  .on("start", function repeat() {

    d3.active(this)
      .tween("text", function() {
        var that = d3.select(this),
          i = d3.interpolateNumber(that.text().replace(/,/g, ""), Math.random() * 1e6);
        return function(t) {
          that.text(format(i(t)));
        };
      })
      .transition()
      .delay(1500)
      .on("start", repeat);
  });

h1 {
  font: 400 120px/500px"Helvetica Neue";
  text-align: center;
  width: 960px;
  height: 500px;
  margin: 0;
}

<script src="//d3js.org/d3.v4.0.0-alpha.23.min.js"></script>
<h1>0</h1>

这篇关于d3js Text Transitioin-示例代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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