javascript - How to create d3 graph using data from Rails database - Stack Overflow

I learned to create d3 graph using the d3 book, in which all the dataset is included directly in the &l

I learned to create d3 graph using the d3 book, in which all the dataset is included directly in the <script> tag, which is in turn included in the html body.

I now want to use d3 in my rails app, but I don't know how to:

  1. Pass data from my Rails database to my d3 script
  2. How to "partial out" the javascript? I have seen materials on "unobstrusive javascript", but they are usually about AJAX form submission and I have a hard time converting that to graph drawing (i.e. including a <div> with graph drawn using app data).

There is still not an answer that deals with both of these issues yet, so hopefully this bees a reference for future Googlers.

I learned to create d3 graph using the d3 book, in which all the dataset is included directly in the <script> tag, which is in turn included in the html body.

I now want to use d3 in my rails app, but I don't know how to:

  1. Pass data from my Rails database to my d3 script
  2. How to "partial out" the javascript? I have seen materials on "unobstrusive javascript", but they are usually about AJAX form submission and I have a hard time converting that to graph drawing (i.e. including a <div> with graph drawn using app data).

There is still not an answer that deals with both of these issues yet, so hopefully this bees a reference for future Googlers.

Share Improve this question asked Jun 30, 2014 at 16:21 HeisenbergHeisenberg 8,83614 gold badges62 silver badges105 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

In my opinion requesting the data via AJAX would be the "cleanest" way to do that. You could use jQuery's ajax method to do a GET request to say '/get_data.json' ( which you have to add to your routes.rb), which would return you a JSON with the data.

Something like this.

//your JS file
$.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: '/get_data',
            dataType: 'json',
            data: "{}", 
            success: function (received_data) {
               var div_where_to_draw = "div.mygraph";
               your_d3_function(div_where_to_draw, received_data);
            },
            error: function (result) {
            }
    });
function draw_histogram(where_to_draw, data_to_draw){
  //Your d3js code here
}

(Note that this JS code is a remake of the answer here)

This is how your controller providing the data could look like(note I made a new controller, you might not want to do that and just use a new action in an existing controller):

#app/controllers/data_controller.rb
class DataController < ApplicationController
  def get_data
    respond_to do |format|
      format.json {}
    end
  end
end

And this is how your data could look like:

#app/views/data/get_data.json
[1,1,2,3,5,8,13,21,34,55]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744718160a4589747.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信