Rails。如何将值从数组传递到Google图表脚本。 [英] Rails. How to Pass values from an array to the google chart script.

查看:111
本文介绍了Rails。如何将值从数组传递到Google图表脚本。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails应用程式。我有一个项目和任务模型。任务有一个列project_id来设置他们属于的项目,几个完整性列表示任务在一段时间内的完整性(所以我有一个列'completeness_at_3hours','completeness_at_6hours'等),表示从1到5级别

Rails App. I have a Project and a Task Model. Tasks have a column project_id to set the project they belong to, and several completeness columns to indicate completeness of the task over the time (so I have a column 'completeness_at_3hours', 'completeness_at_6hours' etc), to indicate from 1 to 5 the level of completeness of the task.

我现在需要用图表来描述属于一个项目的任务的完整性。

I now need to graph, with google charts the completeness over the time of the tasks that belong to a project.

在我的项目控制器中有

@tasks = Tasks.where("project_id = ?", params[:id])

现在我有了所选项目的任务,在@tasks中,我如何将数据包含在google图表脚本中?

So, now that I have the tasks of the selected project, in @tasks, how would I include the data in the google chart script?

function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Time', '<%= @task.name %>', '????????'],
          ['<%= @task.created_at %>',  <%= @task.zerohours %>, '?????????'],
          ['<%= @task.created_at + 3.hours %>',  <%= @task.threehours %>, '????????'],
          ['<%= @task.created_at + 6.hours %>',  <%= @task.sixhours %>, '????????'],
          ['<%= @task.created_at + 9.hours %>',  <%= @task.ninehours %>, '????????'],
          ['<%= @task.created_at + 12.hours %>',  <%= @task.twelvehours %>, '????????']
        ]);

这只适用于一个任务(刚刚创建的任务),但如何传递到脚本中@tasks中包含的所有数据?

this works for only one task (the one that has just been created), but how do I pass to the script all the data contained in @tasks?

我的意思是,在一个简单的表中检索数据,我将使用:

I mean, to retrieve the data in a simple table I would use:

<% @tasks.each do |task| %>
<%= @task.threehours %>
<%= @task.sixhours %>
<%= @task.ninehours %>
<%= @task.twelvehours %>
<% end %>

但是如何做同样的事情,但是在google图表脚本内?

But how do I do the same thing but inside the google chart script?

非常感谢!

推荐答案

据我所知,任务代替问号,对吧?如果是这样,我有一个解决方案。首先,你可能想在一个函数中包装代码:

As far as I can tell you want to pass additional tasks in place of question marks, right? If so, I have a solution. First you might want to wrap code in a function:

var data = google.visualization.arrayToDataTable(<%= make_a_chart %>);

并将其放在相应的助手中:

and place it somewhere in a corresponding helper:

def make_a_chart
  result = []
  result.push make_labels, 
              make_data( 0, "zerohour" ), 
              make_data( 3, "threehours" ),
              make_data( 6, "sixhours" ),
              make_data( 9, "ninehours" ),
              make_data( 12, "twelvehours" )
  return result
end

def make_labels
  y = ["Time"]
  for task in Task.all do
    y.append task.name
  end
  return y
end

def make_data( time, completeness )
  y = Task.first.created_at + time
  for task in Task.all do
    y.append task[completeness]
  end
  return y
end

此代码假定所有任务同时启动,并且您的数据库称为任务。此外,它很可能相当粗糙,可以使用一些改进。

This code assumes that all tasks are started simultaneously and that your database is called "Task". Also it's most likely quite crude and can use some improvements.

这篇关于Rails。如何将值从数组传递到Google图表脚本。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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