Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8390641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:03:07+00:00 2026-06-09T19:03:07+00:00

I have this javascript code, from google charts api right in the bottom of

  • 0

I have this javascript code, from google charts api right in the bottom of my view :

<div id='visualization'></div>
...
<script type="text/javascript">
function drawVisualization() {
        // Create and populate the data table.

        var data = google.visualization.arrayToDataTable(<%=raw @pie_gender %>)

        // Create and draw the visualization.
        new google.visualization.PieChart(document.getElementById('visualization')).
            draw(data, {title:"Men and Women"});
      }
      google.setOnLoadCallback(drawVisualization);
</script>

I’ve tried to put this js code in my assets folder under a .js file, and include it in my headers, as well as replacing <%=raw @pie_gender %> with this.getAttribute('data-message') and putting my div as ‘, but then i get a javascript error "getAttribute" does not exist for object window

I have also tried to pass my array as an input argument like : onload="drawVisualization(<%=raw @pie_gender %>), but then I get “Error: Not an array”

What might I be doing wrong ?

EDIT

@pie_gender = [[‘Gender’, ‘Occurences’][‘M’, 10][‘F’, 5]]

Based on example from google

EDIT 2
If i print the json output

<% logger.debug "Pie Gender : #{@pie_gender.to_json}" %>
        <div id="visualization" onload="drawVisualization(<%= @pie_gender.to_json %>)" > </div>

,it seems just fine :

Pie Gender : [["Gender","Receipts"],["",25000],["F",8658]]

but it seems that something happens while sending this as an argument to my js function, because it still says that message is not an array :

function drawVisualization(message) {
        // Create and populate the data table.
        var data = google.visualization.arrayToDataTable(message);

        // Create and draw the visualization.
        new google.visualization.PieChart(document.getElementById('visualization')).
            draw(data, {title:"Men and Women"});
      }
      google.setOnLoadCallback(drawVisualization);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:03:08+00:00Added an answer on June 9, 2026 at 7:03 pm

    To workaround this, I inserted my javascript code in a partial

    _googlescript.html.erb

    <script type="text/javascript">
    function drawVisualization() {
            // Create and populate the data table.
    
            var data = google.visualization.arrayToDataTable(<%=raw data_array %>)
    
            // Create and draw the visualization.
            new google.visualization.PieChart(document.getElementById('visualization')).
                draw(data, {title:"Men and Women"});
          }
          google.setOnLoadCallback(drawVisualization);
    </script>
    

    in my view I rendered the partial in the bottom of the page, and removed the onload, which just seemed not to be doing a thing… :

    <div id="visualization" > </div>
    ...
    <%= render partial: 'shared/googlescript', locals: {data_array:@pie_gender} %>
    

    Now i can see the chart again…but i still feel there is a better answer to my question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Javascript/JQuery code: <script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script type=text/javascript> name = $(#name).val(); alert(Name:
I have this Javascript code I inherited from another developer. I am very new
I have this javascript code <Script> function getGroupId(){ var group=document.getElementById(selectedOptions).value; var groupFirstLetter=group.substring(2); } </Script>
UPDATE : I have the following code: <script type=text/javascript> function addScript(url) { var script
I have copied this code from the official example... I just replaced the api
Hi I am working with google maps API I have this huge code, so
I have some Google Plus One javascript in my .ASCX file: <script type=text/javascript> (function
i have this javascript code to which i want to add the jquery fade-in
I have this javascript code below that uses jquery, it is suppoed to be
I have this JavaScript code: for (var idx in data) { var row =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.