Javascript 使用rails将哈希传递给js以生成图表
我正在尝试生成一个图表,其中包含每天注册到我网站的新用户的数量 我正在使用一个js,它接受一个变量来填充图表,例如:Javascript 使用rails将哈希传递给js以生成图表,javascript,ruby-on-rails,Javascript,Ruby On Rails,我正在尝试生成一个图表,其中包含每天注册到我网站的新用户的数量 我正在使用一个js,它接受一个变量来填充图表,例如: var pageviews = [ [1, randValue()], [2, randValue()], [3, 2 + randValue()], [4, 3 + randValue()],
var pageviews = [
[1, randValue()],
[2, randValue()],
[3, 2 + randValue()],
[4, 3 + randValue()],
[5, 5 + randValue()],
[6, 10 + randValue()],
[7, 15 + randValue()],
[8, 20 + randValue()],
[9, 25 + randValue()],
[10, 30 + randValue()],
[11, 35 + randValue()],
[12, 25 + randValue()],
[13, 15 + randValue()],
[14, 20 + randValue()],
[15, 45 + randValue()],
[16, 50 + randValue()],
[17, 65 + randValue()],
[18, 70 + randValue()],
[19, 85 + randValue()],
[20, 80 + randValue()],
[21, 75 + randValue()],
[22, 80 + randValue()],
[23, 75 + randValue()],
[24, 70 + randValue()],
[25, 65 + randValue()],
[26, 75 + randValue()],
[27, 80 + randValue()],
[28, 85 + randValue()],
[29, 90 + randValue()],
[30, 95 + randValue()]
];
我有一个用户表,我需要创建一个散列分组,并按日期统计上个月的用户
因此:
等等。。。第一个值是当月的日期,第二个值是当天提交注册的用户数
当我有我的散列时,如何将散列传递给JS?您可以尝试将
作为\u json
并将其填充到数据属性中:
<div id="page_views" data-users="<%= @user_registrations.as_json %>"></div>
假设我通过以下方式生成数据:
u = User.group("EXTRACT(DAY FROM created_at)").count.to_a
#=> [[26.0, 2], [27.0, 2]]
如果脚本位于.erb
文件中,我可以直接将该值传递给js变量(基于上面的示例输入)
像这样:
var pageviews = <%= u %>;
var页面浏览量=;
您可以将其存储在数据属性中我已经完成了,现在我有很多data-contents-01=“0”data-contents-02=“1”data-contents-03=“3”data-contents-04=“0”data-contents-05=“0”data-contents-06=“0”data-contents-07=“0”data-contents-08=“0”
我不知道如何在我的js文件中处理它们!
var pageviews = <%= u %>;