【问题标题】:show JSON data in d3.js rails在 d3.js rails 中显示 JSON 数据
【发布时间】:2015-06-05 17:36:23
【问题描述】:

这需要我几个小时。如果有人能准确地告诉我如何做到这一点,那就太好了..

我想要做的就是将一个 JSON 对象传递给一个带有 ajax 调用的 html.erb 页面,以获取 d3.js 图形。我发现的大多数解释都使用 json 文件,而我的设置使用 JSON 对象。

我的代码如下:模型(user.rb)

class User < ActiveRecord::Base
    has_many :relationships
end

class User
  def self.including_relationships
    User.joins("INNER JOIN relationships ON users.id = relationships.user_id").select("users.name, relationships.user_id, relationships.followsid,users.value").each_with_object(Hash.new{|h, k| h[k] = []}) do |a, obj| 
      obj['nodes'] << a.slice('name')
      obj['links'] << a.slice('user_id', 'followsid', 'value')
    end
  end
end

控制器:user_controller.rb

class UserController < ApplicationController

def index
  render :json =>  User.including_relationships
end

def data
  render :json =>  User.including_relationships
end

我的 routes.rb 是

Rails.application.routes.draw do
  get 'user/data' => 'user#data'
  resources :user
end

视图 (index.html.erb)

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Basic HTML5 Template</title>
  <link href="stylesheets/style.css" rel="stylesheet" type="text/css" media="screen" />
  <script src="example.js"></script>
</head>
<body>
<script>
$.ajax({
  type: 'GET', 
  url: 'localhost:3000/user/index', 
  success: function(data) {
    var miserables = data
  }
})

<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var width = 960,
    height = 500;

var color = d3.scale.category20();

var force = d3.layout.force()
    .charge(-120)
    .linkDistance(30)
    .size([width, height]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

d3.json(miserables, function(error, graph) {
  force
      .nodes(graph.nodes)
      .links(graph.links)
      .start();

  var link = svg.selectAll(".link")
      .data(graph.links)
    .enter().append("line")
      .attr("class", "link")
      .style("stroke-width", function(d) { return Math.sqrt(d.value); });

  var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", 5)
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

  node.append("title")
      .text(function(d) { return d.name; });

  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });
});

</script>

</script>

</body>
</html>


    end

没有错误,但是我返回的只是 JSON 格式的数据。我希望视图使用来自

的 JSON 对象
 render :json =>  User.including_relationships

虽然我把它放在 ajax 脚本中

$.ajax({
      type: 'GET', 
      url: 'localhost:3000/user/index', 
      success: function(data) {
        var miserables = data
      }
    })

应该是这样的。但事实并非如此。我似乎没有成功地将 JSON 对象传递给我不认为的 ajax。

【问题讨论】:

    标签: ruby-on-rails ajax json d3.js


    【解决方案1】:

    这里不需要 jquery $.ajax,只需使用 d3.json 调用它即可。 d3 不关心它是文件还是 API,只要它返回有效的 JSON:

    d3.json('localhost:3000/user/index', function(error, graph) {
    
       // rest of graph code...
    
    }
    

    【讨论】:

    • 好的,这可以简化事情,但是当我调用 /user/data 或 /user 时,我只会返回 JSON 而不是视图。如何获取使用 JSON 调用的视图?
    • @SebastianZeki,我不明白这个问题。您转到它呈现视图的 URL,视图中的 JavaScript 进行 AJAX 调用,完成后,您绘制绘图。
    • 这就是代码应该做的。但是,当我调用视图时,我只是获取 JSON 数据。我不明白为什么我没有将 JSON 传递到视图中,所以我可以看到图表
    • @SebastianZeki,我不是 Rails 人,但 def index render :json =&gt; User.including_relationships end,这应该返回视图,不是吗?您让它返回 JSON 并执行与 def data 相同的操作
    • 是的,def 索引应该返回视图,但由于某种原因它没有返回,我只得到 JSON。也许是我的路线错了,但我不这么认为。我已经删除了 def 数据——虽然它对查看索引页面 JSON 没有任何影响
    猜你喜欢
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    相关资源
    最近更新 更多