0

my google pie chart code is

var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ["Work",     50],
          ["Eat",      20],
          ["Commute",  20],
          ["Watch TV", 5],
          ["Sleep",    5]
        ]);

and my code is (<%= @datas.map { |d| [d.name, d.value] }.inspect %>) which gives the result

([["apple", "10"], ["orange", 20], ["banana", 30], ["grapes", 80], ["papaya", 44]])

when i put this result in my google pie chart code directly like this

  var data = google.visualization.arrayToDataTable([["apple", "10"], ["orange", 20], ["banana", 30], ["grapes", 80], ["papaya", 44]])

then it works perfectly. But keeping directly

var data = google.visualization.arrayToDataTable(<%= @datas.map { |d| [d.name, d.value] }.inspect %>)

does not work. What could be the reason behind this. Do ruby code have some formate to keep inside Or where i am doing wrong?

3 Answers 3

1

What about

(<%= @datas.map { |d| [d.name, d.value] }.to_json.html_safe -%>)
Sign up to request clarification or add additional context in comments.

3 Comments

I already did this but same problem. I think in this format there is mistake var data = google.visualization.arrayToDataTable(<%= @datas.map { |d| [d.name, d.value] }.inspect %>) But where i dont know and why beacause output of this code directly given works well
@regmiprem I think the problem was that you got your quotes escaped, edited. By the way, don't you check the created javascript and check how it looks ?
1

Does this help:

@datas = {:apple => 10, :orange => 20, :banana => 30}
@datas.map { |name, value| [name.to_s, value] }
# => [["apple", 10], ["orange", 20], ["banana", 30]] 

so just write out: <%= @datas.map { |name, value| [name.to_s, value] } %>

Comments

-1

you try this .. u change your code according this

 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

4 Comments

I have to put ruby code inside it with<%= %> otherwise how can i get value?
How can i send the value as (o, "zerohour") to (@data.name, "@data.value") I am confused
give me the contents in the @data variable
you can see the content in my question. as data.name and data.value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.