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.
In my Projects controller I have
@tasks = Tasks.where("project_id = ?", params[:id])
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 %>, '????????']
]);
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 %>
But how do I do the same thing but inside the google chart script?
Thanks a lot!!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With