如何将Ruby数组传递给Javascript来制作折线图 [英] How do I pass a Ruby array to Javascript to make a line graph

查看:104
本文介绍了如何将Ruby数组传递给Javascript来制作折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Ruby on Rails 2.3.14应用程序中创建一个网页来显示线条图。我找到了一个名为JS Charts的工具,它允许我使用Javascript创建漂亮的图形,但是我无法将它发送所需的数据。这是制作静态线条图的方法:

 < script type =text / javascript> 
var myData = new Array([1,395],[2,244],[3,223],[4,210],[5,238],[6,223],[7,275 ],[8,31]);
var myChart = new JSChart('chartcontainer','line');
myChart.setDataArray(myData);
myChart.draw();
< / script>

我将该代码放入stats.html.erb中,并显示出来。但是,我需要它来显示我提供的线图数据。在控制器中创建一个2维数组:

 >> @a 
=> [[1,335],[2,244],[3,223],[4,210],[5,238],[6,223],[7,275],[8,31]]

我应该可以在视图中使用该变量,并将 var myData



  var myData =<%= @a%> ;; 

我试过其他的东西,比如:

  var myData = JSON.parse(<%= @ a.to_json%>); 

但没有任何效果。有什么我可以做的吗?

编辑:

控制器传入的数组有问题视图(@a),它是空的。我可以使用:

  var myData = JSON.parse(<%= @ a.to_json%> ); 

显示线图并传递正确的数据。

解决方案

看起来像你有工作,但你可以清理一些东西:

 <%= javascript_tag do%> 
window.myData =<%= raw @ a.to_json%>;
<%end%>

或者在rails 3中,您可以使用超级HTML5 savy并使用 data 帮助者将您的数据添加为某个html标记的att:

 <%= content_tagdiv ,id:myDiv,data:{stuff:@a} do%> 
<! - 一些html资料...-->
<%end%>

然后在javascript中(使用jQuery):

  var myData = $('#myDiv')。data(stuff)

,对于超级热衷者,请查看这个railscast


I am trying to make a webpage to display line graphs in my Ruby on Rails 2.3.14 application. I found a tool, called JS Charts, which allows me to create nice graphs using Javascript, but I am having trouble sending it the data it needs. Here is the way to make a static line graph:

<script type="text/javascript">
  var myData = new Array([1, 395], [2, 244], [3, 223], [4, 210], [5, 238], [6, 223], [7, 275], [8, 31]);
  var myChart = new JSChart('chartcontainer', 'line');
  myChart.setDataArray(myData);
  myChart.draw();
</script>

I put that code into stats.html.erb, and it shows up. However, I need it to display line graph data that I provide it. A 2 dimensional array is created in the controller:

>> @a
=> [[1, 395], [2, 244], [3, 223], [4, 210], [5, 238], [6, 223], [7, 275], [8, 31]]

I should be able to use that variable in the view, and set var myData to it, with something like:

var myData = "<%= @a %>";

I tried other things like:

var myData = JSON.parse( "<%= @a.to_json %>" );

but nothing seems to work. Is there anything I can do?

EDIT:

There was an issue with the array the controller passed into the view (@a), which was empty. I was able to use:

var myData = JSON.parse( "<%= @a.to_json %>" );

to display the line graph with the right data being passed into the view.

解决方案

Seem's like you got it working but you can clean things up a bit:

<%= javascript_tag do %>
  window.myData = <%=raw @a.to_json %>;
<% end %>

or in rails 3 you can be super HTML5 savy and use the data helpers to add your data as an att of some html tag:

<%= content_tag "div", id: "myDiv", data: {stuff: @a} do %>
<!-- some html stuff...-->
<% end %>

and then in then in the javascript (with jQuery):

var myData = $('#myDiv').data("stuff")

and for the super keen, check out this railscast

这篇关于如何将Ruby数组传递给Javascript来制作折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆