weixin_33716154 2015-06-05 17:36 采纳率: 0%
浏览 58

在d3.js导轨中显示JSON数据

This is taking me hours. It would be great if someone could show me exactly how to do this..

All Im trying to do is pass a JSON object to a html.erb page with an ajax call for a d3.js graph. Most of the explanations i have found use the json file whereas my setup uses a JSON object.

My code is as below: the model (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

The controller: user_controller.rb

class UserController < ApplicationController

def index
  render :json =>  User.including_relationships
end

def data
  render :json =>  User.including_relationships
end

my routes.rb is

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

The view (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

There are no errors, however all I am returned is a JSON formatted data. I would like the view to use the JSON object from

 render :json =>  User.including_relationships

and I though that putting it in the ajax script as

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

would be the way. But it isn't. I don't seem to be successfully pasing the JSON object to the ajax I don't think.

  • 写回答

1条回答 默认 最新

  • weixin_33749242 2015-06-05 17:56
    关注

    You don't need the jquery $.ajax here, just call it with d3.json. d3 doesn't care if it's a file or an API, as long as it returns valid JSON:

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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮