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 7402273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:42:14+00:00 2026-05-29T04:42:14+00:00

I want to draw a stock chart using Google Chart Tools. My self-written webservice

  • 0

I want to draw a stock chart using Google Chart Tools. My self-written webservice returns a JSON-object with stock quotes. The JSON data is read by JQuery using $.getJSON(). Using console log I was able to check, that the data is read in in a correct way.

The JSON data looks like:

{ "share": [
{ "date": "2012-01-30", "open": 72.38, "close": 73.13 },
{ "date": "2012-01-23", "open": 77.71, "close": 72.80 },
{ "date": "2012-01-16", "open": 75.25, "close": 78.05 }
]}

For drawing I’m using the following code snippet:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);

  function drawChart() {
    var options = {
        width: 720, 
        height: 480,
        title: 'Chart'
    };

    var data = new google.visualization.DataTable();
    data.addColumn('number', 'Day');
    data.addColumn('number', 'Sales');

    $.getJSON("StockReceiver.jsp",
        function(json) {
            $.each(json.share, function(i, item){
                console.log ([i, item.open]);
                data.addRow([i, item.open]);
                if ( i == 3 ) {
                    return false;
                }
            });
        });

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>
<div id="chart_div"></div>

The problem I got is that the chart is not drawn! At first I’ve tried to draw the image with about 3.000 values. Since this doesn’t worked I tried to draw one line using two number-values. (To point out if I did something wrong)

When I write

data.addRow([1, 20]);
data.addRow([2, 40]);

I see a line in the chart!

When I look into the $.each function call the loop causes the execution of

data.addRow([i, item.open]);

following values are set eg. (checked using console log)

data.addRow([0, 72.38]);
data.addRow([1, 77.71]);
data.addRow([2, 75.25]);

Why is the chart not drawn, when I try to execute data.addRow within the $.each function? I see a chart but without a line. I receive no error message at all with fireburg in firefox browser! In Internet Explorer it is also not working. Any ideas?

Suggested solution was:

<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {
    packages : [ "corechart" ]
});
google.setOnLoadCallback(drawChart);

function drawChart() {
    var options = {
        width : 720,
        height : 480,
        title : 'Chart'
    };

    $.getJSON("StockReceiver.jsp", function(json) {
        var data = new google.visualization.DataTable();
        data.addColumn('date', 'Date');
        data.addColumn('number', 'Sales');

        $.each(json, function(i, item) {
            data.addRow([ new Date(item.date), item.open ]);
        });

        var chart = new google.visualization.LineChart(document
                .getElementById('chart_div'));
        chart.draw(data, options);
    });
}
</script>
  • 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-05-29T04:42:17+00:00Added an answer on May 29, 2026 at 4:42 am

    Your problem is with execution order. $.getJSON() issues an Ajax request. Ajax is asynchronous, meaning that processing continues in the meantime. What happens is the following.

    1. A request for data is sent to “StockReceiver.jsp”.
    2. An empty chart is drawn.
    3. The response from “StockReceiver.jsp” comes in, too late to be used in the chart.

    The solution is to move the chart.draw() command into the Ajax callback function(json) {...}.

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

Sidebar

Related Questions

So I'm using Processing to draw a map from a data file. I want
I want to draw a text with 32 bit transparency on a graphics object.
I'm using pygame to draw a line between two arbitrary points. I also want
I want to draw DirectX content so that it appears to be floating over
I want to draw a shadow around a thumbnail in my software. It seems
I want to draw a HBITMAP onto HDC, I used StretchDIBits. It works fine.
I want to draw some lines and rectangles on a panel. Sometimes it does
I want to draw line on UIImageView. I already draw line on UIImageView but
I want to draw some listview items disabled and would like to mimic the
I want to draw this in my view to draw this line, I have

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.