TypeError:d3.layout.cloud不是一个函数 [英] TypeError: d3.layout.cloud is not a function

查看:1373
本文介绍了TypeError:d3.layout.cloud不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从这里复制程式码。我的代码本质上与示例代码相同,除非我在我的服务器上运行d3.layout.cloud.js。当我运行它我得到一个类型错误d3.layout.cloud不是一个函数。这可能是什么原因?

I am copying code from here. My code is essentially identical to the example code except I am running d3.layout.cloud.js on my server. When I run it I am getting a type error that d3.layout.cloud is not a function. What could be the reasons for this?

    <!DOCTYPE html>
<meta charset="utf-8">
<script src="../lib/d3/d3.js"></script>
<script src="../d3.layout.cloud.js"></script>
<body>
<script>
  var fill = d3.scale.category20();

  d3.layout.cloud().size([300, 300])
      .words([
        "Hello", "world", "normally", "you", "want", "more", "words",
        "than", "this"].map(function(d) {
        return {text: d, size: 10 + Math.random() * 90};
      }))
      .rotate(function() { return ~~(Math.random() * 2) * 90; })
      .font("Impact")
      .fontSize(function(d) { return d.size; })
      .on("end", draw)
      .start();

  function draw(words) {
    d3.select("body").append("svg")
        .attr("width", 300)
        .attr("height", 300)
      .append("g")
        .attr("transform", "translate(150,150)")
      .selectAll("text")
        .data(words)
      .enter().append("text")
        .style("font-size", function(d) { return d.size + "px"; })
        .style("font-family", "Impact")
        .style("fill", function(d, i) { return fill(i); })
        .attr("text-anchor", "middle")
        .attr("transform", function(d) {
          return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
        })
        .text(function(d) { return d.text; });
  }
</script>


推荐答案

您确定放了d3.cloud。 layout.js在您的服务器上的正确文件夹中?

Are you sure you've put d3.cloud.layout.js in the correct folder on your server?

我编辑您的代码以指向存在的文件:

I edited your code to point to files that do exist:

<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://gist.github.com/emeeks/3361332/raw/61cf57523fe8cf314333e5f60cc266351fec2017/d3.layout.cloud.js"></script>

似乎工作正常:

http://jsfiddle.net/fHBsS/

要查看是否会导致您的问题,请使用chrome(ctrl-shift-j)打开控制台,看看是否有任何无法加载资源错误。

To see if this is causing your problem, open the console in chrome (ctrl-shift-j) and see if there are any "Failed to load resource" errors.

这篇关于TypeError:d3.layout.cloud不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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