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

  • Home
  • SEARCH
  • 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 7790249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:31:45+00:00 2026-06-01T21:31:45+00:00

Im trying to pass a variable into the the google chart but when I

  • 0

Im trying to pass a variable into the the google chart but when I do it the chart stops showing up, my goal is for the chart to change every time I put in a different number in the input field but im just getting no where right now. This is what I have, can someone please offer some guidance on what I am doing wrong?

Javascript

    <script type="text/javascript" src="https://www.google.com/jsapi"></script>

    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['corechart']});
    </script>

    <script type="text/javascript">
         function numbers(){
            var work_field = document.forms['work_form']['work_n_field'].value;
            var work_div = document.getElementById('number-work');
            var numberschart = work_div.innerHTML = work_field;
            return false;
        };

        function drawVisualization() {
            // Create and populate the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Task');
        data.addColumn('number', 'Hours per Day');
        data.addRows(5);
        data.setValue(0, 0, 'Work');
//Right here is where I pass my variable into the chart
        data.setValue(0, 1, numberschart);
//And I leave the rest here until I define more input field.
        data.setValue(1, 0, 'Eat');
        data.setValue(1, 1, 2);
        data.setValue(2, 0, 'Commute');
        data.setValue(2, 1, 2);

        // Create and draw the visualization.
        new google.visualization.PieChart(document.getElementById('visualization')).
            draw(data, {
            title:"Mortgage Rates",
            colors: ['#a4b12d', '#818e0a', '#5c6601', '#f0fd79', '#dbe864'],
            });
      }    
      google.setOnLoadCallback(drawVisualization);
    </script>

Html

  <div id="visualization" style="width: 400px; height: 300px;"></div>
    <form name="work_form" onsubmit="return numbers()">
    <label id="n-work-label">Work</label><input name="work_n_field"/>
    <button name="submit" id="submit" value="submit" onclick="numbers()">Submit</button>
    </form>
    <div id="number-work"></div>
    </div>

Any help is greatly appreciated, Thank you

I have also added it to js fiddle, but when adding the chart to a resource it doesnt seem to detect it. http://jsfiddle.net/pkCCa/

  • 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-01T21:31:45+00:00Added an answer on June 1, 2026 at 9:31 pm

    This worked for me:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>Example</title>
            <script type="text/javascript" src="https://www.google.com/jsapi"></script>
            <script type="text/javascript">
                    var numberschart = 10; //<- Initial value
                    google.load('visualization', '1', {packages: ['corechart']});
    
                    function numbers(){
                            var work_field = document.forms['work_form']['work_n_field'].value;
                            var work_div = document.getElementById('number-work');
                            numberschart = work_div.innerHTML = work_field;
                            drawVisualization();
                            return false;
                    };
    
                    function drawVisualization() {
                        // Create and populate the data table.
                        var data = new google.visualization.DataTable();
                        data.addColumn('string', 'Task');
                        data.addColumn('number', 'Hours per Day');
                        data.addRows(5);
                        data.setValue(0, 0, 'Work');
                        //data.setValue(0, 1, 11);
                        data.setValue(0, 1, parseInt(numberschart));//<- The value you get from input field is a string, Google API will throw an error
                        data.setValue(1, 0, 'Eat');
                        data.setValue(1, 1, 2);
                        data.setValue(2, 0, 'Commute');
                        data.setValue(2, 1, 2);
    
                        // Create and draw the visualization.
                        new google.visualization.PieChart(document.getElementById('visualization')).
                            draw(data, {
                                title:"Mortgage Rates",
                                colors: ['#a4b12d', '#818e0a', '#5c6601', '#f0fd79', '#dbe864'],
                                animation:{
                                    duration:1000,
                                    easing: 'out',
                                },
                                vAxis: {
                                    minValue:0, 
                                    maxValue:1000
                                },
                            });
                      }    
                    google.setOnLoadCallback(drawVisualization);
            </script>
        </head>
        <body>
            <div id="visualization" style="width: 400px; height: 300px;"></div>
            <form name="work_form" onsubmit="return false">
                <label id="n-work-label">Work</label><input name="work_n_field"/>
                <button name="submit" id="submit" value="submit" onclick="numbers();return false;">Submit</button>
            </form>
            <div id="number-work"></div>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to pass in a selector into the ID variable but this
I'm trying to pass external variable into awk using awk -v but just cannot
I'm trying to pass a String variable into my javascript function, but my variable
i'm trying to pass a javascript variable into the ruby on rails 「 url_for」method
I am trying pass a new variable into a template within django-registration. Here is
I'm trying to pass a variable 'edit_id' to a load string but it doesn't
I'm trying to pass a string variable from the App Delegate into the a
I am trying to pass one variable to a jQuery function inline (i.e.: using
I'm trying to pass a variable through a ssh connection, like this: working_dir=/home/user/some_dir/ ssh
I am trying to pass a variable to an ajax request via jquery. I

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.