javascript - 带有 backbone.js 的 D3-cloud

标签 javascript ruby-on-rails backbone.js d3.js

我打算使用d3-cloud生成词云,尝试如下使用 在具有主干 View 的 Rails 应用程序中。我是 d3 和 backbone 的初学者,如果问题很愚蠢,请原谅。

来自 https://github.com/jasondavies/d3-cloud/blob/master/examples/simple.html

我一直收到这个错误 未捕获的类型错误:对象渲染没有方法“应用”

我的 Api 返回结果如下。

[{"text":"Timmy Doe","size":100},{"text":"John Doe","size":50}]

class Mongoauth.Views.TagCloud extends Backbone.View
  id = 0
  defaults: {
    w: 1,
    h: 5
  }
  initialize:->
      _.bindAll(this,"render");
      @collection.bind("reset",this.render);
      @collection.bind("change", this.render);
      this.chart = d3.layout.cloud().size([300, 300]).words(@collection.models).padding(5).rotate(->
                      ~~(Math.random() * 2) * 90
                    ).font("Impact").fontSize((d) ->
                      d.size
                    ).on("end", "render").start()
      # this.chart = d3.select(this.el).append('svg').attr("width", 300)
      #       .attr("height", 200) 
      #       .attr("viewBox","0 0 100 100");
      console.log(@collection)

  render: ->
      d3.select(this.el).append("svg").attr("width", 300).attr("height", 300).append("g").attr("transform", "translate(150,150)").selectAll("text").data(this.collection.models).enter().append("text").style("font-size", (d) ->
        d.size + "px"
      ).style("font-family", "Impact").style("fill", (d, i) ->
        d3.scale.category20() i
      ).attr("text-anchor", "middle").attr("transform", (d) ->
        "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"
      ).text (d) ->
        d.text
      this

最佳答案

您在行 .on("end", "render").start() 上传入一个字符串作为事件监听器。它需要是一个函数引用(例如this.render)。

关于javascript - 带有 backbone.js 的 D3-cloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18669559/

相关文章:

javascript - 返回带有变量名称的值

javascript - 删除下划线后的字符串

javascript - Rails AJAX 反馈,尤其是错误反馈

javascript - 如何用 Class CSS 覆盖内联 CSS?

javascript 自执行函数和作用域

ruby-on-rails - gabba gem 没有正确跟踪

ruby-on-rails - helper 和 partial 有什么区别?

javascript - 如何修改 Backbone 同步期间生成的 URL,但仅限于特定请求

javascript - 在 Backbone 中多次初始化 View

javascript - 将 Backbone 模型或导出的 json 对象传递给像 mustache 这样的模板系统