最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: d3.layout.cloud is not a function - Stack Overflow

programmeradmin4浏览0评论

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>

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>
Share Improve this question asked Apr 2, 2013 at 23:57 jumbopapjumbopap 4,1475 gold badges30 silver badges49 bronze badges 1
  • Where is the d3.layout.cloud.js file located on your server? Same directory as your html file? Different directory? – Andy Thornton Commented Apr 3, 2013 at 3:15
Add a ment  | 

2 Answers 2

Reset to default 7

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/d3.v3.min.js"></script>
<script src="https://gist.github./emeeks/3361332/raw/61cf57523fe8cf314333e5f60cc266351fec2017/d3.layout.cloud.js"></script>

And it seems to work:

http://jsfiddle/fHBsS/

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.

This happened to me because I was loading the cloud layout JS before D3. D3 should be loaded first to fix.

发布评论

评论列表(0)

  1. 暂无评论